@cursor/july 0.1.19 → 0.1.21

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 (217) hide show
  1. package/AGENTS.md +10 -0
  2. package/dist/bin/agent-serve.js +0 -0
  3. package/dist/channels/github/instrument.d.ts +20 -0
  4. package/dist/channels/github/instrument.d.ts.map +1 -0
  5. package/dist/channels/slack/api.d.ts +2 -0
  6. package/dist/channels/slack/api.d.ts.map +1 -1
  7. package/dist/channels/slack/api.js +3 -0
  8. package/dist/channels/slack/bot-mentions.d.ts +106 -0
  9. package/dist/channels/slack/bot-mentions.d.ts.map +1 -0
  10. package/dist/channels/slack/bot-mentions.js +243 -0
  11. package/dist/channels/slack/dispatch.d.ts +6 -0
  12. package/dist/channels/slack/dispatch.d.ts.map +1 -1
  13. package/dist/channels/slack/dispatch.js +24 -3
  14. package/dist/channels/slack/inbound.d.ts +10 -1
  15. package/dist/channels/slack/inbound.d.ts.map +1 -1
  16. package/dist/channels/slack/inbound.js +12 -4
  17. package/dist/channels/slack/index.d.ts +1 -0
  18. package/dist/channels/slack/index.d.ts.map +1 -1
  19. package/dist/channels/slack/index.js +1 -0
  20. package/dist/channels/slack/slack-channel.d.ts.map +1 -1
  21. package/dist/channels/slack/slack-channel.js +61 -22
  22. package/dist/channels/slack/types.d.ts +58 -0
  23. package/dist/channels/slack/types.d.ts.map +1 -1
  24. package/dist/docs/404.html +1 -1
  25. package/dist/docs/ab.html +2 -2
  26. package/dist/docs/assets/{app.CrsWMchO.js → app.jDxLzWv4.js} +1 -1
  27. package/dist/docs/assets/chunks/@localSearchIndexroot.DoJHJjqF.js +1 -0
  28. package/dist/docs/assets/chunks/{VPLocalSearchBox.D1JqzSh8.js → VPLocalSearchBox.Y6bDR1-a.js} +1 -1
  29. package/dist/docs/assets/chunks/{theme.DaBvZYwl.js → theme.CLazCWlJ.js} +2 -2
  30. package/dist/docs/building-with-agents.html +2 -2
  31. package/dist/docs/concepts.html +2 -2
  32. package/dist/docs/deployment.html +2 -2
  33. package/dist/docs/evals.html +2 -2
  34. package/dist/docs/example-agents/approval-buddy.html +2 -2
  35. package/dist/docs/example-agents/benny.html +2 -2
  36. package/dist/docs/example-agents/bugbot.html +2 -2
  37. package/dist/docs/example-agents/codebase-wiki.html +2 -2
  38. package/dist/docs/example-agents/codeowners-review.html +2 -2
  39. package/dist/docs/example-agents/concierge.html +2 -2
  40. package/dist/docs/example-agents/fsd.html +2 -2
  41. package/dist/docs/example-agents/index.html +2 -2
  42. package/dist/docs/example-agents/knowledge-base.html +2 -2
  43. package/dist/docs/example-agents/oncall.html +2 -2
  44. package/dist/docs/example-agents/security-reviewer.html +2 -2
  45. package/dist/docs/example-agents/slack-agent.html +2 -2
  46. package/dist/docs/example-agents/weather-agent.html +2 -2
  47. package/dist/docs/guides/agent-to-agent.html +2 -2
  48. package/dist/docs/guides/cloud-runtime.html +2 -2
  49. package/dist/docs/guides/github.html +2 -2
  50. package/dist/docs/guides/human-in-the-loop.html +2 -2
  51. package/dist/docs/guides/mcp-oauth.html +2 -2
  52. package/dist/docs/guides/slack.html +2 -2
  53. package/dist/docs/guides/webhooks.html +2 -2
  54. package/dist/docs/hillclimbing.html +2 -2
  55. package/dist/docs/index.html +2 -2
  56. package/dist/docs/quickstart.html +2 -2
  57. package/dist/docs/reference/agent-config.html +2 -2
  58. package/dist/docs/reference/channels.html +2 -2
  59. package/dist/docs/reference/cli.html +2 -2
  60. package/dist/docs/reference/connections.html +2 -2
  61. package/dist/docs/reference/hooks.html +2 -2
  62. package/dist/docs/reference/http-api.html +2 -2
  63. package/dist/docs/reference/instructions.html +2 -2
  64. package/dist/docs/reference/playground.html +2 -2
  65. package/dist/docs/reference/project-layout.html +2 -2
  66. package/dist/docs/reference/prompt.html +2 -2
  67. package/dist/docs/reference/schedules.html +2 -2
  68. package/dist/docs/reference/sessions.html +2 -2
  69. package/dist/docs/reference/skills.html +2 -2
  70. package/dist/docs/reference/subagents.html +2 -2
  71. package/dist/docs/reference/tools.html +2 -2
  72. package/dist/docs/scaffolding-agents.html +2 -2
  73. package/dist/docs/storage.html +2 -2
  74. package/dist/docs/troubleshooting.html +2 -2
  75. package/dist/internal/json-dir-store.d.ts +32 -0
  76. package/dist/internal/json-dir-store.d.ts.map +1 -0
  77. package/dist/internal/json-dir-store.js +100 -0
  78. package/dist/internal/resolved-connections.d.ts +1 -1
  79. package/dist/internal/resolved-connections.d.ts.map +1 -1
  80. package/dist/internal/resolved-connections.js +10 -0
  81. package/dist/internal/session-engine.d.ts +4 -1
  82. package/dist/internal/session-engine.d.ts.map +1 -1
  83. package/dist/internal/session-engine.js +13 -2
  84. package/dist/playground/assets/index-CjOQ4hN9.css +1 -0
  85. package/dist/playground/assets/{index-C2SU2xV5.js → index-dshZQJCp.js} +46 -46
  86. package/dist/playground/index.html +2 -2
  87. package/dist/types.d.ts +7 -0
  88. package/dist/types.d.ts.map +1 -1
  89. package/dist/types.js +9 -0
  90. package/package.json +25 -25
  91. package/skills/create-agent/SKILL.md +36 -4
  92. package/skills/evals/SKILL.md +3 -0
  93. package/skills/framework-map/SKILL.md +14 -0
  94. package/skills/github/SKILL.md +6 -1
  95. package/skills/hillclimb/SKILL.md +28 -7
  96. package/src/bin/agent-serve.version.test.ts +62 -0
  97. package/src/channels/github/api.test.ts +64 -0
  98. package/src/channels/github/auth.test.ts +105 -0
  99. package/src/channels/github/cursor-account.test.ts +204 -0
  100. package/src/channels/github/forward.test.ts +457 -0
  101. package/src/channels/github/github.test.ts +937 -0
  102. package/src/channels/github/replay.test.ts +179 -0
  103. package/src/channels/slack/api.post-message.test.ts +148 -0
  104. package/src/channels/slack/api.ts +5 -0
  105. package/src/channels/slack/approvals.test.ts +328 -0
  106. package/src/channels/slack/block-actions.test.ts +452 -0
  107. package/src/channels/slack/bot-mentions.test.ts +217 -0
  108. package/src/channels/slack/bot-mentions.ts +315 -0
  109. package/src/channels/slack/channel-watch.test.ts +363 -0
  110. package/src/channels/slack/cursor-account.test.ts +253 -0
  111. package/src/channels/slack/defaults.final-post.test.ts +182 -0
  112. package/src/channels/slack/dispatch.test.ts +795 -0
  113. package/src/channels/slack/dispatch.ts +27 -1
  114. package/src/channels/slack/eval-directive.test.ts +273 -0
  115. package/src/channels/slack/inbound.ts +25 -4
  116. package/src/channels/slack/index.ts +1 -0
  117. package/src/channels/slack/message-body.test.ts +54 -0
  118. package/src/channels/slack/nudge-store.test.ts +143 -0
  119. package/src/channels/slack/slack-channel.ts +66 -8
  120. package/src/channels/slack/slack.test.ts +391 -0
  121. package/src/channels/slack/stop.test.ts +23 -0
  122. package/src/channels/slack/thread-context.test.ts +202 -0
  123. package/src/channels/slack/types.ts +60 -0
  124. package/src/evals/assertions.test.ts +580 -0
  125. package/src/evals/expect.test.ts +144 -0
  126. package/src/evals/judge.test.ts +181 -0
  127. package/src/evals/loaders.test.ts +132 -0
  128. package/src/evals/matchers.test.ts +95 -0
  129. package/src/evals/reporters.test.ts +303 -0
  130. package/src/evals/run-facts.test.ts +259 -0
  131. package/src/internal/ab-snapshot.test.ts +325 -0
  132. package/src/internal/approval-gate.test.ts +49 -0
  133. package/src/internal/approvals.integration.test.ts +383 -0
  134. package/src/internal/authored-loaders.test.ts +31 -0
  135. package/src/internal/builtin-tools/reminders.test.ts +201 -0
  136. package/src/internal/channel-route-schema.test.ts +294 -0
  137. package/src/internal/chat-attach.test.ts +262 -0
  138. package/src/internal/cli-deploy.test.ts +1991 -0
  139. package/src/internal/cli-mcp.test.ts +789 -0
  140. package/src/internal/cli-skills.test.ts +133 -0
  141. package/src/internal/cli-slack.test.ts +1647 -0
  142. package/src/internal/cloud-merge.test.ts +74 -0
  143. package/src/internal/cron.test.ts +22 -0
  144. package/src/internal/cursor/account-mcp.test.ts +807 -0
  145. package/src/internal/cursor/backend-client.test.ts +591 -0
  146. package/src/internal/cursor/credentials.test.ts +351 -0
  147. package/src/internal/cursor/github-credentials.test.ts +136 -0
  148. package/src/internal/cursor-account-mcp-auth.test.ts +310 -0
  149. package/src/internal/cursor-account.integration.test.ts +441 -0
  150. package/src/internal/cursor-event-relay.test.ts +746 -0
  151. package/src/internal/cursor-github-credentials.integration.test.ts +271 -0
  152. package/src/internal/cursor-slack-relay.test.ts +525 -0
  153. package/src/internal/deploy-source.test.ts +111 -0
  154. package/src/internal/discovery.builtin-tools.test.ts +94 -0
  155. package/src/internal/discovery.concurrency.test.ts +60 -0
  156. package/src/internal/discovery.cursor-account.test.ts +133 -0
  157. package/src/internal/discovery.cwd.test.ts +83 -0
  158. package/src/internal/discovery.hosting.test.ts +80 -0
  159. package/src/internal/discovery.identity.test.ts +44 -0
  160. package/src/internal/docs-site.test.ts +66 -0
  161. package/src/internal/duration.test.ts +29 -0
  162. package/src/internal/eval-judge-model.test.ts +187 -0
  163. package/src/internal/eval-run-store.cancel.test.ts +142 -0
  164. package/src/internal/eval-run-store.storage.test.ts +211 -0
  165. package/src/internal/eval-runner.http.test.ts +403 -0
  166. package/src/internal/eval-runner.run.test.ts +928 -0
  167. package/src/internal/evals-client.test.ts +307 -0
  168. package/src/internal/event-mapper.test.ts +243 -0
  169. package/src/internal/github-fanout.test.ts +213 -0
  170. package/src/internal/handleAgentServeTrigger.test.ts +179 -0
  171. package/src/internal/host-kv.test.ts +82 -0
  172. package/src/internal/host-platforms.test.ts +126 -0
  173. package/src/internal/http-channel.test.ts +402 -0
  174. package/src/internal/init-project.test.ts +269 -0
  175. package/src/internal/install-cursor-skills.test.ts +262 -0
  176. package/src/internal/local-env.test.ts +100 -0
  177. package/src/internal/log-ring.test.ts +31 -0
  178. package/src/internal/logs-client.test.ts +350 -0
  179. package/src/internal/mcp-endpoint.test.ts +436 -0
  180. package/src/internal/mcp-host.test.ts +298 -0
  181. package/src/internal/mcp-oauth.test.ts +148 -0
  182. package/src/internal/net.test.ts +17 -0
  183. package/src/internal/peer-connections.test.ts +128 -0
  184. package/src/internal/peer-mcp.integration.test.ts +289 -0
  185. package/src/internal/playground/toolchain.test.ts +53 -0
  186. package/src/internal/playground-cli.test.ts +187 -0
  187. package/src/internal/playground-proxy.test.ts +376 -0
  188. package/src/internal/prompt-context.integration.test.ts +232 -0
  189. package/src/internal/prompt-context.test.ts +127 -0
  190. package/src/internal/reminder-runner.test.ts +390 -0
  191. package/src/internal/reminder-store.test.ts +53 -0
  192. package/src/internal/request-headers.test.ts +27 -0
  193. package/src/internal/resolve-prod-target.test.ts +787 -0
  194. package/src/internal/resolved-connections.test.ts +295 -0
  195. package/src/internal/resolved-connections.ts +18 -1
  196. package/src/internal/router.test.ts +57 -0
  197. package/src/internal/sdk-runner.test.ts +290 -0
  198. package/src/internal/session-engine.coalesce.test.ts +169 -0
  199. package/src/internal/session-engine.concurrency.test.ts +250 -0
  200. package/src/internal/session-engine.host-oauth-mcp.test.ts +110 -0
  201. package/src/internal/session-engine.interrupt.test.ts +577 -0
  202. package/src/internal/session-engine.storage.test.ts +547 -0
  203. package/src/internal/session-engine.ts +15 -1
  204. package/src/internal/session-urls.test.ts +28 -0
  205. package/src/internal/sessions-client.test.ts +518 -0
  206. package/src/internal/storage-coordinator.test.ts +517 -0
  207. package/src/internal/tool-call.test.ts +458 -0
  208. package/src/internal/tool-result.test.ts +52 -0
  209. package/src/internal/trajectory.approvals.test.ts +83 -0
  210. package/src/internal/trajectory.subagents.test.ts +198 -0
  211. package/src/internal/turn-governor.test.ts +137 -0
  212. package/src/internal/update-check.test.ts +485 -0
  213. package/src/internal/workspace.test.ts +81 -0
  214. package/src/storage-backends/cursor-hosted.test.ts +121 -0
  215. package/src/types.ts +12 -0
  216. package/dist/docs/assets/chunks/@localSearchIndexroot.BnHRjfoe.js +0 -1
  217. package/dist/playground/assets/index-CidizGZv.css +0 -1
@@ -0,0 +1,795 @@
1
+ import { mkdtemp, rm } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { afterEach, describe, expect, it, vi } from "vitest";
5
+ import type {
6
+ ChannelSession,
7
+ HostContext,
8
+ SendMessageFn,
9
+ } from "../../types.js";
10
+ import * as slackApi from "./api.js";
11
+ import {
12
+ createEventIdDedupe,
13
+ createSlackLogger,
14
+ dispatchInboundFromEventBody,
15
+ dispatchInboundMessage,
16
+ SlackAskDedupeStore,
17
+ SlackNudgeStore,
18
+ } from "./index.js";
19
+ import type { SlackMessageHandler } from "./types.js";
20
+
21
+ const testAuth = {
22
+ authenticator: "test",
23
+ principalId: "U1",
24
+ principalType: "user" as const,
25
+ };
26
+
27
+ function mockSession(
28
+ id = "ses_test",
29
+ extras?: { coalesced?: boolean; isNew?: boolean }
30
+ ): ChannelSession {
31
+ return {
32
+ id,
33
+ continuationToken: null,
34
+ isNew: extras?.isNew ?? true,
35
+ ...(extras?.coalesced === true ? { coalesced: true as const } : {}),
36
+ info: {
37
+ id,
38
+ channelId: "slack",
39
+ mode: "chat",
40
+ auth: testAuth,
41
+ },
42
+ getEventStream: async () => new ReadableStream(),
43
+ waitForCompletion: async () => ({ status: "finished" }),
44
+ setContinuationToken: async () => undefined,
45
+ };
46
+ }
47
+
48
+ function createSendRecorder(options?: { coalesced?: boolean }): {
49
+ send: SendMessageFn;
50
+ calls: Array<{ message: string; options: Parameters<SendMessageFn>[1] }>;
51
+ } {
52
+ const calls: Array<{
53
+ message: string;
54
+ options: Parameters<SendMessageFn>[1];
55
+ }> = [];
56
+ const send: SendMessageFn = async (message, sendOptions) => {
57
+ calls.push({ message, options: sendOptions });
58
+ return mockSession("ses_1", {
59
+ coalesced: options?.coalesced,
60
+ isNew: false,
61
+ });
62
+ };
63
+ return { send, calls };
64
+ }
65
+
66
+ function acceptHandler(): SlackMessageHandler {
67
+ return async () => ({ auth: testAuth });
68
+ }
69
+
70
+ function testHost(options?: {
71
+ nudgeRoot?: string;
72
+ askRoot?: string;
73
+ evals?: HostContext["evals"];
74
+ }) {
75
+ const nudgeStore =
76
+ options?.nudgeRoot === undefined
77
+ ? undefined
78
+ : new SlackNudgeStore(options.nudgeRoot);
79
+ const askStore =
80
+ options?.askRoot === undefined
81
+ ? undefined
82
+ : new SlackAskDedupeStore(options.askRoot);
83
+ return {
84
+ mcp: {
85
+ names: () => [] as string[],
86
+ listTools: async () => [],
87
+ callTool: async () => ({ content: [] }),
88
+ },
89
+ github: {
90
+ getOctokit: async () => {
91
+ throw new Error("github client not configured in test");
92
+ },
93
+ },
94
+ slack: {
95
+ getClient: async () => {
96
+ throw new Error("slack client not configured in test");
97
+ },
98
+ },
99
+ kv: {
100
+ get: async () => undefined,
101
+ put: async () => {},
102
+ delete: async () => {},
103
+ },
104
+ evals: options?.evals,
105
+ slackNudges:
106
+ nudgeStore === undefined || askStore === undefined
107
+ ? undefined
108
+ : {
109
+ append: (input: Parameters<SlackNudgeStore["append"]>[0]) =>
110
+ nudgeStore.append(input),
111
+ drainFormatted: (key: string) => nudgeStore.drainFormatted(key),
112
+ clear: (key: string) => nudgeStore.clear(key),
113
+ hasUndelivered: (key: string) => nudgeStore.hasUndelivered(key),
114
+ isDelivered: (key: string, ts: string) =>
115
+ nudgeStore.isDelivered(key, ts),
116
+ tryClaimAsk: (channelId: string, ts: string) =>
117
+ askStore.tryClaim(channelId, ts),
118
+ releaseAsk: (channelId: string, ts: string) =>
119
+ askStore.release(channelId, ts),
120
+ },
121
+ };
122
+ }
123
+
124
+ function mentionBody(
125
+ args: {
126
+ eventId?: string;
127
+ text?: string;
128
+ ts?: string;
129
+ threadTs?: string;
130
+ channel?: string;
131
+ } = {}
132
+ ): Record<string, unknown> {
133
+ return {
134
+ type: "event_callback",
135
+ team_id: "T1",
136
+ ...(args.eventId === undefined ? {} : { event_id: args.eventId }),
137
+ event: {
138
+ type: "app_mention",
139
+ user: "U1",
140
+ text: args.text ?? "<@Ubot> weather?",
141
+ ts: args.ts ?? "10.0",
142
+ // Different from ts so setTitle is skipped (no network).
143
+ thread_ts: args.threadTs ?? "9.0",
144
+ channel: args.channel ?? "C9",
145
+ },
146
+ };
147
+ }
148
+
149
+ const cleanups: Array<() => Promise<void>> = [];
150
+
151
+ afterEach(async () => {
152
+ vi.unstubAllGlobals();
153
+ vi.restoreAllMocks();
154
+ while (cleanups.length > 0) {
155
+ await cleanups.pop()?.();
156
+ }
157
+ });
158
+
159
+ describe("dispatchInboundFromEventBody", () => {
160
+ it("dispatches app_mention to send with continuation token", async () => {
161
+ const { send, calls } = createSendRecorder();
162
+ const background: Promise<unknown>[] = [];
163
+ const lines: string[] = [];
164
+
165
+ const scheduled = dispatchInboundFromEventBody(mentionBody(), {
166
+ send,
167
+ waitUntil: (p) => {
168
+ background.push(p);
169
+ },
170
+ onAppMention: acceptHandler(),
171
+ onDirectMessage: async () => null,
172
+ credentials: undefined,
173
+ eventIds: createEventIdDedupe(),
174
+ host: testHost(),
175
+ log: createSlackLogger((line) => lines.push(line)),
176
+ });
177
+
178
+ expect(scheduled).toBe(true);
179
+ await Promise.all(background);
180
+ expect(calls).toHaveLength(1);
181
+ expect(calls[0]?.options?.continuationToken).toBe("C9:9.0");
182
+ expect(calls[0]?.message).toContain("weather?");
183
+ expect(calls[0]?.message).toContain("<slack_context>");
184
+ expect(lines.some((l) => l.includes("session start"))).toBe(true);
185
+ });
186
+
187
+ it("dedupes by event_id", async () => {
188
+ const { send, calls } = createSendRecorder();
189
+ const background: Promise<unknown>[] = [];
190
+ const eventIds = createEventIdDedupe();
191
+ const deps = {
192
+ send,
193
+ waitUntil: (p: Promise<unknown>) => {
194
+ background.push(p);
195
+ },
196
+ onAppMention: acceptHandler(),
197
+ onDirectMessage: async () => null,
198
+ credentials: undefined as undefined,
199
+ host: testHost(),
200
+ eventIds,
201
+ log: createSlackLogger(() => undefined),
202
+ };
203
+
204
+ const body = mentionBody({ eventId: "Ev1" });
205
+ expect(dispatchInboundFromEventBody(body, deps)).toBe(true);
206
+ expect(dispatchInboundFromEventBody(body, deps)).toBe(false);
207
+ await Promise.all(background);
208
+ expect(calls).toHaveLength(1);
209
+ });
210
+
211
+ it("skips send when handler returns null", async () => {
212
+ const { send, calls } = createSendRecorder();
213
+ const background: Promise<unknown>[] = [];
214
+ dispatchInboundFromEventBody(mentionBody(), {
215
+ send,
216
+ waitUntil: (p) => {
217
+ background.push(p);
218
+ },
219
+ onAppMention: async () => null,
220
+ onDirectMessage: async () => null,
221
+ credentials: undefined,
222
+ eventIds: createEventIdDedupe(),
223
+ host: testHost(),
224
+ });
225
+ await Promise.all(background);
226
+ expect(calls).toHaveLength(0);
227
+ });
228
+
229
+ it("does not send when handler throws", async () => {
230
+ const { send, calls } = createSendRecorder();
231
+ const background: Promise<unknown>[] = [];
232
+ dispatchInboundFromEventBody(mentionBody(), {
233
+ send,
234
+ waitUntil: (p) => {
235
+ background.push(p);
236
+ },
237
+ onAppMention: async () => {
238
+ throw new Error("boom");
239
+ },
240
+ onDirectMessage: async () => null,
241
+ credentials: undefined,
242
+ eventIds: createEventIdDedupe(),
243
+ host: testHost(),
244
+ log: createSlackLogger(() => undefined),
245
+ });
246
+ await Promise.all(background);
247
+ expect(calls).toHaveLength(0);
248
+ });
249
+
250
+ it("dispatches direct messages", async () => {
251
+ const { send, calls } = createSendRecorder();
252
+ const background: Promise<unknown>[] = [];
253
+ dispatchInboundFromEventBody(
254
+ {
255
+ type: "event_callback",
256
+ team_id: "T1",
257
+ event_id: "EvDM",
258
+ event: {
259
+ type: "message",
260
+ channel_type: "im",
261
+ user: "U1",
262
+ text: "hi from dm",
263
+ ts: "2.0",
264
+ thread_ts: "1.0",
265
+ channel: "D1",
266
+ },
267
+ },
268
+ {
269
+ send,
270
+ waitUntil: (p) => {
271
+ background.push(p);
272
+ },
273
+ onAppMention: async () => null,
274
+ onDirectMessage: acceptHandler(),
275
+ credentials: undefined,
276
+ eventIds: createEventIdDedupe(),
277
+ host: testHost(),
278
+ }
279
+ );
280
+ await Promise.all(background);
281
+ expect(calls).toHaveLength(1);
282
+ expect(calls[0]?.options?.continuationToken).toBe("D1:1.0");
283
+ });
284
+
285
+ it("returns false for unhandled shapes", () => {
286
+ expect(
287
+ dispatchInboundFromEventBody(
288
+ { type: "event_callback", event: { type: "reaction_added" } },
289
+ {
290
+ send: async () => mockSession(),
291
+ waitUntil: () => undefined,
292
+ onAppMention: async () => null,
293
+ onDirectMessage: async () => null,
294
+ credentials: undefined,
295
+ eventIds: createEventIdDedupe(),
296
+ host: testHost(),
297
+ }
298
+ )
299
+ ).toBe(false);
300
+ });
301
+
302
+ it("forwards message override, workspaceFiles, and cloud options from the handler", async () => {
303
+ const { send, calls } = createSendRecorder();
304
+ const background: Promise<unknown>[] = [];
305
+
306
+ dispatchInboundFromEventBody(mentionBody(), {
307
+ send,
308
+ waitUntil: (p) => {
309
+ background.push(p);
310
+ },
311
+ onAppMention: async () => ({
312
+ auth: testAuth,
313
+ title: "Bugbot acme/widget#1",
314
+ message: "Read pr.txt and review",
315
+ workspaceFiles: { "pr.txt": "evidence" },
316
+ cloud: {
317
+ repos: [
318
+ {
319
+ url: "https://github.com/acme/widget",
320
+ prUrl: "https://github.com/acme/widget/pull/1",
321
+ },
322
+ ],
323
+ },
324
+ }),
325
+ onDirectMessage: async () => null,
326
+ credentials: undefined,
327
+ eventIds: createEventIdDedupe(),
328
+ host: testHost(),
329
+ });
330
+ await Promise.all(background);
331
+ expect(calls).toHaveLength(1);
332
+ expect(calls[0]?.options?.title).toBe("Bugbot acme/widget#1");
333
+ expect(calls[0]?.message).toContain("Read pr.txt");
334
+ expect(calls[0]?.options?.workspaceFiles).toEqual({
335
+ "pr.txt": "evidence",
336
+ });
337
+ expect(calls[0]?.options?.cloud).toEqual({
338
+ repos: [
339
+ {
340
+ url: "https://github.com/acme/widget",
341
+ prUrl: "https://github.com/acme/widget/pull/1",
342
+ },
343
+ ],
344
+ });
345
+ });
346
+ });
347
+
348
+ describe("engagement routing", () => {
349
+ it("ignores mentions when mentionsEnabled is false", async () => {
350
+ const { send, calls } = createSendRecorder();
351
+ const background: Promise<unknown>[] = [];
352
+ const scheduled = dispatchInboundFromEventBody(mentionBody(), {
353
+ send,
354
+ waitUntil: (p) => {
355
+ background.push(p);
356
+ },
357
+ onAppMention: acceptHandler(),
358
+ onDirectMessage: async () => null,
359
+ credentials: undefined,
360
+ eventIds: createEventIdDedupe(),
361
+ host: testHost(),
362
+ log: createSlackLogger(() => undefined),
363
+ mentionsEnabled: false,
364
+ });
365
+ expect(scheduled).toBe(false);
366
+ await Promise.all(background);
367
+ expect(calls).toHaveLength(0);
368
+ });
369
+
370
+ it("routes channel posts to the watcher when configured", async () => {
371
+ const { send, calls } = createSendRecorder();
372
+ const background: Promise<unknown>[] = [];
373
+ const handled: string[] = [];
374
+ const deps = {
375
+ send,
376
+ waitUntil: (p: Promise<unknown>) => {
377
+ background.push(p);
378
+ },
379
+ onAppMention: acceptHandler(),
380
+ onDirectMessage: async () => null,
381
+ credentials: undefined as undefined,
382
+ eventIds: createEventIdDedupe(),
383
+ host: testHost(),
384
+ log: createSlackLogger(() => undefined),
385
+ channelWatch: {
386
+ handle: (event: { kind: string }, eventId?: string) => {
387
+ handled.push(`${event.kind}:${eventId ?? ""}`);
388
+ return true;
389
+ },
390
+ stop: () => undefined,
391
+ },
392
+ };
393
+ const scheduled = dispatchInboundFromEventBody(
394
+ {
395
+ type: "event_callback",
396
+ team_id: "T1",
397
+ event_id: "Ev9",
398
+ event: {
399
+ type: "message",
400
+ channel: "C9",
401
+ channel_type: "channel",
402
+ ts: "20.0",
403
+ text: "fresh report",
404
+ user: "U1",
405
+ },
406
+ },
407
+ deps
408
+ );
409
+ expect(scheduled).toBe(true);
410
+ await Promise.all(background);
411
+ expect(handled).toEqual(["post:Ev9"]);
412
+ // Watcher decides whether to dispatch; nothing sent by default here.
413
+ expect(calls).toHaveLength(0);
414
+ });
415
+
416
+ it("ignores channel posts without a watcher", () => {
417
+ const { send } = createSendRecorder();
418
+ const scheduled = dispatchInboundFromEventBody(
419
+ {
420
+ type: "event_callback",
421
+ event: {
422
+ type: "message",
423
+ channel: "C9",
424
+ channel_type: "channel",
425
+ ts: "20.0",
426
+ text: "fresh report",
427
+ user: "U1",
428
+ },
429
+ },
430
+ {
431
+ send,
432
+ waitUntil: () => undefined,
433
+ onAppMention: acceptHandler(),
434
+ onDirectMessage: async () => null,
435
+ credentials: undefined,
436
+ eventIds: createEventIdDedupe(),
437
+ host: testHost(),
438
+ log: createSlackLogger(() => undefined),
439
+ }
440
+ );
441
+ expect(scheduled).toBe(false);
442
+ });
443
+ });
444
+
445
+ describe("dispatchInboundMessage", () => {
446
+ it("logs delivery failure when send throws", async () => {
447
+ const lines: string[] = [];
448
+ await dispatchInboundMessage({
449
+ kind: "app_mention",
450
+ message: {
451
+ text: "hi",
452
+ body: "hi",
453
+ markdown: "hi",
454
+ ts: "2.0",
455
+ threadTs: "1.0",
456
+ channelId: "C1",
457
+ teamId: "T1",
458
+ author: { userId: "U1", isBot: false },
459
+ },
460
+ handler: acceptHandler(),
461
+ send: async () => {
462
+ throw new Error("engine down");
463
+ },
464
+ credentials: undefined,
465
+ host: testHost(),
466
+ log: createSlackLogger((line) => lines.push(line)),
467
+ });
468
+ expect(lines.some((line) => line.includes("delivery failed"))).toBe(true);
469
+ });
470
+
471
+ it("coalesces busy follow-ups with admission + nudge (no preempt retry)", async () => {
472
+ const root = await mkdtemp(join(tmpdir(), "dispatch-coalesce-"));
473
+ cleanups.push(() => rm(root, { recursive: true, force: true }));
474
+ const apiSoft = vi.spyOn(slackApi, "callSlackApiSoft").mockResolvedValue({
475
+ ok: true,
476
+ messages: [
477
+ { ts: "1.0", bot_id: "B1", text: "parent" },
478
+ { ts: "2.0", user: "U1", text: "follow-up" },
479
+ ],
480
+ has_more: false,
481
+ });
482
+ const lines: string[] = [];
483
+ const { send, calls } = createSendRecorder({ coalesced: true });
484
+ const host = testHost({
485
+ nudgeRoot: join(root, "nudges"),
486
+ askRoot: join(root, "asks"),
487
+ });
488
+ await dispatchInboundMessage({
489
+ kind: "app_mention",
490
+ message: {
491
+ text: "follow-up",
492
+ body: "follow-up",
493
+ markdown: "follow-up",
494
+ ts: "2.0",
495
+ threadTs: "1.0",
496
+ channelId: "C1",
497
+ teamId: "T1",
498
+ author: { userId: "U1", isBot: false },
499
+ },
500
+ handler: acceptHandler(),
501
+ send,
502
+ credentials: { botToken: "xoxb-test" },
503
+ host,
504
+ hasContinuationSession: async () => true,
505
+ log: createSlackLogger((line) => lines.push(line)),
506
+ });
507
+ expect(calls).toHaveLength(1);
508
+ expect(calls[0]?.options?.admission).toBe("coalesce");
509
+ expect(calls[0]?.options?.coalesceSourceTs).toBe("2.0");
510
+ expect(
511
+ lines.some((line) => line.includes("session busy; coalescing follow-up"))
512
+ ).toBe(true);
513
+ expect(await host.slackNudges?.hasUndelivered("C1:1.0")).toBe(false);
514
+ // Follow-up typing uses same-module setThreadStatus → callSlackApiSoft;
515
+ // vitest cannot spy that path. Coalesce admission + log is the contract.
516
+ expect(apiSoft).toHaveBeenCalled();
517
+ vi.restoreAllMocks();
518
+ });
519
+
520
+ it("handles whole-message stop without send", async () => {
521
+ const root = await mkdtemp(join(tmpdir(), "dispatch-stop-"));
522
+ cleanups.push(() => rm(root, { recursive: true, force: true }));
523
+ vi.spyOn(slackApi, "setThreadStatus").mockResolvedValue({ ok: true });
524
+ const { send, calls } = createSendRecorder();
525
+ let interrupted = false;
526
+ await dispatchInboundMessage({
527
+ kind: "app_mention",
528
+ message: {
529
+ text: "<@Ubot> stop",
530
+ body: "<@Ubot> stop",
531
+ markdown: "@Ubot stop",
532
+ ts: "2.0",
533
+ threadTs: "1.0",
534
+ channelId: "C1",
535
+ teamId: "T1",
536
+ author: { userId: "U1", isBot: false },
537
+ },
538
+ handler: acceptHandler(),
539
+ send,
540
+ credentials: { botToken: "xoxb-test" },
541
+ host: testHost({
542
+ nudgeRoot: join(root, "nudges"),
543
+ askRoot: join(root, "asks"),
544
+ }),
545
+ interruptContinuation: async () => {
546
+ interrupted = true;
547
+ return true;
548
+ },
549
+ });
550
+ expect(calls).toHaveLength(0);
551
+ expect(interrupted).toBe(true);
552
+ vi.restoreAllMocks();
553
+ });
554
+
555
+ it("handles eval list directive without send", async () => {
556
+ // Assert host.evals.list ran and no agent turn was started. Slack
557
+ // postMessage is same-module (not spyable via the export).
558
+ const { send, calls } = createSendRecorder();
559
+ let listed = false;
560
+ await dispatchInboundMessage({
561
+ kind: "app_mention",
562
+ message: {
563
+ text: "<@Ubot> eval list",
564
+ body: "<@Ubot> eval list",
565
+ markdown: "@Ubot eval list",
566
+ ts: "2.0",
567
+ threadTs: "1.0",
568
+ channelId: "C1",
569
+ teamId: "T1",
570
+ author: { userId: "U1", isBot: false },
571
+ },
572
+ handler: acceptHandler(),
573
+ send,
574
+ credentials: { botToken: "xoxb-test" },
575
+ host: testHost({
576
+ evals: {
577
+ list: async () => {
578
+ listed = true;
579
+ return {
580
+ evals: [{ id: "smoke", fileId: "smoke" }],
581
+ config: { maxPlaygroundRuns: 20, durableRuns: false },
582
+ };
583
+ },
584
+ get: async () => undefined,
585
+ listRunsWithActive: async () => ({ runs: [] }),
586
+ start: async () => {
587
+ throw new Error("should not start");
588
+ },
589
+ cancel: async () => {
590
+ throw new Error("should not cancel");
591
+ },
592
+ },
593
+ }),
594
+ });
595
+ expect(calls).toHaveLength(0);
596
+ expect(listed).toBe(true);
597
+ });
598
+
599
+ it("skips eval directive when evalCommandsEnabled is false", async () => {
600
+ const { send, calls } = createSendRecorder();
601
+ await dispatchInboundMessage({
602
+ kind: "app_mention",
603
+ message: {
604
+ text: "<@Ubot> eval list",
605
+ body: "<@Ubot> eval list",
606
+ markdown: "@Ubot eval list",
607
+ ts: "2.0",
608
+ threadTs: "1.0",
609
+ channelId: "C1",
610
+ teamId: "T1",
611
+ author: { userId: "U1", isBot: false },
612
+ },
613
+ handler: acceptHandler(),
614
+ send,
615
+ credentials: undefined,
616
+ host: testHost({
617
+ evals: {
618
+ list: async () => {
619
+ throw new Error("should not list");
620
+ },
621
+ get: async () => undefined,
622
+ listRunsWithActive: async () => ({ runs: [] }),
623
+ start: async () => {
624
+ throw new Error("should not start");
625
+ },
626
+ cancel: async () => {
627
+ throw new Error("should not cancel");
628
+ },
629
+ },
630
+ }),
631
+ evalCommandsEnabled: false,
632
+ });
633
+ expect(calls).toHaveLength(1);
634
+ });
635
+
636
+ it("dedupes asks by channel:ts", async () => {
637
+ const root = await mkdtemp(join(tmpdir(), "dispatch-dedupe-"));
638
+ cleanups.push(() => rm(root, { recursive: true, force: true }));
639
+ const host = testHost({
640
+ nudgeRoot: join(root, "nudges"),
641
+ askRoot: join(root, "asks"),
642
+ });
643
+ const { send, calls } = createSendRecorder();
644
+ const message = {
645
+ text: "hello",
646
+ body: "hello",
647
+ markdown: "hello",
648
+ ts: "2.0",
649
+ threadTs: "1.0",
650
+ channelId: "C1",
651
+ teamId: "T1",
652
+ author: { userId: "U1", isBot: false },
653
+ };
654
+ await dispatchInboundMessage({
655
+ kind: "app_mention",
656
+ message,
657
+ handler: acceptHandler(),
658
+ send,
659
+ credentials: undefined,
660
+ host,
661
+ });
662
+ await dispatchInboundMessage({
663
+ kind: "app_mention",
664
+ message,
665
+ handler: acceptHandler(),
666
+ send,
667
+ credentials: undefined,
668
+ host,
669
+ });
670
+ expect(calls).toHaveLength(1);
671
+ });
672
+
673
+ it("hydrates prior thread messages into the prompt", async () => {
674
+ vi.spyOn(slackApi, "callSlackApiSoft").mockResolvedValue({
675
+ ok: true,
676
+ messages: [
677
+ {
678
+ ts: "1.0",
679
+ bot_id: "BDD",
680
+ username: "Datadog",
681
+ text: "Monitor alert: rate limited",
682
+ },
683
+ {
684
+ ts: "2.0",
685
+ user: "U1",
686
+ text: "@jenny what happened here?",
687
+ },
688
+ ],
689
+ has_more: false,
690
+ });
691
+ const { send, calls } = createSendRecorder();
692
+ const lines: string[] = [];
693
+ await dispatchInboundMessage({
694
+ kind: "app_mention",
695
+ message: {
696
+ text: "<@BJENNY> what happened here?",
697
+ body: "<@BJENNY> what happened here?",
698
+ markdown: "@BJENNY what happened here?",
699
+ ts: "2.0",
700
+ threadTs: "1.0",
701
+ channelId: "C1",
702
+ teamId: "T1",
703
+ author: { userId: "U1", isBot: false },
704
+ },
705
+ handler: acceptHandler(),
706
+ send,
707
+ credentials: { botToken: "xoxb-test" },
708
+ host: testHost(),
709
+ log: createSlackLogger((line) => lines.push(line)),
710
+ });
711
+ expect(calls).toHaveLength(1);
712
+ expect(calls[0]!.message).toContain("<slack_thread>");
713
+ expect(calls[0]!.message).toContain("Monitor alert: rate limited");
714
+ expect(calls[0]!.message).toContain("@BJENNY what happened here?");
715
+ expect(calls[0]!.message).not.toMatch(/\[reply\].*what happened here\?/);
716
+ expect(lines.some((line) => line.includes("thread context loaded"))).toBe(
717
+ true
718
+ );
719
+ vi.restoreAllMocks();
720
+ });
721
+
722
+ it("loads slack_thread_delta on warm continuations", async () => {
723
+ const spy = vi.spyOn(slackApi, "callSlackApiSoft").mockResolvedValue({
724
+ ok: true,
725
+ messages: [
726
+ { ts: "1.0", bot_id: "BDD", text: "parent alert" },
727
+ { ts: "1.5", bot_id: "BJENNY", text: "first reply" },
728
+ // Non-trigger reply after our last post — appears in the delta.
729
+ // The triggering ask (3.0) is excluded from the block and appended
730
+ // as user text at the end of the prompt.
731
+ { ts: "2.0", user: "U2", text: "also looking" },
732
+ { ts: "3.0", user: "U1", text: "any leads?" },
733
+ ],
734
+ has_more: false,
735
+ });
736
+ const { send, calls } = createSendRecorder();
737
+ await dispatchInboundMessage({
738
+ kind: "app_mention",
739
+ message: {
740
+ text: "any leads?",
741
+ body: "any leads?",
742
+ markdown: "any leads?",
743
+ ts: "3.0",
744
+ threadTs: "1.0",
745
+ channelId: "C1",
746
+ teamId: "T1",
747
+ author: { userId: "U1", isBot: false },
748
+ },
749
+ handler: acceptHandler(),
750
+ send,
751
+ credentials: { botToken: "xoxb-test" },
752
+ host: testHost(),
753
+ hasContinuationSession: async () => true,
754
+ // Agent watermark (not Datadog's parent) — human reply at 2.0 is in delta.
755
+ getContinuationLastBotMessageTs: async () => "1.5",
756
+ });
757
+ expect(spy).toHaveBeenCalled();
758
+ expect(calls[0]?.message).toContain("<slack_thread_delta>");
759
+ expect(calls[0]?.message).toContain("also looking");
760
+ expect(calls[0]?.message).not.toContain("<slack_thread>");
761
+ expect(calls[0]?.options?.admission).toBe("coalesce");
762
+ spy.mockRestore();
763
+ });
764
+
765
+ it("releases ask claim when send fails", async () => {
766
+ const root = await mkdtemp(join(tmpdir(), "dispatch-claim-release-"));
767
+ cleanups.push(() => rm(root, { recursive: true, force: true }));
768
+ const host = testHost({
769
+ nudgeRoot: join(root, "nudges"),
770
+ askRoot: join(root, "asks"),
771
+ });
772
+ const send: SendMessageFn = async () => {
773
+ throw new Error("boom");
774
+ };
775
+ await dispatchInboundMessage({
776
+ kind: "app_mention",
777
+ message: {
778
+ text: "hello",
779
+ body: "hello",
780
+ markdown: "hello",
781
+ ts: "2.0",
782
+ threadTs: "1.0",
783
+ channelId: "C1",
784
+ teamId: "T1",
785
+ author: { userId: "U1", isBot: false },
786
+ },
787
+ handler: acceptHandler(),
788
+ send,
789
+ credentials: undefined,
790
+ host,
791
+ log: createSlackLogger(() => undefined),
792
+ });
793
+ expect(await host.slackNudges?.tryClaimAsk("C1", "2.0")).toBe(true);
794
+ });
795
+ });