@cursor/july 0.1.26 → 0.1.27

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