@cursor/july 0.1.25 → 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 (288) 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/channels/github/github-channel.ts +19 -2
  145. package/src/channels/github/types.ts +6 -0
  146. package/src/channels/slack/api.ts +3 -1
  147. package/src/channels/slack/cursor-account.ts +3 -1
  148. package/src/channels/slack/dispatch.ts +12 -1
  149. package/src/channels/slack/interactive.ts +3 -0
  150. package/src/channels/slack/slack-channel.ts +6 -0
  151. package/src/channels/slack/types.ts +6 -0
  152. package/src/internal/ab-fold.ts +4 -0
  153. package/src/internal/ab-snapshot.ts +1 -0
  154. package/src/internal/cli-ax.ts +95 -0
  155. package/src/internal/model-pricing.ts +426 -0
  156. package/src/internal/reminder-runner.ts +1 -0
  157. package/src/internal/schedule-runner.ts +1 -0
  158. package/src/internal/sdk-runner.ts +10 -2
  159. package/src/internal/server.ts +2 -0
  160. package/src/internal/session-cost.ts +231 -0
  161. package/src/internal/session-engine.ts +32 -1
  162. package/src/internal/trajectory.ts +45 -3
  163. package/src/types.ts +42 -2
  164. package/dist/channels/github/instrument.d.ts +0 -20
  165. package/dist/channels/github/instrument.d.ts.map +0 -1
  166. package/dist/docs/assets/chunks/@localSearchIndexroot.BQTzJjR_.js +0 -1
  167. package/dist/internal/json-dir-store.d.ts +0 -32
  168. package/dist/internal/json-dir-store.d.ts.map +0 -1
  169. package/dist/internal/json-dir-store.js +0 -100
  170. package/dist/playground/assets/index-CjOQ4hN9.css +0 -1
  171. package/dist/playground/assets/index-DqXdAFGa.js +0 -85
  172. package/src/bin/agent-serve.version.test.ts +0 -64
  173. package/src/channels/github/api.test.ts +0 -64
  174. package/src/channels/github/auth.test.ts +0 -105
  175. package/src/channels/github/cursor-account.test.ts +0 -204
  176. package/src/channels/github/forward.test.ts +0 -457
  177. package/src/channels/github/github.test.ts +0 -943
  178. package/src/channels/github/replay.test.ts +0 -179
  179. package/src/channels/slack/api.post-message.test.ts +0 -148
  180. package/src/channels/slack/approvals.test.ts +0 -328
  181. package/src/channels/slack/block-actions.test.ts +0 -452
  182. package/src/channels/slack/bot-mentions.test.ts +0 -267
  183. package/src/channels/slack/channel-watch.test.ts +0 -363
  184. package/src/channels/slack/cursor-account.test.ts +0 -253
  185. package/src/channels/slack/defaults.final-post.test.ts +0 -182
  186. package/src/channels/slack/dispatch.test.ts +0 -795
  187. package/src/channels/slack/eval-directive.test.ts +0 -273
  188. package/src/channels/slack/message-body.test.ts +0 -54
  189. package/src/channels/slack/nudge-store.test.ts +0 -143
  190. package/src/channels/slack/slack.test.ts +0 -391
  191. package/src/channels/slack/stop.test.ts +0 -23
  192. package/src/channels/slack/thread-context.test.ts +0 -202
  193. package/src/evals/assertions.test.ts +0 -617
  194. package/src/evals/expect.test.ts +0 -144
  195. package/src/evals/judge.test.ts +0 -181
  196. package/src/evals/loaders.test.ts +0 -132
  197. package/src/evals/matchers.test.ts +0 -95
  198. package/src/evals/reporters.test.ts +0 -303
  199. package/src/evals/run-facts.test.ts +0 -259
  200. package/src/internal/ab-snapshot.test.ts +0 -325
  201. package/src/internal/approval-gate.test.ts +0 -49
  202. package/src/internal/approvals.integration.test.ts +0 -383
  203. package/src/internal/artifacts-store.test.ts +0 -283
  204. package/src/internal/authored-loaders.test.ts +0 -31
  205. package/src/internal/builtin-tools/artifacts.test.ts +0 -247
  206. package/src/internal/builtin-tools/reminders.test.ts +0 -201
  207. package/src/internal/channel-route-schema.test.ts +0 -294
  208. package/src/internal/chat-attach.test.ts +0 -262
  209. package/src/internal/cli-deploy.test.ts +0 -1991
  210. package/src/internal/cli-docs.test.ts +0 -161
  211. package/src/internal/cli-mcp.test.ts +0 -789
  212. package/src/internal/cli-skills.test.ts +0 -133
  213. package/src/internal/cli-slack.test.ts +0 -1654
  214. package/src/internal/cloud-merge.test.ts +0 -74
  215. package/src/internal/cron.test.ts +0 -22
  216. package/src/internal/cursor/account-mcp.test.ts +0 -807
  217. package/src/internal/cursor/backend-client.test.ts +0 -591
  218. package/src/internal/cursor/credentials.test.ts +0 -351
  219. package/src/internal/cursor/github-credentials.test.ts +0 -136
  220. package/src/internal/cursor-account-mcp-auth.test.ts +0 -310
  221. package/src/internal/cursor-account.integration.test.ts +0 -441
  222. package/src/internal/cursor-event-relay.test.ts +0 -746
  223. package/src/internal/cursor-github-credentials.integration.test.ts +0 -271
  224. package/src/internal/cursor-slack-relay.test.ts +0 -525
  225. package/src/internal/deploy-source.test.ts +0 -111
  226. package/src/internal/discovery.artifact-tool.test.ts +0 -136
  227. package/src/internal/discovery.artifacts.test.ts +0 -119
  228. package/src/internal/discovery.builtin-tools.test.ts +0 -94
  229. package/src/internal/discovery.concurrency.test.ts +0 -60
  230. package/src/internal/discovery.cursor-account.test.ts +0 -133
  231. package/src/internal/discovery.cwd.test.ts +0 -83
  232. package/src/internal/discovery.hosting.test.ts +0 -80
  233. package/src/internal/discovery.identity.test.ts +0 -44
  234. package/src/internal/docs-site.test.ts +0 -66
  235. package/src/internal/duration.test.ts +0 -29
  236. package/src/internal/eval-judge-model.test.ts +0 -187
  237. package/src/internal/eval-run-store.cancel.test.ts +0 -142
  238. package/src/internal/eval-run-store.storage.test.ts +0 -211
  239. package/src/internal/eval-runner.http.test.ts +0 -403
  240. package/src/internal/eval-runner.run.test.ts +0 -928
  241. package/src/internal/evals-client.test.ts +0 -307
  242. package/src/internal/event-mapper.test.ts +0 -243
  243. package/src/internal/github-fanout.test.ts +0 -213
  244. package/src/internal/handleAgentServeTrigger.test.ts +0 -185
  245. package/src/internal/host-kv.test.ts +0 -82
  246. package/src/internal/host-platforms.test.ts +0 -126
  247. package/src/internal/http-channel.test.ts +0 -402
  248. package/src/internal/init-project.test.ts +0 -270
  249. package/src/internal/install-cursor-skills.test.ts +0 -262
  250. package/src/internal/local-env.test.ts +0 -120
  251. package/src/internal/log-ring.test.ts +0 -31
  252. package/src/internal/logs-client.test.ts +0 -350
  253. package/src/internal/mcp-endpoint.test.ts +0 -436
  254. package/src/internal/mcp-host.test.ts +0 -298
  255. package/src/internal/mcp-oauth.test.ts +0 -148
  256. package/src/internal/net.test.ts +0 -17
  257. package/src/internal/peer-connections.test.ts +0 -128
  258. package/src/internal/peer-mcp.integration.test.ts +0 -289
  259. package/src/internal/playground/toolchain.test.ts +0 -53
  260. package/src/internal/playground-cli.test.ts +0 -187
  261. package/src/internal/playground-proxy.test.ts +0 -376
  262. package/src/internal/prompt-context.integration.test.ts +0 -232
  263. package/src/internal/prompt-context.test.ts +0 -127
  264. package/src/internal/reminder-runner.test.ts +0 -390
  265. package/src/internal/reminder-store.test.ts +0 -53
  266. package/src/internal/request-headers.test.ts +0 -27
  267. package/src/internal/resolve-prod-target.test.ts +0 -787
  268. package/src/internal/resolved-connections.test.ts +0 -295
  269. package/src/internal/router.test.ts +0 -57
  270. package/src/internal/sdk-runner.test.ts +0 -290
  271. package/src/internal/server.artifacts.test.ts +0 -303
  272. package/src/internal/session-engine.artifacts.test.ts +0 -243
  273. package/src/internal/session-engine.coalesce.test.ts +0 -169
  274. package/src/internal/session-engine.concurrency.test.ts +0 -250
  275. package/src/internal/session-engine.host-oauth-mcp.test.ts +0 -110
  276. package/src/internal/session-engine.interrupt.test.ts +0 -577
  277. package/src/internal/session-engine.storage.test.ts +0 -547
  278. package/src/internal/session-urls.test.ts +0 -28
  279. package/src/internal/sessions-client.test.ts +0 -518
  280. package/src/internal/storage-coordinator.test.ts +0 -517
  281. package/src/internal/tool-call.test.ts +0 -458
  282. package/src/internal/tool-result.test.ts +0 -52
  283. package/src/internal/trajectory.approvals.test.ts +0 -83
  284. package/src/internal/trajectory.subagents.test.ts +0 -198
  285. package/src/internal/turn-governor.test.ts +0 -137
  286. package/src/internal/update-check.test.ts +0 -485
  287. package/src/internal/workspace.test.ts +0 -207
  288. package/src/storage-backends/cursor-hosted.test.ts +0 -121
@@ -1,591 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import {
3
- CursorBackendClient,
4
- CursorBackendError,
5
- createUserApiKeyWithSessionToken,
6
- revokeMintedUserApiKey,
7
- } from "./backend-client.js";
8
-
9
- const BACKEND = "https://backend.example";
10
-
11
- /** A JWT-shaped token whose exp claim is one hour out. */
12
- function fakeJwt(expiresInSeconds = 3600): string {
13
- const claims = Buffer.from(
14
- JSON.stringify({ exp: Math.floor(Date.now() / 1000) + expiresInSeconds })
15
- ).toString("base64url");
16
- return `header.${claims}.signature`;
17
- }
18
-
19
- interface SeenRequest {
20
- url: string;
21
- headers: Record<string, string>;
22
- body: unknown;
23
- }
24
-
25
- /**
26
- * Fake Cursor backend: implements the API-key exchange plus a handler per
27
- * DashboardService method, recording every request it sees.
28
- */
29
- function fakeBackend(
30
- handlers: Record<string, (body: unknown) => Response | Promise<Response>>,
31
- options: { accessToken?: string; failExchanges?: number } = {}
32
- ): { fetchImpl: typeof fetch; seen: SeenRequest[]; exchanges: () => number } {
33
- const seen: SeenRequest[] = [];
34
- let exchangeCount = 0;
35
- const fetchImpl = (async (input: RequestInfo | URL, init?: RequestInit) => {
36
- const url = String(input);
37
- const body =
38
- typeof init?.body === "string" && init.body !== ""
39
- ? (JSON.parse(init.body) as unknown)
40
- : undefined;
41
- seen.push({
42
- url,
43
- headers: Object.fromEntries(new Headers(init?.headers).entries()),
44
- body,
45
- });
46
- if (url.endsWith("/auth/exchange_user_api_key")) {
47
- exchangeCount++;
48
- if (
49
- options.failExchanges !== undefined &&
50
- exchangeCount <= options.failExchanges
51
- ) {
52
- return new Response("{}", { status: 401 });
53
- }
54
- return Response.json({
55
- accessToken: options.accessToken ?? fakeJwt(),
56
- refreshToken: "r",
57
- });
58
- }
59
- const method = url.split("/").pop() ?? "";
60
- const handler = handlers[method];
61
- if (handler === undefined) {
62
- return new Response(JSON.stringify({ message: "no handler" }), {
63
- status: 404,
64
- });
65
- }
66
- return handler(body);
67
- }) as typeof fetch;
68
- return { fetchImpl, seen, exchanges: () => exchangeCount };
69
- }
70
-
71
- describe("CursorBackendClient", () => {
72
- it("exchanges the API key once and reuses the access token across RPCs", async () => {
73
- const backend = fakeBackend({
74
- GetMe: () => Response.json({ authId: "auth0|1", email: "a@b.co" }),
75
- });
76
- const client = new CursorBackendClient({
77
- backendUrl: BACKEND,
78
- apiKey: "crsr_key",
79
- fetchImpl: backend.fetchImpl,
80
- });
81
-
82
- expect(await client.getMe()).toMatchObject({ email: "a@b.co" });
83
- expect(await client.getMe()).toMatchObject({ email: "a@b.co" });
84
- expect(backend.exchanges()).toBe(1);
85
-
86
- const exchange = backend.seen[0];
87
- expect(exchange?.url).toBe(`${BACKEND}/auth/exchange_user_api_key`);
88
- expect(exchange?.headers.authorization).toBe("Bearer crsr_key");
89
- const rpc = backend.seen[1];
90
- expect(rpc?.url).toBe(`${BACKEND}/aiserver.v1.DashboardService/GetMe`);
91
- expect(rpc?.headers.authorization).toMatch(/^Bearer header\./);
92
- expect(rpc?.headers["x-request-id"]).toBeTruthy();
93
- });
94
-
95
- it("re-exchanges once when an RPC comes back 401", async () => {
96
- let getMeCalls = 0;
97
- const backend = fakeBackend({
98
- GetMe: () => {
99
- getMeCalls++;
100
- if (getMeCalls === 1) {
101
- return new Response("{}", { status: 401 });
102
- }
103
- return Response.json({ authId: "auth0|1" });
104
- },
105
- });
106
- const client = new CursorBackendClient({
107
- backendUrl: BACKEND,
108
- apiKey: "crsr_key",
109
- fetchImpl: backend.fetchImpl,
110
- });
111
- expect(await client.getMe()).toMatchObject({ authId: "auth0|1" });
112
- expect(backend.exchanges()).toBe(2);
113
- });
114
-
115
- it("surfaces a sign-in hint when the API key itself is rejected", async () => {
116
- const backend = fakeBackend({}, { failExchanges: 10 });
117
- const client = new CursorBackendClient({
118
- backendUrl: BACKEND,
119
- apiKey: "crsr_bad",
120
- fetchImpl: backend.fetchImpl,
121
- });
122
- await expect(client.getMe()).rejects.toThrow(/agent-serve login/);
123
- });
124
-
125
- it("mints a scoped GitHub token directly from the account API key", async () => {
126
- const expiresAt = new Date(Date.now() + 45 * 60_000).toISOString();
127
- const backend = fakeBackend({
128
- "github-token": (body) => {
129
- expect(body).toEqual({
130
- repos: ["acme/widgets"],
131
- permissions: "pr-write",
132
- });
133
- return Response.json({ token: "ghs_scoped", expiresAt });
134
- },
135
- });
136
- const client = new CursorBackendClient({
137
- backendUrl: BACKEND,
138
- apiKey: "crsr_key",
139
- fetchImpl: backend.fetchImpl,
140
- });
141
-
142
- await expect(client.getGithubToken(["acme/widgets"])).resolves.toEqual({
143
- token: "ghs_scoped",
144
- expiresAt,
145
- });
146
- expect(backend.exchanges()).toBe(0);
147
- expect(backend.seen[0]?.url).toBe(`${BACKEND}/v0/scm-events/github-token`);
148
- expect(backend.seen[0]?.headers.authorization).toBe("Bearer crsr_key");
149
- });
150
-
151
- it("requests a read-only GitHub token when permissions is read", async () => {
152
- const expiresAt = new Date(Date.now() + 45 * 60_000).toISOString();
153
- const backend = fakeBackend({
154
- "github-token": (body) => {
155
- expect(body).toEqual({
156
- repos: ["acme/widgets"],
157
- permissions: "read",
158
- });
159
- return Response.json({ token: "ghs_readonly", expiresAt });
160
- },
161
- });
162
- const client = new CursorBackendClient({
163
- backendUrl: BACKEND,
164
- apiKey: "crsr_key",
165
- fetchImpl: backend.fetchImpl,
166
- });
167
-
168
- await expect(
169
- client.getGithubToken(["acme/widgets"], { permissions: "read" })
170
- ).resolves.toEqual({
171
- token: "ghs_readonly",
172
- expiresAt,
173
- });
174
- });
175
-
176
- it("requests a contents-write GitHub token when opted up", async () => {
177
- const expiresAt = new Date(Date.now() + 45 * 60_000).toISOString();
178
- const backend = fakeBackend({
179
- "github-token": (body) => {
180
- expect(body).toEqual({
181
- repos: ["acme/widgets"],
182
- permissions: "contents-write",
183
- });
184
- return Response.json({ token: "ghs_push", expiresAt });
185
- },
186
- });
187
- const client = new CursorBackendClient({
188
- backendUrl: BACKEND,
189
- apiKey: "crsr_key",
190
- fetchImpl: backend.fetchImpl,
191
- });
192
-
193
- await expect(
194
- client.getGithubToken(["acme/widgets"], {
195
- permissions: "contents-write",
196
- })
197
- ).resolves.toEqual({
198
- token: "ghs_push",
199
- expiresAt,
200
- });
201
- });
202
-
203
- it("keeps GitHub-connection denials distinct from API-key rejections", async () => {
204
- const backend = fakeBackend({
205
- "github-token": () =>
206
- Response.json(
207
- {
208
- error:
209
- "No GitHub connection found for this Cursor account. Connect GitHub in the Cursor dashboard (Integrations) and try again.",
210
- },
211
- { status: 403 }
212
- ),
213
- });
214
- const client = new CursorBackendClient({
215
- backendUrl: BACKEND,
216
- apiKey: "crsr_key",
217
- fetchImpl: backend.fetchImpl,
218
- });
219
-
220
- const error = await client.getGithubToken(["acme/widgets"]).catch((e) => e);
221
- expect(error).toMatchObject({
222
- name: "CursorBackendError",
223
- status: 403,
224
- });
225
- expect(String(error.message)).toMatch(/GitHub connection/i);
226
- expect(String(error.message)).not.toMatch(
227
- /agent-serve login|API key was rejected/
228
- );
229
- });
230
-
231
- it("honors Retry-After on GitHub token rate limits", async () => {
232
- const backend = fakeBackend({
233
- "github-token": () =>
234
- new Response(JSON.stringify({ error: "rate limited" }), {
235
- status: 429,
236
- headers: {
237
- "content-type": "application/json",
238
- "retry-after": "120",
239
- },
240
- }),
241
- });
242
- const client = new CursorBackendClient({
243
- backendUrl: BACKEND,
244
- apiKey: "crsr_key",
245
- fetchImpl: backend.fetchImpl,
246
- });
247
-
248
- await expect(client.getGithubToken(["acme/widgets"])).rejects.toMatchObject(
249
- {
250
- name: "CursorBackendError",
251
- status: 429,
252
- retryAfterMs: 120_000,
253
- message: expect.stringMatching(/rate limited/i),
254
- }
255
- );
256
- });
257
-
258
- it("lists account MCP tools with normalized schemas", async () => {
259
- const backend = fakeBackend({
260
- ListSandMcpTools: (body) => {
261
- expect(body).toEqual({ serverIdentifiers: ["Linear"] });
262
- return Response.json({
263
- servers: [
264
- {
265
- serverIdentifier: "Linear",
266
- status: "connected",
267
- tools: [
268
- {
269
- name: "Linear-list_issues",
270
- description: "List issues.",
271
- inputSchema: {
272
- type: "object",
273
- properties: { query: { type: "string" } },
274
- },
275
- },
276
- { name: "Linear-create_issue" },
277
- ],
278
- },
279
- { serverIdentifier: "Notion", status: "needsAuth" },
280
- ],
281
- });
282
- },
283
- });
284
- const client = new CursorBackendClient({
285
- backendUrl: BACKEND,
286
- apiKey: "crsr_key",
287
- fetchImpl: backend.fetchImpl,
288
- });
289
-
290
- const servers = await client.listAccountMcpTools(["Linear"]);
291
- expect(servers).toHaveLength(2);
292
- expect(servers[0]?.tools[0]).toMatchObject({
293
- name: "Linear-list_issues",
294
- description: "List issues.",
295
- });
296
- // Missing schema defaults to an open object so MCP clients accept it.
297
- expect(servers[0]?.tools[1]?.inputSchema).toEqual({
298
- type: "object",
299
- properties: {},
300
- });
301
- expect(servers[1]).toMatchObject({ status: "needsAuth", tools: [] });
302
- });
303
-
304
- it("converts success results (text, image, structured content)", async () => {
305
- const backend = fakeBackend({
306
- ExecuteSandMcpTool: (body) => {
307
- expect(body).toMatchObject({
308
- serverIdentifier: "Linear",
309
- toolName: "Linear-list_issues",
310
- args: { query: "bug" },
311
- });
312
- expect((body as { toolCallId?: string }).toolCallId).toBeTruthy();
313
- return Response.json({
314
- result: {
315
- success: {
316
- content: [
317
- { text: { text: "2 issues" } },
318
- { image: { data: "aGk=", mimeType: "image/png" } },
319
- ],
320
- isError: false,
321
- structuredContent: { total: 2 },
322
- },
323
- },
324
- });
325
- },
326
- });
327
- const client = new CursorBackendClient({
328
- backendUrl: BACKEND,
329
- apiKey: "crsr_key",
330
- fetchImpl: backend.fetchImpl,
331
- });
332
-
333
- const result = await client.executeAccountMcpTool({
334
- serverIdentifier: "Linear",
335
- toolName: "Linear-list_issues",
336
- args: { query: "bug" },
337
- });
338
- expect(result).toEqual({
339
- content: [
340
- { type: "text", text: "2 issues" },
341
- { type: "image", data: "aGk=", mimeType: "image/png" },
342
- ],
343
- isError: false,
344
- structuredContent: { total: 2 },
345
- });
346
- });
347
-
348
- it("tolerates proto-JSON omitting default-valued content fields", async () => {
349
- // The backend's proto-JSON omits default values, so an empty text chunk
350
- // arrives as `{"text": {}}` — it must parse as "" rather than fail.
351
- const backend = fakeBackend({
352
- ExecuteSandMcpTool: () =>
353
- Response.json({
354
- result: { success: { content: [{ text: {} }, { image: {} }] } },
355
- }),
356
- });
357
- const client = new CursorBackendClient({
358
- backendUrl: BACKEND,
359
- apiKey: "crsr_key",
360
- fetchImpl: backend.fetchImpl,
361
- });
362
- const result = await client.executeAccountMcpTool({
363
- serverIdentifier: "s",
364
- toolName: "t",
365
- args: {},
366
- });
367
- expect(result).toEqual({
368
- content: [
369
- { type: "text", text: "" },
370
- { type: "image", data: "" },
371
- ],
372
- isError: false,
373
- });
374
- });
375
-
376
- it.each([
377
- [{ error: { error: "server exploded" } }, /server exploded/],
378
- [{ permissionDenied: { error: "not allowed" } }, /not allowed/],
379
- [{ rejected: { reason: "readonly session" } }, /readonly session/],
380
- [
381
- { toolNotFound: { name: "nope", availableTools: ["a", "b"] } },
382
- /Unknown tool "nope"\. Available: a, b\./,
383
- ],
384
- [
385
- { serverNotFound: { name: "gone", availableServers: [] } },
386
- /Unknown MCP server "gone"/,
387
- ],
388
- [{}, /returned no result/],
389
- ])("converts failure results to error text: %j", async (result, pattern) => {
390
- const backend = fakeBackend({
391
- ExecuteSandMcpTool: () => Response.json({ result }),
392
- });
393
- const client = new CursorBackendClient({
394
- backendUrl: BACKEND,
395
- apiKey: "crsr_key",
396
- fetchImpl: backend.fetchImpl,
397
- });
398
- const outcome = await client.executeAccountMcpTool({
399
- serverIdentifier: "s",
400
- toolName: "t",
401
- args: {},
402
- });
403
- expect(outcome.isError).toBe(true);
404
- expect(outcome.content[0]).toMatchObject({ type: "text" });
405
- expect((outcome.content[0] as { text: string }).text).toMatch(pattern);
406
- });
407
-
408
- it("clears a rejected token after a retry still returns 401", async () => {
409
- let getMeCalls = 0;
410
- const backend = fakeBackend({
411
- GetMe: () => {
412
- getMeCalls++;
413
- return new Response("{}", { status: 401 });
414
- },
415
- });
416
- const client = new CursorBackendClient({
417
- backendUrl: BACKEND,
418
- apiKey: "crsr_key",
419
- fetchImpl: backend.fetchImpl,
420
- });
421
- await expect(client.getMe()).rejects.toThrow(/GetMe failed \(401\)/);
422
- expect(backend.exchanges()).toBe(2);
423
- expect(getMeCalls).toBe(2);
424
- });
425
-
426
- it("surfaces Connect error messages from failed RPCs", async () => {
427
- const backend = fakeBackend({
428
- ListSandMcpTools: () =>
429
- new Response(
430
- JSON.stringify({ code: "permission_denied", message: "MCP off" }),
431
- { status: 403 }
432
- ),
433
- });
434
- const client = new CursorBackendClient({
435
- backendUrl: BACKEND,
436
- apiKey: "crsr_key",
437
- fetchImpl: backend.fetchImpl,
438
- });
439
- await expect(client.listAccountMcpTools([])).rejects.toThrow(
440
- /ListSandMcpTools failed \(403\): MCP off/
441
- );
442
- });
443
- });
444
-
445
- describe("createUserApiKeyWithSessionToken", () => {
446
- it("mints a key with the session bearer and returns it", async () => {
447
- const seen: SeenRequest[] = [];
448
- const fetchImpl = (async (input: RequestInfo | URL, init?: RequestInit) => {
449
- seen.push({
450
- url: String(input),
451
- headers: Object.fromEntries(new Headers(init?.headers).entries()),
452
- body: JSON.parse(String(init?.body)) as unknown,
453
- });
454
- return Response.json({ apiKey: "crsr_minted" });
455
- }) as typeof fetch;
456
-
457
- const apiKey = await createUserApiKeyWithSessionToken({
458
- backendUrl: BACKEND,
459
- sessionToken: "session-jwt",
460
- keyName: "agent-serve (host)",
461
- fetchImpl,
462
- });
463
- expect(apiKey).toBe("crsr_minted");
464
- expect(seen[0]?.url).toBe(
465
- `${BACKEND}/aiserver.v1.DashboardService/CreateUserApiKey`
466
- );
467
- expect(seen[0]?.headers.authorization).toBe("Bearer session-jwt");
468
- expect(seen[0]?.body).toEqual({ name: "agent-serve (host)" });
469
- });
470
-
471
- it("carries the backend status through on failure (team admin policy)", async () => {
472
- const fetchImpl = (async () =>
473
- new Response(
474
- JSON.stringify({
475
- message: "Only team admins can create user API keys",
476
- }),
477
- { status: 401 }
478
- )) as typeof fetch;
479
- const attempt = createUserApiKeyWithSessionToken({
480
- backendUrl: BACKEND,
481
- sessionToken: "session-jwt",
482
- keyName: "x",
483
- fetchImpl,
484
- });
485
- await expect(attempt).rejects.toThrow(/Only team admins/);
486
- await expect(attempt).rejects.toBeInstanceOf(CursorBackendError);
487
- });
488
- });
489
-
490
- describe("revokeMintedUserApiKey", () => {
491
- it("authenticates cleanup with the browser session token", async () => {
492
- const backend = fakeBackend({
493
- ListUserApiKeys: () =>
494
- Response.json({
495
- apiKeys: [
496
- {
497
- id: 9,
498
- name: "agent-serve (host)",
499
- maskedKey: "crsr_...mint",
500
- createdAt: 100,
501
- },
502
- ],
503
- }),
504
- RevokeUserApiKey: (body) => {
505
- expect(body).toEqual({ id: 9 });
506
- return Response.json({});
507
- },
508
- });
509
-
510
- await revokeMintedUserApiKey({
511
- backendUrl: BACKEND,
512
- keyName: "agent-serve (host)",
513
- apiKey:
514
- "crsr_0123456789abcdef0123456789abcdef0123456789abcdef0123456789mint",
515
- sessionToken: "session-jwt",
516
- fetchImpl: backend.fetchImpl,
517
- });
518
-
519
- expect(backend.exchanges()).toBe(0);
520
- const list = backend.seen.find((req) =>
521
- req.url.endsWith("/ListUserApiKeys")
522
- );
523
- expect(list?.headers.authorization).toBe("Bearer session-jwt");
524
- });
525
-
526
- it("revokes by masked key match, not an older same-named key", async () => {
527
- const backend = fakeBackend({
528
- ListUserApiKeys: () =>
529
- Response.json({
530
- apiKeys: [
531
- {
532
- id: 1,
533
- name: "agent-serve (host)",
534
- maskedKey: "crsr_...old1",
535
- createdAt: 100,
536
- },
537
- {
538
- id: 2,
539
- name: "agent-serve (host)",
540
- maskedKey: "crsr_...new2",
541
- createdAt: 200,
542
- },
543
- ],
544
- }),
545
- RevokeUserApiKey: (body) => {
546
- expect(body).toEqual({ id: 2 });
547
- return Response.json({});
548
- },
549
- });
550
-
551
- await revokeMintedUserApiKey({
552
- backendUrl: BACKEND,
553
- keyName: "agent-serve (host)",
554
- apiKey:
555
- "crsr_0123456789abcdef0123456789abcdef0123456789abcdef0123456789new2",
556
- sessionToken: "session-jwt",
557
- fetchImpl: backend.fetchImpl,
558
- });
559
- });
560
-
561
- it("skips revoke when the minted key is not listed yet", async () => {
562
- const backend = fakeBackend({
563
- ListUserApiKeys: () =>
564
- Response.json({
565
- apiKeys: [
566
- {
567
- id: 1,
568
- name: "agent-serve (host)",
569
- maskedKey: "crsr_...old1",
570
- createdAt: 100,
571
- },
572
- ],
573
- }),
574
- RevokeUserApiKey: () => {
575
- throw new Error("should not revoke");
576
- },
577
- });
578
-
579
- await revokeMintedUserApiKey({
580
- backendUrl: BACKEND,
581
- keyName: "agent-serve (host)",
582
- apiKey:
583
- "crsr_0123456789abcdef0123456789abcdef0123456789abcdef0123456789new2",
584
- sessionToken: "session-jwt",
585
- fetchImpl: backend.fetchImpl,
586
- });
587
- expect(
588
- backend.seen.some((req) => req.url.endsWith("/RevokeUserApiKey"))
589
- ).toBe(false);
590
- });
591
- });