@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,267 +0,0 @@
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
- });
@@ -1,363 +0,0 @@
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
- });