@cursor/july 0.1.20 → 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 (212) hide show
  1. package/dist/bin/agent-serve.js +0 -0
  2. package/dist/channels/github/instrument.d.ts +20 -0
  3. package/dist/channels/github/instrument.d.ts.map +1 -0
  4. package/dist/channels/slack/api.d.ts +2 -0
  5. package/dist/channels/slack/api.d.ts.map +1 -1
  6. package/dist/channels/slack/api.js +3 -0
  7. package/dist/channels/slack/bot-mentions.d.ts +106 -0
  8. package/dist/channels/slack/bot-mentions.d.ts.map +1 -0
  9. package/dist/channels/slack/bot-mentions.js +243 -0
  10. package/dist/channels/slack/dispatch.d.ts +6 -0
  11. package/dist/channels/slack/dispatch.d.ts.map +1 -1
  12. package/dist/channels/slack/dispatch.js +24 -3
  13. package/dist/channels/slack/inbound.d.ts +10 -1
  14. package/dist/channels/slack/inbound.d.ts.map +1 -1
  15. package/dist/channels/slack/inbound.js +12 -4
  16. package/dist/channels/slack/index.d.ts +1 -0
  17. package/dist/channels/slack/index.d.ts.map +1 -1
  18. package/dist/channels/slack/index.js +1 -0
  19. package/dist/channels/slack/slack-channel.d.ts.map +1 -1
  20. package/dist/channels/slack/slack-channel.js +61 -22
  21. package/dist/channels/slack/types.d.ts +58 -0
  22. package/dist/channels/slack/types.d.ts.map +1 -1
  23. package/dist/docs/404.html +1 -1
  24. package/dist/docs/ab.html +2 -2
  25. package/dist/docs/assets/{app.T-5ImnM3.js → app.jDxLzWv4.js} +1 -1
  26. package/dist/docs/assets/chunks/@localSearchIndexroot.DoJHJjqF.js +1 -0
  27. package/dist/docs/assets/chunks/{VPLocalSearchBox.OMGMWH6H.js → VPLocalSearchBox.Y6bDR1-a.js} +1 -1
  28. package/dist/docs/assets/chunks/{theme.DLg4yB4D.js → theme.CLazCWlJ.js} +2 -2
  29. package/dist/docs/building-with-agents.html +2 -2
  30. package/dist/docs/concepts.html +2 -2
  31. package/dist/docs/deployment.html +2 -2
  32. package/dist/docs/evals.html +2 -2
  33. package/dist/docs/example-agents/approval-buddy.html +2 -2
  34. package/dist/docs/example-agents/benny.html +2 -2
  35. package/dist/docs/example-agents/bugbot.html +2 -2
  36. package/dist/docs/example-agents/codebase-wiki.html +2 -2
  37. package/dist/docs/example-agents/codeowners-review.html +2 -2
  38. package/dist/docs/example-agents/concierge.html +2 -2
  39. package/dist/docs/example-agents/fsd.html +2 -2
  40. package/dist/docs/example-agents/index.html +2 -2
  41. package/dist/docs/example-agents/knowledge-base.html +2 -2
  42. package/dist/docs/example-agents/oncall.html +2 -2
  43. package/dist/docs/example-agents/security-reviewer.html +2 -2
  44. package/dist/docs/example-agents/slack-agent.html +2 -2
  45. package/dist/docs/example-agents/weather-agent.html +2 -2
  46. package/dist/docs/guides/agent-to-agent.html +2 -2
  47. package/dist/docs/guides/cloud-runtime.html +2 -2
  48. package/dist/docs/guides/github.html +2 -2
  49. package/dist/docs/guides/human-in-the-loop.html +2 -2
  50. package/dist/docs/guides/mcp-oauth.html +2 -2
  51. package/dist/docs/guides/slack.html +2 -2
  52. package/dist/docs/guides/webhooks.html +2 -2
  53. package/dist/docs/hillclimbing.html +2 -2
  54. package/dist/docs/index.html +2 -2
  55. package/dist/docs/quickstart.html +2 -2
  56. package/dist/docs/reference/agent-config.html +2 -2
  57. package/dist/docs/reference/channels.html +2 -2
  58. package/dist/docs/reference/cli.html +2 -2
  59. package/dist/docs/reference/connections.html +2 -2
  60. package/dist/docs/reference/hooks.html +2 -2
  61. package/dist/docs/reference/http-api.html +2 -2
  62. package/dist/docs/reference/instructions.html +2 -2
  63. package/dist/docs/reference/playground.html +2 -2
  64. package/dist/docs/reference/project-layout.html +2 -2
  65. package/dist/docs/reference/prompt.html +2 -2
  66. package/dist/docs/reference/schedules.html +2 -2
  67. package/dist/docs/reference/sessions.html +2 -2
  68. package/dist/docs/reference/skills.html +2 -2
  69. package/dist/docs/reference/subagents.html +2 -2
  70. package/dist/docs/reference/tools.html +2 -2
  71. package/dist/docs/scaffolding-agents.html +2 -2
  72. package/dist/docs/storage.html +2 -2
  73. package/dist/docs/troubleshooting.html +2 -2
  74. package/dist/internal/json-dir-store.d.ts +32 -0
  75. package/dist/internal/json-dir-store.d.ts.map +1 -0
  76. package/dist/internal/json-dir-store.js +100 -0
  77. package/dist/internal/resolved-connections.d.ts +1 -1
  78. package/dist/internal/resolved-connections.d.ts.map +1 -1
  79. package/dist/internal/resolved-connections.js +10 -0
  80. package/dist/internal/session-engine.d.ts +4 -1
  81. package/dist/internal/session-engine.d.ts.map +1 -1
  82. package/dist/internal/session-engine.js +13 -2
  83. package/dist/playground/assets/index-CjOQ4hN9.css +1 -0
  84. package/dist/playground/assets/index-dshZQJCp.js +85 -0
  85. package/dist/playground/index.html +2 -2
  86. package/dist/types.d.ts +7 -0
  87. package/dist/types.d.ts.map +1 -1
  88. package/dist/types.js +9 -0
  89. package/package.json +24 -24
  90. package/src/bin/agent-serve.version.test.ts +62 -0
  91. package/src/channels/github/api.test.ts +64 -0
  92. package/src/channels/github/auth.test.ts +105 -0
  93. package/src/channels/github/cursor-account.test.ts +204 -0
  94. package/src/channels/github/forward.test.ts +457 -0
  95. package/src/channels/github/github.test.ts +937 -0
  96. package/src/channels/github/replay.test.ts +179 -0
  97. package/src/channels/slack/api.post-message.test.ts +148 -0
  98. package/src/channels/slack/api.ts +5 -0
  99. package/src/channels/slack/approvals.test.ts +328 -0
  100. package/src/channels/slack/block-actions.test.ts +452 -0
  101. package/src/channels/slack/bot-mentions.test.ts +217 -0
  102. package/src/channels/slack/bot-mentions.ts +315 -0
  103. package/src/channels/slack/channel-watch.test.ts +363 -0
  104. package/src/channels/slack/cursor-account.test.ts +253 -0
  105. package/src/channels/slack/defaults.final-post.test.ts +182 -0
  106. package/src/channels/slack/dispatch.test.ts +795 -0
  107. package/src/channels/slack/dispatch.ts +27 -1
  108. package/src/channels/slack/eval-directive.test.ts +273 -0
  109. package/src/channels/slack/inbound.ts +25 -4
  110. package/src/channels/slack/index.ts +1 -0
  111. package/src/channels/slack/message-body.test.ts +54 -0
  112. package/src/channels/slack/nudge-store.test.ts +143 -0
  113. package/src/channels/slack/slack-channel.ts +66 -8
  114. package/src/channels/slack/slack.test.ts +391 -0
  115. package/src/channels/slack/stop.test.ts +23 -0
  116. package/src/channels/slack/thread-context.test.ts +202 -0
  117. package/src/channels/slack/types.ts +60 -0
  118. package/src/evals/assertions.test.ts +580 -0
  119. package/src/evals/expect.test.ts +144 -0
  120. package/src/evals/judge.test.ts +181 -0
  121. package/src/evals/loaders.test.ts +132 -0
  122. package/src/evals/matchers.test.ts +95 -0
  123. package/src/evals/reporters.test.ts +303 -0
  124. package/src/evals/run-facts.test.ts +259 -0
  125. package/src/internal/ab-snapshot.test.ts +325 -0
  126. package/src/internal/approval-gate.test.ts +49 -0
  127. package/src/internal/approvals.integration.test.ts +383 -0
  128. package/src/internal/authored-loaders.test.ts +31 -0
  129. package/src/internal/builtin-tools/reminders.test.ts +201 -0
  130. package/src/internal/channel-route-schema.test.ts +294 -0
  131. package/src/internal/chat-attach.test.ts +262 -0
  132. package/src/internal/cli-deploy.test.ts +1991 -0
  133. package/src/internal/cli-mcp.test.ts +789 -0
  134. package/src/internal/cli-skills.test.ts +133 -0
  135. package/src/internal/cli-slack.test.ts +1647 -0
  136. package/src/internal/cloud-merge.test.ts +74 -0
  137. package/src/internal/cron.test.ts +22 -0
  138. package/src/internal/cursor/account-mcp.test.ts +807 -0
  139. package/src/internal/cursor/backend-client.test.ts +591 -0
  140. package/src/internal/cursor/credentials.test.ts +351 -0
  141. package/src/internal/cursor/github-credentials.test.ts +136 -0
  142. package/src/internal/cursor-account-mcp-auth.test.ts +310 -0
  143. package/src/internal/cursor-account.integration.test.ts +441 -0
  144. package/src/internal/cursor-event-relay.test.ts +746 -0
  145. package/src/internal/cursor-github-credentials.integration.test.ts +271 -0
  146. package/src/internal/cursor-slack-relay.test.ts +525 -0
  147. package/src/internal/deploy-source.test.ts +111 -0
  148. package/src/internal/discovery.builtin-tools.test.ts +94 -0
  149. package/src/internal/discovery.concurrency.test.ts +60 -0
  150. package/src/internal/discovery.cursor-account.test.ts +133 -0
  151. package/src/internal/discovery.cwd.test.ts +83 -0
  152. package/src/internal/discovery.hosting.test.ts +80 -0
  153. package/src/internal/discovery.identity.test.ts +44 -0
  154. package/src/internal/docs-site.test.ts +66 -0
  155. package/src/internal/duration.test.ts +29 -0
  156. package/src/internal/eval-judge-model.test.ts +187 -0
  157. package/src/internal/eval-run-store.cancel.test.ts +142 -0
  158. package/src/internal/eval-run-store.storage.test.ts +211 -0
  159. package/src/internal/eval-runner.http.test.ts +403 -0
  160. package/src/internal/eval-runner.run.test.ts +928 -0
  161. package/src/internal/evals-client.test.ts +307 -0
  162. package/src/internal/event-mapper.test.ts +243 -0
  163. package/src/internal/github-fanout.test.ts +213 -0
  164. package/src/internal/handleAgentServeTrigger.test.ts +179 -0
  165. package/src/internal/host-kv.test.ts +82 -0
  166. package/src/internal/host-platforms.test.ts +126 -0
  167. package/src/internal/http-channel.test.ts +402 -0
  168. package/src/internal/init-project.test.ts +269 -0
  169. package/src/internal/install-cursor-skills.test.ts +262 -0
  170. package/src/internal/local-env.test.ts +100 -0
  171. package/src/internal/log-ring.test.ts +31 -0
  172. package/src/internal/logs-client.test.ts +350 -0
  173. package/src/internal/mcp-endpoint.test.ts +436 -0
  174. package/src/internal/mcp-host.test.ts +298 -0
  175. package/src/internal/mcp-oauth.test.ts +148 -0
  176. package/src/internal/net.test.ts +17 -0
  177. package/src/internal/peer-connections.test.ts +128 -0
  178. package/src/internal/peer-mcp.integration.test.ts +289 -0
  179. package/src/internal/playground/toolchain.test.ts +53 -0
  180. package/src/internal/playground-cli.test.ts +187 -0
  181. package/src/internal/playground-proxy.test.ts +376 -0
  182. package/src/internal/prompt-context.integration.test.ts +232 -0
  183. package/src/internal/prompt-context.test.ts +127 -0
  184. package/src/internal/reminder-runner.test.ts +390 -0
  185. package/src/internal/reminder-store.test.ts +53 -0
  186. package/src/internal/request-headers.test.ts +27 -0
  187. package/src/internal/resolve-prod-target.test.ts +787 -0
  188. package/src/internal/resolved-connections.test.ts +295 -0
  189. package/src/internal/resolved-connections.ts +18 -1
  190. package/src/internal/router.test.ts +57 -0
  191. package/src/internal/sdk-runner.test.ts +290 -0
  192. package/src/internal/session-engine.coalesce.test.ts +169 -0
  193. package/src/internal/session-engine.concurrency.test.ts +250 -0
  194. package/src/internal/session-engine.host-oauth-mcp.test.ts +110 -0
  195. package/src/internal/session-engine.interrupt.test.ts +577 -0
  196. package/src/internal/session-engine.storage.test.ts +547 -0
  197. package/src/internal/session-engine.ts +15 -1
  198. package/src/internal/session-urls.test.ts +28 -0
  199. package/src/internal/sessions-client.test.ts +518 -0
  200. package/src/internal/storage-coordinator.test.ts +517 -0
  201. package/src/internal/tool-call.test.ts +458 -0
  202. package/src/internal/tool-result.test.ts +52 -0
  203. package/src/internal/trajectory.approvals.test.ts +83 -0
  204. package/src/internal/trajectory.subagents.test.ts +198 -0
  205. package/src/internal/turn-governor.test.ts +137 -0
  206. package/src/internal/update-check.test.ts +485 -0
  207. package/src/internal/workspace.test.ts +81 -0
  208. package/src/storage-backends/cursor-hosted.test.ts +121 -0
  209. package/src/types.ts +12 -0
  210. package/dist/docs/assets/chunks/@localSearchIndexroot.CvejISXF.js +0 -1
  211. package/dist/playground/assets/index-CidizGZv.css +0 -1
  212. package/dist/playground/assets/index-CuiAKZaP.js +0 -85
@@ -0,0 +1,937 @@
1
+ import { createHmac } from "node:crypto";
2
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3
+ import { scmMetadataToGitHubPayload } from "../../internal/cursor-event-relay.js";
4
+ import type {
5
+ ChannelHandlerArgs,
6
+ ChannelRouteHandler,
7
+ ChannelSession,
8
+ SendMessageFn,
9
+ } from "../../types.js";
10
+ import { formatGitHubOutboundComment } from "./binding.js";
11
+ import {
12
+ clearGitHubAuthCaches,
13
+ defaultGitHubAuth,
14
+ defaultGitHubPullRequestMessage,
15
+ defaultOnComment,
16
+ type GitHubEventContext,
17
+ type GitHubRawEvent,
18
+ githubChannel,
19
+ githubPullRequestContinuationKey,
20
+ isGitHubAppBotAuthor,
21
+ parseGitHubPullRequestEvent,
22
+ shouldDispatchGitHubComment,
23
+ verifyGitHubWebhookSignature,
24
+ } from "./index.js";
25
+
26
+ describe("parseGitHubPullRequestEvent", () => {
27
+ it("normalizes a GitHub pull_request payload", () => {
28
+ const parsed = parseGitHubPullRequestEvent({
29
+ deliveryId: "delivery-1",
30
+ payload: pullRequestPayload(),
31
+ });
32
+
33
+ expect(parsed).toEqual({
34
+ ctx: {
35
+ deliveryId: "delivery-1",
36
+ eventName: "pull_request",
37
+ repository: {
38
+ fullName: "acme/widgets",
39
+ url: "https://github.com/acme/widgets",
40
+ owner: "acme",
41
+ name: "widgets",
42
+ },
43
+ conversation: {
44
+ kind: "pull_request",
45
+ number: 42,
46
+ url: "https://github.com/acme/widgets/pull/42",
47
+ },
48
+ senderLogin: "sender",
49
+ sender: { login: "sender" },
50
+ },
51
+ pullRequest: {
52
+ action: "opened",
53
+ number: 42,
54
+ url: "https://github.com/acme/widgets/pull/42",
55
+ title: "Add widgets",
56
+ body: "PR body",
57
+ headRef: "feature/widgets",
58
+ headSha: "abc123",
59
+ baseRef: "main",
60
+ draft: false,
61
+ merged: false,
62
+ authorLogin: "octocat",
63
+ },
64
+ });
65
+ });
66
+
67
+ it("rejects malformed payloads", () => {
68
+ expect(
69
+ parseGitHubPullRequestEvent({
70
+ payload: { action: "opened" },
71
+ })
72
+ ).toBeNull();
73
+ });
74
+ });
75
+
76
+ describe("GitHub pull request prompt helpers", () => {
77
+ it("builds stable continuation keys and context-rich messages", () => {
78
+ const parsed = parseGitHubPullRequestEvent({
79
+ payload: pullRequestPayload(),
80
+ });
81
+ expect(parsed).not.toBeNull();
82
+ if (parsed === null) {
83
+ return;
84
+ }
85
+
86
+ expect(githubPullRequestContinuationKey(parsed.ctx)).toBe(
87
+ "github-pr:acme/widgets#42"
88
+ );
89
+ const message = defaultGitHubPullRequestMessage(
90
+ parsed.ctx,
91
+ parsed.pullRequest
92
+ );
93
+ expect(message).toContain("GitHub pull request trigger: acme/widgets#42");
94
+ expect(message).toContain("Head: feature/widgets (abc123)");
95
+ });
96
+
97
+ it("derives the turn auth from the delivery actor", () => {
98
+ const parsed = parseGitHubPullRequestEvent({
99
+ payload: pullRequestPayload(),
100
+ });
101
+ expect(parsed).not.toBeNull();
102
+ if (parsed === null) {
103
+ return;
104
+ }
105
+
106
+ expect(defaultGitHubAuth(parsed.ctx)).toEqual({
107
+ authenticator: "github-webhook",
108
+ principalId: "sender",
109
+ principalType: "user",
110
+ attributes: expect.objectContaining({ repository: "acme/widgets" }),
111
+ });
112
+ });
113
+ });
114
+
115
+ describe("defaultOnComment / mention gate", () => {
116
+ it("dispatches only when the bot is @mentioned", () => {
117
+ expect(
118
+ shouldDispatchGitHubComment({
119
+ body: "hey @my-agent please look",
120
+ botName: "my-agent",
121
+ })
122
+ ).toBe(true);
123
+ expect(
124
+ shouldDispatchGitHubComment({
125
+ body: "no mention here",
126
+ botName: "my-agent",
127
+ })
128
+ ).toBe(false);
129
+ expect(
130
+ shouldDispatchGitHubComment({
131
+ body: "@my-agent hi",
132
+ botName: undefined,
133
+ })
134
+ ).toBe(false);
135
+ });
136
+
137
+ it("ignores comments from the GitHub App bot actor", () => {
138
+ expect(
139
+ shouldDispatchGitHubComment({
140
+ body: "hey @my-agent please look",
141
+ author: { login: "my-agent[bot]", type: "Bot" },
142
+ botName: "my-agent",
143
+ })
144
+ ).toBe(false);
145
+ expect(
146
+ isGitHubAppBotAuthor({ login: "my-agent[bot]", type: "Bot" }, "my-agent")
147
+ ).toBe(true);
148
+ });
149
+
150
+ it("returns auth when mentioned", () => {
151
+ const ctx = {
152
+ eventName: "issue_comment",
153
+ repository: {
154
+ fullName: "acme/widgets",
155
+ url: "https://github.com/acme/widgets",
156
+ owner: "acme",
157
+ name: "widgets",
158
+ },
159
+ conversation: {
160
+ kind: "pull_request" as const,
161
+ number: 42,
162
+ url: "https://github.com/acme/widgets/pull/42",
163
+ },
164
+ senderLogin: "sender",
165
+ };
166
+ expect(
167
+ defaultOnComment(
168
+ ctx,
169
+ {
170
+ id: 1,
171
+ body: "@my-agent triage this",
172
+ url: "https://github.com/acme/widgets/issues/42#issuecomment-1",
173
+ htmlUrl: "https://github.com/acme/widgets/issues/42#issuecomment-1",
174
+ },
175
+ { botName: "my-agent" }
176
+ )
177
+ ).toEqual({ auth: defaultGitHubAuth(ctx) });
178
+ });
179
+ });
180
+
181
+ describe("verifyGitHubWebhookSignature", () => {
182
+ it("accepts a valid sha256 signature", async () => {
183
+ const body = JSON.stringify(pullRequestPayload());
184
+ const signature = `sha256=${createHmac("sha256", "secret")
185
+ .update(body)
186
+ .digest("hex")}`;
187
+
188
+ await expect(
189
+ verifyGitHubWebhookSignature({
190
+ body,
191
+ signatureHeader: signature,
192
+ secret: "secret",
193
+ })
194
+ ).resolves.toBe("verified");
195
+ });
196
+
197
+ it("rejects mismatched signatures", async () => {
198
+ await expect(
199
+ verifyGitHubWebhookSignature({
200
+ body: "{}",
201
+ signatureHeader: "sha256=bad",
202
+ secret: "secret",
203
+ })
204
+ ).resolves.toBe("mismatch");
205
+ });
206
+ });
207
+
208
+ describe("githubChannel", () => {
209
+ let priorWebhookSecret: string | undefined;
210
+
211
+ beforeEach(() => {
212
+ priorWebhookSecret = process.env.GITHUB_WEBHOOK_SECRET;
213
+ delete process.env.GITHUB_WEBHOOK_SECRET;
214
+ });
215
+
216
+ afterEach(() => {
217
+ if (priorWebhookSecret === undefined) {
218
+ delete process.env.GITHUB_WEBHOOK_SECRET;
219
+ } else {
220
+ process.env.GITHUB_WEBHOOK_SECRET = priorWebhookSecret;
221
+ }
222
+ });
223
+
224
+ it("dispatches default review triggers as the delivery actor", async () => {
225
+ const body = JSON.stringify(pullRequestPayload());
226
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
227
+ const response = await routeHandler(
228
+ githubChannel({
229
+ auth: [],
230
+ cloud: true,
231
+ })
232
+ )(
233
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
234
+ method: "POST",
235
+ headers: {
236
+ "x-github-event": "pull_request",
237
+ "x-github-delivery": "delivery-1",
238
+ "content-type": "application/json",
239
+ },
240
+ body,
241
+ }),
242
+ handlerArgs(send)
243
+ );
244
+
245
+ await expect(response.json()).resolves.toEqual({
246
+ ok: true,
247
+ sessionId: "session-1",
248
+ isNew: true,
249
+ continuationToken: "github-pr:acme/widgets#42",
250
+ });
251
+ expect(send).toHaveBeenCalledExactlyOnceWith(
252
+ expect.stringContaining("GitHub pull request trigger: acme/widgets#42"),
253
+ expect.objectContaining({
254
+ auth: expect.objectContaining({
255
+ authenticator: "github-webhook",
256
+ principalId: "sender",
257
+ principalType: "user",
258
+ }),
259
+ continuationToken: "github-pr:acme/widgets#42",
260
+ title: "GitHub acme/widgets#42",
261
+ cloud: {
262
+ repos: [
263
+ {
264
+ url: "https://github.com/acme/widgets",
265
+ prUrl: "https://github.com/acme/widgets/pull/42",
266
+ startingRef: "abc123",
267
+ },
268
+ ],
269
+ },
270
+ })
271
+ );
272
+ });
273
+
274
+ it("ignores actions outside the default gate without starting a session", async () => {
275
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
276
+ const response = await routeHandler(githubChannel({ auth: [] }))(
277
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
278
+ method: "POST",
279
+ headers: { "x-github-event": "pull_request" },
280
+ body: JSON.stringify(pullRequestPayload({ action: "closed" })),
281
+ }),
282
+ handlerArgs(send)
283
+ );
284
+
285
+ await expect(response.json()).resolves.toEqual({
286
+ ok: true,
287
+ skipped: true,
288
+ reason: "not_dispatched",
289
+ action: "closed",
290
+ });
291
+ expect(send).not.toHaveBeenCalled();
292
+ });
293
+
294
+ it("replaces the default gate with the onPullRequest hook", async () => {
295
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
296
+ const handler = routeHandler(
297
+ githubChannel({
298
+ auth: [],
299
+ onPullRequest: (ctx, pr) =>
300
+ pr.action === "synchronize"
301
+ ? { auth: defaultGitHubAuth(ctx), context: ["Focus on the diff."] }
302
+ : null,
303
+ })
304
+ );
305
+
306
+ const ignored = await handler(
307
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
308
+ method: "POST",
309
+ headers: { "x-github-event": "pull_request" },
310
+ body: JSON.stringify(pullRequestPayload({ action: "opened" })),
311
+ }),
312
+ handlerArgs(send)
313
+ );
314
+ await expect(ignored.json()).resolves.toEqual({
315
+ ok: true,
316
+ skipped: true,
317
+ reason: "not_dispatched",
318
+ action: "opened",
319
+ });
320
+ expect(send).not.toHaveBeenCalled();
321
+
322
+ const dispatched = await handler(
323
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
324
+ method: "POST",
325
+ headers: { "x-github-event": "pull_request" },
326
+ body: JSON.stringify(pullRequestPayload({ action: "synchronize" })),
327
+ }),
328
+ handlerArgs(send)
329
+ );
330
+ await expect(dispatched.json()).resolves.toMatchObject({ ok: true });
331
+ expect(send).toHaveBeenCalledExactlyOnceWith(
332
+ expect.stringContaining("Focus on the diff."),
333
+ expect.anything()
334
+ );
335
+ });
336
+
337
+ it("dispatches @mention issue comments via the default onComment gate", async () => {
338
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
339
+ const response = await routeHandler(
340
+ githubChannel({
341
+ auth: [],
342
+ botName: "my-agent",
343
+ })
344
+ )(
345
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
346
+ method: "POST",
347
+ headers: {
348
+ "x-github-event": "issue_comment",
349
+ "x-github-delivery": "delivery-comment",
350
+ },
351
+ body: JSON.stringify(issueCommentPayload("@my-agent please triage")),
352
+ }),
353
+ handlerArgs(send)
354
+ );
355
+
356
+ await expect(response.json()).resolves.toMatchObject({ ok: true });
357
+ expect(send).toHaveBeenCalledExactlyOnceWith(
358
+ "please triage",
359
+ expect.objectContaining({
360
+ continuationToken: "github-pr:acme/widgets#42",
361
+ auth: expect.objectContaining({ principalId: "sender" }),
362
+ })
363
+ );
364
+ });
365
+
366
+ it("dispatches relay-stubbed issue_comment payloads (scm-events metadata)", async () => {
367
+ // Regression guard for the /v0/scm-events comment gap: a metadata-only
368
+ // envelope with a comment snapshot must clear the issue_comment Zod
369
+ // schema and dispatch, without a payload round-trip to GitHub.
370
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
371
+ const response = await routeHandler(
372
+ githubChannel({ auth: [], botName: "my-agent" })
373
+ )(
374
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
375
+ method: "POST",
376
+ headers: {
377
+ "x-github-event": "issue_comment",
378
+ "x-github-delivery": "delivery-relay-comment",
379
+ },
380
+ body: JSON.stringify(
381
+ scmMetadataToGitHubPayload({
382
+ repo: "acme/widgets",
383
+ action: "created",
384
+ prNumber: 42,
385
+ prUrl: "https://github.com/acme/widgets/pull/42",
386
+ comment: {
387
+ id: 7,
388
+ body: "@my-agent please triage",
389
+ htmlUrl: "https://github.com/acme/widgets/pull/42#issuecomment-7",
390
+ authorLogin: "sender",
391
+ },
392
+ })
393
+ ),
394
+ }),
395
+ handlerArgs(send)
396
+ );
397
+
398
+ await expect(response.json()).resolves.toMatchObject({ ok: true });
399
+ expect(send).toHaveBeenCalledExactlyOnceWith(
400
+ "please triage",
401
+ expect.objectContaining({
402
+ continuationToken: "github-pr:acme/widgets#42",
403
+ auth: expect.objectContaining({ principalId: "sender" }),
404
+ })
405
+ );
406
+ });
407
+
408
+ it("skips relay-stubbed comments from the agent's own bot (self/bot-loop gate)", async () => {
409
+ // Regression guard: the relay stub must carry user.type through so
410
+ // isGitHubAppBotAuthor keeps failing CLOSED for the agent's own
411
+ // comments on the scm-events path. Without it, a bot comment that
412
+ // @mentions the agent would wake it again (self-wake loop).
413
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
414
+ const response = await routeHandler(
415
+ githubChannel({ auth: [], botName: "my-agent" })
416
+ )(
417
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
418
+ method: "POST",
419
+ headers: {
420
+ "x-github-event": "issue_comment",
421
+ "x-github-delivery": "delivery-relay-bot-comment",
422
+ },
423
+ body: JSON.stringify(
424
+ scmMetadataToGitHubPayload({
425
+ repo: "acme/widgets",
426
+ action: "created",
427
+ prNumber: 42,
428
+ prUrl: "https://github.com/acme/widgets/pull/42",
429
+ comment: {
430
+ id: 8,
431
+ body: "@my-agent looks done to me",
432
+ htmlUrl: "https://github.com/acme/widgets/pull/42#issuecomment-8",
433
+ authorLogin: "my-agent[bot]",
434
+ authorType: "Bot",
435
+ },
436
+ })
437
+ ),
438
+ }),
439
+ handlerArgs(send)
440
+ );
441
+
442
+ await expect(response.json()).resolves.toEqual({
443
+ ok: true,
444
+ skipped: true,
445
+ reason: "ignored_comment",
446
+ });
447
+ expect(send).not.toHaveBeenCalled();
448
+ });
449
+
450
+ it("skips issue comments without an @mention", async () => {
451
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
452
+ const response = await routeHandler(
453
+ githubChannel({ auth: [], botName: "my-agent" })
454
+ )(
455
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
456
+ method: "POST",
457
+ headers: { "x-github-event": "issue_comment" },
458
+ body: JSON.stringify(issueCommentPayload("no bot here")),
459
+ }),
460
+ handlerArgs(send)
461
+ );
462
+ await expect(response.json()).resolves.toEqual({
463
+ ok: true,
464
+ skipped: true,
465
+ reason: "not_dispatched",
466
+ });
467
+ expect(send).not.toHaveBeenCalled();
468
+ });
469
+
470
+ it("dispatches failed check_suite via onCheckSuite when a PR is attached", async () => {
471
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
472
+ const response = await routeHandler(
473
+ githubChannel({
474
+ auth: [],
475
+ onCheckSuite: (ctx, suite) =>
476
+ suite.action === "completed" &&
477
+ suite.conclusion === "failure" &&
478
+ suite.app.slug === "github-actions" &&
479
+ suite.pullRequests.length > 0
480
+ ? {
481
+ auth: defaultGitHubAuth(ctx),
482
+ context: [
483
+ `Triage failed check suite ${suite.checkSuiteId} at ${suite.headSha}.`,
484
+ ],
485
+ }
486
+ : null,
487
+ })
488
+ )(
489
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
490
+ method: "POST",
491
+ headers: { "x-github-event": "check_suite" },
492
+ body: JSON.stringify(checkSuitePayload()),
493
+ }),
494
+ handlerArgs(send)
495
+ );
496
+
497
+ await expect(response.json()).resolves.toMatchObject({ ok: true });
498
+ expect(send).toHaveBeenCalledExactlyOnceWith(
499
+ expect.stringContaining("Triage failed check suite 99 at deadbeef"),
500
+ expect.objectContaining({
501
+ continuationToken: "github-pr:acme/widgets#42",
502
+ })
503
+ );
504
+ });
505
+
506
+ it("requires a valid signature when a webhook secret is configured", async () => {
507
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
508
+ const response = await routeHandler(
509
+ githubChannel({
510
+ credentials: { webhookSecret: "secret" },
511
+ })
512
+ )(
513
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
514
+ method: "POST",
515
+ headers: { "x-github-event": "pull_request" },
516
+ body: JSON.stringify(pullRequestPayload()),
517
+ }),
518
+ handlerArgs(send)
519
+ );
520
+
521
+ expect(response.status).toBe(401);
522
+ await expect(response.json()).resolves.toEqual({
523
+ ok: false,
524
+ error: "missing_signature",
525
+ });
526
+ expect(send).not.toHaveBeenCalled();
527
+ });
528
+
529
+ it("returns 202 and schedules waitUntil for task dispatch", async () => {
530
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
531
+ const waitUntil = vi.fn();
532
+ const task = vi.fn(async () => {});
533
+ const response = await routeHandler(
534
+ githubChannel({
535
+ auth: [],
536
+ onPullRequest: async () => ({ task }),
537
+ })
538
+ )(
539
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
540
+ method: "POST",
541
+ headers: { "x-github-event": "pull_request" },
542
+ body: JSON.stringify(pullRequestPayload()),
543
+ }),
544
+ { ...handlerArgs(send), waitUntil }
545
+ );
546
+
547
+ expect(response.status).toBe(202);
548
+ await expect(response.json()).resolves.toEqual({
549
+ ok: true,
550
+ dispatched: true,
551
+ mode: "task",
552
+ action: "opened",
553
+ prUrl: "https://github.com/acme/widgets/pull/42",
554
+ headSha: "abc123",
555
+ });
556
+ expect(waitUntil).toHaveBeenCalledTimes(1);
557
+ expect(task).not.toHaveBeenCalled();
558
+ expect(send).not.toHaveBeenCalled();
559
+
560
+ const scheduled = waitUntil.mock.calls[0]?.[0] as Promise<unknown>;
561
+ await scheduled;
562
+ expect(task).toHaveBeenCalledTimes(1);
563
+ });
564
+
565
+ it("exposes ctx.github and ctx.thread to inbound hooks", async () => {
566
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
567
+ let captured: GitHubEventContext | undefined;
568
+ await routeHandler(
569
+ githubChannel({
570
+ auth: [],
571
+ onPullRequest: (ctx, pr) => {
572
+ captured = ctx;
573
+ return pr.action === "opened"
574
+ ? { auth: defaultGitHubAuth(ctx) }
575
+ : null;
576
+ },
577
+ })
578
+ )(
579
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
580
+ method: "POST",
581
+ headers: { "x-github-event": "pull_request" },
582
+ body: JSON.stringify(pullRequestPayload()),
583
+ }),
584
+ handlerArgs(send)
585
+ );
586
+
587
+ expect(captured).toBeDefined();
588
+ expect(typeof captured?.github.request).toBe("function");
589
+ expect(typeof captured?.github.createStatus).toBe("function");
590
+ expect(typeof captured?.github.getOctokit).toBe("function");
591
+ expect(typeof captured?.thread.post).toBe("function");
592
+ expect(captured?.thread.kind).toBe("pull_request");
593
+ expect(captured?.github.repository.fullName).toBe("acme/widgets");
594
+ });
595
+
596
+ it("runs onStatus task dispatch with ctx.github.createStatus", async () => {
597
+ clearGitHubAuthCaches();
598
+ const fetchMock = vi.fn(
599
+ async () => new Response(JSON.stringify({}), { status: 201 })
600
+ );
601
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
602
+ const waitUntil = vi.fn();
603
+ const response = await routeHandler(
604
+ githubChannel({
605
+ auth: [],
606
+ credentials: { token: "test-token" },
607
+ api: { fetch: fetchMock as unknown as typeof fetch },
608
+ onStatus: (ctx, status) =>
609
+ status.state === "failure"
610
+ ? {
611
+ task: async () => {
612
+ await ctx.github.createStatus({
613
+ sha: status.sha,
614
+ state: "pending",
615
+ context: "agent/triage",
616
+ });
617
+ },
618
+ }
619
+ : null,
620
+ })
621
+ )(
622
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
623
+ method: "POST",
624
+ headers: { "x-github-event": "status" },
625
+ body: JSON.stringify(statusPayload()),
626
+ }),
627
+ { ...handlerArgs(send), waitUntil }
628
+ );
629
+
630
+ expect(response.status).toBe(202);
631
+ await expect(response.json()).resolves.toMatchObject({
632
+ ok: true,
633
+ dispatched: true,
634
+ mode: "task",
635
+ event: "status",
636
+ state: "failure",
637
+ sha: "deadbeef",
638
+ });
639
+ expect(send).not.toHaveBeenCalled();
640
+
641
+ const scheduled = waitUntil.mock.calls[0]?.[0] as Promise<unknown>;
642
+ await scheduled;
643
+ expect(fetchMock).toHaveBeenCalledOnce();
644
+ const call = fetchMock.mock.calls[0];
645
+ expect(call).toBeDefined();
646
+ const [url, init] = call as unknown as [unknown, RequestInit | undefined];
647
+ expect(String(url)).toContain("/repos/acme/widgets/statuses/deadbeef");
648
+ expect(init?.method).toBe("POST");
649
+ });
650
+
651
+ it("dispatches unknown events through onEvent", async () => {
652
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
653
+ let capturedEvent: GitHubRawEvent | undefined;
654
+ const response = await routeHandler(
655
+ githubChannel({
656
+ auth: [],
657
+ onEvent: (ctx, event) => {
658
+ capturedEvent = event;
659
+ return event.eventName === "label"
660
+ ? { auth: defaultGitHubAuth(ctx) }
661
+ : null;
662
+ },
663
+ })
664
+ )(
665
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
666
+ method: "POST",
667
+ headers: {
668
+ "x-github-event": "label",
669
+ "x-github-delivery": "delivery-label",
670
+ },
671
+ body: JSON.stringify(labelPayload()),
672
+ }),
673
+ handlerArgs(send)
674
+ );
675
+
676
+ await expect(response.json()).resolves.toMatchObject({ ok: true });
677
+ expect(capturedEvent?.eventName).toBe("label");
678
+ expect(capturedEvent?.action).toBe("created");
679
+ expect(send).toHaveBeenCalledExactlyOnceWith(
680
+ expect.stringContaining("GitHub label event"),
681
+ expect.objectContaining({
682
+ continuationToken: "github-issue:acme/widgets#42",
683
+ })
684
+ );
685
+ });
686
+
687
+ it("schedules task dispatch for unanchored check_suite events", async () => {
688
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
689
+ const waitUntil = vi.fn();
690
+ const task = vi.fn(async () => {});
691
+ const response = await routeHandler(
692
+ githubChannel({
693
+ auth: [],
694
+ onCheckSuite: async () => ({ task }),
695
+ })
696
+ )(
697
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
698
+ method: "POST",
699
+ headers: { "x-github-event": "check_suite" },
700
+ body: JSON.stringify({
701
+ ...checkSuitePayload(),
702
+ check_suite: {
703
+ ...(checkSuitePayload().check_suite as Record<string, unknown>),
704
+ pull_requests: [],
705
+ },
706
+ }),
707
+ }),
708
+ { ...handlerArgs(send), waitUntil }
709
+ );
710
+
711
+ expect(response.status).toBe(202);
712
+ await expect(response.json()).resolves.toMatchObject({
713
+ ok: true,
714
+ dispatched: true,
715
+ mode: "task",
716
+ event: "check_suite",
717
+ });
718
+ expect(send).not.toHaveBeenCalled();
719
+ const scheduled = waitUntil.mock.calls[0]?.[0] as Promise<unknown>;
720
+ await scheduled;
721
+ expect(task).toHaveBeenCalledTimes(1);
722
+ });
723
+
724
+ it("ignores outbound comments stamped with the agent marker", async () => {
725
+ const send = vi.fn<SendMessageFn>(async () => fakeSession());
726
+ const response = await routeHandler(
727
+ githubChannel({
728
+ auth: [],
729
+ botName: "my-agent",
730
+ })
731
+ )(
732
+ new Request("http://127.0.0.1:3000/v1/channels/github", {
733
+ method: "POST",
734
+ headers: { "x-github-event": "issue_comment" },
735
+ body: JSON.stringify(
736
+ issueCommentPayload(
737
+ formatGitHubOutboundComment("Thanks @my-agent for the help!")
738
+ )
739
+ ),
740
+ }),
741
+ handlerArgs(send)
742
+ );
743
+
744
+ await expect(response.json()).resolves.toEqual({
745
+ ok: true,
746
+ skipped: true,
747
+ reason: "ignored_comment",
748
+ });
749
+ expect(send).not.toHaveBeenCalled();
750
+ });
751
+ });
752
+
753
+ function routeHandler(
754
+ channel: ReturnType<typeof githubChannel>
755
+ ): ChannelRouteHandler {
756
+ const route = channel.routes?.[0];
757
+ if (route === undefined) {
758
+ throw new Error("expected github channel route");
759
+ }
760
+ return route.handler;
761
+ }
762
+
763
+ function handlerArgs(send: SendMessageFn): ChannelHandlerArgs {
764
+ return {
765
+ params: {},
766
+ requestIp: "127.0.0.1",
767
+ auth: null,
768
+ host: {
769
+ mcp: {
770
+ names: () => [],
771
+ listTools: async () => [],
772
+ callTool: async () => ({ content: [] }),
773
+ },
774
+ github: {
775
+ getOctokit: async () => {
776
+ throw new Error("github client not configured in test");
777
+ },
778
+ },
779
+ slack: {
780
+ getClient: async () => {
781
+ throw new Error("slack client not configured in test");
782
+ },
783
+ },
784
+ kv: {
785
+ get: async () => undefined,
786
+ put: async () => {},
787
+ delete: async () => {},
788
+ },
789
+ },
790
+ send,
791
+ getSession: async () => null,
792
+ receive: async () => fakeSession(),
793
+ callTool: async (toolName) => ({
794
+ toolName,
795
+ callId: "call-1",
796
+ isError: false,
797
+ result: null,
798
+ durationMs: 0,
799
+ }),
800
+ waitUntil: () => {},
801
+ sessionUrls: (_request, sessionId) => ({
802
+ playgroundUrl: `http://127.0.0.1:3000/playground?sessionId=${sessionId}`,
803
+ traceUrl: `http://127.0.0.1:3000/v1/session/${sessionId}/events`,
804
+ }),
805
+ };
806
+ }
807
+
808
+ function fakeSession(): ChannelSession {
809
+ return {
810
+ id: "session-1",
811
+ isNew: true,
812
+ continuationToken: "github-pr:acme/widgets#42",
813
+ info: {
814
+ id: "session-1",
815
+ channelId: "github",
816
+ mode: "chat",
817
+ auth: null,
818
+ sdkAgentId: undefined,
819
+ title: "GitHub acme/widgets#42",
820
+ },
821
+ getEventStream: async () => new ReadableStream<Uint8Array>(),
822
+ waitForCompletion: async () => ({ status: "finished", result: "done" }),
823
+ setContinuationToken: async () => {},
824
+ };
825
+ }
826
+
827
+ function pullRequestPayload(
828
+ overrides: { action?: string } = {}
829
+ ): Record<string, unknown> {
830
+ return {
831
+ action: overrides.action ?? "opened",
832
+ repository: {
833
+ full_name: "acme/widgets",
834
+ html_url: "https://github.com/acme/widgets",
835
+ },
836
+ pull_request: {
837
+ number: 42,
838
+ html_url: "https://github.com/acme/widgets/pull/42",
839
+ title: "Add widgets",
840
+ body: "PR body",
841
+ draft: false,
842
+ user: { login: "octocat" },
843
+ head: {
844
+ ref: "feature/widgets",
845
+ sha: "abc123",
846
+ },
847
+ base: {
848
+ ref: "main",
849
+ },
850
+ },
851
+ sender: { login: "sender" },
852
+ };
853
+ }
854
+
855
+ function issueCommentPayload(body: string): Record<string, unknown> {
856
+ return {
857
+ action: "created",
858
+ repository: {
859
+ full_name: "acme/widgets",
860
+ html_url: "https://github.com/acme/widgets",
861
+ },
862
+ issue: {
863
+ number: 42,
864
+ html_url: "https://github.com/acme/widgets/pull/42",
865
+ pull_request: {},
866
+ },
867
+ comment: {
868
+ id: 7,
869
+ body,
870
+ html_url: "https://github.com/acme/widgets/pull/42#issuecomment-7",
871
+ url: "https://api.github.com/repos/acme/widgets/issues/comments/7",
872
+ user: { login: "sender", type: "User" },
873
+ },
874
+ sender: { login: "sender", type: "User" },
875
+ };
876
+ }
877
+
878
+ function statusPayload(): Record<string, unknown> {
879
+ return {
880
+ state: "failure",
881
+ sha: "deadbeef",
882
+ context: "ci/build",
883
+ description: "Build failed",
884
+ target_url: "https://ci.example.com/build/1",
885
+ name: "acme/widgets",
886
+ branches: [{ name: "main", commit: { sha: "deadbeef" } }],
887
+ commit: {
888
+ sha: "deadbeef",
889
+ html_url: "https://github.com/acme/widgets/commit/deadbeef",
890
+ },
891
+ repository: {
892
+ full_name: "acme/widgets",
893
+ html_url: "https://github.com/acme/widgets",
894
+ },
895
+ sender: { login: "ci-bot", type: "Bot" },
896
+ };
897
+ }
898
+
899
+ function labelPayload(): Record<string, unknown> {
900
+ return {
901
+ action: "created",
902
+ label: { name: "bug", color: "ff0000" },
903
+ repository: {
904
+ full_name: "acme/widgets",
905
+ html_url: "https://github.com/acme/widgets",
906
+ },
907
+ issue: {
908
+ number: 42,
909
+ html_url: "https://github.com/acme/widgets/issues/42",
910
+ },
911
+ sender: { login: "sender", type: "User" },
912
+ };
913
+ }
914
+
915
+ function checkSuitePayload(): Record<string, unknown> {
916
+ return {
917
+ action: "completed",
918
+ repository: {
919
+ full_name: "acme/widgets",
920
+ html_url: "https://github.com/acme/widgets",
921
+ },
922
+ check_suite: {
923
+ id: 99,
924
+ head_sha: "deadbeef",
925
+ status: "completed",
926
+ conclusion: "failure",
927
+ app: { slug: "github-actions", name: "GitHub Actions" },
928
+ pull_requests: [
929
+ {
930
+ number: 42,
931
+ html_url: "https://github.com/acme/widgets/pull/42",
932
+ },
933
+ ],
934
+ },
935
+ sender: { login: "github-actions[bot]", type: "Bot" },
936
+ };
937
+ }