@desplega.ai/agent-swarm 1.146.0 → 1.147.0

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 (214) hide show
  1. package/README.md +1 -1
  2. package/dist/{acp-adapter-jnq4612m.js → acp-adapter-9n319wqc.js} +42 -4
  3. package/dist/{actions-s41xcn3f.js → actions-2vxqvpr9.js} +9 -8
  4. package/dist/{app-j8bfw3f6.js → app-w066xfy0.js} +6 -5
  5. package/dist/assistant-kt31dj2h.js +236 -0
  6. package/dist/{boot-reembed-jxdz46jp.js → boot-reembed-az5rassp.js} +7 -6
  7. package/dist/{boot-reembed-x1476agm.js → boot-reembed-j3mm3rfz.js} +8 -7
  8. package/dist/{boot-scrub-logs-rab9h5bt.js → boot-scrub-logs-b6h54817.js} +4 -4
  9. package/dist/{claude-adapter-ep6qekbm.js → claude-adapter-w1z04ann.js} +8 -6
  10. package/dist/{claude-managed-setup-w619jncn.js → claude-managed-setup-f5pmsyrp.js} +24 -3
  11. package/dist/{claude-sdk-session-0wp3qrcy.js → claude-sdk-session-f0cc1kec.js} +8 -6
  12. package/dist/{cli-bafd6xnf.js → cli-0b6y7kxm.js} +1 -1
  13. package/dist/{cli-jyy3e193.js → cli-12fz972k.js} +4 -4
  14. package/dist/{cli-pzfc7j7g.js → cli-1th1728g.js} +2 -2
  15. package/dist/{cli-68h2mfrn.js → cli-3tgwnf43.js} +3 -3
  16. package/dist/{cli-q3qr220d.js → cli-4nr988h6.js} +1 -1
  17. package/dist/{cli-m4xsy03s.js → cli-53s590z8.js} +1 -1
  18. package/dist/{cli-c8q7292f.js → cli-54wwve05.js} +5 -4
  19. package/dist/{cli-rymrjneb.js → cli-719k9j2c.js} +2 -2
  20. package/dist/{cli-zn65x0w8.js → cli-89xfd7f9.js} +21 -47
  21. package/dist/{cli-506z6jqa.js → cli-8v0g1yc8.js} +1 -1
  22. package/dist/cli-9d8ntetj.js +42 -0
  23. package/dist/{cli-dhg6j8h3.js → cli-atcve9y8.js} +826 -737
  24. package/dist/{cli-46yck6nj.js → cli-b5bq9crk.js} +14 -31
  25. package/dist/cli-d8brzxjb.js +190 -0
  26. package/dist/{cli-a960nkfb.js → cli-de14znh8.js} +2 -2
  27. package/dist/{cli-s8sbe32s.js → cli-dttph1bw.js} +2 -2
  28. package/dist/cli-ep19k3mb.js +19 -0
  29. package/dist/{cli-d9pnxgth.js → cli-f146mn5s.js} +7 -50
  30. package/dist/{cli-qar1f940.js → cli-f7dsy61y.js} +5 -3
  31. package/dist/{cli-d4xqde2b.js → cli-ftht3zzj.js} +2780 -2587
  32. package/dist/{cli-pmwbdw36.js → cli-hbv0kq6w.js} +2 -2
  33. package/dist/{cli-fhsz3twr.js → cli-jsp14b6c.js} +171 -1
  34. package/dist/{cli-2zvtzevh.js → cli-kpv03zhj.js} +13 -11
  35. package/dist/{cli-a9bn9svs.js → cli-m8yrsg97.js} +2 -2
  36. package/dist/cli-m9sxbkhm.js +779 -0
  37. package/dist/cli-mmemxxdc.js +823 -0
  38. package/dist/{cli-6xwhtc64.js → cli-n8508nre.js} +1 -1
  39. package/dist/{cli-q0qq1sxw.js → cli-p1d5b073.js} +37 -2
  40. package/dist/{cli-yqeb27t1.js → cli-rw51bq3j.js} +13 -10
  41. package/dist/{cli-zww3vcnm.js → cli-smby5m17.js} +7 -6
  42. package/dist/{cli-ah5hjk49.js → cli-tzvk9haz.js} +94 -372
  43. package/dist/{cli-yxwe913z.js → cli-w9kb3bk1.js} +1 -1
  44. package/dist/{cli-skd674mt.js → cli-y6b6fb76.js} +25 -3
  45. package/dist/{cli-3k2g3kg2.js → cli-yz3djrm0.js} +9 -9
  46. package/dist/{cli-4secrq32.js → cli-zhjmqxzh.js} +1 -1
  47. package/dist/cli.js +13 -13
  48. package/dist/{codex-adapter-d6qajxj3.js → codex-adapter-zw4dwsx7.js} +2 -2
  49. package/dist/{codex-hook-n7m5k7cr.js → codex-hook-bw8p5nh4.js} +2 -2
  50. package/dist/{codex-session-runner-v9krqe4x.js → codex-session-runner-k981s65v.js} +2 -2
  51. package/dist/{commands-923wsas7.js → commands-sykrxx7e.js} +4 -4
  52. package/dist/{db-m1hy4zh5.js → db-sfbsc8wt.js} +6 -4
  53. package/dist/handlers-0aspcejm.js +603 -0
  54. package/dist/{hook-ee4hff56.js → hook-4nvqj2sb.js} +7 -5
  55. package/dist/{hook-bxe3bkdy.js → hook-hxccs7m5.js} +24 -49
  56. package/dist/{http-a8rx91me.js → http-y06z517d.js} +223 -92
  57. package/dist/{index-697zfzv1.js → index-hssww7kj.js} +15 -14
  58. package/dist/{index-3x9g3nbg.js → index-k8znfx0z.js} +13 -12
  59. package/dist/{index-1j9va48r.js → index-tce1rss4.js} +371 -110
  60. package/dist/{index-jj4q3x59.js → index-x5j894zf.js} +14 -13
  61. package/dist/{keepalive-sds4e4ce.js → keepalive-w733ax66.js} +6 -6
  62. package/dist/{lead-ggq6szk8.js → lead-75bjqa9d.js} +29 -27
  63. package/dist/{maintenance-093p41d4.js → maintenance-w4f1zjck.js} +8 -7
  64. package/dist/{oauth-refresh-sweep-dvzq4ckj.js → oauth-refresh-sweep-wdetky26.js} +5 -5
  65. package/dist/{onboard-ydr9tzw5.js → onboard-bb5net8s.js} +2 -2
  66. package/dist/{otel-impl-zx3tdf3j.js → otel-impl-0w7c14ap.js} +1 -1
  67. package/dist/{pi-mono-adapter-dtkz4e7j.js → pi-mono-adapter-2has4gnp.js} +12 -2
  68. package/dist/{pricing-refresh-7e6pqnzp.js → pricing-refresh-9nzsrgcq.js} +6 -6
  69. package/dist/{rbac-roles-p2rh6d0r.js → rbac-roles-r3hqjp09.js} +5 -5
  70. package/dist/{rbac-roles-0dv1wt0w.js → rbac-roles-z1jrffbt.js} +4 -4
  71. package/dist/{registry-9kdck5ye.js → registry-v5wytg0s.js} +1 -2
  72. package/dist/{render-v2-4k3jt4r2.js → render-v2-vbr89fnb.js} +6 -5
  73. package/dist/{seed-pricing-2cc1qmcd.js → seed-pricing-g62hy1pk.js} +7 -5
  74. package/dist/{setup-c5yss30k.js → setup-gnfqnptp.js} +2 -2
  75. package/dist/{worker-7srgeqcr.js → worker-kznxp4qm.js} +29 -27
  76. package/openapi.json +263 -12
  77. package/package.json +1 -1
  78. package/plugin/commands/start-worker.md +4 -9
  79. package/plugin/pi-skills/start-worker/SKILL.md +4 -9
  80. package/src/be/db/agents.ts +556 -0
  81. package/src/be/db/context-versions.ts +117 -0
  82. package/src/be/db/runtime.ts +406 -0
  83. package/src/be/db/tasks/read.ts +1167 -0
  84. package/src/be/db/tasks/write.ts +953 -0
  85. package/src/be/db.ts +583 -3428
  86. package/src/be/migrations/147_scheduled_tasks_parent_task.sql +7 -0
  87. package/src/be/migrations/148_pages_svg.sql +123 -0
  88. package/src/be/migrations/149_session_tokens.sql +24 -0
  89. package/src/be/scripts/typecheck.ts +1 -0
  90. package/src/be/seed-pricing.ts +13 -8
  91. package/src/be/seed-scripts/catalog/ops-catalog-audit.inline.ts +1 -1
  92. package/src/be/seed-scripts/catalog/ops-catalog-audit.ts +1 -1
  93. package/src/be/seed-scripts/catalog/star-history-refresh.ts +101 -0
  94. package/src/be/seed-scripts/catalog/star-history-renderer.ts +119 -0
  95. package/src/be/seed-scripts/index.ts +17 -0
  96. package/src/be/seed-skills/index.ts +14 -0
  97. package/src/be/swarm-config-guard.ts +1 -1
  98. package/src/be/task-attachment-store.ts +70 -0
  99. package/src/be/users.ts +77 -0
  100. package/src/commands/attachments-section.ts +37 -0
  101. package/src/commands/claude-managed-setup.ts +45 -3
  102. package/src/commands/claude-md-session.ts +408 -0
  103. package/src/commands/context-preamble.ts +15 -3
  104. package/src/commands/profile-sync.ts +205 -5
  105. package/src/commands/provider-credentials.ts +60 -27
  106. package/src/commands/runner.ts +63 -58
  107. package/src/commands/templates.ts +25 -2
  108. package/src/hooks/hook.ts +26 -67
  109. package/src/http/agents.ts +40 -1
  110. package/src/http/auth.ts +13 -1
  111. package/src/http/fs.ts +21 -51
  112. package/src/http/mcp.ts +32 -0
  113. package/src/http/pages-public.ts +20 -4
  114. package/src/http/poll.ts +4 -0
  115. package/src/http/repos.ts +3 -3
  116. package/src/http/sessions.ts +82 -0
  117. package/src/http/tasks.ts +10 -2
  118. package/src/prompts/session-templates.ts +4 -2
  119. package/src/providers/acp-adapter.ts +42 -6
  120. package/src/providers/pi-mono-adapter.ts +23 -6
  121. package/src/scheduler/scheduler.test.ts +49 -2
  122. package/src/scheduler/scheduler.ts +4 -0
  123. package/src/scripts-runtime/sdk-allowlist.ts +1 -0
  124. package/src/scripts-runtime/types/stdlib.d.ts +8 -0
  125. package/src/scripts-runtime/types/swarm-sdk.d.ts +8 -0
  126. package/src/server.ts +2 -0
  127. package/src/slack/assistant.ts +87 -46
  128. package/src/slack/handlers.test.ts +1 -3
  129. package/src/slack/handlers.ts +102 -87
  130. package/src/slack/inbound-files.ts +466 -0
  131. package/src/slack/templates.ts +14 -0
  132. package/src/tasks/task-terminal-effects.ts +158 -0
  133. package/src/tests/acp-adapter.test.ts +327 -192
  134. package/src/tests/budget-claim-gate.test.ts +1 -0
  135. package/src/tests/build-pi-skills.test.ts +5 -7
  136. package/src/tests/claude-managed-setup.test.ts +52 -4
  137. package/src/tests/claude-md-session.test.ts +603 -0
  138. package/src/tests/codex-skill-resolver.test.ts +19 -0
  139. package/src/tests/context-versioning.test.ts +107 -0
  140. package/src/tests/credential-check.test.ts +157 -14
  141. package/src/tests/db-agents.test.ts +91 -0
  142. package/src/tests/db-context-versions.test.ts +37 -0
  143. package/src/tests/db-extraction-contract.test.ts +45 -0
  144. package/src/tests/db-runtime.test.ts +70 -0
  145. package/src/tests/db-task-read-contract.test.ts +624 -0
  146. package/src/tests/db-task-write-contract.test.ts +653 -0
  147. package/src/tests/db-tasks-read.test.ts +183 -0
  148. package/src/tests/db-tasks-write.test.ts +230 -0
  149. package/src/tests/defer-task.test.ts +389 -0
  150. package/src/tests/docs-index-footprint.test.js +2 -2
  151. package/src/tests/e2e-visuals-stage.test.ts +151 -0
  152. package/src/tests/file-lock.test.ts +144 -0
  153. package/src/tests/fixtures/file-lock-holder.ts +13 -0
  154. package/src/tests/fixtures/hold-file-lock.ts +25 -0
  155. package/src/tests/heartbeat-expiry.test.ts +188 -0
  156. package/src/tests/http-api-integration.test.ts +78 -1
  157. package/src/tests/pages-public-html.test.ts +43 -0
  158. package/src/tests/pages-svg-migration.test.ts +42 -0
  159. package/src/tests/preload.ts +4 -1
  160. package/src/tests/profile-sync-warning.test.ts +16 -0
  161. package/src/tests/profile-sync.test.ts +126 -0
  162. package/src/tests/prompt-template-session.test.ts +20 -4
  163. package/src/tests/provider-registration.test.ts +93 -0
  164. package/src/tests/rbac-e2e-helpers.test.ts +53 -0
  165. package/src/tests/rbac-e2e-helpers.ts +19 -2
  166. package/src/tests/routing-affinity-inventory.test.ts +1 -1
  167. package/src/tests/runner-attachments-section.test.ts +22 -2
  168. package/src/tests/runner-context-preamble.test.ts +30 -0
  169. package/src/tests/runner-deployment-resume.test.ts +107 -0
  170. package/src/tests/runner-repo-autostash.test.ts +12 -0
  171. package/src/tests/script-connections.test.ts +4 -0
  172. package/src/tests/seed-scripts.test.ts +2 -2
  173. package/src/tests/session-tokens.test.ts +279 -0
  174. package/src/tests/slack-file-tools-attachments.test.ts +408 -0
  175. package/src/tests/slack-inbound-files.test.ts +847 -0
  176. package/src/tests/star-history-refresh.test.ts +102 -0
  177. package/src/tests/swarm-repos.test.ts +20 -0
  178. package/src/tests/tool-annotations.test.ts +4 -3
  179. package/src/tests/ui-logs-parser.test.ts +176 -0
  180. package/src/tools/create-page.ts +4 -2
  181. package/src/tools/defer-task.ts +239 -0
  182. package/src/tools/repos/update-repo.ts +2 -2
  183. package/src/tools/schedules/create-schedule.ts +1 -0
  184. package/src/tools/schedules/list-schedules.ts +1 -0
  185. package/src/tools/schedules/patch-schedule.ts +1 -0
  186. package/src/tools/schedules/update-schedule.ts +1 -0
  187. package/src/tools/slack-download-file.ts +60 -29
  188. package/src/tools/slack-read.ts +73 -32
  189. package/src/tools/store-progress.ts +16 -146
  190. package/src/tools/tool-config.ts +1 -0
  191. package/src/tools/utils.ts +21 -0
  192. package/src/types.ts +37 -4
  193. package/src/utils/acp-session-token.ts +56 -0
  194. package/src/utils/file-lock.ts +111 -0
  195. package/src/utils/heartbeat-expiry.ts +78 -0
  196. package/src/utils/request-auth-context.ts +2 -0
  197. package/src/utils/task-attachment-links.ts +22 -0
  198. package/templates/skills/code-quality/content.md +8 -5
  199. package/templates/skills/download-task-attachment/content.md +2 -1
  200. package/{plugin/pi-skills → templates/skills}/review-offered-task/SKILL.md +5 -5
  201. package/templates/skills/review-offered-task/config.json +14 -0
  202. package/{plugin/commands/review-offered-task.md → templates/skills/review-offered-task/content.md} +3 -8
  203. package/templates/skills/scheduling/content.md +4 -0
  204. package/templates/skills/slack-interaction/content.md +2 -2
  205. package/templates/skills/work-on-task/SKILL.md +16 -0
  206. package/templates/skills/work-on-task/config.json +14 -0
  207. package/templates/skills/work-on-task/content.md +11 -0
  208. package/dist/assistant-dx3ew4d8.js +0 -212
  209. package/dist/cli-00kyrp1a.js +0 -317
  210. package/dist/cli-n1q8q14d.js +0 -177
  211. package/dist/cli-ss6vybmt.js +0 -269
  212. package/dist/handlers-e9vp2fr8.js +0 -592
  213. package/plugin/commands/work-on-task.md +0 -39
  214. package/plugin/pi-skills/work-on-task/SKILL.md +0 -39
@@ -1,6 +1,6 @@
1
1
  import { afterEach, describe, expect, test } from "bun:test";
2
2
  import { mkdtempSync, rmSync } from "node:fs";
3
- import { createServer } from "node:http";
3
+ import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
4
4
  import { tmpdir } from "node:os";
5
5
  import { join } from "node:path";
6
6
  import type { Usage } from "@agentclientprotocol/sdk";
@@ -57,6 +57,32 @@ function baseConfig(overrides: Partial<ProviderSessionConfig> = {}): ProviderSes
57
57
  };
58
58
  }
59
59
 
60
+ /**
61
+ * Start a minimal swarm API stub that handles POST /api/sessions/tokens and
62
+ * DELETE /api/sessions/tokens/:id. Returns the server and its base URL.
63
+ * The caller is responsible for closing the server.
64
+ */
65
+ async function startTokenStubServer(
66
+ tokenId: string,
67
+ plaintext: string,
68
+ ): Promise<{ server: Server; apiUrl: string }> {
69
+ const stub = createServer(async (req: IncomingMessage, res: ServerResponse) => {
70
+ if (req.method === "POST" && req.url === "/api/sessions/tokens") {
71
+ res.writeHead(200, { "Content-Type": "application/json" });
72
+ res.end(JSON.stringify({ tokenId, plaintext }));
73
+ } else if (req.method === "DELETE" && req.url?.startsWith("/api/sessions/tokens/")) {
74
+ res.writeHead(204);
75
+ res.end();
76
+ } else {
77
+ res.writeHead(404);
78
+ res.end();
79
+ }
80
+ });
81
+ await new Promise<void>((resolve) => stub.listen(0, "127.0.0.1", resolve));
82
+ const addr = stub.address() as import("net").AddressInfo;
83
+ return { server: stub, apiUrl: `http://127.0.0.1:${addr.port}` };
84
+ }
85
+
60
86
  describe("ACPAdapter", () => {
61
87
  test("is registered with MCP and local-environment traits", async () => {
62
88
  const adapter = await createProviderAdapter("acp");
@@ -274,211 +300,205 @@ new AgentSideConnection((connection) => new FakeAgent(connection), stream);
274
300
  `,
275
301
  );
276
302
 
277
- const adapter = new ACPAdapter();
278
- const session = await adapter.createSession(
279
- baseConfig({
280
- cwd,
281
- env: {
282
- PATH: process.env.PATH ?? "",
283
- HOME: process.env.HOME ?? "",
284
- ACP_TARGET_COMMAND: "bun",
285
- ACP_TARGET_ARGS: JSON.stringify([agentPath]),
286
- ACP_CONFIG_OPTIONS: JSON.stringify({ thought: "high" }),
287
- },
288
- }),
303
+ const { server: tokenStub, apiUrl } = await startTokenStubServer(
304
+ "stub-token-id",
305
+ "aseph_stubtokenfortest1234567890",
289
306
  );
290
307
 
291
- const events: ProviderEvent[] = [];
292
- session.onEvent((event) => events.push(event));
293
- const result = await session.waitForCompletion();
308
+ try {
309
+ const adapter = new ACPAdapter();
310
+ const session = await adapter.createSession(
311
+ baseConfig({
312
+ cwd,
313
+ apiUrl,
314
+ env: {
315
+ PATH: process.env.PATH ?? "",
316
+ HOME: process.env.HOME ?? "",
317
+ ACP_TARGET_COMMAND: "bun",
318
+ ACP_TARGET_ARGS: JSON.stringify([agentPath]),
319
+ ACP_CONFIG_OPTIONS: JSON.stringify({ thought: "high" }),
320
+ },
321
+ }),
322
+ );
294
323
 
295
- expect(result).toMatchObject({
296
- exitCode: 0,
297
- sessionId: "acp-session-1",
298
- output: "done",
299
- isError: false,
300
- });
301
- expect(events.some((event) => event.type === "session_init")).toBe(true);
302
- expect(events.find((event) => event.type === "session_init")).toMatchObject({
303
- providerMeta: {
304
- target: "custom",
305
- configOptions: [
306
- { id: "model", currentValue: "fake-model" },
307
- { id: "thought", currentValue: "high" },
308
- ],
309
- },
310
- });
311
- expect(events.some((event) => event.type === "message" && event.content === "done")).toBe(true);
312
- expect(events.some((event) => event.type === "tool_start")).toBe(true);
313
- expect(events.some((event) => event.type === "tool_end")).toBe(true);
314
- expect(events.some((event) => event.type === "result")).toBe(true);
315
-
316
- const rawLogs = events
317
- .filter(
318
- (event): event is Extract<ProviderEvent, { type: "raw_log" }> => event.type === "raw_log",
319
- )
320
- .map((event) => event.content);
321
- expect(rawLogs.length).toBeGreaterThan(0);
322
- expect(
323
- rawLogs.some((content) => {
324
- const event = JSON.parse(content) as Record<string, unknown>;
325
- const update = event.update as Record<string, unknown> | undefined;
326
- return update?.sessionUpdate === "agent_message_chunk";
327
- }),
328
- ).toBe(true);
329
- expect(
330
- rawLogs.some((content) => {
331
- const event = JSON.parse(content) as Record<string, unknown>;
332
- const update = event.update as Record<string, unknown> | undefined;
333
- return update?.sessionUpdate === "current_mode_update";
334
- }),
335
- ).toBe(true);
336
- expect(
337
- rawLogs.some((content) => {
338
- const event = JSON.parse(content) as Record<string, unknown>;
339
- return event.type === "message" && event.content === "done";
340
- }),
341
- ).toBe(true);
342
- expect(rawLogs.every((content) => content.length <= 30_000)).toBe(true);
324
+ const events: ProviderEvent[] = [];
325
+ session.onEvent((event) => events.push(event));
326
+ const result = await session.waitForCompletion();
343
327
 
344
- initDb(":memory:");
345
- try {
346
- const task = await createTaskExtended("ACP persistence test");
347
- await createSessionLogs({
348
- taskId: task.id,
349
- sessionId: session.sessionId,
350
- iteration: 1,
351
- cli: "acp",
352
- lines: rawLogs,
328
+ expect(result).toMatchObject({
329
+ exitCode: 0,
330
+ sessionId: "acp-session-1",
331
+ output: "done",
332
+ isError: false,
353
333
  });
354
- const persisted = await getSessionLogsByTaskId(task.id);
355
- expect(persisted).toHaveLength(rawLogs.length);
356
- expect(persisted.map((entry) => entry.content)).toEqual(rawLogs);
357
- expect(persisted.every((entry) => entry.cli === "acp")).toBe(true);
358
- const responseLog = persisted
359
- .map((entry) => JSON.parse(entry.content))
360
- .find((entry) => entry.name === "acp_prompt_response");
361
- expect(responseLog).toEqual({
362
- type: "custom",
363
- name: "acp_prompt_response",
364
- data: {
365
- sessionId: session.sessionId,
366
- stopReason: "end_turn",
367
- usage: usage ?? null,
368
- _meta: { debug: "[REDACTED:github_token]", note: "response metadata" },
334
+ expect(events.some((event) => event.type === "session_init")).toBe(true);
335
+ expect(events.find((event) => event.type === "session_init")).toMatchObject({
336
+ providerMeta: {
337
+ target: "custom",
338
+ configOptions: [
339
+ { id: "model", currentValue: "fake-model" },
340
+ { id: "thought", currentValue: "high" },
341
+ ],
369
342
  },
370
343
  });
371
- expect(result.cost?.totalCostUsd).toBe(0);
372
- expect(result.cost?.inputTokens).toBe(usage?.inputTokens);
373
- expect(result.cost?.outputTokens).toBe(usage?.outputTokens);
374
- expect(result.cost?.cacheReadTokens).toBe(usage?.cachedReadTokens ?? undefined);
375
- expect(result.cost?.cacheWriteTokens).toBe(usage?.cachedWriteTokens ?? undefined);
376
- // Match saveCostData's JSON transport: undefined counters disappear on the wire.
377
- // The existing API deliberately coalesces them to zero; only raw logs retain absence.
378
- const agent = await createAgent({ name: "ACP cost test", isLead: false, status: "idle" });
379
- const server = createServer(async (req, res) => {
380
- const handled = await handleSessionData(
381
- req,
382
- res,
383
- getPathSegments(req.url ?? ""),
384
- parseQueryParams(req.url ?? ""),
385
- agent.id,
386
- );
387
- if (!handled) {
388
- res.writeHead(404);
389
- res.end();
390
- }
391
- });
392
- try {
393
- const port = await listenOnFreePort(server);
394
- const endpoint = `http://127.0.0.1:${port}/api/session-costs`;
395
- const body = JSON.stringify({ ...result.cost, agentId: agent.id, taskId: task.id });
396
- if (!usage) {
397
- for (const counter of [
398
- "inputTokens",
399
- "outputTokens",
400
- "cacheReadTokens",
401
- "cacheWriteTokens",
402
- ]) {
403
- expect(JSON.parse(body)).not.toHaveProperty(counter);
404
- }
405
- }
406
- const response = await fetch(endpoint, {
407
- method: "POST",
408
- headers: { "Content-Type": "application/json" },
409
- body,
410
- });
411
- expect(response.status).toBe(201);
412
- const { cost } = await response.json();
413
- expect(cost).toMatchObject({
414
- inputTokens: usage?.inputTokens ?? 0,
415
- outputTokens: usage?.outputTokens ?? 0,
416
- cacheReadTokens: usage?.cachedReadTokens ?? 0,
417
- cacheWriteTokens: usage?.cachedWriteTokens ?? 0,
418
- costSource: "unpriced",
419
- });
420
- const readback = await fetch(`${endpoint}?taskId=${task.id}`);
421
- expect(readback.status).toBe(200);
422
- const { costs } = await readback.json();
423
- expect(costs).toHaveLength(1);
424
- expect(costs[0]).toMatchObject({
425
- id: cost.id,
426
- inputTokens: cost.inputTokens,
427
- outputTokens: cost.outputTokens,
428
- cacheReadTokens: cost.cacheReadTokens,
429
- cacheWriteTokens: cost.cacheWriteTokens,
430
- costSource: cost.costSource,
431
- });
432
- } finally {
433
- await new Promise<void>((resolve) => server.close(() => resolve()));
434
- }
435
- const persistedJson = persisted.map((entry) => entry.content).join("\n");
436
- expect(persistedJson).not.toContain("opaque-response-credential");
437
- const credentialHeaderNames = [
438
- "authorization",
439
- "proxy-authorization",
440
- "cookie",
441
- "set-cookie",
442
- "www-authenticate",
443
- "proxy-authenticate",
444
- "x-api-key",
445
- "api-key",
446
- "x-auth-token",
447
- "x-access-token",
448
- "x-session-token",
449
- ];
450
- for (const headerName of credentialHeaderNames) {
451
- expect(persistedJson.toLowerCase()).not.toContain(headerName);
452
- }
453
- const credentialValues = credentialHeaderNames.flatMap((headerName) => [
454
- `opaque-array-${headerName}-credential`,
455
- `opaque-map-${headerName}-credential`,
456
- ]);
457
- for (const credentialValue of credentialValues) {
458
- expect(persistedJson).not.toContain(credentialValue);
459
- }
460
- expect(persistedJson).toContain("X-Debug");
461
- expect(persistedJson).toContain("X-Debug-Map");
462
- expect(persistedJson).not.toContain("ghp_abcdefghijklmnopqrstuvwxyz0123456789");
463
- expect(persistedJson).toContain("[REDACTED:github_token]");
464
- expect(persistedJson).toContain("… [truncated]");
465
- expect(persistedJson).not.toContain("x".repeat(30_001));
466
- const transcript = normalizeSessionLogs(persisted);
467
- expect(transcript.items.some((item) => item.kind === "unknown")).toBe(false);
344
+ expect(events.some((event) => event.type === "message" && event.content === "done")).toBe(
345
+ true,
346
+ );
347
+ expect(events.some((event) => event.type === "tool_start")).toBe(true);
348
+ expect(events.some((event) => event.type === "tool_end")).toBe(true);
349
+ expect(events.some((event) => event.type === "result")).toBe(true);
350
+
351
+ const rawLogs = events
352
+ .filter(
353
+ (event): event is Extract<ProviderEvent, { type: "raw_log" }> => event.type === "raw_log",
354
+ )
355
+ .map((event) => event.content);
356
+ expect(rawLogs.length).toBeGreaterThan(0);
468
357
  expect(
469
- transcript.items.some(
470
- (item) => item.kind === "text" && item.role === "assistant" && item.text === "done",
471
- ),
358
+ rawLogs.some((content) => {
359
+ const event = JSON.parse(content) as Record<string, unknown>;
360
+ const update = event.update as Record<string, unknown> | undefined;
361
+ return update?.sessionUpdate === "agent_message_chunk";
362
+ }),
472
363
  ).toBe(true);
473
364
  expect(
474
- transcript.items.some((item) => item.kind === "tool_call" && item.tool?.id === "tool-1"),
365
+ rawLogs.some((content) => {
366
+ const event = JSON.parse(content) as Record<string, unknown>;
367
+ const update = event.update as Record<string, unknown> | undefined;
368
+ return update?.sessionUpdate === "current_mode_update";
369
+ }),
475
370
  ).toBe(true);
476
371
  expect(
477
- transcript.items.find((item) => item.kind === "tool_result" && item.result?.id === "tool-1")
478
- ?.result?.isError,
372
+ rawLogs.some((content) => {
373
+ const event = JSON.parse(content) as Record<string, unknown>;
374
+ return event.type === "message" && event.content === "done";
375
+ }),
479
376
  ).toBe(true);
377
+ expect(rawLogs.every((content) => content.length <= 30_000)).toBe(true);
378
+
379
+ initDb(":memory:");
380
+ try {
381
+ const task = await createTaskExtended("ACP persistence test");
382
+ await createSessionLogs({
383
+ taskId: task.id,
384
+ sessionId: session.sessionId,
385
+ iteration: 1,
386
+ cli: "acp",
387
+ lines: rawLogs,
388
+ });
389
+ const persisted = await getSessionLogsByTaskId(task.id);
390
+ expect(persisted).toHaveLength(rawLogs.length);
391
+ expect(persisted.map((entry) => entry.content)).toEqual(rawLogs);
392
+ expect(persisted.every((entry) => entry.cli === "acp")).toBe(true);
393
+ // Match saveCostData's JSON transport: undefined counters disappear on the wire.
394
+ // The existing API deliberately coalesces them to zero; only raw logs retain absence.
395
+ const agent = await createAgent({ name: "ACP cost test", isLead: false, status: "idle" });
396
+ const server = createServer(async (req, res) => {
397
+ const handled = await handleSessionData(
398
+ req,
399
+ res,
400
+ getPathSegments(req.url ?? ""),
401
+ parseQueryParams(req.url ?? ""),
402
+ agent.id,
403
+ );
404
+ if (!handled) {
405
+ res.writeHead(404);
406
+ res.end();
407
+ }
408
+ });
409
+ try {
410
+ const port = await listenOnFreePort(server);
411
+ const endpoint = `http://127.0.0.1:${port}/api/session-costs`;
412
+ const body = JSON.stringify({ ...result.cost, agentId: agent.id, taskId: task.id });
413
+ if (!usage) {
414
+ for (const counter of [
415
+ "inputTokens",
416
+ "outputTokens",
417
+ "cacheReadTokens",
418
+ "cacheWriteTokens",
419
+ ]) {
420
+ expect(JSON.parse(body)).not.toHaveProperty(counter);
421
+ }
422
+ }
423
+ const response = await fetch(endpoint, {
424
+ method: "POST",
425
+ headers: { "Content-Type": "application/json" },
426
+ body,
427
+ });
428
+ expect(response.status).toBe(201);
429
+ const { cost } = await response.json();
430
+ expect(cost).toMatchObject({
431
+ inputTokens: usage?.inputTokens ?? 0,
432
+ outputTokens: usage?.outputTokens ?? 0,
433
+ cacheReadTokens: usage?.cachedReadTokens ?? 0,
434
+ cacheWriteTokens: usage?.cachedWriteTokens ?? 0,
435
+ costSource: "unpriced",
436
+ });
437
+ const readback = await fetch(`${endpoint}?taskId=${task.id}`);
438
+ expect(readback.status).toBe(200);
439
+ const { costs } = await readback.json();
440
+ expect(costs).toHaveLength(1);
441
+ expect(costs[0]).toMatchObject({
442
+ id: cost.id,
443
+ inputTokens: cost.inputTokens,
444
+ outputTokens: cost.outputTokens,
445
+ cacheReadTokens: cost.cacheReadTokens,
446
+ cacheWriteTokens: cost.cacheWriteTokens,
447
+ costSource: cost.costSource,
448
+ });
449
+ } finally {
450
+ await new Promise<void>((resolve) => server.close(() => resolve()));
451
+ }
452
+ const persistedJson = persisted.map((entry) => entry.content).join("\n");
453
+ const credentialHeaderNames = [
454
+ "authorization",
455
+ "proxy-authorization",
456
+ "cookie",
457
+ "set-cookie",
458
+ "www-authenticate",
459
+ "proxy-authenticate",
460
+ "x-api-key",
461
+ "api-key",
462
+ "x-auth-token",
463
+ "x-access-token",
464
+ "x-session-token",
465
+ ];
466
+ for (const headerName of credentialHeaderNames) {
467
+ expect(persistedJson.toLowerCase()).not.toContain(headerName);
468
+ }
469
+ const credentialValues = credentialHeaderNames.flatMap((headerName) => [
470
+ `opaque-array-${headerName}-credential`,
471
+ `opaque-map-${headerName}-credential`,
472
+ ]);
473
+ for (const credentialValue of credentialValues) {
474
+ expect(persistedJson).not.toContain(credentialValue);
475
+ }
476
+ expect(persistedJson).toContain("X-Debug");
477
+ expect(persistedJson).toContain("X-Debug-Map");
478
+ expect(persistedJson).not.toContain("ghp_abcdefghijklmnopqrstuvwxyz0123456789");
479
+ expect(persistedJson).toContain("[REDACTED:github_token]");
480
+ expect(persistedJson).toContain("… [truncated]");
481
+ expect(persistedJson).not.toContain("x".repeat(30_001));
482
+ const transcript = normalizeSessionLogs(persisted);
483
+ expect(transcript.items.some((item) => item.kind === "unknown")).toBe(false);
484
+ expect(
485
+ transcript.items.some(
486
+ (item) => item.kind === "text" && item.role === "assistant" && item.text === "done",
487
+ ),
488
+ ).toBe(true);
489
+ expect(
490
+ transcript.items.some((item) => item.kind === "tool_call" && item.tool?.id === "tool-1"),
491
+ ).toBe(true);
492
+ expect(
493
+ transcript.items.find(
494
+ (item) => item.kind === "tool_result" && item.result?.id === "tool-1",
495
+ )?.result?.isError,
496
+ ).toBe(true);
497
+ } finally {
498
+ closeDb();
499
+ }
480
500
  } finally {
481
- closeDb();
501
+ await new Promise<void>((resolve) => tokenStub.close(() => resolve()));
482
502
  }
483
503
  });
484
504
 
@@ -667,4 +687,119 @@ new AgentSideConnection((connection) => new FakeAgent(connection), stream);
667
687
  expect(toAcpMcpServers({ broken: { foo: "bar" } })).toEqual([]);
668
688
  expect(toAcpMcpServers(null)).toEqual([]);
669
689
  });
690
+
691
+ test("mints an ephemeral token and revokes it when the session ends", async () => {
692
+ const cwd = makeTempDir();
693
+ const agentPath = join(cwd, "fake-acp-ephem-agent.ts");
694
+ const sdkPath = join(process.cwd(), "node_modules/@agentclientprotocol/sdk/dist/acp.js");
695
+
696
+ const captureFile = join(cwd, "captured-auth.txt");
697
+
698
+ await Bun.write(
699
+ agentPath,
700
+ `
701
+ import { Readable, Writable } from "node:stream";
702
+ import { AgentSideConnection, PROTOCOL_VERSION, ndJsonStream } from "${sdkPath}";
703
+ class FakeAgent {
704
+ constructor(connection) { this.connection = connection; }
705
+ async initialize() {
706
+ return { protocolVersion: PROTOCOL_VERSION, agentCapabilities: { loadSession: false } };
707
+ }
708
+ async newSession(params) {
709
+ const swarm = params.mcpServers.find((s) => s.name === "swarm");
710
+ const auth = swarm?.headers?.find((h) => h.name === "Authorization")?.value ?? "";
711
+ // Write bearer to a file so the test can read it back without relying on
712
+ // a custom sessionUpdate type that the ACP SDK schema rejects.
713
+ await Bun.write(${JSON.stringify(captureFile)}, auth);
714
+ // newSession must return { sessionId, configOptions? }, not a prompt response.
715
+ return { sessionId: "ephem-session-1" };
716
+ }
717
+ async prompt(params) {
718
+ await this.connection.sessionUpdate({
719
+ sessionId: params.sessionId,
720
+ update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "done" }, messageId: "m1" },
721
+ });
722
+ return { stopReason: "end_turn" };
723
+ }
724
+ async cancel() {}
725
+ }
726
+ const stream = ndJsonStream(Writable.toWeb(process.stdout), Readable.toWeb(process.stdin));
727
+ new AgentSideConnection((connection) => new FakeAgent(connection), stream);
728
+ `,
729
+ );
730
+
731
+ // Minimal swarm API stub: tracks calls to the session token endpoints.
732
+ const FAKE_TOKEN = "aseph_ephemtesttoken12345678901234";
733
+ const FAKE_TOKEN_ID = "fake-ephem-token-id";
734
+ let mintCalled = false;
735
+ let revokeCalled = false;
736
+ let mintBody: Record<string, unknown> = {};
737
+
738
+ const swarmServer = createServer(async (req: IncomingMessage, res: ServerResponse) => {
739
+ if (req.method === "POST" && req.url === "/api/sessions/tokens") {
740
+ mintCalled = true;
741
+ const chunks: Buffer[] = [];
742
+ for await (const chunk of req) chunks.push(chunk as Buffer);
743
+ try {
744
+ mintBody = JSON.parse(Buffer.concat(chunks).toString()) as Record<string, unknown>;
745
+ } catch {
746
+ /* ignore */
747
+ }
748
+ res.writeHead(200, { "Content-Type": "application/json" });
749
+ res.end(JSON.stringify({ tokenId: FAKE_TOKEN_ID, plaintext: FAKE_TOKEN }));
750
+ } else if (req.method === "DELETE" && req.url === `/api/sessions/tokens/${FAKE_TOKEN_ID}`) {
751
+ revokeCalled = true;
752
+ res.writeHead(204);
753
+ res.end();
754
+ } else {
755
+ res.writeHead(404);
756
+ res.end();
757
+ }
758
+ });
759
+ await new Promise<void>((resolve) => swarmServer.listen(0, "127.0.0.1", resolve));
760
+ const swarmPort = (swarmServer.address() as import("net").AddressInfo).port;
761
+
762
+ try {
763
+ const adapter = new ACPAdapter();
764
+ const session = await adapter.createSession(
765
+ baseConfig({
766
+ cwd,
767
+ apiUrl: `http://127.0.0.1:${swarmPort}`,
768
+ apiKey: "real-operator-key",
769
+ env: {
770
+ PATH: process.env.PATH ?? "",
771
+ HOME: process.env.HOME ?? "",
772
+ ACP_TARGET_COMMAND: "bun",
773
+ ACP_TARGET_ARGS: JSON.stringify([agentPath]),
774
+ },
775
+ }),
776
+ );
777
+
778
+ const events: ProviderEvent[] = [];
779
+ session.onEvent((e) => events.push(e));
780
+ const result = await session.waitForCompletion();
781
+ // Give the revoke call (fire-and-forget) a moment to complete.
782
+ await Bun.sleep(100);
783
+
784
+ expect(result.exitCode).toBe(0);
785
+
786
+ // The adapter must have minted an ephemeral token.
787
+ expect(mintCalled).toBe(true);
788
+ expect(mintBody).toMatchObject({
789
+ agentId: "agent-1",
790
+ taskId: "task-1",
791
+ });
792
+
793
+ // The adapter must have revoked the token after the session ended.
794
+ expect(revokeCalled).toBe(true);
795
+
796
+ // Verify the bearer forwarded to the ACP target is the ephemeral token.
797
+ // The fake agent wrote the captured Authorization header value to a file
798
+ // during newSession; read it back here.
799
+ const captured = await Bun.file(captureFile).text();
800
+ expect(captured).toBe(`Bearer ${FAKE_TOKEN}`);
801
+ } finally {
802
+ await new Promise<void>((resolve) => swarmServer.close(() => resolve()));
803
+ }
804
+ });
670
805
  });
@@ -157,6 +157,7 @@ describe("Phase 3 — /api/poll budget admission gate", () => {
157
157
  if ("error" in body) throw new Error("unexpected error response");
158
158
  expect(body.trigger?.type).toBe("task_assigned");
159
159
  expect(body.trigger?.requestedBy).toEqual({
160
+ id: requester.id,
160
161
  name: "Requester One",
161
162
  email: "requester@example.com",
162
163
  role: "engineering manager",
@@ -23,7 +23,7 @@ describe("build-pi-skills", () => {
23
23
  .map((d) => d.name);
24
24
 
25
25
  test("build script runs successfully", () => {
26
- expect(buildOutput).toContain("Converted 13 skills");
26
+ expect(buildOutput).toContain("Converted 11 skills");
27
27
  });
28
28
 
29
29
  test("every command has a corresponding pi-skill", () => {
@@ -75,12 +75,10 @@ describe("build-pi-skills", () => {
75
75
  });
76
76
 
77
77
  describe("slash command syntax", () => {
78
- const skillsWithCrossRefs = [
79
- "work-on-task",
80
- "start-worker",
81
- "start-leader",
82
- "review-offered-task",
83
- ];
78
+ // `work-on-task` / `review-offered-task` are seeded skills, not commands —
79
+ // they have no pi-skill directory. The commands that link to them still
80
+ // must render the reference as `/skill:work-on-task`.
81
+ const skillsWithCrossRefs = ["start-worker", "start-leader"];
84
82
 
85
83
  for (const skill of skillsWithCrossRefs) {
86
84
  test(`${skill} uses /skill: prefix for cross-references`, () => {