@cursor/july 0.1.19 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/AGENTS.md +10 -0
  2. package/dist/bin/agent-serve.js +0 -0
  3. package/dist/channels/github/instrument.d.ts +20 -0
  4. package/dist/channels/github/instrument.d.ts.map +1 -0
  5. package/dist/channels/slack/api.d.ts +2 -0
  6. package/dist/channels/slack/api.d.ts.map +1 -1
  7. package/dist/channels/slack/api.js +3 -0
  8. package/dist/channels/slack/bot-mentions.d.ts +106 -0
  9. package/dist/channels/slack/bot-mentions.d.ts.map +1 -0
  10. package/dist/channels/slack/bot-mentions.js +243 -0
  11. package/dist/channels/slack/dispatch.d.ts +6 -0
  12. package/dist/channels/slack/dispatch.d.ts.map +1 -1
  13. package/dist/channels/slack/dispatch.js +24 -3
  14. package/dist/channels/slack/inbound.d.ts +10 -1
  15. package/dist/channels/slack/inbound.d.ts.map +1 -1
  16. package/dist/channels/slack/inbound.js +12 -4
  17. package/dist/channels/slack/index.d.ts +1 -0
  18. package/dist/channels/slack/index.d.ts.map +1 -1
  19. package/dist/channels/slack/index.js +1 -0
  20. package/dist/channels/slack/slack-channel.d.ts.map +1 -1
  21. package/dist/channels/slack/slack-channel.js +61 -22
  22. package/dist/channels/slack/types.d.ts +58 -0
  23. package/dist/channels/slack/types.d.ts.map +1 -1
  24. package/dist/docs/404.html +1 -1
  25. package/dist/docs/ab.html +2 -2
  26. package/dist/docs/assets/{app.CrsWMchO.js → app.jDxLzWv4.js} +1 -1
  27. package/dist/docs/assets/chunks/@localSearchIndexroot.DoJHJjqF.js +1 -0
  28. package/dist/docs/assets/chunks/{VPLocalSearchBox.D1JqzSh8.js → VPLocalSearchBox.Y6bDR1-a.js} +1 -1
  29. package/dist/docs/assets/chunks/{theme.DaBvZYwl.js → theme.CLazCWlJ.js} +2 -2
  30. package/dist/docs/building-with-agents.html +2 -2
  31. package/dist/docs/concepts.html +2 -2
  32. package/dist/docs/deployment.html +2 -2
  33. package/dist/docs/evals.html +2 -2
  34. package/dist/docs/example-agents/approval-buddy.html +2 -2
  35. package/dist/docs/example-agents/benny.html +2 -2
  36. package/dist/docs/example-agents/bugbot.html +2 -2
  37. package/dist/docs/example-agents/codebase-wiki.html +2 -2
  38. package/dist/docs/example-agents/codeowners-review.html +2 -2
  39. package/dist/docs/example-agents/concierge.html +2 -2
  40. package/dist/docs/example-agents/fsd.html +2 -2
  41. package/dist/docs/example-agents/index.html +2 -2
  42. package/dist/docs/example-agents/knowledge-base.html +2 -2
  43. package/dist/docs/example-agents/oncall.html +2 -2
  44. package/dist/docs/example-agents/security-reviewer.html +2 -2
  45. package/dist/docs/example-agents/slack-agent.html +2 -2
  46. package/dist/docs/example-agents/weather-agent.html +2 -2
  47. package/dist/docs/guides/agent-to-agent.html +2 -2
  48. package/dist/docs/guides/cloud-runtime.html +2 -2
  49. package/dist/docs/guides/github.html +2 -2
  50. package/dist/docs/guides/human-in-the-loop.html +2 -2
  51. package/dist/docs/guides/mcp-oauth.html +2 -2
  52. package/dist/docs/guides/slack.html +2 -2
  53. package/dist/docs/guides/webhooks.html +2 -2
  54. package/dist/docs/hillclimbing.html +2 -2
  55. package/dist/docs/index.html +2 -2
  56. package/dist/docs/quickstart.html +2 -2
  57. package/dist/docs/reference/agent-config.html +2 -2
  58. package/dist/docs/reference/channels.html +2 -2
  59. package/dist/docs/reference/cli.html +2 -2
  60. package/dist/docs/reference/connections.html +2 -2
  61. package/dist/docs/reference/hooks.html +2 -2
  62. package/dist/docs/reference/http-api.html +2 -2
  63. package/dist/docs/reference/instructions.html +2 -2
  64. package/dist/docs/reference/playground.html +2 -2
  65. package/dist/docs/reference/project-layout.html +2 -2
  66. package/dist/docs/reference/prompt.html +2 -2
  67. package/dist/docs/reference/schedules.html +2 -2
  68. package/dist/docs/reference/sessions.html +2 -2
  69. package/dist/docs/reference/skills.html +2 -2
  70. package/dist/docs/reference/subagents.html +2 -2
  71. package/dist/docs/reference/tools.html +2 -2
  72. package/dist/docs/scaffolding-agents.html +2 -2
  73. package/dist/docs/storage.html +2 -2
  74. package/dist/docs/troubleshooting.html +2 -2
  75. package/dist/internal/json-dir-store.d.ts +32 -0
  76. package/dist/internal/json-dir-store.d.ts.map +1 -0
  77. package/dist/internal/json-dir-store.js +100 -0
  78. package/dist/internal/resolved-connections.d.ts +1 -1
  79. package/dist/internal/resolved-connections.d.ts.map +1 -1
  80. package/dist/internal/resolved-connections.js +10 -0
  81. package/dist/internal/session-engine.d.ts +4 -1
  82. package/dist/internal/session-engine.d.ts.map +1 -1
  83. package/dist/internal/session-engine.js +13 -2
  84. package/dist/playground/assets/index-CjOQ4hN9.css +1 -0
  85. package/dist/playground/assets/{index-C2SU2xV5.js → index-dshZQJCp.js} +46 -46
  86. package/dist/playground/index.html +2 -2
  87. package/dist/types.d.ts +7 -0
  88. package/dist/types.d.ts.map +1 -1
  89. package/dist/types.js +9 -0
  90. package/package.json +25 -25
  91. package/skills/create-agent/SKILL.md +36 -4
  92. package/skills/evals/SKILL.md +3 -0
  93. package/skills/framework-map/SKILL.md +14 -0
  94. package/skills/github/SKILL.md +6 -1
  95. package/skills/hillclimb/SKILL.md +28 -7
  96. package/src/bin/agent-serve.version.test.ts +62 -0
  97. package/src/channels/github/api.test.ts +64 -0
  98. package/src/channels/github/auth.test.ts +105 -0
  99. package/src/channels/github/cursor-account.test.ts +204 -0
  100. package/src/channels/github/forward.test.ts +457 -0
  101. package/src/channels/github/github.test.ts +937 -0
  102. package/src/channels/github/replay.test.ts +179 -0
  103. package/src/channels/slack/api.post-message.test.ts +148 -0
  104. package/src/channels/slack/api.ts +5 -0
  105. package/src/channels/slack/approvals.test.ts +328 -0
  106. package/src/channels/slack/block-actions.test.ts +452 -0
  107. package/src/channels/slack/bot-mentions.test.ts +217 -0
  108. package/src/channels/slack/bot-mentions.ts +315 -0
  109. package/src/channels/slack/channel-watch.test.ts +363 -0
  110. package/src/channels/slack/cursor-account.test.ts +253 -0
  111. package/src/channels/slack/defaults.final-post.test.ts +182 -0
  112. package/src/channels/slack/dispatch.test.ts +795 -0
  113. package/src/channels/slack/dispatch.ts +27 -1
  114. package/src/channels/slack/eval-directive.test.ts +273 -0
  115. package/src/channels/slack/inbound.ts +25 -4
  116. package/src/channels/slack/index.ts +1 -0
  117. package/src/channels/slack/message-body.test.ts +54 -0
  118. package/src/channels/slack/nudge-store.test.ts +143 -0
  119. package/src/channels/slack/slack-channel.ts +66 -8
  120. package/src/channels/slack/slack.test.ts +391 -0
  121. package/src/channels/slack/stop.test.ts +23 -0
  122. package/src/channels/slack/thread-context.test.ts +202 -0
  123. package/src/channels/slack/types.ts +60 -0
  124. package/src/evals/assertions.test.ts +580 -0
  125. package/src/evals/expect.test.ts +144 -0
  126. package/src/evals/judge.test.ts +181 -0
  127. package/src/evals/loaders.test.ts +132 -0
  128. package/src/evals/matchers.test.ts +95 -0
  129. package/src/evals/reporters.test.ts +303 -0
  130. package/src/evals/run-facts.test.ts +259 -0
  131. package/src/internal/ab-snapshot.test.ts +325 -0
  132. package/src/internal/approval-gate.test.ts +49 -0
  133. package/src/internal/approvals.integration.test.ts +383 -0
  134. package/src/internal/authored-loaders.test.ts +31 -0
  135. package/src/internal/builtin-tools/reminders.test.ts +201 -0
  136. package/src/internal/channel-route-schema.test.ts +294 -0
  137. package/src/internal/chat-attach.test.ts +262 -0
  138. package/src/internal/cli-deploy.test.ts +1991 -0
  139. package/src/internal/cli-mcp.test.ts +789 -0
  140. package/src/internal/cli-skills.test.ts +133 -0
  141. package/src/internal/cli-slack.test.ts +1647 -0
  142. package/src/internal/cloud-merge.test.ts +74 -0
  143. package/src/internal/cron.test.ts +22 -0
  144. package/src/internal/cursor/account-mcp.test.ts +807 -0
  145. package/src/internal/cursor/backend-client.test.ts +591 -0
  146. package/src/internal/cursor/credentials.test.ts +351 -0
  147. package/src/internal/cursor/github-credentials.test.ts +136 -0
  148. package/src/internal/cursor-account-mcp-auth.test.ts +310 -0
  149. package/src/internal/cursor-account.integration.test.ts +441 -0
  150. package/src/internal/cursor-event-relay.test.ts +746 -0
  151. package/src/internal/cursor-github-credentials.integration.test.ts +271 -0
  152. package/src/internal/cursor-slack-relay.test.ts +525 -0
  153. package/src/internal/deploy-source.test.ts +111 -0
  154. package/src/internal/discovery.builtin-tools.test.ts +94 -0
  155. package/src/internal/discovery.concurrency.test.ts +60 -0
  156. package/src/internal/discovery.cursor-account.test.ts +133 -0
  157. package/src/internal/discovery.cwd.test.ts +83 -0
  158. package/src/internal/discovery.hosting.test.ts +80 -0
  159. package/src/internal/discovery.identity.test.ts +44 -0
  160. package/src/internal/docs-site.test.ts +66 -0
  161. package/src/internal/duration.test.ts +29 -0
  162. package/src/internal/eval-judge-model.test.ts +187 -0
  163. package/src/internal/eval-run-store.cancel.test.ts +142 -0
  164. package/src/internal/eval-run-store.storage.test.ts +211 -0
  165. package/src/internal/eval-runner.http.test.ts +403 -0
  166. package/src/internal/eval-runner.run.test.ts +928 -0
  167. package/src/internal/evals-client.test.ts +307 -0
  168. package/src/internal/event-mapper.test.ts +243 -0
  169. package/src/internal/github-fanout.test.ts +213 -0
  170. package/src/internal/handleAgentServeTrigger.test.ts +179 -0
  171. package/src/internal/host-kv.test.ts +82 -0
  172. package/src/internal/host-platforms.test.ts +126 -0
  173. package/src/internal/http-channel.test.ts +402 -0
  174. package/src/internal/init-project.test.ts +269 -0
  175. package/src/internal/install-cursor-skills.test.ts +262 -0
  176. package/src/internal/local-env.test.ts +100 -0
  177. package/src/internal/log-ring.test.ts +31 -0
  178. package/src/internal/logs-client.test.ts +350 -0
  179. package/src/internal/mcp-endpoint.test.ts +436 -0
  180. package/src/internal/mcp-host.test.ts +298 -0
  181. package/src/internal/mcp-oauth.test.ts +148 -0
  182. package/src/internal/net.test.ts +17 -0
  183. package/src/internal/peer-connections.test.ts +128 -0
  184. package/src/internal/peer-mcp.integration.test.ts +289 -0
  185. package/src/internal/playground/toolchain.test.ts +53 -0
  186. package/src/internal/playground-cli.test.ts +187 -0
  187. package/src/internal/playground-proxy.test.ts +376 -0
  188. package/src/internal/prompt-context.integration.test.ts +232 -0
  189. package/src/internal/prompt-context.test.ts +127 -0
  190. package/src/internal/reminder-runner.test.ts +390 -0
  191. package/src/internal/reminder-store.test.ts +53 -0
  192. package/src/internal/request-headers.test.ts +27 -0
  193. package/src/internal/resolve-prod-target.test.ts +787 -0
  194. package/src/internal/resolved-connections.test.ts +295 -0
  195. package/src/internal/resolved-connections.ts +18 -1
  196. package/src/internal/router.test.ts +57 -0
  197. package/src/internal/sdk-runner.test.ts +290 -0
  198. package/src/internal/session-engine.coalesce.test.ts +169 -0
  199. package/src/internal/session-engine.concurrency.test.ts +250 -0
  200. package/src/internal/session-engine.host-oauth-mcp.test.ts +110 -0
  201. package/src/internal/session-engine.interrupt.test.ts +577 -0
  202. package/src/internal/session-engine.storage.test.ts +547 -0
  203. package/src/internal/session-engine.ts +15 -1
  204. package/src/internal/session-urls.test.ts +28 -0
  205. package/src/internal/sessions-client.test.ts +518 -0
  206. package/src/internal/storage-coordinator.test.ts +517 -0
  207. package/src/internal/tool-call.test.ts +458 -0
  208. package/src/internal/tool-result.test.ts +52 -0
  209. package/src/internal/trajectory.approvals.test.ts +83 -0
  210. package/src/internal/trajectory.subagents.test.ts +198 -0
  211. package/src/internal/turn-governor.test.ts +137 -0
  212. package/src/internal/update-check.test.ts +485 -0
  213. package/src/internal/workspace.test.ts +81 -0
  214. package/src/storage-backends/cursor-hosted.test.ts +121 -0
  215. package/src/types.ts +12 -0
  216. package/dist/docs/assets/chunks/@localSearchIndexroot.BnHRjfoe.js +0 -1
  217. package/dist/playground/assets/index-CidizGZv.css +0 -1
@@ -0,0 +1,376 @@
1
+ import { createServer, type Server } from "node:http";
2
+ import { afterEach, describe, expect, it } from "vitest";
3
+ import { startPlaygroundProxy } from "./playground-proxy.js";
4
+
5
+ const servers: Server[] = [];
6
+ const proxies: Array<{ close: () => Promise<void> }> = [];
7
+
8
+ afterEach(async () => {
9
+ while (proxies.length > 0) {
10
+ const proxy = proxies.pop();
11
+ await proxy?.close().catch(() => {});
12
+ }
13
+ await Promise.all(
14
+ servers.splice(0).map(
15
+ (server) =>
16
+ new Promise<void>((resolve) => {
17
+ server.close(() => resolve());
18
+ })
19
+ )
20
+ );
21
+ });
22
+
23
+ function listen(
24
+ handler: (
25
+ req: import("node:http").IncomingMessage,
26
+ res: import("node:http").ServerResponse
27
+ ) => void
28
+ ): Promise<{ url: string; server: Server }> {
29
+ const server = createServer(handler);
30
+ servers.push(server);
31
+ return new Promise((resolve, reject) => {
32
+ server.listen(0, "127.0.0.1", () => {
33
+ const address = server.address();
34
+ if (address === null || typeof address === "string") {
35
+ reject(new Error("expected AddressInfo"));
36
+ return;
37
+ }
38
+ resolve({ url: `http://127.0.0.1:${address.port}`, server });
39
+ });
40
+ });
41
+ }
42
+
43
+ function cookieHeader(accessToken: string): Record<string, string> {
44
+ return { cookie: `agentkit_playground_proxy=${accessToken}` };
45
+ }
46
+
47
+ describe("startPlaygroundProxy", () => {
48
+ it("injects headers and forwards path + query", async () => {
49
+ let seenAuth: string | undefined;
50
+ let seenWake: string | undefined;
51
+ let seenPath: string | undefined;
52
+ const upstream = await listen((req, res) => {
53
+ seenAuth = req.headers.authorization;
54
+ seenWake = req.headers["x-anyrun-hibernation-lower-bound-s"] as
55
+ | string
56
+ | undefined;
57
+ seenPath = req.url;
58
+ if (req.url?.startsWith("/playground")) {
59
+ res.writeHead(200, { "content-type": "text/html" });
60
+ res.end("<html>ok</html>");
61
+ return;
62
+ }
63
+ if (req.url?.startsWith("/v1/health")) {
64
+ res.writeHead(200, { "content-type": "application/json" });
65
+ res.end(JSON.stringify({ ok: true }));
66
+ return;
67
+ }
68
+ res.writeHead(404);
69
+ res.end();
70
+ });
71
+
72
+ const proxy = await startPlaygroundProxy({
73
+ upstreamUrl: upstream.url,
74
+ headers: {
75
+ authorization: "Bearer engine-token",
76
+ "x-anyrun-hibernation-lower-bound-s": "1800",
77
+ },
78
+ });
79
+ proxies.push(proxy);
80
+
81
+ const playground = await fetch(
82
+ `${proxy.localUrl}/playground?sessionId=ses_1`,
83
+ { headers: cookieHeader(proxy.accessToken) }
84
+ );
85
+ expect(playground.status).toBe(200);
86
+ expect(await playground.text()).toBe("<html>ok</html>");
87
+ expect(seenPath).toBe("/playground?sessionId=ses_1");
88
+ expect(seenAuth).toBe("Bearer engine-token");
89
+ expect(seenWake).toBe("1800");
90
+
91
+ const health = await fetch(`${proxy.localUrl}/v1/health`, {
92
+ headers: cookieHeader(proxy.accessToken),
93
+ });
94
+ expect(health.status).toBe(200);
95
+ expect(await health.json()).toEqual({ ok: true });
96
+ });
97
+
98
+ it("bootstraps access via query token then cookie", async () => {
99
+ const upstream = await listen((_req, res) => {
100
+ res.writeHead(200);
101
+ res.end("ok");
102
+ });
103
+ const proxy = await startPlaygroundProxy({
104
+ upstreamUrl: upstream.url,
105
+ accessToken: "test-token",
106
+ });
107
+ proxies.push(proxy);
108
+
109
+ const denied = await fetch(`${proxy.localUrl}/v1/health`);
110
+ expect(denied.status).toBe(401);
111
+
112
+ const bootstrap = await fetch(proxy.playgroundUrl(), {
113
+ redirect: "manual",
114
+ });
115
+ expect(bootstrap.status).toBe(302);
116
+ expect(bootstrap.headers.get("location")).toBe("/playground");
117
+ expect(bootstrap.headers.get("set-cookie")).toContain(
118
+ "agentkit_playground_proxy=test-token"
119
+ );
120
+
121
+ const allowed = await fetch(`${proxy.localUrl}/v1/health`, {
122
+ headers: cookieHeader("test-token"),
123
+ });
124
+ expect(allowed.status).toBe(200);
125
+ });
126
+
127
+ it("streams NDJSON responses without buffering the whole body", async () => {
128
+ const upstream = await listen((req, res) => {
129
+ if (req.url !== "/v1/session/ses_x/stream?startIndex=0") {
130
+ res.writeHead(404);
131
+ res.end();
132
+ return;
133
+ }
134
+ res.writeHead(200, {
135
+ "content-type": "application/x-ndjson",
136
+ "cache-control": "no-cache",
137
+ });
138
+ res.write('{"type":"a"}\n');
139
+ setTimeout(() => {
140
+ res.write('{"type":"b"}\n');
141
+ res.end();
142
+ }, 20);
143
+ });
144
+
145
+ const proxy = await startPlaygroundProxy({
146
+ upstreamUrl: upstream.url,
147
+ headers: { "x-test": "1" },
148
+ });
149
+ proxies.push(proxy);
150
+
151
+ const resp = await fetch(
152
+ `${proxy.localUrl}/v1/session/ses_x/stream?startIndex=0`,
153
+ { headers: cookieHeader(proxy.accessToken) }
154
+ );
155
+ expect(resp.status).toBe(200);
156
+ const text = await resp.text();
157
+ expect(text).toBe('{"type":"a"}\n{"type":"b"}\n');
158
+ });
159
+
160
+ it("prefers bearerToken over client Authorization", async () => {
161
+ let seenAuth: string | undefined;
162
+ const upstream = await listen((req, res) => {
163
+ seenAuth = req.headers.authorization;
164
+ res.writeHead(200);
165
+ res.end("ok");
166
+ });
167
+
168
+ const proxy = await startPlaygroundProxy({
169
+ upstreamUrl: upstream.url,
170
+ bearerToken: "from-cli",
171
+ });
172
+ proxies.push(proxy);
173
+
174
+ await fetch(`${proxy.localUrl}/v1/info`, {
175
+ headers: {
176
+ ...cookieHeader(proxy.accessToken),
177
+ authorization: "Bearer from-browser",
178
+ },
179
+ });
180
+ expect(seenAuth).toBe("Bearer from-cli");
181
+ });
182
+
183
+ it("prefixes upstream pathname mounts", async () => {
184
+ let seenPath: string | undefined;
185
+ const upstream = await listen((req, res) => {
186
+ seenPath = req.url;
187
+ res.writeHead(200);
188
+ res.end("ok");
189
+ });
190
+
191
+ const proxy = await startPlaygroundProxy({
192
+ upstreamUrl: `${upstream.url}/my-agent`,
193
+ });
194
+ proxies.push(proxy);
195
+
196
+ await fetch(`${proxy.localUrl}/playground`, {
197
+ headers: cookieHeader(proxy.accessToken),
198
+ });
199
+ expect(seenPath).toBe("/my-agent/playground");
200
+ });
201
+
202
+ it("does not treat a short mount prefix as a path match", async () => {
203
+ let seenPath: string | undefined;
204
+ const upstream = await listen((req, res) => {
205
+ seenPath = req.url;
206
+ res.writeHead(200);
207
+ res.end("ok");
208
+ });
209
+
210
+ const proxy = await startPlaygroundProxy({
211
+ upstreamUrl: `${upstream.url}/v`,
212
+ });
213
+ proxies.push(proxy);
214
+
215
+ await fetch(`${proxy.localUrl}/v1/health`, {
216
+ headers: cookieHeader(proxy.accessToken),
217
+ });
218
+ expect(seenPath).toBe("/v/v1/health");
219
+ });
220
+
221
+ it("rejects mutating requests from another 127.0.0.1 origin", async () => {
222
+ let hits = 0;
223
+ const upstream = await listen((_req, res) => {
224
+ hits += 1;
225
+ res.writeHead(200);
226
+ res.end("ok");
227
+ });
228
+
229
+ const proxy = await startPlaygroundProxy({
230
+ upstreamUrl: upstream.url,
231
+ headers: { authorization: "Bearer secret" },
232
+ });
233
+ proxies.push(proxy);
234
+
235
+ const csrf = await fetch(`${proxy.localUrl}/v1/session`, {
236
+ method: "POST",
237
+ headers: {
238
+ ...cookieHeader(proxy.accessToken),
239
+ origin: "http://127.0.0.1:9",
240
+ "content-type": "text/plain",
241
+ },
242
+ body: '{"message":"pwn"}',
243
+ });
244
+ expect(csrf.status).toBe(403);
245
+ expect(hits).toBe(0);
246
+
247
+ const ok = await fetch(`${proxy.localUrl}/v1/session`, {
248
+ method: "POST",
249
+ headers: {
250
+ ...cookieHeader(proxy.accessToken),
251
+ origin: proxy.localUrl,
252
+ "content-type": "application/json",
253
+ },
254
+ body: '{"message":"hi"}',
255
+ });
256
+ expect(ok.status).toBe(200);
257
+ expect(hits).toBe(1);
258
+ });
259
+
260
+ it("rejects absolute-form targets without forwarding credentials", async () => {
261
+ let hits = 0;
262
+ const upstream = await listen((_req, res) => {
263
+ hits += 1;
264
+ res.writeHead(200);
265
+ res.end("ok");
266
+ });
267
+
268
+ const proxy = await startPlaygroundProxy({
269
+ upstreamUrl: upstream.url,
270
+ headers: { authorization: "Bearer secret" },
271
+ });
272
+ proxies.push(proxy);
273
+
274
+ const { request } = await import("node:http");
275
+ const status = await new Promise<number>((resolve, reject) => {
276
+ const req = request(
277
+ {
278
+ hostname: "127.0.0.1",
279
+ port: proxy.port,
280
+ path: "http://127.0.0.1:9/exfil",
281
+ method: "GET",
282
+ headers: cookieHeader(proxy.accessToken),
283
+ },
284
+ (res) => {
285
+ res.resume();
286
+ res.on("end", () => resolve(res.statusCode ?? 0));
287
+ }
288
+ );
289
+ req.on("error", reject);
290
+ req.end();
291
+ });
292
+
293
+ expect(status).toBe(400);
294
+ expect(hits).toBe(0);
295
+ });
296
+
297
+ it("refreshes credentials on upstream 401 and retries once", async () => {
298
+ const seen: string[] = [];
299
+ const upstream = await listen((req, res) => {
300
+ const token = String(req.headers["x-port-token"] ?? "");
301
+ seen.push(token);
302
+ if (token !== "fresh") {
303
+ res.writeHead(401);
304
+ res.end("expired");
305
+ return;
306
+ }
307
+ res.writeHead(200);
308
+ res.end("ok");
309
+ });
310
+
311
+ let refreshCount = 0;
312
+ const proxy = await startPlaygroundProxy({
313
+ upstreamUrl: upstream.url,
314
+ headers: { "x-port-token": "stale" },
315
+ refreshUpstream: async () => {
316
+ refreshCount += 1;
317
+ return {
318
+ upstreamUrl: upstream.url,
319
+ headers: { "x-port-token": "fresh" },
320
+ expiresAt: "2099-01-01T00:00:00.000Z",
321
+ };
322
+ },
323
+ });
324
+ proxies.push(proxy);
325
+
326
+ const resp = await fetch(`${proxy.localUrl}/v1/health`, {
327
+ headers: cookieHeader(proxy.accessToken),
328
+ });
329
+ expect(resp.status).toBe(200);
330
+ expect(await resp.text()).toBe("ok");
331
+ expect(seen).toEqual(["stale", "fresh"]);
332
+ expect(refreshCount).toBe(1);
333
+ });
334
+
335
+ it("proactively refreshes near expiresAt", async () => {
336
+ let seenToken: string | undefined;
337
+ const upstream = await listen((req, res) => {
338
+ seenToken = req.headers["x-port-token"] as string | undefined;
339
+ res.writeHead(200);
340
+ res.end("ok");
341
+ });
342
+
343
+ let nowMs = Date.parse("2026-01-01T00:00:00.000Z");
344
+ let refreshCount = 0;
345
+ const proxy = await startPlaygroundProxy({
346
+ upstreamUrl: upstream.url,
347
+ headers: { "x-port-token": "old" },
348
+ // Within the 5-minute leeway of "now".
349
+ expiresAt: "2026-01-01T00:04:00.000Z",
350
+ now: () => nowMs,
351
+ refreshUpstream: async () => {
352
+ refreshCount += 1;
353
+ return {
354
+ upstreamUrl: upstream.url,
355
+ headers: { "x-port-token": "new" },
356
+ expiresAt: "2099-01-01T00:00:00.000Z",
357
+ };
358
+ },
359
+ });
360
+ proxies.push(proxy);
361
+
362
+ const resp = await fetch(`${proxy.localUrl}/v1/health`, {
363
+ headers: cookieHeader(proxy.accessToken),
364
+ });
365
+ expect(resp.status).toBe(200);
366
+ expect(seenToken).toBe("new");
367
+ expect(refreshCount).toBe(1);
368
+
369
+ // Still valid — no second refresh.
370
+ nowMs = Date.parse("2026-01-01T00:01:00.000Z");
371
+ await fetch(`${proxy.localUrl}/v1/health`, {
372
+ headers: cookieHeader(proxy.accessToken),
373
+ });
374
+ expect(refreshCount).toBe(1);
375
+ });
376
+ });
@@ -0,0 +1,232 @@
1
+ import { mkdir, mkdtemp, readFile, rm } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { afterEach, describe, expect, it } from "vitest";
5
+ import { httpChannel } from "../channels.js";
6
+ import { defineTool } from "../tools.js";
7
+ import type {
8
+ AgentProject,
9
+ AgentRuntime,
10
+ DiscoveredTool,
11
+ RunnerTurnRequest,
12
+ TurnOutcome,
13
+ } from "../types.js";
14
+ import { setDevMode } from "./dev-mode.js";
15
+ import { toolInputJsonSchema } from "./json-schema.js";
16
+ import type { AgentRunner } from "./sdk-runner.js";
17
+ import { SessionEngine } from "./session-engine.js";
18
+
19
+ function testProject(
20
+ rootDir: string,
21
+ options: {
22
+ runtime?: AgentRuntime;
23
+ localCwd?: string;
24
+ tools?: DiscoveredTool[];
25
+ } = {}
26
+ ): AgentProject {
27
+ return {
28
+ rootDir,
29
+ agentDir: join(rootDir, "agent"),
30
+ name: "prompt-context-test",
31
+ agent: {
32
+ name: "prompt-context-test",
33
+ description: "Exercises framework prompt context.",
34
+ runtime: options.runtime ?? "local",
35
+ instructions: "Answer briefly.",
36
+ local: { cwd: options.localCwd },
37
+ tools: options.tools ?? [],
38
+ skills: [],
39
+ connections: [],
40
+ subagents: [],
41
+ seedFiles: [],
42
+ },
43
+ httpChannel: httpChannel(),
44
+ channels: [],
45
+ schedules: [],
46
+ hooks: [],
47
+ abs: [],
48
+ diagnostics: [],
49
+ };
50
+ }
51
+
52
+ function recordingRunner(requests: RunnerTurnRequest[]): AgentRunner {
53
+ return {
54
+ runTurn: async (request): Promise<TurnOutcome> => {
55
+ requests.push(request);
56
+ return { status: "finished", result: "ok" };
57
+ },
58
+ };
59
+ }
60
+
61
+ const cleanups: Array<() => Promise<void>> = [];
62
+
63
+ afterEach(async () => {
64
+ setDevMode(false);
65
+ while (cleanups.length > 0) {
66
+ await cleanups.pop()?.();
67
+ }
68
+ });
69
+
70
+ async function makeEngine(options: {
71
+ runtime?: AgentRuntime;
72
+ localCwd?: string;
73
+ tools?: DiscoveredTool[];
74
+ }): Promise<{ engine: SessionEngine; requests: RunnerTurnRequest[] }> {
75
+ const stateRoot = await mkdtemp(join(tmpdir(), "agent-serve-promptctx-"));
76
+ cleanups.push(() => rm(stateRoot, { recursive: true, force: true }));
77
+ const requests: RunnerTurnRequest[] = [];
78
+ const engine = new SessionEngine({
79
+ project: testProject(stateRoot, options),
80
+ stateRoot,
81
+ runner: recordingRunner(requests),
82
+ });
83
+ cleanups.push(() => engine.close());
84
+ return { engine, requests };
85
+ }
86
+
87
+ describe("framework prompt context (local runtime)", () => {
88
+ it("writes the identity section into AGENTS.md after the instructions", async () => {
89
+ const { engine } = await makeEngine({});
90
+ const session = await engine.send("http", "hello", { auth: null });
91
+ await session.waitForCompletion();
92
+
93
+ const agentsMd = await readFile(
94
+ join(engine.sessions.workspaceDir(session.id), "AGENTS.md"),
95
+ "utf8"
96
+ );
97
+ expect(agentsMd.startsWith("Answer briefly.")).toBe(true);
98
+ expect(agentsMd).toContain("## About you");
99
+ expect(agentsMd).toContain(
100
+ 'You are "prompt-context-test" — Exercises framework prompt context.'
101
+ );
102
+ });
103
+
104
+ it("prepends the context block to the first turn only", async () => {
105
+ setDevMode(true);
106
+ const { engine, requests } = await makeEngine({});
107
+ const session = await engine.send("http", "hello", { auth: null });
108
+ await session.waitForCompletion();
109
+ const follow = await engine.send("http", "again", {
110
+ auth: null,
111
+ continuationToken: session.continuationToken ?? undefined,
112
+ });
113
+ await follow.waitForCompletion();
114
+
115
+ const first = requests[0]?.prompt ?? "";
116
+ expect(first.startsWith("<agentkit_context>\n")).toBe(true);
117
+ expect(first).toContain(`session: ${session.id}`);
118
+ expect(first).toContain("channel: http");
119
+ expect(first).toContain("environment: local dev server");
120
+ expect(first.endsWith("\n\nhello")).toBe(true);
121
+ expect(requests[1]?.prompt).toBe("again");
122
+ });
123
+
124
+ it("reports hosted deployment outside dev mode", async () => {
125
+ const { engine, requests } = await makeEngine({});
126
+ const session = await engine.send("http", "hello", { auth: null });
127
+ await session.waitForCompletion();
128
+ expect(requests[0]?.prompt).toContain("environment: hosted deployment");
129
+ });
130
+
131
+ it("inlines instructions on the first turn when the workspace is nested inside a git repo", async () => {
132
+ // The harness roots project rules at the nearest enclosing git root, so
133
+ // the scaffolded AGENTS.md in a nested (gitignored) session workspace
134
+ // never loads — instructions must ride in the first prompt instead.
135
+ const repoDir = await mkdtemp(join(tmpdir(), "agent-serve-nested-repo-"));
136
+ cleanups.push(() => rm(repoDir, { recursive: true, force: true }));
137
+ await mkdir(join(repoDir, ".git"), { recursive: true });
138
+ const harnessParent = join(repoDir, ".state", "harness");
139
+
140
+ const { engine, requests } = await makeEngine({ localCwd: harnessParent });
141
+ const session = await engine.send("http", "hello", { auth: null });
142
+ await session.waitForCompletion();
143
+ const follow = await engine.send("http", "again", {
144
+ auth: null,
145
+ continuationToken: session.continuationToken ?? undefined,
146
+ });
147
+ await follow.waitForCompletion();
148
+
149
+ const first = requests[0]?.prompt ?? "";
150
+ expect(first.startsWith("Answer briefly.")).toBe(true);
151
+ expect(first).toContain("## About you");
152
+ const blockIndex = first.indexOf("<agentkit_context>");
153
+ expect(blockIndex).toBeGreaterThan(first.indexOf("## About you"));
154
+ expect(first.endsWith("\n\nhello")).toBe(true);
155
+ expect(requests[1]?.prompt).toBe("again");
156
+ });
157
+
158
+ it("inlines instructions when the harness cwd is a per-session override", async () => {
159
+ // With a workspace override (e.g. a PR worktree) the scaffold lands in
160
+ // the durable session workspace, which the harness never roots at.
161
+ const overrideDir = await mkdtemp(join(tmpdir(), "agent-serve-override-"));
162
+ cleanups.push(() => rm(overrideDir, { recursive: true, force: true }));
163
+
164
+ const { engine, requests } = await makeEngine({});
165
+ const session = await engine.send("http", "hello", {
166
+ auth: null,
167
+ workspaceDir: overrideDir,
168
+ });
169
+ await session.waitForCompletion();
170
+
171
+ const first = requests[0]?.prompt ?? "";
172
+ expect(first.startsWith("Answer briefly.")).toBe(true);
173
+ expect(first).toContain("## About you");
174
+ expect(first).toContain("<agentkit_context>");
175
+ });
176
+
177
+ it("embeds agent tool scripts when inlining with a workspace override", async () => {
178
+ const overrideDir = await mkdtemp(
179
+ join(tmpdir(), "agent-serve-override-tools-")
180
+ );
181
+ cleanups.push(() => rm(overrideDir, { recursive: true, force: true }));
182
+ const pipeTool = defineTool({
183
+ description: "Pipe stdin to stdout.",
184
+ execution: "agent",
185
+ script: "#!/usr/bin/env bash\ncat\n",
186
+ });
187
+ const agentTool: DiscoveredTool = {
188
+ name: "pipe",
189
+ description: pipeTool.description,
190
+ inputSchema: toolInputJsonSchema(pipeTool.inputSchema),
191
+ execution: "agent",
192
+ script: pipeTool.script,
193
+ needsApproval: false,
194
+ definition: pipeTool,
195
+ };
196
+
197
+ const { engine, requests } = await makeEngine({ tools: [agentTool] });
198
+ const session = await engine.send("http", "hello", {
199
+ auth: null,
200
+ workspaceDir: overrideDir,
201
+ });
202
+ await session.waitForCompletion();
203
+
204
+ const first = requests[0]?.prompt ?? "";
205
+ expect(first).toContain(
206
+ "Script body (write to the path above if missing):"
207
+ );
208
+ expect(first).toContain("#!/usr/bin/env bash");
209
+ });
210
+ });
211
+
212
+ describe("framework prompt context (cloud runtime)", () => {
213
+ it("prepends instructions, identity, and context block on the first turn only", async () => {
214
+ const { engine, requests } = await makeEngine({ runtime: "cloud" });
215
+ const session = await engine.send("http", "hello", { auth: null });
216
+ await session.waitForCompletion();
217
+ const follow = await engine.send("http", "again", {
218
+ auth: null,
219
+ continuationToken: session.continuationToken ?? undefined,
220
+ });
221
+ await follow.waitForCompletion();
222
+
223
+ const first = requests[0]?.prompt ?? "";
224
+ expect(first.startsWith("Answer briefly.")).toBe(true);
225
+ expect(first).toContain("## About you");
226
+ const blockIndex = first.indexOf("<agentkit_context>");
227
+ expect(blockIndex).toBeGreaterThan(first.indexOf("## About you"));
228
+ expect(first).toContain(`session: ${session.id}`);
229
+ expect(first.endsWith("\n\nhello")).toBe(true);
230
+ expect(requests[1]?.prompt).toBe("again");
231
+ });
232
+ });