@desplega.ai/agent-swarm 1.164.1 → 1.166.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.
- package/README.md +1 -1
- package/dist/acp-adapter-yzfxn8vj.js +23 -0
- package/dist/{actions-hp2h32dx.js → actions-qz3m692w.js} +24 -28
- package/dist/{amp-adapter-zfd7djyx.js → amp-adapter-j1xbmsqz.js} +7 -7
- package/dist/{app-w2d26h4f.js → app-43vrdmv8.js} +9 -10
- package/dist/{assistant-45pc4en7.js → assistant-ertffmka.js} +25 -29
- package/dist/{boot-reembed-k1q2s4fc.js → boot-reembed-998jtpdg.js} +12 -15
- package/dist/{boot-reembed-6rhcpsjf.js → boot-reembed-s7k7c6v2.js} +13 -15
- package/dist/boot-scrub-sweep-nepsk4t1.js +211 -0
- package/dist/{boot-seeds-ex93edx7.js → boot-seeds-vvzs1zhk.js} +12 -12
- package/dist/{claude-adapter-hcxd0szm.js → claude-adapter-2zvmekrp.js} +15 -17
- package/dist/{claude-managed-adapter-fz7xps2r.js → claude-managed-adapter-s49wbk8t.js} +5 -5
- package/dist/{claude-managed-setup-xx6f15h9.js → claude-managed-setup-cvhqq8qn.js} +4 -4
- package/dist/{claude-sdk-session-6nb9raem.js → claude-sdk-session-kkbxtw5q.js} +2624 -2606
- package/dist/{cli-etbrt8x1.js → cli-0nhdwxd5.js} +4 -4
- package/dist/{cli-jeefgppq.js → cli-0qjcsxgz.js} +7 -7
- package/dist/{cli-6behs2bt.js → cli-32bca7nr.js} +1 -1
- package/dist/{cli-gt8v96qx.js → cli-3ddybbbx.js} +4 -4
- package/dist/{acp-adapter-rxvek4ym.js → cli-3qn4r334.js} +317 -35
- package/dist/{cli-1d3w4xm3.js → cli-4r1sjfc4.js} +14 -6
- package/dist/{cli-691f2rxm.js → cli-66zh26sp.js} +68 -16
- package/dist/{cli-et8wdyqh.js → cli-68r1j4sg.js} +9 -9
- package/dist/{cli-xb7kyanp.js → cli-8kgvmkak.js} +3 -3
- package/dist/{cli-fdpkw8vk.js → cli-8t229cmr.js} +8 -8
- package/dist/{cli-gc54qc0p.js → cli-aqcey741.js} +1 -1
- package/dist/{cli-4q4cmr4t.js → cli-cpfmqz3n.js} +8 -8
- package/dist/{cli-a9bw0xxt.js → cli-df56tqdw.js} +997 -408
- package/dist/{cli-n7h1kqpf.js → cli-dmtrxqhg.js} +6 -1
- package/dist/{cli-xfb7333f.js → cli-dw2q9f7g.js} +5 -5
- package/dist/{cli-4drstbb9.js → cli-ecknp7p1.js} +81 -23
- package/dist/{cli-v0pvvhfq.js → cli-efnd2kce.js} +36 -1
- package/dist/{cli-1wetf34q.js → cli-f2n17zhp.js} +1 -1
- package/dist/{cli-2c8eky5y.js → cli-feh3dkah.js} +1 -1
- package/dist/cli-fn697bnt.js +126 -0
- package/dist/{cli-v5pr2fgt.js → cli-gt8azyet.js} +3 -3
- package/dist/{cli-dbszf3rw.js → cli-h3x613ze.js} +4 -4
- package/dist/{cli-nwhb8y2e.js → cli-hf5s9pgf.js} +1 -1
- package/dist/cli-j2jbr8af.js +516 -0
- package/dist/{cli-agjzafns.js → cli-jhm6hzjn.js} +593 -10
- package/dist/{cli-j0tmc908.js → cli-jvj6a0cv.js} +484 -477
- package/dist/{cli-xxz89c2n.js → cli-mb75wpxf.js} +1 -1
- package/dist/{cli-07s39g1n.js → cli-mv2520nm.js} +1 -1
- package/dist/{cli-v90vfwyb.js → cli-mwegs4af.js} +5 -5
- package/dist/{cli-bz1gbac9.js → cli-p4ztb80h.js} +2 -2
- package/dist/{cli-1wd1n2ya.js → cli-rfphkzh5.js} +1 -1
- package/dist/{cli-976r1zqx.js → cli-rggdq8wq.js} +5 -5
- package/dist/{cli-3ajw5v5v.js → cli-rsy933yz.js} +6 -6
- package/dist/{cli-yj790e19.js → cli-tadfgd0t.js} +8 -1
- package/dist/{cli-cn53e7vc.js → cli-ttbsse3f.js} +6 -1
- package/dist/{cli-dyqsrqcf.js → cli-v2a8x45d.js} +4 -4
- package/dist/{cli-wbvnn6tk.js → cli-v6vv7dy8.js} +91 -9
- package/dist/{cli-z2ap2c0c.js → cli-vzc73wet.js} +90 -55
- package/dist/cli-wt7ccy4r.js +2654 -0
- package/dist/{cli-4cn19jvn.js → cli-x7kpr72a.js} +3 -3
- package/dist/{cli-003785v9.js → cli-xvc3q9br.js} +1 -1
- package/dist/{cli-v0e7jpr5.js → cli-xvdgasgy.js} +13 -4
- package/dist/{cli-asqeg4ve.js → cli-y59h2pk9.js} +2 -2
- package/dist/{cli-0wn3ypqv.js → cli-yshaazs1.js} +14121 -3478
- package/dist/{cli-wg8yjqtw.js → cli-ysnyhws9.js} +16 -3
- package/dist/cli-zp67qg5x.js +92 -0
- package/dist/{cli-g3qgrzck.js → cli-zsh093hk.js} +71 -3
- package/dist/{cli-ma3zvh39.js → cli-zy7vwsne.js} +6066 -5829
- package/dist/cli.js +79 -26
- package/dist/{codex-adapter-pcxdn10v.js → codex-adapter-dw1wxqt8.js} +10 -10
- package/dist/{codex-hook-b1fb7c2k.js → codex-hook-d2rkvf39.js} +1 -1
- package/dist/{codex-login-059nm145.js → codex-login-terbdsqq.js} +2 -2
- package/dist/{codex-session-runner-xzwwddp8.js → codex-session-runner-3cgb8y1g.js} +10 -10
- package/dist/{commands-98at8mkn.js → commands-mhxam6z2.js} +9 -9
- package/dist/{cursor-adapter-dkg6qnkp.js → cursor-adapter-2qda3st7.js} +6 -6
- package/dist/{db-01g8wtvw.js → db-sjks418k.js} +11 -9
- package/dist/{devin-adapter-qmttt2fz.js → devin-adapter-bffjj5n4.js} +2 -2
- package/dist/{dispatcher-nn2k1wxd.js → dispatcher-1es759rn.js} +22 -26
- package/dist/{dsh-adapter-w4d4vwdq.js → dsh-adapter-bek4cyw9.js} +6 -6
- package/dist/{e2b-9e4g2hvt.js → e2b-nd50eccg.js} +1 -1
- package/dist/{event-bus-z7v2t26e.js → event-bus-vnfthafg.js} +2 -2
- package/dist/grok-adapter-ar9cw9jg.js +29 -0
- package/dist/{handlers-b0kqsbg8.js → handlers-1gkbhktr.js} +25 -29
- package/dist/{hook-sap7v1aj.js → hook-ds2zxq9v.js} +16 -18
- package/dist/{hook-pz50gqep.js → hook-vxa3tcsy.js} +10 -12
- package/dist/{http-ncr8rmd0.js → http-3h21z9md.js} +679 -227
- package/dist/{index-c4aqcyz1.js → index-9b65j1m5.js} +11 -11
- package/dist/{index-r46s01nq.js → index-bn6cy8ps.js} +22 -26
- package/dist/{index-38kqdt3e.js → index-dkfxpkmm.js} +23 -27
- package/dist/{index-630kj5vd.js → index-f8xnqrye.js} +55 -43
- package/dist/{index-fdk1t4d6.js → index-n0rxa4ca.js} +11962 -9900
- package/dist/{index-9nrj7797.js → index-n99fycq7.js} +22 -26
- package/dist/{keepalive-31613r2h.js → keepalive-3xspa9jw.js} +14 -11
- package/dist/{lead-mx59tr5s.js → lead-6qphc124.js} +43 -43
- package/dist/{maintenance-55ff39ca.js → maintenance-0teem1bb.js} +10 -12
- package/dist/{node-f0ykebbv.js → node-9sg7ajby.js} +25 -7
- package/dist/{oauth-refresh-sweep-anma954w.js → oauth-refresh-sweep-sh9q25xp.js} +13 -10
- package/dist/{onboard-b2fy8ftb.js → onboard-54qmnqxe.js} +4 -3
- package/dist/{opencode-adapter-p1s92tmy.js → opencode-adapter-wjfa2wvd.js} +6 -6
- package/dist/{otel-impl-9brvt7xv.js → otel-impl-ycqzp2a1.js} +2 -2
- package/dist/{pi-mono-adapter-kz4rrwvd.js → pi-mono-adapter-e77e9zst.js} +6 -6
- package/dist/{pricing-refresh-7w8mbmva.js → pricing-refresh-atnkngqs.js} +10 -10
- package/dist/{rbac-roles-ejp7qwvb.js → rbac-roles-9pjphh9q.js} +10 -10
- package/dist/{rbac-roles-nhk4r7bc.js → rbac-roles-j78wdksm.js} +10 -10
- package/dist/{render-v2-2gzt8skq.js → render-v2-pxjs49fx.js} +9 -10
- package/dist/{setup-5et8t412.js → setup-ymej0xtx.js} +2 -2
- package/dist/{work-objects-tb0er7qa.js → work-objects-3nh4ymzb.js} +26 -30
- package/dist/{worker-62cv075d.js → worker-qa9fkz5s.js} +43 -43
- package/dist/{x-vbxb31jr.js → x-ma4gnfgy.js} +2 -2
- package/openapi.json +457 -31
- package/package.json +14 -6
- package/plugin/commands/create-pr.md +7 -1
- package/plugin/pi-skills/create-pr/SKILL.md +7 -1
- package/src/agentmail/app.ts +5 -4
- package/src/be/automation-preflight.ts +11 -9
- package/src/be/boot-scrub-sweep.ts +302 -0
- package/src/be/connector-codes.ts +81 -0
- package/src/be/crypto/key-bootstrap.ts +1 -1
- package/src/be/db/approvals.ts +15 -1
- package/src/be/db/codex-oauth-device-flows.ts +40 -0
- package/src/be/db/config-secrets.ts +31 -0
- package/src/be/db/runtime.ts +4 -0
- package/src/be/db/script-runs.ts +15 -0
- package/src/be/db/tasks/write.ts +1 -1
- package/src/be/db-queries/mcp-oauth.ts +5 -0
- package/src/be/db-queries/oauth.ts +77 -0
- package/src/be/db.ts +162 -59
- package/src/be/events.ts +17 -1
- package/src/be/memory/index-content.ts +2 -2
- package/src/be/memory/providers/sqlite-store.ts +63 -11
- package/src/be/memory/types.ts +14 -0
- package/src/be/migrations/197_connector_codes.sql +23 -0
- package/src/be/migrations/198_permission_audit_guest.sql +33 -0
- package/src/be/migrations/200_backfill_auto_memory_expiry.sql +20 -0
- package/src/be/migrations/201_workflow_replay_columns.sql +10 -0
- package/src/be/migrations/202_codex_oauth_device_flows.sql +24 -0
- package/src/be/model-tier-resolution.ts +2 -1
- package/src/be/model-validation.ts +1 -1
- package/src/be/pricing-normalize.ts +3 -0
- package/src/be/rbac-audit.ts +2 -1
- package/src/be/scripts/db.ts +28 -4
- package/src/be/scripts/source-secrets.ts +37 -0
- package/src/be/scripts/typecheck.ts +23 -2
- package/src/be/scrub-json.ts +32 -0
- package/src/be/sealed-json.ts +59 -0
- package/src/be/secret-registry.ts +131 -0
- package/src/be/seed-pricing.ts +14 -0
- package/src/be/users.ts +7 -1
- package/src/be/workflow-replay.ts +77 -0
- package/src/cli.tsx +7 -0
- package/src/commands/provider-credentials.ts +46 -8
- package/src/commands/runner.ts +2 -10
- package/src/extensions/contract-types.generated.ts +1 -1
- package/src/github/app.ts +4 -4
- package/src/heartbeat/heartbeat.ts +20 -8
- package/src/hooks/pr-body-guard.ts +1 -1
- package/src/http/agents.ts +1 -0
- package/src/http/approval-requests.ts +1 -0
- package/src/http/apps.ts +3 -0
- package/src/http/auth.ts +4 -1
- package/src/http/codex-oauth-device.ts +8 -10
- package/src/http/codex-oauth-keep-warm.ts +2 -2
- package/src/http/config-values.ts +59 -0
- package/src/http/config.ts +5 -0
- package/src/http/db-query-bounded.ts +3 -0
- package/src/http/db-query-shared.ts +107 -15
- package/src/http/db-query.ts +10 -2
- package/src/http/extensions.ts +1 -0
- package/src/http/index.ts +23 -6
- package/src/http/integrations.ts +1 -38
- package/src/http/ip-rate-limit.ts +82 -0
- package/src/http/kv.ts +38 -4
- package/src/http/mcp-servers.ts +1 -0
- package/src/http/memory.ts +9 -0
- package/src/http/models-catalog.ts +2 -0
- package/src/http/operator-actor.ts +4 -0
- package/src/http/page-proxy.ts +22 -0
- package/src/http/pages.ts +4 -2
- package/src/http/realtime.ts +1 -1
- package/src/http/request-principal.ts +14 -1
- package/src/http/script-connections.ts +8 -0
- package/src/http/script-runs.ts +12 -4
- package/src/http/scripts.ts +15 -0
- package/src/http/session-cost-recompute.ts +3 -2
- package/src/http/tasks.ts +9 -0
- package/src/http/users.ts +226 -0
- package/src/http/workflows.ts +3 -2
- package/src/http.ts +1 -0
- package/src/integrations/kapso/client.ts +3 -1
- package/src/jira/app.ts +2 -2
- package/src/kv-view.ts +324 -0
- package/src/linear/app.ts +2 -2
- package/src/oauth/mcp-wrapper.ts +1 -1
- package/src/prompts/memories.ts +42 -1
- package/src/providers/acp-adapter.ts +85 -24
- package/src/providers/acp-target-catalog.ts +54 -1
- package/src/providers/acp-targets.ts +358 -4
- package/src/providers/amp-adapter.ts +1 -1
- package/src/providers/cursor-adapter.ts +1 -1
- package/src/providers/grok-adapter.ts +186 -0
- package/src/providers/index.ts +5 -1
- package/src/providers/pricing-sources.md +6 -0
- package/src/providers/reasoning-effort.ts +17 -1
- package/src/providers/types.ts +2 -1
- package/src/rbac/can.ts +4 -3
- package/src/rbac/legacy-policy.ts +3 -3
- package/src/rbac/types.ts +6 -0
- package/src/script-workflows/supervisor.ts +11 -2
- package/src/scripts-runtime/eval-harness.ts +2 -12
- package/src/scripts-runtime/executors/quickjs-bundle.ts +152 -0
- package/src/scripts-runtime/executors/quickjs-prelude.ts +285 -0
- package/src/scripts-runtime/executors/quickjs-runner.ts +580 -0
- package/src/scripts-runtime/executors/quickjs-worker.ts +81 -0
- package/src/scripts-runtime/executors/quickjs.ts +196 -0
- package/src/scripts-runtime/executors/registry.ts +2 -0
- package/src/scripts-runtime/executors/sourcemap.ts +79 -0
- package/src/scripts-runtime/script-error-hints.ts +14 -0
- package/src/scripts-runtime/swarm-sdk.ts +9 -2
- package/src/scripts-runtime/types/stdlib.d.ts +27 -8
- package/src/scripts-runtime/types/swarm-extension.d.ts +3 -0
- package/src/scripts-runtime/types/swarm-sdk.d.ts +27 -8
- package/src/server-user.ts +2 -20
- package/src/server.ts +3 -1
- package/src/slack/app.ts +4 -0
- package/src/slack/config.ts +3 -1
- package/src/slack/egress-scrub.ts +87 -0
- package/src/tests/acp-adapter.test.ts +191 -2
- package/src/tests/boot-scrub-sweep.test.ts +390 -0
- package/src/tests/budget-user-scope.test.ts +7 -21
- package/src/tests/check-pr-body.test.ts +149 -6
- package/src/tests/check-text-columns.test.ts +145 -0
- package/src/tests/claude-adapter-binary.test.ts +1 -1
- package/src/tests/claude-sdk-process.fixture.ts +13 -2
- package/src/tests/codex-oauth-device.test.ts +23 -37
- package/src/tests/codex-oauth-keep-warm.test.ts +29 -0
- package/src/tests/console-scrub.test.ts +141 -0
- package/src/tests/credential-check.test.ts +90 -0
- package/src/tests/db-query-denylist.test.ts +202 -0
- package/src/tests/db-task-read-contract.test.ts +1 -0
- package/src/tests/db-task-write-contract.test.ts +1 -0
- package/src/tests/docs-index-footprint.test.js +3 -3
- package/src/tests/entrypoint-acp-target-binary.test.ts +44 -1
- package/src/tests/events-scrub.test.ts +88 -0
- package/src/tests/explicit-model-validation.test.ts +81 -0
- package/src/tests/extensions-pre-task.test.ts +33 -1
- package/src/tests/fixtures/grok/acp-updates-recorded.jsonl +53 -0
- package/src/tests/fixtures/grok/prompt-response-meta.json +36 -0
- package/src/tests/fixtures/secret-corpus.ts +320 -0
- package/src/tests/grok-adapter.test.ts +647 -0
- package/src/tests/heartbeat-checklist.test.ts +44 -0
- package/src/tests/integration-disable-flags.test.ts +156 -0
- package/src/tests/kv-http.test.ts +34 -0
- package/src/tests/kv-tool.test.ts +274 -1
- package/src/tests/kv-view.test.ts +147 -0
- package/src/tests/mcp-user-route.test.ts +257 -15
- package/src/tests/memory-injection-outcome.test.ts +143 -0
- package/src/tests/model-tier-resolution.test.ts +76 -3
- package/src/tests/page-guest-session.test.ts +424 -0
- package/src/tests/page-proxy.test.ts +1 -1
- package/src/tests/page-viewer-auth.test.ts +22 -1
- package/src/tests/pr-body-leaks.test.ts +157 -2
- package/src/tests/preload.ts +10 -0
- package/src/tests/provider-registration.test.ts +3 -1
- package/src/tests/rbac-audit.test.ts +32 -0
- package/src/tests/rbac-mcp-admission-e2e.test.ts +4 -62
- package/src/tests/script-connections.test.ts +9 -0
- package/src/tests/script-executor-conformance.test.ts +3 -0
- package/src/tests/script-executor-quickjs.test.ts +442 -0
- package/src/tests/script-executor-registry.test.ts +6 -1
- package/src/tests/script-runs-http.test.ts +126 -1
- package/src/tests/script-runs-scrub.test.ts +275 -0
- package/src/tests/scripts-http.test.ts +82 -1
- package/src/tests/scripts-mcp-e2e.test.ts +42 -6
- package/src/tests/scripts-runtime.test.ts +37 -0
- package/src/tests/scrubbed-text-12e.test.ts +289 -0
- package/src/tests/sealed-key-guard.test.ts +166 -0
- package/src/tests/secret-canary-sweep.test.ts +958 -0
- package/src/tests/secret-registry.test.ts +355 -0
- package/src/tests/secret-rules.test.ts +230 -0
- package/src/tests/secret-scrubber.test.ts +317 -1
- package/src/tests/session-costs-golden.test.ts +17 -0
- package/src/tests/slack-egress-scrub.test.ts +216 -0
- package/src/tests/swarm-tool-result-gate.test.ts +143 -30
- package/src/tests/task-tools-ctx.test.ts +3 -0
- package/src/tests/user-token-routes.test.ts +342 -3
- package/src/tests/workflow-replay.test.ts +518 -0
- package/src/tests/workflow-scrub.test.ts +242 -0
- package/src/tests/workflow-system-one-decision-providers.test.ts +737 -0
- package/src/tools/db-query.ts +1 -1
- package/src/tools/inject-learning.ts +1 -1
- package/src/tools/kv/kv-get.ts +136 -6
- package/src/tools/send-task.ts +61 -17
- package/src/tools/utils.ts +186 -43
- package/src/tools/workflows/get-workflow-run.ts +3 -2
- package/src/types.ts +16 -1
- package/src/utils/console-scrub-install.ts +5 -0
- package/src/utils/console-scrub.ts +57 -0
- package/src/utils/credentials.ts +8 -0
- package/src/utils/page-session.ts +6 -2
- package/src/utils/pr-body-leaks.ts +149 -10
- package/src/utils/request-auth-context.ts +2 -0
- package/src/utils/secret-rules.generated.ts +2055 -0
- package/src/utils/secret-scrubber.ts +662 -36
- package/src/workflows/engine.ts +39 -4
- package/src/workflows/executors/notify.ts +6 -1
- package/src/workflows/executors/system-one-decision.ts +31 -12
- package/src/workflows/executors/system-one-openai-wire.ts +168 -0
- package/src/workflows/executors/system-one-providers.ts +112 -11
- package/src/workflows/executors/system-one-wire.ts +79 -0
- package/src/workflows/recovery.ts +10 -1
- package/src/workflows/resume.ts +62 -3
- package/src/workflows/retry-poller.ts +3 -0
- package/templates/ai-toolbox.manifest.json +2 -2
- package/templates/skills/agent-browser/SKILL.md +5 -5
- package/templates/skills/agent-browser/config.json +1 -1
- package/templates/skills/agent-browser/content.md +4 -4
- package/templates/skills/artifacts/content.md +17 -7
- package/templates/skills/jira-interaction/config.json +1 -1
- package/templates/skills/jira-interaction/content.md +56 -205
- package/templates/skills/linear-interaction/config.json +1 -1
- package/templates/skills/linear-interaction/content.md +31 -50
- package/templates/skills/pages/content.md +8 -4
- package/templates/skills/qa/content.md +2 -2
- package/templates/skills/workflow-iterate/content.md +4 -4
- package/tsconfig.json +3 -3
- package/dist/boot-scrub-logs-t8xg7wd7.js +0 -99
- package/dist/cli-0a7r5331.js +0 -168
- package/dist/cli-93rxw9yd.js +0 -951
- package/dist/cli-cvk6yzw5.js +0 -40
- package/dist/cli-dgnj0zcc.js +0 -40
- package/dist/cli-qt5wzq6v.js +0 -256
- package/dist/cli-v7fqehqd.js +0 -9281
- package/dist/cli-vbr859rz.js +0 -572
- package/src/be/boot-scrub-logs.ts +0 -132
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
type SwarmToolResult,
|
|
15
15
|
type SwarmToolTruncation,
|
|
16
16
|
WORKFLOW_LONG_SCRIPT_TIMEOUT_NUDGE,
|
|
17
|
+
wireChannelBytes,
|
|
17
18
|
} from "../tools/utils";
|
|
18
19
|
import { clearVolatileSecretsForTesting, registerVolatileSecret } from "../utils/secret-scrubber";
|
|
19
20
|
|
|
@@ -21,6 +22,26 @@ const TEST_DB_PATH = "./test-swarm-tool-result-gate.sqlite";
|
|
|
21
22
|
const TEST_AGENT_ID = "tool-result-test-agent";
|
|
22
23
|
const TEST_OVERFLOW_NAMESPACE = mcpOverflowNamespace(TEST_AGENT_ID);
|
|
23
24
|
|
|
25
|
+
function channelSizes(result: { content?: unknown; structuredContent?: unknown }) {
|
|
26
|
+
const text = ((result.content as Array<{ text?: string }> | undefined) ?? [])
|
|
27
|
+
.map((block) => block.text ?? "")
|
|
28
|
+
.join("");
|
|
29
|
+
return {
|
|
30
|
+
text: Buffer.byteLength(text, "utf8"),
|
|
31
|
+
structured: Buffer.byteLength(JSON.stringify(result.structuredContent ?? {}), "utf8"),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function expectEachChannelWithinLimit(result: { content?: unknown; structuredContent?: unknown }) {
|
|
36
|
+
const sizes = channelSizes(result);
|
|
37
|
+
expect(sizes.text).toBeLessThanOrEqual(MCP_RESULT_WIRE_LIMIT_BYTES);
|
|
38
|
+
expect(sizes.structured).toBeLessThanOrEqual(MCP_RESULT_WIRE_LIMIT_BYTES);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function occurrences(haystack: string, needle: string): number {
|
|
42
|
+
return haystack.split(needle).length - 1;
|
|
43
|
+
}
|
|
44
|
+
|
|
24
45
|
// ── Part 1: finalize pipeline contract ────────────────────────────────────────
|
|
25
46
|
// Both channels must be independently self-sufficient and semantically
|
|
26
47
|
// identical (see runbooks/mcp-tool-results.md). These tests freeze the
|
|
@@ -388,11 +409,12 @@ describe("finalizeSwarmToolResult", () => {
|
|
|
388
409
|
};
|
|
389
410
|
};
|
|
390
411
|
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
expect(text).
|
|
412
|
+
// The head of the JSON rendering fills the budget as labelled prose; the
|
|
413
|
+
// structured `data` is never cut (a cut value would read as real).
|
|
414
|
+
expect(text).toContain(`JSON payload head:\n{\n "blob": "${blob.slice(0, 100)}`);
|
|
415
|
+
expect(text).not.toContain('"truncated":true');
|
|
394
416
|
expect(structured).not.toHaveProperty("blob");
|
|
395
|
-
expect(structured.details).toContain("JSON payload
|
|
417
|
+
expect(structured.details).toContain("JSON payload head:");
|
|
396
418
|
const fullValueAt = structured.truncation!.fullValueAt;
|
|
397
419
|
const retrieval = structured.truncation!.retrieval;
|
|
398
420
|
expect(fullValueAt).toMatch(/^kv:\/\/mcp:overflow:tool-result-test-agent\/v1\/some-tool\//);
|
|
@@ -404,19 +426,16 @@ describe("finalizeSwarmToolResult", () => {
|
|
|
404
426
|
});
|
|
405
427
|
const key = fullValueAt.replace(`kv://${TEST_OVERFLOW_NAMESPACE}/`, "");
|
|
406
428
|
expect(retrieval).toContain(
|
|
407
|
-
`kv-get({"namespace":"${TEST_OVERFLOW_NAMESPACE}","key":"${key}"})`,
|
|
408
|
-
);
|
|
409
|
-
expect(text).toContain(fullValueAt);
|
|
410
|
-
expect(text).toContain(`"key":"${key}"`);
|
|
411
|
-
expect(structured.details).toContain(fullValueAt);
|
|
412
|
-
expect(structured.details).toContain(`"key":"${key}"`);
|
|
413
|
-
expect(Buffer.byteLength(JSON.stringify(result), "utf8")).toBeLessThanOrEqual(
|
|
414
|
-
MCP_RESULT_WIRE_LIMIT_BYTES,
|
|
415
|
-
);
|
|
416
|
-
expect(Buffer.byteLength(text, "utf8")).toBeLessThanOrEqual(MCP_RESULT_WIRE_LIMIT_BYTES);
|
|
417
|
-
expect(Buffer.byteLength(JSON.stringify(result.structuredContent), "utf8")).toBeLessThanOrEqual(
|
|
418
|
-
MCP_RESULT_WIRE_LIMIT_BYTES,
|
|
429
|
+
`kv-get({"namespace":"${TEST_OVERFLOW_NAMESPACE}","key":"${key}","path":"outcome.data.blob","offset":0})`,
|
|
419
430
|
);
|
|
431
|
+
// One pointer per channel: a single kv:// line plus the retrieval call in
|
|
432
|
+
// text; only the truncation object in structuredContent.
|
|
433
|
+
expect(occurrences(text, fullValueAt)).toBe(1);
|
|
434
|
+
expect(occurrences(text, `"key":"${key}"`)).toBe(1);
|
|
435
|
+
expect(structured.details).not.toContain(fullValueAt);
|
|
436
|
+
expect(structured.details).not.toContain("Retrieval:");
|
|
437
|
+
expect(occurrences(JSON.stringify(result.structuredContent), fullValueAt)).toBe(1);
|
|
438
|
+
expectEachChannelWithinLimit(result);
|
|
420
439
|
|
|
421
440
|
const stored = await getKv(TEST_OVERFLOW_NAMESPACE, key);
|
|
422
441
|
expect(stored?.valueType).toBe("string");
|
|
@@ -474,9 +493,7 @@ describe("finalizeSwarmToolResult", () => {
|
|
|
474
493
|
truncated: true,
|
|
475
494
|
limitBytes: MCP_RESULT_WIRE_LIMIT_BYTES,
|
|
476
495
|
});
|
|
477
|
-
|
|
478
|
-
MCP_RESULT_WIRE_LIMIT_BYTES,
|
|
479
|
-
);
|
|
496
|
+
expectEachChannelWithinLimit(result);
|
|
480
497
|
|
|
481
498
|
const key = structured.truncation.fullValueAt.replace(`kv://${TEST_OVERFLOW_NAMESPACE}/`, "");
|
|
482
499
|
const stored = await getKv(TEST_OVERFLOW_NAMESPACE, key);
|
|
@@ -509,9 +526,7 @@ describe("finalizeSwarmToolResult", () => {
|
|
|
509
526
|
expect(structured.items).toEqual([{ id: 1 }, { id: 2 }]);
|
|
510
527
|
expect(structured).not.toHaveProperty("blob");
|
|
511
528
|
expect(structured.truncation.truncated).toBe(true);
|
|
512
|
-
|
|
513
|
-
MCP_RESULT_WIRE_LIMIT_BYTES,
|
|
514
|
-
);
|
|
529
|
+
expectEachChannelWithinLimit(result);
|
|
515
530
|
});
|
|
516
531
|
|
|
517
532
|
test("oversized prose keeps a readable prefix + marker + resolvable pointer on both channels", async () => {
|
|
@@ -540,12 +555,11 @@ describe("finalizeSwarmToolResult", () => {
|
|
|
540
555
|
|
|
541
556
|
expect(structured.details).toContain(blob.slice(0, 100));
|
|
542
557
|
expect(structured.details).toContain("[truncated");
|
|
543
|
-
expect(structured.details).toContain(`kv://${TEST_OVERFLOW_NAMESPACE}/`);
|
|
544
|
-
expect(structured.details!.length).toBeLessThan(2_500);
|
|
545
|
-
expect(text).toBe(`Big rendered payload.\n\n${structured.details}`);
|
|
558
|
+
expect(structured.details).not.toContain(`kv://${TEST_OVERFLOW_NAMESPACE}/`);
|
|
546
559
|
expect(structured).not.toHaveProperty("blob");
|
|
547
560
|
const fullValueAt = structured.truncation!.fullValueAt;
|
|
548
561
|
const retrieval = structured.truncation!.retrieval;
|
|
562
|
+
const originalBytes = structured.truncation!.originalBytes;
|
|
549
563
|
expect(structured.truncation).toMatchObject({
|
|
550
564
|
truncated: true,
|
|
551
565
|
fullValueAt: expect.stringMatching(/^kv:\/\/mcp:overflow:tool-result-test-agent\//),
|
|
@@ -553,10 +567,109 @@ describe("finalizeSwarmToolResult", () => {
|
|
|
553
567
|
limitBytes: MCP_RESULT_WIRE_LIMIT_BYTES,
|
|
554
568
|
retrieval: expect.stringContaining("kv-get("),
|
|
555
569
|
});
|
|
556
|
-
expect(text).
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
570
|
+
expect(text).toBe(
|
|
571
|
+
`Big rendered payload.\n\n${structured.details}\n\n` +
|
|
572
|
+
`Full value: ${fullValueAt} (${originalBytes} bytes)\n` +
|
|
573
|
+
`Shape: outcome.details string 50004 chars 48.8KB; outcome.data.blob string 50000 chars 48.8KB\n` +
|
|
574
|
+
`Retrieval: ${retrieval}`,
|
|
575
|
+
);
|
|
576
|
+
expect(occurrences(text, fullValueAt)).toBe(1);
|
|
577
|
+
expectEachChannelWithinLimit(result);
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
test("the prose preview fills the remaining budget instead of a fixed 1,200 chars", async () => {
|
|
581
|
+
const prose = Array.from({ length: 400 }, (_, i) => `line ${i}: ${"p".repeat(60)}`).join("\n");
|
|
582
|
+
const result = await finalizeSwarmToolResult(
|
|
583
|
+
"some-tool",
|
|
584
|
+
{ ok: true, message: "Long prose.", details: prose },
|
|
585
|
+
{ agentId: TEST_AGENT_ID },
|
|
586
|
+
);
|
|
587
|
+
const structured = result.structuredContent as { details: string };
|
|
588
|
+
const preview = structured.details.slice(0, structured.details.indexOf("\n… [truncated"));
|
|
589
|
+
expect(prose.startsWith(preview)).toBe(true);
|
|
590
|
+
// Fills to within one line of the budget: far past the old 1,200-char cut.
|
|
591
|
+
expect(preview.length).toBeGreaterThan(8_000);
|
|
592
|
+
const sizes = channelSizes(result);
|
|
593
|
+
expect(Math.max(sizes.text, sizes.structured)).toBeGreaterThan(
|
|
594
|
+
MCP_RESULT_WIRE_LIMIT_BYTES - 200,
|
|
595
|
+
);
|
|
596
|
+
expectEachChannelWithinLimit(result);
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
test("the cap is max(text, structured), not their sum", async () => {
|
|
600
|
+
// ~6.5KB of payload: each channel holds one copy (~6.6KB), the old summed
|
|
601
|
+
// measure was ~13KB and spilled it.
|
|
602
|
+
const rows = Array.from({ length: 65 }, (_, i) => ({ id: i, note: "n".repeat(70) }));
|
|
603
|
+
const result = await finalizeSwarmToolResult(
|
|
604
|
+
"some-tool",
|
|
605
|
+
{ ok: true, message: "65 rows.", data: { rows } },
|
|
606
|
+
{ agentId: TEST_AGENT_ID },
|
|
607
|
+
);
|
|
608
|
+
const sizes = channelSizes(result);
|
|
609
|
+
expect(sizes.text + sizes.structured).toBeGreaterThan(MCP_RESULT_WIRE_LIMIT_BYTES);
|
|
610
|
+
expect(Math.max(sizes.text, sizes.structured)).toBeLessThanOrEqual(MCP_RESULT_WIRE_LIMIT_BYTES);
|
|
611
|
+
expect(result.structuredContent).not.toHaveProperty("truncation");
|
|
612
|
+
expect((result.structuredContent as { rows: unknown[] }).rows).toEqual(rows);
|
|
613
|
+
expect(wireChannelBytes(result)).toBe(Math.max(sizes.text, sizes.structured));
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
test("a details rendering of data leaves structuredContent before anything spills", async () => {
|
|
617
|
+
// script-run shape: `data` plus a pretty-printed `details` of the same
|
|
618
|
+
// payload, so structuredContent alone carries it twice (~14KB).
|
|
619
|
+
const result = {
|
|
620
|
+
rows: Array.from({ length: 65 }, (_, i) => ({ id: i, note: "n".repeat(70) })),
|
|
621
|
+
};
|
|
622
|
+
const finalized = await finalizeSwarmToolResult(
|
|
623
|
+
"script-run",
|
|
624
|
+
{
|
|
625
|
+
ok: true,
|
|
626
|
+
message: "Script run completed.",
|
|
627
|
+
details: `result:\n${JSON.stringify(result, null, 2)}`,
|
|
628
|
+
data: { status: 200, data: { result } },
|
|
629
|
+
},
|
|
630
|
+
{ agentId: TEST_AGENT_ID },
|
|
631
|
+
);
|
|
632
|
+
const text = (finalized.content?.[0] as { text: string }).text;
|
|
633
|
+
expect(finalized.structuredContent).not.toHaveProperty("truncation");
|
|
634
|
+
expect(finalized.structuredContent).not.toHaveProperty("details");
|
|
635
|
+
expect((finalized.structuredContent as { data: unknown }).data).toEqual({ result });
|
|
636
|
+
expect(text).toContain(`result:\n${JSON.stringify(result, null, 2)}`);
|
|
637
|
+
expectEachChannelWithinLimit(finalized);
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
test("a spill carries a bounded shape summary on both channels, inside the cap", async () => {
|
|
641
|
+
const rows = Array.from({ length: 400 }, (_, id) => ({ id, note: "r".repeat(100) }));
|
|
642
|
+
const result = await finalizeSwarmToolResult(
|
|
643
|
+
"script-run",
|
|
644
|
+
{
|
|
645
|
+
ok: true,
|
|
646
|
+
message: "Script run completed.",
|
|
647
|
+
details: `result:\n${JSON.stringify({ rows }, null, 2)}`,
|
|
648
|
+
data: { status: 200, data: { result: { rows } } },
|
|
649
|
+
},
|
|
650
|
+
{ agentId: TEST_AGENT_ID },
|
|
651
|
+
);
|
|
652
|
+
const text = (result.content?.[0] as { text: string }).text;
|
|
653
|
+
const truncation = (result.structuredContent as { truncation: SwarmToolTruncation }).truncation;
|
|
654
|
+
|
|
655
|
+
// Sorted by bytes: the details rendering outweighs the rows it renders.
|
|
656
|
+
expect(truncation.shape?.map((entry) => entry.path)).toEqual([
|
|
657
|
+
"outcome.details",
|
|
658
|
+
"outcome.data.data.result.rows",
|
|
659
|
+
]);
|
|
660
|
+
expect(truncation.shape?.[1]).toMatchObject({ type: "array", items: 400 });
|
|
661
|
+
expect(truncation.shape!.length).toBeLessThanOrEqual(8);
|
|
662
|
+
expect(text).toContain("Shape: ");
|
|
663
|
+
expect(text).toContain("outcome.data.data.result.rows array[400]");
|
|
664
|
+
expect(occurrences(text, "Shape: ")).toBe(1);
|
|
665
|
+
expect(truncation.retrieval).toContain('"path":"outcome.data.data.result.rows","offset":0');
|
|
666
|
+
// Structured channel: shape only inside the truncation object.
|
|
667
|
+
expect(JSON.stringify(result.structuredContent)).not.toContain("Shape:");
|
|
668
|
+
// The array path keeps tool prose out of structuredContent (it carries the
|
|
669
|
+
// rows themselves), so each channel fills its own budget with payload.
|
|
670
|
+
expect(result.structuredContent).not.toHaveProperty("details");
|
|
671
|
+
expect(text.indexOf("… [truncated")).toBeGreaterThan(8_000);
|
|
672
|
+
expectEachChannelWithinLimit(result);
|
|
560
673
|
});
|
|
561
674
|
|
|
562
675
|
test("oversized results without an agent identity never spill to the flat namespace", async () => {
|
|
@@ -617,7 +730,7 @@ describe("finalizeSwarmToolResult", () => {
|
|
|
617
730
|
}
|
|
618
731
|
).truncation;
|
|
619
732
|
expect(truncation.retrieval).toMatch(
|
|
620
|
-
/^kv-get\(\{"namespace":"mcp:overflow:tool-result-test-agent","key":"[^"]+"\}\) returns
|
|
733
|
+
/^kv-get\(\{"namespace":"mcp:overflow:tool-result-test-agent","key":"[^"]+","path":"outcome.details","offset":0\}\) returns a bounded page/,
|
|
621
734
|
);
|
|
622
735
|
expect(truncation.retrieval).toContain("ctx.swarm.kv_get");
|
|
623
736
|
const key = truncation.fullValueAt.replace(`kv://${TEST_OVERFLOW_NAMESPACE}/`, "");
|
|
@@ -37,6 +37,7 @@ afterAll(async () => {
|
|
|
37
37
|
describe("task tool ctx", () => {
|
|
38
38
|
test("sendTaskHandler with user ctx writes requestedByUserId", async () => {
|
|
39
39
|
const user = await createUser({ name: "MCP User" });
|
|
40
|
+
const lead = await createAgent({ name: "MCP User Lead", isLead: true, status: "idle" });
|
|
40
41
|
|
|
41
42
|
const result = await sendTaskHandler(userCtx(user), {
|
|
42
43
|
task: "user requested task",
|
|
@@ -53,6 +54,8 @@ describe("task tool ctx", () => {
|
|
|
53
54
|
expect(data.task.requestedByUserId).toBe(user.id);
|
|
54
55
|
|
|
55
56
|
const stored = await getTaskById(data.task.id);
|
|
57
|
+
expect(stored?.status).toBe("pending");
|
|
58
|
+
expect(stored?.agentId).toBe(lead.id);
|
|
56
59
|
expect(stored?.creatorAgentId).toBeUndefined();
|
|
57
60
|
expect(stored?.requestedByUserId).toBe(user.id);
|
|
58
61
|
});
|
|
@@ -6,10 +6,12 @@ import {
|
|
|
6
6
|
type Server,
|
|
7
7
|
type ServerResponse,
|
|
8
8
|
} from "node:http";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
9
|
+
import { deleteExpiredConnectorCodes, exchangeConnectorCode } from "../be/connector-codes";
|
|
10
|
+
import { closeDb, createUser, getDbClient, initDb, upsertSwarmConfig } from "../be/db";
|
|
11
|
+
import { fingerprintApiKey, resolveUserByToken } from "../be/users";
|
|
11
12
|
import { handleCore } from "../http/core";
|
|
12
|
-
import {
|
|
13
|
+
import { clientIp, createIpRateLimiter } from "../http/ip-rate-limit";
|
|
14
|
+
import { _resetConnectorExchangeRateLimitForTests, handleUsers } from "../http/users";
|
|
13
15
|
import { getPathSegments, parseQueryParams } from "../http/utils";
|
|
14
16
|
import { listenOnFreePort } from "./test-net";
|
|
15
17
|
|
|
@@ -66,6 +68,9 @@ afterAll(async () => {
|
|
|
66
68
|
|
|
67
69
|
beforeEach(async () => {
|
|
68
70
|
const client = getDbClient();
|
|
71
|
+
_resetConnectorExchangeRateLimitForTests();
|
|
72
|
+
await client.run("DELETE FROM connector_codes");
|
|
73
|
+
await client.run("DELETE FROM swarm_config WHERE key IN ('PUBLIC_MCP_BASE_URL', 'APP_URL')");
|
|
69
74
|
await client.run("DELETE FROM user_identity_events");
|
|
70
75
|
await client.run("DELETE FROM user_tokens");
|
|
71
76
|
await client.run("DELETE FROM users");
|
|
@@ -204,3 +209,337 @@ describe("operator MCP token routes", () => {
|
|
|
204
209
|
expect(row?.actor).toBe(`operator:${fingerprintApiKey(API_KEY)}`);
|
|
205
210
|
});
|
|
206
211
|
});
|
|
212
|
+
|
|
213
|
+
describe("ChatGPT connector codes", () => {
|
|
214
|
+
const ORIGINAL_PUBLIC_URL = process.env.PUBLIC_MCP_BASE_URL;
|
|
215
|
+
const ORIGINAL_CONNECT_URL = process.env.CONNECTOR_CONNECT_URL;
|
|
216
|
+
|
|
217
|
+
beforeEach(() => {
|
|
218
|
+
process.env.PUBLIC_MCP_BASE_URL = "https://swarm.example.com";
|
|
219
|
+
delete process.env.CONNECTOR_CONNECT_URL;
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
afterAll(() => {
|
|
223
|
+
if (ORIGINAL_PUBLIC_URL === undefined) delete process.env.PUBLIC_MCP_BASE_URL;
|
|
224
|
+
else process.env.PUBLIC_MCP_BASE_URL = ORIGINAL_PUBLIC_URL;
|
|
225
|
+
if (ORIGINAL_CONNECT_URL === undefined) delete process.env.CONNECTOR_CONNECT_URL;
|
|
226
|
+
else process.env.CONNECTOR_CONNECT_URL = ORIGINAL_CONNECT_URL;
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
type CodeBody = { code: string; expiresAt: string; connectUrl: string };
|
|
230
|
+
|
|
231
|
+
async function createCode(userId: string, body: object = {}): Promise<CodeBody> {
|
|
232
|
+
const response = await authedFetch(`/api/users/${userId}/connector-codes`, {
|
|
233
|
+
method: "POST",
|
|
234
|
+
body: JSON.stringify(body),
|
|
235
|
+
});
|
|
236
|
+
expect(response.status).toBe(201);
|
|
237
|
+
return (await response.json()) as CodeBody;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function exchange(code: string, headers: Record<string, string> = {}): Promise<Response> {
|
|
241
|
+
return fetch(url("/api/connector/exchange"), {
|
|
242
|
+
method: "POST",
|
|
243
|
+
headers: { "Content-Type": "application/json", ...headers },
|
|
244
|
+
body: JSON.stringify({ code }),
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
test("POST creates a single-use code, stores only its hash, and mints no token", async () => {
|
|
249
|
+
const user = await createUser({ name: "Connector User" });
|
|
250
|
+
const before = Date.now();
|
|
251
|
+
const body = await createCode(user.id);
|
|
252
|
+
|
|
253
|
+
expect(body.code).toMatch(/^[A-Za-z0-9_-]{43}$/);
|
|
254
|
+
const ttl = new Date(body.expiresAt).getTime() - before;
|
|
255
|
+
expect(ttl).toBeGreaterThan(9 * 60 * 1000);
|
|
256
|
+
expect(ttl).toBeLessThanOrEqual(10 * 60 * 1000 + 1000);
|
|
257
|
+
expect(body.connectUrl).toBe(
|
|
258
|
+
`https://mcp.agent-swarm.dev/connections?swarm=${encodeURIComponent("https://swarm.example.com")}&code=${body.code}`,
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
const rows = await getDbClient().query<{
|
|
262
|
+
code_hash: string;
|
|
263
|
+
label: string;
|
|
264
|
+
used_at: string | null;
|
|
265
|
+
}>("SELECT code_hash, label, used_at FROM connector_codes WHERE user_id = ?", [user.id]);
|
|
266
|
+
expect(rows).toHaveLength(1);
|
|
267
|
+
expect(rows[0]!.code_hash).toHaveLength(64);
|
|
268
|
+
expect(rows[0]!.code_hash).not.toContain(body.code);
|
|
269
|
+
expect(rows[0]!.label).toBe("ChatGPT connector");
|
|
270
|
+
expect(rows[0]!.used_at).toBeNull();
|
|
271
|
+
const tokens = await getDbClient().query("SELECT id FROM user_tokens WHERE userId = ?", [
|
|
272
|
+
user.id,
|
|
273
|
+
]);
|
|
274
|
+
expect(tokens).toHaveLength(0);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test("POST honors CONNECTOR_CONNECT_URL (keeping its query) and a custom label", async () => {
|
|
278
|
+
process.env.CONNECTOR_CONNECT_URL = "https://connector.test/connect?ref=swarm";
|
|
279
|
+
const user = await createUser({ name: "Custom Connector User" });
|
|
280
|
+
const body = await createCode(user.id, { label: "my chatgpt" });
|
|
281
|
+
const connect = new URL(body.connectUrl);
|
|
282
|
+
expect(`${connect.origin}${connect.pathname}`).toBe("https://connector.test/connect");
|
|
283
|
+
expect(connect.searchParams.get("ref")).toBe("swarm");
|
|
284
|
+
expect(connect.searchParams.get("swarm")).toBe("https://swarm.example.com");
|
|
285
|
+
expect(connect.searchParams.get("code")).toBe(body.code);
|
|
286
|
+
const row = await getDbClient().get<{ label: string }>(
|
|
287
|
+
"SELECT label FROM connector_codes WHERE user_id = ?",
|
|
288
|
+
[user.id],
|
|
289
|
+
);
|
|
290
|
+
expect(row?.label).toBe("my chatgpt");
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
test("POST keeps a fragment on CONNECTOR_CONNECT_URL outside the query", async () => {
|
|
294
|
+
process.env.CONNECTOR_CONNECT_URL = "https://connector.test/connect#signup";
|
|
295
|
+
const user = await createUser({ name: "Fragment Connector User" });
|
|
296
|
+
const body = await createCode(user.id);
|
|
297
|
+
const connect = new URL(body.connectUrl);
|
|
298
|
+
expect(connect.hash).toBe("#signup");
|
|
299
|
+
expect(connect.searchParams.get("code")).toBe(body.code);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test("POST rejects an insecure or malformed CONNECTOR_CONNECT_URL without storing a code", async () => {
|
|
303
|
+
const user = await createUser({ name: "Insecure Connector User" });
|
|
304
|
+
for (const value of ["http://connector.test/connect", "not a url", "javascript:alert(1)"]) {
|
|
305
|
+
process.env.CONNECTOR_CONNECT_URL = value;
|
|
306
|
+
const response = await authedFetch(`/api/users/${user.id}/connector-codes`, {
|
|
307
|
+
method: "POST",
|
|
308
|
+
body: "{}",
|
|
309
|
+
});
|
|
310
|
+
expect(response.status).toBe(400);
|
|
311
|
+
}
|
|
312
|
+
expect(await getDbClient().query("SELECT code_hash FROM connector_codes")).toHaveLength(0);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
test("POST resolves the public origin from swarm_config before env", async () => {
|
|
316
|
+
process.env.PUBLIC_MCP_BASE_URL = "http://localhost:3013";
|
|
317
|
+
await upsertSwarmConfig({
|
|
318
|
+
scope: "global",
|
|
319
|
+
key: "PUBLIC_MCP_BASE_URL",
|
|
320
|
+
value: "https://configured.example.com/",
|
|
321
|
+
});
|
|
322
|
+
const user = await createUser({ name: "Config Origin User" });
|
|
323
|
+
const body = await createCode(user.id);
|
|
324
|
+
expect(new URL(body.connectUrl).searchParams.get("swarm")).toBe(
|
|
325
|
+
"https://configured.example.com",
|
|
326
|
+
);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test("POST accepts a loopback http public origin for local stacks", async () => {
|
|
330
|
+
const user = await createUser({ name: "Loopback Origin User" });
|
|
331
|
+
for (const origin of ["http://localhost:3013", "http://127.0.0.1:3013", "http://[::1]:3013"]) {
|
|
332
|
+
process.env.PUBLIC_MCP_BASE_URL = origin;
|
|
333
|
+
const body = await createCode(user.id);
|
|
334
|
+
expect(new URL(body.connectUrl).searchParams.get("swarm")).toBe(origin);
|
|
335
|
+
}
|
|
336
|
+
process.env.PUBLIC_MCP_BASE_URL = "https://swarm.example.com";
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
test("discovery is public and reports the API, dashboard and connect URLs", async () => {
|
|
340
|
+
const originalAppUrl = process.env.APP_URL;
|
|
341
|
+
const originalDashboardUrl = process.env.DASHBOARD_URL;
|
|
342
|
+
try {
|
|
343
|
+
delete process.env.APP_URL;
|
|
344
|
+
delete process.env.DASHBOARD_URL;
|
|
345
|
+
const bare = await fetch(url("/api/connector/discovery"));
|
|
346
|
+
expect(bare.status).toBe(200);
|
|
347
|
+
expect(await bare.json()).toEqual({
|
|
348
|
+
apiUrl: "https://swarm.example.com",
|
|
349
|
+
connectUrl: "https://mcp.agent-swarm.dev/connections",
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
process.env.APP_URL = "https://env-app.example.com";
|
|
353
|
+
await upsertSwarmConfig({
|
|
354
|
+
scope: "global",
|
|
355
|
+
key: "APP_URL",
|
|
356
|
+
value: "https://app.example.com/, https://other.example.com",
|
|
357
|
+
});
|
|
358
|
+
process.env.CONNECTOR_CONNECT_URL = "http://insecure.test/connect";
|
|
359
|
+
const configured = await fetch(url("/api/connector/discovery"));
|
|
360
|
+
expect(await configured.json()).toEqual({
|
|
361
|
+
apiUrl: "https://swarm.example.com",
|
|
362
|
+
appUrl: "https://app.example.com",
|
|
363
|
+
connectUrl: null,
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
// The connector ignores an http dashboard, so discovery does not report it.
|
|
367
|
+
await upsertSwarmConfig({ scope: "global", key: "APP_URL", value: "http://app.example.com" });
|
|
368
|
+
const insecureApp = await fetch(url("/api/connector/discovery"));
|
|
369
|
+
expect(await insecureApp.json()).toEqual({
|
|
370
|
+
apiUrl: "https://swarm.example.com",
|
|
371
|
+
connectUrl: null,
|
|
372
|
+
});
|
|
373
|
+
} finally {
|
|
374
|
+
if (originalAppUrl === undefined) delete process.env.APP_URL;
|
|
375
|
+
else process.env.APP_URL = originalAppUrl;
|
|
376
|
+
if (originalDashboardUrl === undefined) delete process.env.DASHBOARD_URL;
|
|
377
|
+
else process.env.DASHBOARD_URL = originalDashboardUrl;
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
test("POST rejects a non-HTTPS public origin, unknown users, and missing auth", async () => {
|
|
382
|
+
const user = await createUser({ name: "Http Origin User" });
|
|
383
|
+
process.env.PUBLIC_MCP_BASE_URL = "http://swarm.example.com";
|
|
384
|
+
const insecure = await authedFetch(`/api/users/${user.id}/connector-codes`, {
|
|
385
|
+
method: "POST",
|
|
386
|
+
body: "{}",
|
|
387
|
+
});
|
|
388
|
+
expect(insecure.status).toBe(400);
|
|
389
|
+
process.env.PUBLIC_MCP_BASE_URL = "https://swarm.example.com";
|
|
390
|
+
|
|
391
|
+
const unknown = await authedFetch("/api/users/not-a-user/connector-codes", {
|
|
392
|
+
method: "POST",
|
|
393
|
+
body: "{}",
|
|
394
|
+
});
|
|
395
|
+
expect(unknown.status).toBe(404);
|
|
396
|
+
|
|
397
|
+
const unauthed = await fetch(url(`/api/users/${user.id}/connector-codes`), {
|
|
398
|
+
method: "POST",
|
|
399
|
+
headers: { "Content-Type": "application/json" },
|
|
400
|
+
body: "{}",
|
|
401
|
+
});
|
|
402
|
+
expect(unauthed.status).toBe(401);
|
|
403
|
+
expect(await getDbClient().query("SELECT code_hash FROM connector_codes")).toHaveLength(0);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
test("exchange mints a working token without the API key and records token_minted", async () => {
|
|
407
|
+
const user = await createUser({ name: "Exchange User" });
|
|
408
|
+
const { code } = await createCode(user.id);
|
|
409
|
+
|
|
410
|
+
const response = await exchange(code);
|
|
411
|
+
expect(response.status).toBe(200);
|
|
412
|
+
const body = (await response.json()) as { token: string; userId: string; version: string };
|
|
413
|
+
expect(body.token.startsWith("aswt_")).toBe(true);
|
|
414
|
+
expect(body.userId).toBe(user.id);
|
|
415
|
+
const pkg = (await Bun.file("package.json").json()) as { version: string };
|
|
416
|
+
expect(body.version).toBe(pkg.version);
|
|
417
|
+
|
|
418
|
+
expect((await resolveUserByToken(body.token))?.id).toBe(user.id);
|
|
419
|
+
const event = await getDbClient().get<{ actor: string; afterJson: string }>(
|
|
420
|
+
"SELECT actor, afterJson FROM user_identity_events WHERE userId = ? AND eventType = 'token_minted'",
|
|
421
|
+
[user.id],
|
|
422
|
+
);
|
|
423
|
+
expect(event?.actor).toBe(`operator:${fingerprintApiKey(API_KEY)}`);
|
|
424
|
+
expect(JSON.parse(event!.afterJson).label).toBe("ChatGPT connector");
|
|
425
|
+
expect(JSON.parse(event!.afterJson).source).toBe("connector_exchange");
|
|
426
|
+
const row = await getDbClient().get<{ used_at: string | null }>(
|
|
427
|
+
"SELECT used_at FROM connector_codes WHERE user_id = ?",
|
|
428
|
+
[user.id],
|
|
429
|
+
);
|
|
430
|
+
expect(row?.used_at).toBeTruthy();
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
test("exchange returns the same 404 for reused, expired and unknown codes", async () => {
|
|
434
|
+
const user = await createUser({ name: "Invalid Code User" });
|
|
435
|
+
const { code } = await createCode(user.id);
|
|
436
|
+
expect((await exchange(code)).status).toBe(200);
|
|
437
|
+
|
|
438
|
+
const reused = await exchange(code);
|
|
439
|
+
expect(reused.status).toBe(404);
|
|
440
|
+
expect(await reused.json()).toEqual({ error: "code_invalid" });
|
|
441
|
+
|
|
442
|
+
const { code: expiredCode } = await createCode(user.id);
|
|
443
|
+
await getDbClient().run("UPDATE connector_codes SET expires_at = ? WHERE used_at IS NULL", [
|
|
444
|
+
new Date(Date.now() - 1000).toISOString(),
|
|
445
|
+
]);
|
|
446
|
+
const expired = await exchange(expiredCode);
|
|
447
|
+
expect(expired.status).toBe(404);
|
|
448
|
+
expect(await expired.json()).toEqual({ error: "code_invalid" });
|
|
449
|
+
|
|
450
|
+
const unknown = await exchange("not-a-real-code");
|
|
451
|
+
expect(unknown.status).toBe(404);
|
|
452
|
+
expect(await unknown.json()).toEqual({ error: "code_invalid" });
|
|
453
|
+
|
|
454
|
+
const tokens = await getDbClient().query("SELECT id FROM user_tokens WHERE userId = ?", [
|
|
455
|
+
user.id,
|
|
456
|
+
]);
|
|
457
|
+
expect(tokens).toHaveLength(1);
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
test("exchange rejects malformed codes with 404 and oversized bodies with 413", async () => {
|
|
461
|
+
const user = await createUser({ name: "Malformed Code User" });
|
|
462
|
+
const { code } = await createCode(user.id);
|
|
463
|
+
|
|
464
|
+
const truncated = await exchange(code.slice(0, 42));
|
|
465
|
+
expect(truncated.status).toBe(404);
|
|
466
|
+
expect(await truncated.json()).toEqual({ error: "code_invalid" });
|
|
467
|
+
|
|
468
|
+
const oversized = await fetch(url("/api/connector/exchange"), {
|
|
469
|
+
method: "POST",
|
|
470
|
+
headers: { "Content-Type": "application/json" },
|
|
471
|
+
body: JSON.stringify({ code: "x".repeat(4096) }),
|
|
472
|
+
});
|
|
473
|
+
expect(oversized.status).toBe(413);
|
|
474
|
+
|
|
475
|
+
// The real code still works after the rejected attempts.
|
|
476
|
+
expect((await exchange(code)).status).toBe(200);
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
test("concurrent exchanges of one code mint exactly one token", async () => {
|
|
480
|
+
const user = await createUser({ name: "Race User" });
|
|
481
|
+
const { code } = await createCode(user.id);
|
|
482
|
+
const results = await Promise.all([exchangeConnectorCode(code), exchangeConnectorCode(code)]);
|
|
483
|
+
expect(results.filter(Boolean)).toHaveLength(1);
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
test("exchange is rate limited to 10 per minute per IP", async () => {
|
|
487
|
+
const ip = { "X-Forwarded-For": "203.0.113.7" };
|
|
488
|
+
for (let i = 0; i < 10; i++) {
|
|
489
|
+
expect((await exchange(`unknown-${i}`, ip)).status).toBe(404);
|
|
490
|
+
}
|
|
491
|
+
const limited = await exchange("unknown-11", ip);
|
|
492
|
+
expect(limited.status).toBe(429);
|
|
493
|
+
expect(limited.headers.get("retry-after")).toBe("6");
|
|
494
|
+
expect((await exchange("other-ip", { "X-Forwarded-For": "203.0.113.8" })).status).toBe(404);
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
test("cleanup deletes codes that expired more than an hour ago", async () => {
|
|
498
|
+
const user = await createUser({ name: "Sweep User" });
|
|
499
|
+
await createCode(user.id);
|
|
500
|
+
await createCode(user.id);
|
|
501
|
+
const now = Date.now();
|
|
502
|
+
await getDbClient().run(
|
|
503
|
+
"UPDATE connector_codes SET expires_at = ? WHERE rowid = (SELECT MIN(rowid) FROM connector_codes)",
|
|
504
|
+
[new Date(now - 61 * 60 * 1000).toISOString()],
|
|
505
|
+
);
|
|
506
|
+
expect(await deleteExpiredConnectorCodes(new Date(now))).toBe(1);
|
|
507
|
+
expect(await getDbClient().query("SELECT code_hash FROM connector_codes")).toHaveLength(1);
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
describe("clientIp", () => {
|
|
512
|
+
function fakeReq(remoteAddress: string, xff?: string): IncomingMessage {
|
|
513
|
+
return {
|
|
514
|
+
socket: { remoteAddress },
|
|
515
|
+
headers: xff ? { "x-forwarded-for": xff } : {},
|
|
516
|
+
} as unknown as IncomingMessage;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
test("trusts the rightmost X-Forwarded-For hop only behind a private peer", () => {
|
|
520
|
+
expect(clientIp(fakeReq("127.0.0.1", "1.1.1.1, 203.0.113.7"))).toBe("203.0.113.7");
|
|
521
|
+
expect(clientIp(fakeReq("::ffff:172.18.0.2", "203.0.113.7"))).toBe("203.0.113.7");
|
|
522
|
+
expect(clientIp(fakeReq("10.0.0.5", "203.0.113.7"))).toBe("203.0.113.7");
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
test("ignores X-Forwarded-For from a public peer", () => {
|
|
526
|
+
expect(clientIp(fakeReq("198.51.100.4", "203.0.113.7"))).toBe("198.51.100.4");
|
|
527
|
+
expect(clientIp(fakeReq("172.32.0.1", "203.0.113.7"))).toBe("172.32.0.1");
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
test("falls back to the peer when the header is absent", () => {
|
|
531
|
+
expect(clientIp(fakeReq("127.0.0.1"))).toBe("127.0.0.1");
|
|
532
|
+
});
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
describe("createIpRateLimiter", () => {
|
|
536
|
+
test("caps tracked buckets by evicting the oldest key", () => {
|
|
537
|
+
const limiter = createIpRateLimiter({ capacity: 1, refillPerMs: 0 });
|
|
538
|
+
const now = 1_000;
|
|
539
|
+
expect(limiter.take("first", now)).toBe(true);
|
|
540
|
+
expect(limiter.take("first", now)).toBe(false);
|
|
541
|
+
for (let i = 0; i < 50_000; i++) limiter.take(`ip-${i}`, now);
|
|
542
|
+
// "first" was evicted, so it starts with a fresh bucket.
|
|
543
|
+
expect(limiter.take("first", now)).toBe(true);
|
|
544
|
+
});
|
|
545
|
+
});
|