Compare commits

..
Author SHA1 Message Date
bsodfather 0de1e94df4 MCP: dispatch incoming requests and notifications through SDK envelopes
CI / UI Checks (pull_request) Successful in 14s
CI / Rust Checks (pull_request) Successful in 35m55s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-12 21:53:24 +00:00
bsodfather 156a849087 refactor(mcp): consume SDK initialized notification in legacy handshake
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 33m16s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-12 19:06:52 +00:00
bsodfather 326f209bc5 refactor(mcp): route runtime ping through rmcp request and result
CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Successful in 29m49s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-12 16:30:34 +00:00
bsodfather 8068fd7910 refactor(mcp): parse legacy initialize protocol version through SDK
CI / UI Checks (pull_request) Successful in 10s
CI / Rust Checks (pull_request) Successful in 31m1s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-12 13:43:04 +00:00
bsodfather 2f9c4cb568 refactor(mcp): serialize negotiation results through rmcp SDK
CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Successful in 31m21s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 2s
CI / Deploy (pull_request) Skipped
2026-11-12 10:48:24 +00:00
bsodfather 61378224b2 Merge pull request 'MCP: SDK success/error envelopes with preserved wire contract' (#129) from delivery/rmcp107-envelopes into main
CI / UI Checks (push) Successful in 9s
CI / Rust Checks (push) Successful in 30m11s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
Community Release / release (push) Successful in 50m47s
2026-11-12 08:23:58 +00:00
bsodfather 1b51d44121 refactor(mcp): serialize success and error envelopes through SDK
CI / UI Checks (pull_request) Successful in 10s
CI / Rust Checks (pull_request) Successful in 30m13s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-12 07:52:03 +00:00
bsodfather 077be3b069 Merge pull request 'refactor(mcp): catalog-change signals/listen through rmcp SDK' (#128) from work/rmcp107-signals into main
CI / UI Checks (push) Successful in 8s
CI / Rust Checks (push) Successful in 29m23s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
Community Release / release (push) Successful in 49m26s
2026-11-12 05:31:56 +00:00
bsodfather 42e7728b3a refactor(mcp): migrate catalog-change signals and listen to rmcp SDK
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 29m12s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-12 05:02:14 +00:00
bsodfather 541b135baf Merge pull request 'refactor(mcp): progress and legacy cancellation via rmcp SDK (#107 slice)' (#127) from delivery/rmcp107-progress-cancel into main
CI / UI Checks (push) Successful in 10s
CI / Rust Checks (push) Successful in 27m48s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
Community Release / release (push) Successful in 47m12s
2026-11-12 02:49:10 +00:00
bsodfather 835947e9fc refactor(mcp): migrate progress and legacy cancel params to rmcp SDK
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 27m22s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-12 02:20:43 +00:00
bsodfather 2b36add550 Merge pull request 'refactor(mcp): compatible logging through rmcp SDK (#107 slice)' (#126) from work/rmcp107-logging into main
CI / UI Checks (push) Successful in 9s
CI / Rust Checks (push) Successful in 32m30s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 2s
CI / Deploy (push) Skipped
Community Release / release (push) Successful in 55m33s
2026-11-11 23:19:36 +00:00
bsodfather 4832878468 refactor(mcp): migrate compatible logging paths to rmcp SDK
CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Successful in 27m19s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-11 22:51:42 +00:00
bsodfather 5bf4e3e9bf Merge pull request 'fix(deps): rustix 1.1.6 for release security gate' (#125) from fix/rustix-rustsec-2026-0486 into main
CI / UI Checks (push) Successful in 13s
CI / Rust Checks (push) Successful in 34m16s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
Community Release / release (push) Successful in 52m54s
2026-11-11 12:10:13 +00:00
bsodfather 1ac5b10c67 fix(deps): update rustix for RUSTSEC-2026-0486
CI / UI Checks (pull_request) Successful in 7s
CI / Rust Checks (pull_request) Successful in 27m46s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-11 11:41:23 +00:00
bsodfather 07a9ef0a4a Merge pull request 'refactor(mcp): completion request/result via rmcp SDK' (#124) from delivery/rmcp107-completion into main
CI / UI Checks (push) Successful in 12s
CI / Rust Checks (push) Successful in 31m41s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
Community Release / release (push) Failing after 12s
2026-11-11 10:52:36 +00:00
bsodfather 4f83d8d24c refactor(mcp): route completion requests and results through rmcp SDK
CI / UI Checks (pull_request) Successful in 12s
CI / Rust Checks (pull_request) Successful in 36m53s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-11 10:11:35 +00:00
bsodfather 2a57800f63 Merge pull request 'fix(mcp): rmcp 3.9.0 — RUSTSEC-2026-0462' (#123) from fix/rmcp-security-0462 into main
CI / UI Checks (push) Successful in 9s
CI / Rust Checks (push) Successful in 25m46s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 3s
CI / Deploy (push) Skipped
Community Release / release (push) Successful in 48m48s
2026-11-10 21:51:10 +00:00
bsodfather e359077f6e fix(mcp): update rmcp to 3.9.0 for RUSTSEC-2026-0462
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 32m46s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-10 21:17:38 +00:00
bsodfather c0117ff6b8 Merge pull request 'refactor(mcp): migrate Prompts list/get through rmcp SDK' (#122) from delivery/rmcp107-prompts into main
CI / UI Checks (push) Successful in 13s
CI / Rust Checks (push) Successful in 31m52s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 5s
CI / Deploy (push) Skipped
Community Release / release (push) Failing after 12s
2026-11-10 19:51:33 +00:00
bsodfather 24bba2cf33 refactor(mcp): migrate Prompts requests and results through rmcp SDK
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 30m13s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Deploy (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
2026-11-10 19:17:54 +00:00
bsodfather f56553c4d6 Merge pull request 'refactor(mcp): Resources request/result runtime through rmcp SDK' (#121) from delivery/rmcp107-resources into main
CI / UI Checks (push) Successful in 12s
CI / Rust Checks (push) Successful in 25m54s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 2s
CI / Deploy (push) Skipped
Community Release / release (push) Successful in 56m48s
2026-11-09 05:16:06 +00:00
bsodfather 027e2cb911 refactor(mcp): route Resources requests and results through rmcp SDK
CI / UI Checks (pull_request) Successful in 12s
CI / Rust Checks (pull_request) Successful in 30m41s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-09 04:42:43 +00:00
bsodfather adabb2065a Merge pull request 'Serialize MCP Tools responses through rmcp SDK models (partial #107)' (#120) from delivery/rmcp107-tools-responses into main
CI / UI Checks (push) Successful in 8s
CI / Rust Checks (push) Successful in 33m25s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
Community Release / release (push) Successful in 51m19s
2026-11-08 23:19:11 +00:00
bsodfather bf773d61b8 feat(mcp): serialize Tools responses through rmcp SDK models
CI / UI Checks (pull_request) Successful in 10s
CI / Rust Checks (pull_request) Successful in 24m38s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-08 22:52:00 +00:00
bsodfather 74a98d9362 Merge pull request 'Route existing MCP Tools requests through typed rmcp runtime seam (partial #107)' (#119) from feat/rmcp107-tools-runtime into main
CI / Rust Checks (push) Successful in 24m5s
CI / Rust Feature Validation (push) Skipped
CI / UI Checks (push) Successful in 7s
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
Community Release / release (push) Failing after 45m55s
2026-11-08 15:30:17 +00:00
bsodfather 7f18bce679 feat(mcp): route Tools requests through typed rmcp runtime seam
CI / Rust Checks (pull_request) Successful in 24m17s
CI / Rust Feature Validation (pull_request) Skipped
CI / UI Checks (pull_request) Successful in 7s
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-08 15:00:22 +00:00
bsodfather 4ec9d9201b Merge pull request 'Use verified Trivy runner cache and avoid Cargo builds for release-only fixes' (#118) from fix/trivy-runner-cache-20261108 into main
CI / UI Checks (push) Successful in 14s
CI / Rust Checks (push) Failing after 1h39m35s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Failing after 1s
CI / Deploy (push) Skipped
2026-11-08 12:26:19 +00:00
bsodfather cf2909273b perf(ci): scope release tooling changes without unrelated Cargo builds
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 24m39s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-08 14:47:35 +03:00
bsodfather 4a7b230f35 fix(ci): load checksum-verified Trivy from runner tool cache 2026-11-08 14:21:16 +03:00
bsodfather cfe11a0a04 Merge pull request 'fix(ci): download pinned Trivy asset through GitHub API on HTTP failure' (#117) from fix/trivy-asset-api-20261108 into main
CI / UI Checks (push) Successful in 14s
CI / Rust Checks (push) Failing after 17m52s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Failing after 2s
CI / Deploy (push) Skipped
2026-11-08 10:59:43 +00:00
bsodfather 35a21f8ab5 fix(ci): fall back to pinned GitHub asset API for Trivy
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 25m14s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-08 13:02:56 +03:00
bsodfather 2d0deb8963 Merge pull request 'fix(ci): download Trivy database from reachable official registry' (#116) from fix/release-db-source-20261107 into main
CI / UI Checks (push) Successful in 14s
CI / Rust Checks (push) Successful in 25m23s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
2026-11-08 09:40:01 +00:00
bsodfather f0a79362eb fix(ci): prefer reachable official Trivy database source
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 30m33s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
2026-11-07 18:24:08 +03:00
bsodfather 27f86d4916 Merge pull request 'fix(ci): continue interrupted releases without replacing assets' (#115) from fix/release-resume-20261107 into main
CI / UI Checks (push) Successful in 10s
CI / Rust Checks (push) Successful in 33m36s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
fix(ci): safely resume incomplete immutable releases
2026-11-07 14:49:41 +00:00
bsodfather f728d31dbf fix(ci): safely resume incomplete immutable releases
CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Successful in 24m48s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 3s
CI / Deploy (pull_request) Skipped
2026-11-07 17:24:26 +03:00
bsodfather 29ee01b6ef Merge pull request 'Prototype isolated rmcp Tools adapter (#106)' (#114) from feat/rmcp106-tools-adapter into main
CI / UI Checks (push) Successful in 9s
CI / Rust Checks (push) Successful in 24m5s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
Community Release / release (push) Failing after 35m1s
2026-11-07 08:57:25 +00:00
bsodfather bcf8b43516 fix(ci): reuse installed pinned Rust toolchain
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 24m46s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 2s
CI / Deploy (pull_request) Skipped
2026-11-07 11:29:53 +03:00
bsodfather 1c256e6c75 Add isolated rmcp Tools adapter prototype
CI / Rust Checks (pull_request) Failing after 40s
CI / Rust Feature Validation (pull_request) Skipped
CI / UI Checks (pull_request) Successful in 12s
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Failing after 2s
CI / Deploy (pull_request) Skipped
2026-11-06 07:56:21 +00:00
bsodfather e4953abcd2 Merge pull request 'Story 4.5 — продолжение ожидающей Task после ввода' (#113) from feat/story-4.5-input-continuation into main
CI / UI Checks (push) Successful in 14s
CI / Rust Checks (push) Successful in 28m27s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 2s
CI / Deploy (push) Skipped
Community Release / release (push) Successful in 48m41s
2026-10-30 16:47:18 +00:00
bsodfather 90e5357a08 feat(epic4): продолжение ожидающей Task после ввода (Story 4.5)
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 21m42s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Durable worker приостанавливает Task как input_required до dispatch, если
закреплённая схема требует отсутствующее поле; authorized schema-validated
update атомарно и с аудитом возобновляет тот же Task ровно один раз.
Replay/conflict/TTL безопасны, ожидание удерживает admission permit.
Additive schema v30: task_input_requests, fingerprint/version, TTL-индекс.
2026-10-30 16:24:12 +00:00
bsodfather d80eda7329 Merge pull request 'Story 4.4 — безопасные повторные попытки и отмена durable Task' (#112) from delivery/epic4-story-4.4 into main
CI / UI Checks (push) Successful in 11s
CI / Rust Checks (push) Successful in 27m33s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
Community Release / release (push) Successful in 44m29s
Merge pull request 'Story 4.4 — безопасные повторные попытки и отмена durable Task' (#112) from delivery/epic4-story-4.4 into main
2026-10-30 12:46:29 +00:00
bsodfather a3057de51e feat(epic4): безопасные повторы и отмена durable Task (Story 4.4)
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 20m46s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Durable per-outbound attempt до отправки запроса, bounded retry/read,
server-owned verified writes, честный cancel/unknown, deadline/fencing,
additive schema v29. Staged renames task.rs/tasks.rs разложены на модули.

Includes disclosed cosmetic parity exception: единственная правка вне
принятой дельты — удалена лишняя пустая строка в конце
scripts/ci-story-checks.sh (EOF blank line); story-код не менялся.
2026-10-30 12:21:57 +00:00
bsodfather 8c02380762 fix(ui): временно ограничить npm build-стадию UI TLS 1.2 на CI-хосте
CI / UI Checks (push) Successful in 9s
CI / Rust Checks (push) Successful in 25m23s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 3s
CI / Deploy (push) Skipped
Merge pull request #111 'fix(ui): временно ограничить npm build-стадию UI TLS 1.2 на CI-хосте' from delivery/ui-npm-tls12-cap
2026-10-30 10:51:38 +00:00
hermes_dev 3b5fcc51cc fix(ui): pin npm build stage to TLS 1.2 on the CI host uplink
CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Successful in 20m41s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
The CI host uplink (192.168.1.108) drops TLS 1.3 ClientHello carrying the
hybrid X25519MLKEM768 key share, so registry.npmjs.org is unreachable from
the seeded node:22-alpine build stage while the same host answers over TLS
1.2. RUN npm ci hung until the build was cancelled.

Cap only the UI build stage at TLS 1.2 via NODE_OPTIONS=--tls-max-v1.2.
Certificates, SRI and the lockfile are untouched; no mirror, fallback or
cache-substituted packages. Remove the line once TLS 1.3 works from the CI
host again.
2026-10-30 09:56:19 +00:00
bsodfather 433987413e Merge pull request 'Story 4.3 — исполнять и восстанавливать durable Task через PG lease/fence' (#110) from delivery/epic4-story-4.3 into main
CI / UI Checks (push) Successful in 8s
CI / Rust Checks (push) Successful in 26m20s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 2s
CI / Deploy (push) Skipped
Community Release / release (push) Failing after 31m2s
2026-10-29 23:08:04 +00:00
bsodfather 5488eb73ec chore(migrations): зафиксировать каноническую последовательность v26–v28
CI / Rust Checks (pull_request) Successful in 22m6s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
CI / UI Checks (pull_request) Successful in 12s
Обязательный CI-гейт «Check canonical migration contract» сравнивает
`crank-migrate plan` с docs/schemas/migration-sequence.json. Принятая дельта
Story 4.3 добавила миграции v26–v28, но снимок контракта не был регенерирован,
поэтому exact-head CI падал на точном PR head с contract_drift (plan.compare).

Файл получен машинной регенерацией из того же дерева:
`crank-migrate plan > docs/schemas/migration-sequence.json`; проверка
`crank-migrate plan --check` возвращает {"status":"contract_current"}.
Содержимое меняется только добавлением трёх записей 26/27/28; существующие
записи 1–25 и их checksum не изменены, поведение продукта не меняется.
2026-10-29 22:43:05 +00:00
bsodfather f9f8c625d1 feat(epic4): исполнять durable Task через PG lease/fence и восстанавливать после сбоев (Story 4.3)
CI / UI Checks (pull_request) Successful in 15s
CI / Rust Checks (pull_request) Failing after 4m58s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Failing after 1s
CI / Deploy (pull_request) Skipped
mcp-server запускает сохранённые фоновые задачи: захват по PostgreSQL lease с
fence-токеном, зашифрованный input и повторная проверка доступа до сетевого
вызова, checkpoint с AttemptID до dispatch и восстановление только по PG.
Устаревший worker не может зафиксировать результат.

Добавлены миграции v26–v28 (V25 не меняется), guard их отсутствия в старой
схеме, метрики/трассы попыток (attempt_trace) и остановка до отправки запроса:
стоп в окне после checkpoint и до permit завершает задачу как
failed(shutdown_before_dispatch) без сетевых вызовов, соседние queued-задачи
остаются доступными. Retry-попытки получают отдельные дочерние spans с durable
current_attempt_id. Shared admission/fairness из Story 4.1 сохранены.

Проверки: durable_tasks 13/13, stages 11/11 (retry 5), runtime lib 47,
bin shutdown 1, registry tasks 16, admission 11, migrations 58 (независимая QA
и исходный runtime-прогон), точный exact-head CI остаётся обязательным.
2026-10-29 22:26:52 +00:00
bsodfather 6df6ffac4e Merge pull request 'Story 4.2 — подтверждать durable Task после атомарного PostgreSQL commit (FR-22, FR-23)' (#109) from delivery/epic4-story-4.2 into main
CI / UI Checks (push) Successful in 13s
CI / Rust Checks (push) Successful in 21m12s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
Community Release / release (push) Successful in 36m42s
Merge accepted Story 4.2 durable Task acceptance
2026-10-27 07:33:30 +00:00
bsodfather 2219a7217d feat(epic4): подтверждать durable Task после атомарного PG-commit (Story 4.2)
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 19m34s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Application entry выдаёт Task ID только после атомарного PostgreSQL commit
Task+intent+capacity; replay/conflict, owner scope, bounded envelope,
terminal CAS и fail-closed readiness проверены на реальном PG.

Также включён принятый план перехода на rmcp (только документ, миграция не
запускается).
2026-10-27 00:09:36 +00:00
bsodfather cc612516a2 Merge pull request 'Story 4.1 — публиковать execution mode и применять admission limits (FR-21, FR-24)' (#104) from delivery/epic4-story-4.1 into main
CI / Rust Checks (push) Successful in 20m52s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / UI Checks (push) Successful in 10s
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 1s
CI / Deploy (push) Skipped
2026-10-26 21:38:06 +00:00
bsodfather 599dc1867b Merge pull request 'ci: безопасно интегрировать принятый Crank и отделить production deploy' (#105) from ops/separate-production-deploy into main
CI / UI Checks (push) Successful in 12s
CI / Rust Checks (push) Successful in 16m0s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 2s
CI / Deploy (push) Skipped
2026-10-26 21:36:09 +00:00
Codex 4e7440f25e ci: separate protected source integration from explicit production deployment
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 12m54s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 3s
CI / Deploy (pull_request) Skipped
2026-10-27 00:21:37 +03:00
bsodfather e80d30ddb0 feat(epic4): публиковать execution mode и применять общий admission (Story 4.1, FR-21, FR-24)
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 12m45s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Community Release / release (push) Successful in 40m35s
Tool Draft фиксирует synchronous/background/adaptive, deadline и fallback;
runtime применяет единый снимок политики (concurrency, очередь, approval,
deadline) до внешнего действия. Насыщение отклоняется typed overload с
retry guidance, mandatory safety store fail closed, background/adaptive
используют опубликованный synchronous/unsupported fallback.

Refs: #29
2026-10-26 21:08:08 +00:00
bsodfather 44774883a0 fix(epic3): выводить Tools-сигнал из клиентской проекции tools/list (Story 3.5, FR-14)
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 10m49s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Community Release / release (push) Successful in 25m35s
QA F1: CatalogFingerprint сравнивал только Tool-bindings, а tools/list
строится из catalog_tool_definitions, которая ещё зависит от
tool_selection_policy (mode, groups, search.max_results); в Search-режиме
отдаваемый список вообще не зависит от bindings. Из-за этого commit,
меняющий только политику, не порождал notifications/tools/list_changed
(клиент оставался со stale-каталогом), а binding-only commit в Search
порождал ложный сигнал.

Теперь Tools-часть отпечатка — канонизированные определения, которые
реально отдаёт tools/list (отсортированы, чтобы порядок чтения не был
ложным изменением): сигнал есть ровно тогда, когда меняется видимая
клиенту проекция.

ATDD: новый сценарий на обеих ревизиях — policy-only commit (Direct ->
Search) даёт ровно один Tools-кадр и авторитетный tools/list с
search_tools/call_tool, а последующий binding-only commit в Search-режиме
не сигналит. Юнит-оракулы переведены на реальную проекцию: policy-only
change, невидимое поле политики, version bump с идентичным определением и
binding change в Search-режиме — silent.
2026-10-26 17:22:07 +00:00
hermes-dev a0365bb80b feat(epic3): отправлять Tools catalog change signals после commit (Story 3.5, FR-14)
Клиент, согласовавший tools list-changed, получает bounded
`notifications/tools/list_changed` после коммита видимой Tool-проекции
опубликованного Agent snapshot; `tools/list` остаётся авторитетным для
клиента без notification и после reconnect.

- signals.rs: третий тип сигнала в существующем stream-local watcher
  (один tick = максимум один кадр на тип, fixed-capacity queue, без
  cross-process bus и без post-commit hook); fingerprint сравнивает
  tool_name/operation.version/title/description, поэтому Tool-only
  commit не даёт Resource/Prompt сигналов и наоборот.
- registry.rs: tools.listChanged=true (реально отправляется в обеих
  revision); modern filter toolsListChanged теперь подтверждается
  acknowledge и не шлётся вне согласованного набора.
- legacy GET-стрим несёт Tools-сигнал вместе с Resource/Prompt.
- ATDD: RED на baseline (ack пуст, Tools-кадра нет, capability false),
  GREEN — 8 integration-сценариев × обе revision, unit-оракулы wire,
  representatives official MCP SDK 1.32.2 smoke со штатными headers.
- capability inventory/baseline manifest и frozen projection обновлены
  под tools.listChanged=true; ci-story-checks.sh дополнен AC 3.5.
2026-10-26 17:22:07 +00:00
bsodfather 2499016dc7 fix(epic3): принимать поток progress при JSON-first Accept официального SDK (Story 3.4, FR-14)
CI / Rust Checks (pull_request) Successful in 9m12s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
CI / UI Checks (pull_request) Successful in 10s
Community Release / release (push) Successful in 24m39s
Официальный MCP SDK перечисляет медиатипы в `Accept` JSON-first
(`application/json, text/event-stream`). Прежняя реализация выбирала
negotiated JSON-тело, и клиент, которому нужен progress, не получал
ни одного кадра.

Коррекция переиспользует уже провалидированные шаги: для `tools/call`
с допустимым progress token и предложенным event-stream режим ответа
переключается на SSE. Admission/авторизация не двигались; другой метод,
отсутствующий или непригодный token и JSON-only `Accept` сохраняют
negotiated режим.

ATDD: тот же JSON-first `Accept` даёт строго монотонные progress 1/2/3 и
прежний result в обеих supported revisions, а негативные контроли
(без token и с непригодным token) остаются на JSON-теле.
2026-10-26 16:03:48 +00:00
bsodfather f613250fd3 fix(epic3): сделать ATDD отмены чувствительным к регрессии и покрыть bounds токена в обеих revision (Story 3.4, FR-14)
- CANCEL_GRACE_MS 1200 -> 2500 (> LONG_HOLD_MS) и const-assert на этот порядок:
  при незавершённой отмене вызов успевает завершиться и записать исход внутри
  окна проверки, поэтому тест доказывает остановку, а не только «ещё не готово».
- malformed/oversized/empty/object/array token теперь проверяются и в modern
  revision, не только в legacy (обе читают одно поле RequestMetaObject).
- комментарий cancelled_response приведён к фактическому поведению: у
  остановленного вызова строки Invocation History нет вообще.
2026-10-26 13:07:14 +00:00
hermes_dev 862cde8559 feat(epic3): progress и безопасная отмена Tool calls (Story 3.4, FR-14)
- request-scoped lifecycle одного tools/call: валидированный `_meta.progressToken`
  (строковый/числовой, 1..=128 байт) и bounded очередь реальных этапов
  (tool_resolved → authorization_resolved → execution_dispatched) в собственный
  SSE-stream запроса; без token или без stream-carrier результат не меняется;
- сериализация `notifications/progress` по pinned схеме обеих revisions
  (progressToken/progress/message, total не изобретается);
- отмена по revision: 2025-11-25 — `notifications/cancelled` через bounded
  in-flight таблицу (владение = session + platform credential, не progress token);
  2026-07-28 — закрытие response stream; в обоих случаях drop будущего вызова =
  кооперативная остановка, terminal-сообщение не отправляется;
- streamed ответ сохраняет проекцию Story 3.18: closed `notifications/message`
  перед финальным кадром (перенос logging-scope в тело стрима);
- ATDD: 12 end-to-end тестов на реальном endpoint/каталоге/counted upstream
  (обе revisions, malformed/oversized/reused token, owner/foreign/late cancel,
  disconnect, ambiguous outbound, representative client smoke) + 7 unit-тестов
  границ lifecycle; entry в scripts/ci-story-checks.sh.
2026-10-26 12:02:11 +00:00
bsodfather 49c8af24ed fix(epic3): обновить checksum inventory в capability baseline (Story 3.19)
CI / Rust Checks (pull_request) Successful in 8m5s
CI / Rust Feature Validation (pull_request) Skipped
CI / UI Checks (pull_request) Successful in 31m50s
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Community Release / release (push) Successful in 22m34s
Story 3.19 меняет `docs/capability-inventory.json` (статусы `listChanged`,
`subscriptions/listen`, `notifications/*/list_changed`), поэтому
`docs/capability-baseline/manifest.json` должен ссылаться на новый SHA-256 того
же артефакта: `85970569…` → `10ca3538aa1b6b25868f80e19932c7ef918398c4817c52bc1aae52f18c31522b`,
ровно как в `tests/fixtures/mcp-revisions/*.provenance.json` этого же delta.

Поймано Required CI на `a965bc4` (run 887, job 2491): шаг
`Check Capability Baseline` падал с `CHECKSUM_MISMATCH
pointer=/artifacts/0/sha256`; остальные шаги того же job'а (tooling unit tests
305 OK, capability inventory `status=valid protocol_items=50`, community scope)
были зелёными. Локально после правки:
`validate-capability-baseline.py` → `baseline_version=2026.08.24.1 flows=35`,
`validate-capability-inventory.py` → `status=valid`, community scope (1263
файла), runtime-config/config/metrics/rust boundaries и code health — 0.

Содержательных изменений продукта здесь нет: только checksum-указатель на уже
изменённый в предыдущем коммите артефакт.
2026-10-25 21:08:20 +00:00
bsodfather a965bc4137 feat(epic3): уведомления об изменении Resource/Prompt каталога MCP (Story 3.19)
CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Failing after 4m10s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Failing after 1s
CI / Deploy (pull_request) Skipped
Story 3.19 / FR-19 на принятой базе Story 3.18 (`47a6e54`, PR #99): после
commit опубликованного каталога Agent клиент получает
`notifications/resources/list_changed` и/или
`notifications/prompts/list_changed`, обе pinned revisions, а обычное чтение
каталога остаётся авторитетным при потере сигнала. Scope ограничен
list-change: `resources/subscribe`/`unsubscribe` остаются `-32601`, значение
`resources.subscribe` остаётся `false` (одобренный владельцем вариант A).

Публикация переносит только semantic delta Story 3.19 из
`/home/codex/code/crank-worktrees/story31-worktree`
(`1b6db09` + cumulative overlay) на принятый head 3.18, а не копирует
worktree:
- новый `protocol/signals.rs` (watcher, отпечаток каталога, bounded filter,
  revision-correct encoders, bounded legacy stream registry) и
  `protocol/signals_tests.rs` (17 unit oracles);
- `subscriptions/listen` (modern-only dispatch registration) + честные
  `listChanged: true` объявления; `app/endpoint.rs` получает legacy GET
  producer/DELETE retire, `app/dispatch.rs` — modern listen stream;
- ATDD `mcp_signals` 5 сценариев на реальном endpoint/PostgreSQL/artifact
  store; capability inventory и frozen/provenance проекции обновлены
  (`b90b59f3…`, inventory `10ca3538…`);
- `scripts/ci-story-checks.sh`: добавлены 3.19-проверки, записи Story 3.18
  сохранены (гейт не ослаблен).

Принятая правка code-health `206ca78` сохранена: `app/endpoint.rs` 402 строки
(было 381), `app/legacy_dispatch.rs` 731 без изменений, т.е. split не
переписан. Новый ATDD-файл `mcp_signals.rs` до 1138 строк превышал лимит
`scripts/check-rust-code-health.sh`, поэтому его сценарии механически
вынесены в `mcp_signals/scenarios.rs` без изменения проверок.

Наследованный дефект tooling-гейта: `tests/unit/test_validate_capability_inventory.py`
требует, чтобы `Run tooling unit tests` шёл раньше
`scripts/validate-capability-inventory.py`; в принятом `ci.yml` (3.18) шаг
оказался в условном feature-job. Шаг перенесён в обязательный `rust` job перед
проверкой inventory — исполняемый tooling-гейт теперь проверяется там же, где
применяется; ни одна проверка не отключена и не ослаблена.

Проверки на этом коммите: `ci-story-checks.sh` exit 0 — 4/9/1/12/10/3/5(+1
manual ignored)/17 passed; `crank-community-mcp --lib` 102/0; transport 20/0,
mcp_prompts 15/0, revision_matrix::negotiation 23/0, era_isolation 5/0;
`cargo fmt --check` и `cargo clippy -p crank-community-mcp -p mcp-server
--all-targets -D warnings` — 0; `check-rust-code-health.sh` — pass;
capability inventory `status=valid protocol_items=50 revisions=2`;
`python3 -m unittest discover -s tests/unit` — 305 OK.

NOT_RUN (честно, не PASS): полный `just verify`, межкомпонентная интеграция,
нагрузка и квалификация ≥3 реальных клиентов — остаются на собранной фиче 3.20
по validation-policy.md.
2026-10-25 21:00:51 +00:00
hermes-dev 47a6e54660 fix(ui): bump nginx runtime base to 1.30.6 for the image scan gate
CI / Rust Checks (pull_request) Successful in 6m33s
CI / Rust Feature Validation (pull_request) Skipped
CI / UI Checks (pull_request) Successful in 16m36s
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
Community Release / release (push) Failing after 16m35s
2026-10-25 16:19:11 +00:00
bsodfather 206ca784f7 fix(epic3): уложить endpoint.rs Story 3.18 в лимит 1000 строк
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 9m2s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
The Story 3.18 logging arm pushed `crates/crank-community-mcp/src/app/endpoint.rs`
to 1058 lines, so `scripts/check-rust-code-health.sh` failed the Required CI
(Rust Checks, run 870 / job 2408) at the pinned 1000-line default.

Move the whole legacy-era dispatch of `mcp_post_response` verbatim into
`app/legacy_dispatch.rs` (`handle_legacy_request`), leaving the endpoint as the
transport/admission layer it documents itself to be. endpoint.rs is now 381
lines and the new module 731, both inside the limit; no baseline pin and no
check was touched.

Behaviour is unchanged: the moved match keeps every arm, envelope, status code
and safe detail as-is (legacy-only logging/setLevel, -32602 INVALID_LEVEL,
EVENT_LEVEL_APPLIED Notice acknowledgment, per-session level isolation), the
pre-dispatch legacy level read stays in `mcp_post_response`, and the only edits
to the moved text are the mechanical `&credential` -> `credential` (the
credential now arrives as a reference) and `protocol_version` already being
`Option<&str>`.
2026-10-25 15:45:01 +00:00
bsodfather a438c1df42 feat(epic3): безопасное structured logging MCP (Story 3.18)
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Failing after 4m11s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Failing after 1s
CI / Deploy (pull_request) Skipped
Story 3.18 (#25) surface: клиент получает только закрытые structured
`notifications/message` выбранного уровня, без запросов, секретов и
internal causes.

- `logging` capability advertising в обеих поддерживаемых revisions;
  `logging/setLevel` — legacy-only dispatch (modern — `_meta` per request);
- закрытый словарь LoggingLevel (RFC 5424), malformed → -32602 с одним
  safe detail; закрытый набор safe events, task-local request scope;
  bounded in-memory opt-in, без миграций;
- `notifications/message` идут перед ответом в SSE; JSON-режим их не несёт;
- ATDD в `scripts/ci-story-checks.sh`: mcp_logging (10), inventory projection (3),
  protocol::logging unit (12);
- проекция capability inventory/frozen oracle переведена на `logging`.

Story ATDD и affected проверки пройдены на этом дереве (fmt, clippy,
python oracle 56 OK, capability inventory valid). Полный `just verify` —
NOT_RUN на story по validation-policy, остаётся на собранной фиче.
2026-10-25 12:50:00 +00:00
219 changed files with 40027 additions and 1383 deletions

No files matched your search

+44 -12
View File
@@ -11,6 +11,12 @@ on:
options: [story, feature]
default: story
required: true
deploy_target:
description: Deployment target for this run
type: choice
options: [none, production]
default: none
required: true
push:
branches:
- main
@@ -34,6 +40,15 @@ jobs:
with:
fetch-depth: 0
- name: Detect release-tooling-only changes
id: release_tooling
env:
GITEA_EVENT_NAME: ${{ gitea.event_name }}
GITEA_EVENT_BEFORE: ${{ gitea.event.before }}
GITEA_PR_BASE_SHA: ${{ gitea.event.pull_request.base.sha }}
FORCE_FULL: ${{ inputs.validation_level == 'feature' || contains(gitea.event.pull_request.labels.*.name, 'feature-ready') }}
run: python3 scripts/ci-release-tooling-scope.py
- name: Install Rust toolchain
run: |
set -eu
@@ -42,9 +57,13 @@ jobs:
echo "Unable to read Rust toolchain channel from rust-toolchain.toml" >&2
exit 1
fi
rustup toolchain install "$toolchain" --profile minimal --component clippy --component rustfmt
rustup default "$toolchain"
host="$(rustc -vV | sed -n 's/^host: //p')"
if ! rustup run "$toolchain" rustc --version >/dev/null 2>&1 || \
! rustup run "$toolchain" cargo --version >/dev/null 2>&1 || \
! rustup run "$toolchain" rustfmt --version >/dev/null 2>&1 || \
! rustup run "$toolchain" cargo-clippy --version >/dev/null 2>&1; then
rustup toolchain install "$toolchain" --profile minimal --component clippy --component rustfmt
fi
host="$(rustup run "$toolchain" rustc -vV | sed -n 's/^host: //p')"
toolchain_dir="${RUSTUP_HOME:-$HOME/.rustup}/toolchains/${toolchain}-${host}"
toolchain_bin="$toolchain_dir/bin"
if [ ! -x "$toolchain_bin/rustc" ] || [ ! -x "$toolchain_bin/cargo" ]; then
@@ -68,19 +87,25 @@ jobs:
docker info
- name: Check typed runtime configuration contract
if: steps.release_tooling.outputs.ci_only != 'true'
run: |
cargo run -p crank-config --bin crank-config-contract -- --check
python3 scripts/check-runtime-config.py --root .
python3 scripts/check-config-boundaries.py --root .
- name: Check canonical migration contract
if: steps.release_tooling.outputs.ci_only != 'true'
run: cargo run -p admin-api --bin crank-migrate -- plan --check
- name: Check typed metrics contract
if: steps.release_tooling.outputs.ci_only != 'true'
run: |
cargo run -p crank-metrics --bin crank-metrics-contract -- --check
python3 scripts/check-metrics-boundaries.py --root .
- name: Run tooling unit tests
run: python3 -m unittest discover -s tests/unit
- name: Check Capability Inventory
run: |
required_args=""
@@ -129,6 +154,7 @@ jobs:
python3 scripts/check-openapi-matrix-compatibility.py --base "$base_ref"
- name: Run Crank story ATDD
if: steps.release_tooling.outputs.ci_only != 'true'
run: scripts/ci-story-checks.sh
ui:
name: UI Checks
@@ -178,9 +204,13 @@ jobs:
echo "Unable to read Rust toolchain channel from rust-toolchain.toml" >&2
exit 1
fi
rustup toolchain install "$toolchain" --profile minimal --component clippy --component rustfmt
rustup default "$toolchain"
host="$(rustc -vV | sed -n 's/^host: //p')"
if ! rustup run "$toolchain" rustc --version >/dev/null 2>&1 || \
! rustup run "$toolchain" cargo --version >/dev/null 2>&1 || \
! rustup run "$toolchain" rustfmt --version >/dev/null 2>&1 || \
! rustup run "$toolchain" cargo-clippy --version >/dev/null 2>&1; then
rustup toolchain install "$toolchain" --profile minimal --component clippy --component rustfmt
fi
host="$(rustup run "$toolchain" rustc -vV | sed -n 's/^host: //p')"
toolchain_dir="${RUSTUP_HOME:-$HOME/.rustup}/toolchains/${toolchain}-${host}"
toolchain_bin="$toolchain_dir/bin"
if [ ! -x "$toolchain_bin/rustc" ] || [ ! -x "$toolchain_bin/cargo" ]; then
@@ -203,8 +233,6 @@ jobs:
docker info
- name: Install dependency policy tool
run: cargo install cargo-deny --version 0.20.2 --locked
- name: Run tooling unit tests
run: python3 -m unittest discover -s tests/unit
- name: Check dependency licenses and advisories
run: cargo deny --locked check advisories bans licenses sources
- name: Run offline OpenAPI support corpus gate
@@ -259,9 +287,13 @@ jobs:
echo "Unable to read Rust toolchain channel from rust-toolchain.toml" >&2
exit 1
fi
rustup toolchain install "$toolchain" --profile minimal --component clippy --component rustfmt
rustup default "$toolchain"
host="$(rustc -vV | sed -n 's/^host: //p')"
if ! rustup run "$toolchain" rustc --version >/dev/null 2>&1 || \
! rustup run "$toolchain" cargo --version >/dev/null 2>&1 || \
! rustup run "$toolchain" rustfmt --version >/dev/null 2>&1 || \
! rustup run "$toolchain" cargo-clippy --version >/dev/null 2>&1; then
rustup toolchain install "$toolchain" --profile minimal --component clippy --component rustfmt
fi
host="$(rustup run "$toolchain" rustc -vV | sed -n 's/^host: //p')"
toolchain_dir="${RUSTUP_HOME:-$HOME/.rustup}/toolchains/${toolchain}-${host}"
toolchain_bin="$toolchain_dir/bin"
if [ ! -x "$toolchain_bin/rustc" ] || [ ! -x "$toolchain_bin/cargo" ]; then
@@ -498,7 +530,7 @@ jobs:
runs-on: ubuntu-latest
needs:
- required-checks
if: ${{ gitea.event_name == 'push' && gitea.ref == 'refs/heads/main' }}
if: ${{ gitea.event_name == 'workflow_dispatch' && gitea.ref == 'refs/heads/main' && inputs.validation_level == 'feature' && inputs.deploy_target == 'production' }}
env:
REGISTRY: git.itexp.me
IMAGE_TAG: ${{ gitea.sha }}
+74 -8
View File
@@ -12,10 +12,20 @@ on:
options: [story, feature]
default: story
required: true
resume_tag:
description: Existing draft release tag to resume with image-only steps
type: string
default: ""
required: false
resume_source_sha:
description: Exact accepted source commit SHA for the resume tag
type: string
default: ""
required: false
env:
REGISTRY: git.itexp.me
IMAGE_TAG: ${{ gitea.ref_name }}
IMAGE_TAG: ${{ inputs.resume_tag || gitea.ref_name }}
ADMIN_API_IMAGE: git.itexp.me/bsodfather/crank-community-admin-api
MCP_SERVER_IMAGE: git.itexp.me/bsodfather/crank-community-mcp-server
UI_IMAGE: git.itexp.me/bsodfather/crank-community-ui
@@ -31,12 +41,47 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
fetch-depth: 0
- name: Verify existing release before resume
id: resume
if: inputs.resume_tag != ''
env:
GITEA_SERVER_URL: https://git.itexp.me
GITEA_REPOSITORY: ${{ gitea.repository }}
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
RELEASE_TAG: ${{ inputs.resume_tag }}
RESUME_SOURCE_SHA: ${{ inputs.resume_source_sha }}
run: python3 scripts/ci-release-resume.py
- name: Preserve updated image scanner for resume
id: pin_resume_scanner
if: inputs.resume_tag != ''
run: |
set -eu
scanner_dir="$(mktemp -d "${RUNNER_TEMP:?RUNNER_TEMP is unavailable}/crank-release-scanner.XXXXXX")"
cp scripts/scan-images.sh "$scanner_dir/scan-images.sh"
cp scripts/check-image-scan.py "$scanner_dir/check-image-scan.py"
chmod +x "$scanner_dir/scan-images.sh"
printf 'path=%s\n' "$scanner_dir/scan-images.sh" >> "$GITHUB_OUTPUT"
- name: Checkout validated release source
if: inputs.resume_tag != ''
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
ref: ${{ steps.resume.outputs.release_tag }}
fetch-depth: 0
- name: Assert exact release source checkout
if: inputs.resume_tag != ''
env:
EXPECTED_SOURCE_SHA: ${{ steps.resume.outputs.source_sha }}
run: test "$(git rev-parse HEAD)" = "$EXPECTED_SOURCE_SHA"
- name: Clear optional release evidence
if: inputs.resume_tag == ''
run: rm -f .tmp/openapi-playwright.json .tmp/openapi-ui-evidence.json
- name: Use preinstalled Rust toolchain
if: inputs.resume_tag == ''
run: |
set -eu
toolchain_dir="${RUSTUP_HOME:-$HOME/.rustup}/toolchains/1.96.1-x86_64-unknown-linux-gnu"
@@ -54,6 +99,7 @@ jobs:
"$toolchain_bin/cargo-clippy" --version
- name: Verify runner toolchain
if: inputs.resume_tag == ''
run: |
python3 --version
rustc --version
@@ -65,19 +111,23 @@ jobs:
bao version
- name: Install dependency policy tool
if: inputs.resume_tag == ''
run: cargo install cargo-deny --version 0.20.2 --locked
- name: Check formatting
if: inputs.resume_tag == ''
run: cargo fmt --all --check
- name: Audit Rust dependencies
if: inputs.resume_tag == ''
run: cargo deny --locked check advisories bans licenses sources
- name: Run tooling unit tests
if: inputs.validation_level == 'feature'
if: inputs.resume_tag == '' && inputs.validation_level == 'feature'
run: python3 -m unittest discover -s tests/unit
- name: Check typed runtime configuration contract
if: inputs.resume_tag == ''
run: |
cargo run -p crank-config --bin crank-config-contract -- --check
python3 scripts/check-runtime-config.py --root .
@@ -85,14 +135,17 @@ jobs:
scripts/check-rust-boundaries.sh
- name: Check canonical migration contract
if: inputs.resume_tag == ''
run: cargo run -p admin-api --bin crank-migrate -- plan --check
- name: Check typed metrics contract
if: inputs.resume_tag == ''
run: |
cargo run -p crank-metrics --bin crank-metrics-contract -- --check
python3 scripts/check-metrics-boundaries.py --root .
- name: Check Capability Inventory
if: inputs.resume_tag == ''
run: |
required_args=""
for number in $(seq 1 54); do
@@ -105,6 +158,7 @@ jobs:
$required_args
- name: Check Capability Baseline
if: inputs.resume_tag == ''
run: |
python3 scripts/validate-capability-baseline.py \
--root . \
@@ -112,40 +166,46 @@ jobs:
--schema docs/schemas/capability-baseline.schema.json
- name: Check Community scope
if: inputs.resume_tag == ''
run: scripts/check-community-scope.sh
- name: Run Crank story ATDD
if: inputs.resume_tag == ''
run: scripts/ci-story-checks.sh
- name: Run release quality gates
if: inputs.validation_level == 'feature'
if: inputs.resume_tag == '' && inputs.validation_level == 'feature'
run: |
cargo clippy --workspace --all-targets --all-features -- -D warnings
cargo test --workspace --all-targets -- --test-threads=1
- name: Build release binaries
if: inputs.resume_tag == ''
run: cargo build --release -p admin-api -p mcp-server
- name: Install UI dependencies
if: inputs.resume_tag == ''
working-directory: apps/ui
run: npm ci
- name: Audit UI dependencies
if: inputs.resume_tag == ''
working-directory: apps/ui
run: npm audit --audit-level=high
- name: Build UI dist
if: inputs.resume_tag == ''
working-directory: apps/ui
run: npm run build
- name: Install Playwright browser
if: inputs.validation_level == 'feature'
if: inputs.resume_tag == '' && inputs.validation_level == 'feature'
working-directory: apps/ui
run: npx playwright install --with-deps chromium
- name: Run release end-to-end tests
if: inputs.validation_level == 'feature'
if: inputs.resume_tag == '' && inputs.validation_level == 'feature'
working-directory: apps/ui
run: |
mkdir -p ../../.tmp
@@ -153,7 +213,7 @@ jobs:
PLAYWRIGHT_JSON_OUTPUT=../../.tmp/openapi-playwright.json npm run e2e
- name: Collect sanitized OpenAPI UI release evidence
if: inputs.validation_level == 'feature'
if: inputs.resume_tag == '' && inputs.validation_level == 'feature'
working-directory: apps/ui
run: |
trap 'rm -f ../../.tmp/openapi-playwright.json' EXIT
@@ -175,12 +235,14 @@ jobs:
--require-accepted
- name: Validate deployment manifests
if: inputs.resume_tag == ''
run: |
docker compose -f docker-compose.yml --env-file .env.example config -q
docker compose -f deploy/community/docker-compose.yml --env-file deploy/community/.env.example config -q
docker compose -f deploy/community/docker-compose.images.yml --env-file deploy/community/.env.images.example --profile local-db config -q
- name: Package release artifacts
if: inputs.resume_tag == ''
run: |
set -euo pipefail
mkdir -p dist/release
@@ -204,13 +266,14 @@ jobs:
sha256sum "${files[@]}" > "dist/crank-community-${IMAGE_TAG}-checksums.txt"
- name: Generate SBOM
if: inputs.resume_tag == ''
run: |
docker run --rm -v "$PWD:/workspace" anchore/syft:latest \
dir:/workspace -o spdx-json \
> dist/crank-community-${IMAGE_TAG}-sbom.spdx.json
- name: Publish release bundles, checksums, evidence, and SBOM to Gitea
if: startsWith(gitea.ref, 'refs/tags/v')
if: inputs.resume_tag == '' && startsWith(gitea.ref, 'refs/tags/v')
env:
# gitea.server_url inside the job container is the internal http://gitea:3000 origin;
# the publisher only forwards its token to the public HTTPS origin.
@@ -249,6 +312,8 @@ jobs:
docker login '${{ env.REGISTRY }}' -u "$DEPLOY_REGISTRY_USER" --password-stdin
- name: Build and push release images
env:
RELEASE_IMAGE_SCANNER: ${{ steps.pin_resume_scanner.outputs.path || 'scripts/scan-images.sh' }}
run: |
docker build -f apps/admin-api/Dockerfile \
-t '${{ env.ADMIN_API_IMAGE }}:${{ env.IMAGE_TAG }}' \
@@ -285,7 +350,7 @@ jobs:
--env-file .tmp/release-migration-smoke.env --profile local-db up -d --wait
docker compose -f deploy/community/docker-compose.images.yml \
--env-file .tmp/release-migration-smoke.env --profile local-db logs migrate | grep '"status":"applied"'
scripts/scan-images.sh \
"$RELEASE_IMAGE_SCANNER" \
'${{ env.ADMIN_API_IMAGE }}:${{ env.IMAGE_TAG }}' \
'${{ env.MCP_SERVER_IMAGE }}:${{ env.IMAGE_TAG }}' \
'${{ env.UI_IMAGE }}:${{ env.IMAGE_TAG }}'
@@ -297,5 +362,6 @@ jobs:
docker push '${{ env.UI_IMAGE }}:latest'
- name: Show release bundle summary
if: inputs.resume_tag == ''
run: |
ls -lh dist
Generated
+216 -65
View File
@@ -25,7 +25,7 @@ dependencies = [
"async-trait",
"axum",
"axum-extra",
"base64",
"base64 0.22.1",
"crank-artifacts",
"crank-community-auth",
"crank-community-mcp",
@@ -186,7 +186,7 @@ checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -197,7 +197,7 @@ checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -349,6 +349,12 @@ version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "base64"
version = "0.23.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5"
[[package]]
name = "base64ct"
version = "1.8.3"
@@ -398,7 +404,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee04c4c84f1f811b017f2fbb7dd8815c976e7ca98593de9c1e2afad0f636bff4"
dependencies = [
"async-stream",
"base64",
"base64 0.22.1",
"bitflags",
"bollard-buildkit-proto",
"bollard-stubs",
@@ -455,7 +461,7 @@ version = "1.52.1-rc.29.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f0a8ca8799131c1837d1282c3f81f31e76ceb0ce426e04a7fe1ccee3287c066"
dependencies = [
"base64",
"base64 0.22.1",
"bollard-buildkit-proto",
"bytes",
"prost",
@@ -584,6 +590,12 @@ dependencies = [
"tokio-util",
]
[[package]]
name = "const-oid"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a"
[[package]]
name = "cookie"
version = "0.18.1"
@@ -653,7 +665,7 @@ version = "0.3.1"
dependencies = [
"async-trait",
"axum",
"base64",
"base64 0.22.1",
"crank-core",
"crank-metrics",
"crank-trace",
@@ -688,7 +700,7 @@ dependencies = [
"argon2",
"async-trait",
"axum-extra",
"base64",
"base64 0.22.1",
"crank-core",
"crank-registry",
"rand 0.10.2",
@@ -705,7 +717,7 @@ version = "0.3.1"
dependencies = [
"async-trait",
"axum",
"base64",
"base64 0.22.1",
"crank-adapter-rest",
"crank-artifacts",
"crank-core",
@@ -722,6 +734,7 @@ dependencies = [
"opentelemetry",
"opentelemetry_sdk",
"reqwest 0.12.28",
"rmcp",
"serde",
"serde_json",
"sha2 0.10.9",
@@ -826,6 +839,7 @@ dependencies = [
name = "crank-registry"
version = "0.3.1"
dependencies = [
"async-trait",
"crank-artifacts",
"crank-core",
"crank-mapping",
@@ -849,7 +863,7 @@ dependencies = [
"aes-gcm",
"async-trait",
"axum",
"base64",
"base64 0.22.1",
"crank-adapter-rest",
"crank-core",
"crank-import",
@@ -991,8 +1005,18 @@ version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25ae13da2f202d56bd7f91c25fba009e7717a1e4a1cc98a76d844b65ae912e9d"
dependencies = [
"darling_core",
"darling_macro",
"darling_core 0.23.0",
"darling_macro 0.23.0",
]
[[package]]
name = "darling"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed17f5901b6630b993ca003def43f2f8ef4014fc13b047b57aad617ff32bc2ec"
dependencies = [
"darling_core 0.24.1",
"darling_macro 0.24.1",
]
[[package]]
@@ -1005,7 +1029,20 @@ dependencies = [
"proc-macro2",
"quote",
"strsim",
"syn",
"syn 2.0.118",
]
[[package]]
name = "darling_core"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6837e2cf7485aaae18f86181d2f0e9a7ed297a025e220aeabf63fdebd3a2ddff"
dependencies = [
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn 3.0.9",
]
[[package]]
@@ -1014,9 +1051,20 @@ version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3984ec7bd6cfa798e62b4a642426a5be0e68f9401cfc2a01e3fa9ea2fcdb8d"
dependencies = [
"darling_core",
"darling_core 0.23.0",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
name = "darling_macro"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ac7135c3ef02b2f7833bbeb1be5ba7f966dcde8a87c6b87f65a778d71a02785"
dependencies = [
"darling_core 0.24.1",
"quote",
"syn 3.0.9",
]
[[package]]
@@ -1056,6 +1104,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2"
dependencies = [
"block-buffer 0.12.1",
"const-oid",
"crypto-common 0.2.2",
"ctutils",
]
@@ -1068,7 +1117,7 @@ checksum = "1ac70aa55017e108007fbaf5aa0f54b021c98f92ff8af59d42eda9da96e3dd4f"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -1077,7 +1126,7 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29547a1dc60885a552306986316bc9701ba120c1a8db6769fa68691529ad373d"
dependencies = [
"base64",
"base64 0.22.1",
"serde",
"serde_json",
]
@@ -1146,7 +1195,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -1308,7 +1357,7 @@ checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -1660,7 +1709,7 @@ version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0"
dependencies = [
"base64",
"base64 0.22.1",
"bytes",
"futures-channel",
"futures-util",
@@ -1671,7 +1720,7 @@ dependencies = [
"libc",
"percent-encoding",
"pin-project-lite",
"socket2 0.5.10",
"socket2 0.6.4",
"tokio",
"tower-service",
"tracing",
@@ -1919,7 +1968,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38c0b942f458fe50cdac086d2f946512305e5631e720728f2a61aabcd47a6264"
dependencies = [
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -2055,7 +2104,7 @@ version = "0.3.1"
dependencies = [
"async-trait",
"axum",
"base64",
"base64 0.22.1",
"crank-artifacts",
"crank-community-mcp",
"crank-config",
@@ -2120,7 +2169,7 @@ version = "0.18.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1db0d8f1fc9e62caebd0319e11eaec5822b0186c171568f0480b46a0137f9108"
dependencies = [
"base64",
"base64 0.22.1",
"evmap",
"indexmap 2.14.0",
"metrics",
@@ -2456,7 +2505,7 @@ dependencies = [
"regex",
"regex-syntax",
"structmeta",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -2470,6 +2519,12 @@ dependencies = [
"subtle",
]
[[package]]
name = "pastey"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4"
[[package]]
name = "percent-encoding"
version = "2.3.2"
@@ -2493,7 +2548,7 @@ checksum = "c96395f0a926bc13b1c17622aaddda1ecb55d49c8f1bf9777e4d877800a43f8b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -2579,7 +2634,7 @@ dependencies = [
"itertools",
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -2635,7 +2690,7 @@ dependencies = [
"quinn-udp",
"rustc-hash",
"rustls",
"socket2 0.5.10",
"socket2 0.6.4",
"thiserror 2.0.18",
"tokio",
"tracing",
@@ -2674,9 +2729,9 @@ dependencies = [
"cfg_aliases",
"libc",
"once_cell",
"socket2 0.5.10",
"socket2 0.6.4",
"tracing",
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -2851,7 +2906,7 @@ checksum = "b7186006dcb21920990093f30e3dea63b7d6e977bf1256be20c3563a5db070da"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -2889,7 +2944,7 @@ version = "0.12.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eddd3ca559203180a307f12d114c268abf583f59b03cb906fd0b3ff8646c1147"
dependencies = [
"base64",
"base64 0.22.1",
"bytes",
"cookie",
"cookie_store",
@@ -2933,7 +2988,7 @@ version = "0.13.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "219c5811de6525e5416c7d5d53bb656d3afdbc6c5af816e0802bcfa42dbdc1c3"
dependencies = [
"base64",
"base64 0.22.1",
"bytes",
"futures-channel",
"futures-core",
@@ -2980,6 +3035,53 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "rmcp"
version = "3.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d24a7e92f4bcaa36b97fa93c4fa7ca8f3e669eced949345b3462dba333c7f909"
dependencies = [
"async-trait",
"base64 0.23.1",
"bytes",
"chrono",
"futures",
"http",
"http-body",
"http-body-util",
"indexmap 2.14.0",
"libc",
"pastey",
"pin-project-lite",
"rand 0.10.2",
"rmcp-macros",
"schemars 1.2.1",
"serde",
"serde_json",
"sha2 0.11.0",
"sse-stream",
"thiserror 2.0.18",
"tokio",
"tokio-stream",
"tokio-util",
"tower-service",
"tracing",
"uuid",
]
[[package]]
name = "rmcp-macros"
version = "3.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3140f24a766f80f9ec1607bde85680959a69b83d46fa9ce87dadab7e67ab029"
dependencies = [
"darling 0.24.1",
"proc-macro2",
"quote",
"serde_json",
"syn 3.0.9",
]
[[package]]
name = "rustc-demangle"
version = "0.1.28"
@@ -3007,15 +3109,15 @@ dependencies = [
[[package]]
name = "rustix"
version = "1.1.4"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa"
dependencies = [
"bitflags",
"errno",
"libc",
"linux-raw-sys 0.12.1",
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -3074,7 +3176,7 @@ dependencies = [
"security-framework",
"security-framework-sys",
"webpki-root-certs",
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -3143,12 +3245,26 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2b42f36aa1cd011945615b92222f6bf73c599a102a300334cd7f8dbeec726cc"
dependencies = [
"chrono",
"dyn-clone",
"ref-cast",
"schemars_derive",
"serde",
"serde_json",
]
[[package]]
name = "schemars_derive"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d115b50f4aaeea07e79c1912f645c7513d81715d0420f8bc77a18c6260b307f"
dependencies = [
"proc-macro2",
"quote",
"serde_derive_internals",
"syn 2.0.118",
]
[[package]]
name = "scoped-tls"
version = "1.0.1"
@@ -3292,7 +3408,18 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
name = "serde_derive_internals"
version = "0.29.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.118",
]
[[package]]
@@ -3327,7 +3454,7 @@ checksum = "175ee3e80ae9982737ca543e96133087cbd9a485eecc3bc4de9c1a37b47ea59c"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -3348,7 +3475,7 @@ version = "3.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76a5c54c7310e7b8b9577c286d7e399ddd876c3e12b3ed917a8aabc4b96e9e8c"
dependencies = [
"base64",
"base64 0.22.1",
"bs58",
"chrono",
"hex",
@@ -3368,10 +3495,10 @@ version = "3.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84d57bc0c8b9a17920c178daa6bb924850d54a9c97ab45194bb8c17ad66bb660"
dependencies = [
"darling",
"darling 0.23.0",
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -3409,7 +3536,7 @@ checksum = "94e153fc76e1c6a068703d6d29c508a0b15c061c4b7e43da59cc097bc342673c"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -3543,7 +3670,7 @@ dependencies = [
name = "sqlx-core"
version = "0.9.0"
dependencies = [
"base64",
"base64 0.22.1",
"bytes",
"cfg-if",
"crc",
@@ -3585,7 +3712,7 @@ dependencies = [
"quote",
"sqlx-core",
"sqlx-macros-core",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -3608,7 +3735,7 @@ dependencies = [
"sqlx-mysql",
"sqlx-postgres",
"sqlx-sqlite",
"syn",
"syn 2.0.118",
"thiserror 2.0.18",
"tokio",
"url",
@@ -3649,7 +3776,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87a2bdd6e83f6b3ea525ca9fee568030508b58355a43d0b2c1674d5f79dcd65e"
dependencies = [
"atoi",
"base64",
"base64 0.22.1",
"bitflags",
"byteorder",
"crc",
@@ -3705,6 +3832,19 @@ dependencies = [
"uuid",
]
[[package]]
name = "sse-stream"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c25ac7aff0abd1dbc474536e40416e1102c7dd9bfba0b9861c6d357f835dcfb4"
dependencies = [
"bytes",
"futures-util",
"http-body",
"http-body-util",
"pin-project-lite",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.1"
@@ -3737,7 +3877,7 @@ dependencies = [
"proc-macro2",
"quote",
"structmeta-derive",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -3748,7 +3888,7 @@ checksum = "152a0b65a590ff6c3da95cabe2353ee04e6167c896b28e3b14478c2636c922fc"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -3768,6 +3908,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "syn"
version = "3.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "sync_wrapper"
version = "1.0.2"
@@ -3785,7 +3936,7 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -3854,7 +4005,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -3865,7 +4016,7 @@ checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -3956,7 +4107,7 @@ checksum = "385a6cb71ab9ab790c5fe8d67f1645e6c450a7ce006a33de03daa956cf70a496"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -4001,7 +4152,7 @@ checksum = "ac2a5518c70fa84342385732db33fb3f44bc4cc748936eb5833d2df34d6445ef"
dependencies = [
"async-trait",
"axum",
"base64",
"base64 0.22.1",
"bytes",
"h2",
"http",
@@ -4102,7 +4253,7 @@ checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -4231,7 +4382,7 @@ version = "3.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dea7109cdcd5864d4eeb1b58a1648dc9bf520360d7af16ec26d0a9354bafcfc0"
dependencies = [
"base64",
"base64 0.22.1",
"log",
"percent-encoding",
"rustls",
@@ -4247,7 +4398,7 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e994ba84b0bd1b1b0cf92878b7ef898a5c1760108fe7b6010327e274917a808c"
dependencies = [
"base64",
"base64 0.22.1",
"http",
"httparse",
"log",
@@ -4384,7 +4535,7 @@ dependencies = [
"bumpalo",
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
"wasm-bindgen-shared",
]
@@ -4476,7 +4627,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -4506,7 +4657,7 @@ checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -4517,7 +4668,7 @@ checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -4711,7 +4862,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156"
dependencies = [
"libc",
"rustix 1.1.4",
"rustix 1.1.6",
]
[[package]]
@@ -4733,7 +4884,7 @@ checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
"synstructure",
]
@@ -4754,7 +4905,7 @@ checksum = "4714fd92cf900833d49538023a9b3915155210801d1c1169eba513b2addefd71"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
@@ -4774,7 +4925,7 @@ checksum = "11532158c46691caf0f2593ea8358fed6bbf68a0315e80aae9bd41fbade684a1"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
"synstructure",
]
@@ -4814,7 +4965,7 @@ checksum = "625dc425cab0dca6dc3c3319506e6593dcb08a9f387ea3b284dbd52a92c40555"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.118",
]
[[package]]
+99 -87
View File
@@ -2,6 +2,10 @@
mod admin_auth_command;
#[path = "crank_migrate/db_connect.rs"]
mod db_connect;
#[path = "crank_migrate/reporting.rs"]
mod reporting;
use reporting::{active_rotation, ensure_no_options, read_required_key_file, rotation_json};
use crank_config::{ConfigSource, parse_migrator};
use crank_registry::{
@@ -11,7 +15,7 @@ use crank_registry::{
};
use crank_runtime::SecretCrypto;
use serde_json::json;
use std::{path::Path, process::ExitCode};
use std::process::ExitCode;
use time::OffsetDateTime;
const MASTER_KEY_ROTATION_PAGE_SIZE: i64 = 1_000;
#[tokio::main]
@@ -765,6 +769,59 @@ async fn process_rotation_batch(
processed += 1;
}
}
let mut after_task_id: Option<String> = None;
loop {
let tasks = registry
.list_tasks_for_master_key_epoch_page(
rotation.source_epoch,
after_task_id.as_deref(),
MASTER_KEY_ROTATION_PAGE_SIZE,
)
.await
.map_err(CliError::from_registry)?;
if tasks.is_empty() {
break;
}
for task in tasks {
after_task_id = Some(task.task_id.clone());
if task.target_epoch == Some(rotation.target_epoch) {
continue;
}
if max_versions.is_some_and(|limit| processed as usize >= limit) {
return Ok(processed);
}
let plaintext = current_crypto
.decrypt_for_epoch(&task.key_version, task.epoch, &task.ciphertext)
.map_err(|_| {
CliError::new(
"master_key_rotation_verification_failed",
"master_key.rotation",
"rerun_rotation_or_abort",
)
})?;
let target_ciphertext = target_crypto.encrypt(&plaintext).map_err(|_| {
CliError::new(
"master_key_rotation_verification_failed",
"master_key.rotation",
"rerun_rotation_or_abort",
)
})?;
let now = OffsetDateTime::now_utc();
registry
.stage_master_key_rotation_task_ciphertext(
&rotation.id,
&task.task_id,
rotation.source_epoch,
&target_ciphertext,
target_crypto.key_version(),
rotation.target_epoch,
&now,
)
.await
.map_err(CliError::from_registry)?;
processed += 1;
}
}
Ok(processed)
}
@@ -818,6 +875,47 @@ async fn verify_staged_targets(
verified += 1;
}
}
let mut after_task_id: Option<String> = None;
loop {
let tasks = registry
.list_target_tasks_for_master_key_rotation_page(
rotation.target_epoch,
after_task_id.as_deref(),
MASTER_KEY_ROTATION_PAGE_SIZE,
)
.await
.map_err(CliError::from_registry)?;
if tasks.is_empty() {
break;
}
for task in tasks {
after_task_id = Some(task.task_id.clone());
let ciphertext = task.target_ciphertext.as_deref().ok_or_else(|| {
CliError::new(
"master_key_rotation_verification_failed",
"master_key.rotation",
"rerun_rotation_or_abort",
)
})?;
let key_version = task.target_key_version.as_deref().ok_or_else(|| {
CliError::new(
"master_key_rotation_verification_failed",
"master_key.rotation",
"rerun_rotation_or_abort",
)
})?;
target_crypto
.decrypt_for_epoch(key_version, rotation.target_epoch, ciphertext)
.map_err(|_| {
CliError::new(
"master_key_rotation_verification_failed",
"master_key.rotation",
"rerun_rotation_or_abort",
)
})?;
verified += 1;
}
}
if verified != rotation.total_secret_versions {
return Err(CliError::new(
"master_key_rotation_verification_failed",
@@ -877,89 +975,3 @@ fn decrypt_current(
)
})
}
async fn active_rotation(
registry: &PostgresRegistry,
allowed_states: &[&str],
) -> Result<Option<MasterKeyRotationRecord>, CliError> {
let status = registry
.master_key_rotation_status()
.await
.map_err(CliError::from_registry)?;
Ok(status
.rotations
.into_iter()
.find(|rotation| allowed_states.contains(&rotation.state.as_str())))
}
fn rotation_json(rotation: &MasterKeyRotationRecord) -> serde_json::Value {
json!({
"id": rotation.id,
"source_epoch": rotation.source_epoch,
"target_epoch": rotation.target_epoch,
"state": rotation.state,
"backup_ref": rotation.backup_ref.as_ref().map(|_| "configured"),
"checkpoint_secret_id": rotation.checkpoint_secret_id,
"total_secret_versions": rotation.total_secret_versions,
"processed_secret_versions": rotation.processed_secret_versions,
"verified_secret_versions": rotation.verified_secret_versions,
"failure_code": rotation.failure_code,
})
}
fn read_required_key_file(path: Option<&str>) -> Result<String, CliError> {
let path = path.ok_or_else(|| {
CliError::new("invalid_command", "cli.arguments", "run_master_key_status")
})?;
if path.len() > 512 || path.bytes().any(|byte| byte.is_ascii_control()) {
return Err(CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
));
}
let metadata = std::fs::metadata(Path::new(path)).map_err(|_| {
CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
)
})?;
if !metadata.is_file() || metadata.len() > 16_384 {
return Err(CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
));
}
let value = std::fs::read_to_string(Path::new(path)).map_err(|_| {
CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
)
})?;
if value.trim().is_empty() {
return Err(CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
));
}
Ok(value)
}
fn ensure_no_options(options: &MasterKeyOptions) -> Result<(), CliError> {
if options.current_key_file.is_some()
|| options.target_key_file.is_some()
|| options.backup_ref.is_some()
|| options.rotation_id.is_some()
|| options.max_versions.is_some()
{
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_master_key_status",
));
}
Ok(())
}
@@ -0,0 +1,98 @@
//! Leaf/reporting helpers of the migrator CLI.
//!
//! Extracted from `crank-migrate.rs` without behaviour changes so the binary entry stays within
//! the code-health line limit.
use std::path::Path;
use crank_registry::{MasterKeyRotationRecord, PostgresRegistry};
use serde_json::json;
use super::{CliError, MasterKeyOptions};
pub(super) async fn active_rotation(
registry: &PostgresRegistry,
allowed_states: &[&str],
) -> Result<Option<MasterKeyRotationRecord>, CliError> {
let status = registry
.master_key_rotation_status()
.await
.map_err(CliError::from_registry)?;
Ok(status
.rotations
.into_iter()
.find(|rotation| allowed_states.contains(&rotation.state.as_str())))
}
pub(super) fn rotation_json(rotation: &MasterKeyRotationRecord) -> serde_json::Value {
json!({
"id": rotation.id,
"source_epoch": rotation.source_epoch,
"target_epoch": rotation.target_epoch,
"state": rotation.state,
"backup_ref": rotation.backup_ref.as_ref().map(|_| "configured"),
"checkpoint_secret_id": rotation.checkpoint_secret_id,
"total_secret_versions": rotation.total_secret_versions,
"processed_secret_versions": rotation.processed_secret_versions,
"verified_secret_versions": rotation.verified_secret_versions,
"failure_code": rotation.failure_code,
})
}
pub(super) fn read_required_key_file(path: Option<&str>) -> Result<String, CliError> {
let path = path.ok_or_else(|| {
CliError::new("invalid_command", "cli.arguments", "run_master_key_status")
})?;
if path.len() > 512 || path.bytes().any(|byte| byte.is_ascii_control()) {
return Err(CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
));
}
let metadata = std::fs::metadata(Path::new(path)).map_err(|_| {
CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
)
})?;
if !metadata.is_file() || metadata.len() > 16_384 {
return Err(CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
));
}
let value = std::fs::read_to_string(Path::new(path)).map_err(|_| {
CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
)
})?;
if value.trim().is_empty() {
return Err(CliError::new(
"master_key_input_invalid",
"master_key.input",
"verify_operator_input",
));
}
Ok(value)
}
pub(super) fn ensure_no_options(options: &MasterKeyOptions) -> Result<(), CliError> {
if options.current_key_file.is_some()
|| options.target_key_file.is_some()
|| options.backup_ref.is_some()
|| options.rotation_id.is_some()
|| options.max_versions.is_some()
{
return Err(CliError::new(
"invalid_command",
"cli.arguments",
"run_master_key_status",
));
}
Ok(())
}
+1
View File
@@ -181,6 +181,7 @@ mod tests {
idempotency: None,
safety: None,
approval_policy: None,
execution_policy: None,
auth_profile_ref: None,
headers: BTreeMap::new(),
},
+18 -1
View File
@@ -3,7 +3,7 @@ use std::{io, net::SocketAddr, process::ExitCode, time::Duration};
use admin_api::{
app::build_app,
auth::{AuthSettings, BootstrapAdminConfig},
pool_metrics::spawn_postgres_pool_metrics,
pool_metrics::{spawn_admission_metrics, spawn_postgres_pool_metrics},
reconciliation::{open_reconciliation_store, spawn_artifact_reconciliation},
service::AdminServiceBuilder,
state::AppState,
@@ -169,6 +169,7 @@ async fn run(
};
if metrics_enabled {
spawn_postgres_pool_metrics(registry.pool().clone());
spawn_admission_metrics(registry.pool().clone());
}
let base_url = config
.runtime
@@ -214,6 +215,16 @@ async fn run(
.with_limits(runtime_limits)
.with_response_cache(cache_stores.response.clone())
.with_coordination_store(cache_stores.coordination.clone())
.with_admission(std::sync::Arc::new(
crank_registry::PostgresExecutionAdmission::new(
registry.pool().clone(),
crank_registry::AdmissionLimits {
global_limit: runtime_limits.max_concurrent_unary,
..crank_registry::AdmissionLimits::default()
},
crank_core::AdmissionSchedulerConfig::default(),
)?,
))
.build();
let identity_provider =
PasswordIdentityProvider::new(registry.clone(), auth_settings.password_pepper.clone());
@@ -321,6 +332,12 @@ fn preflight_config(config: &AdminProcessConfig) -> Result<(), crank_config::Con
config.runtime.max_concurrent_sessions,
)
.map_err(|_| invalid("runtime.limits"))?;
crank_registry::AdmissionLimits {
global_limit: config.runtime.max_concurrent_unary,
..crank_registry::AdmissionLimits::default()
}
.validate()
.map_err(|_| invalid("runtime.admission"))?;
runtime_cache_config(config).map_err(|_| invalid("cache"))?;
RequestRateLimitConfig::new(
config.rate_limit.requests_per_second,
+14
View File
@@ -11,3 +11,17 @@ pub fn spawn_postgres_pool_metrics(pool: PgPool) -> tokio::task::JoinHandle<()>
}
})
}
/// Story 4.1: samples the authoritative admission queue so the oldest-waiting age grows (and returns
/// to zero) without waiting for a new request.
pub fn spawn_admission_metrics(pool: PgPool) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(5));
loop {
interval.tick().await;
if let Ok(snapshot) = crank_registry::admission_snapshot(&pool).await {
crank_observability::record_admission_oldest_waiting(snapshot.oldest_waiting_ms);
}
}
})
}
+27 -2
View File
@@ -37,13 +37,22 @@ pub async fn readiness(State(state): State<AppState>) -> impl IntoResponse {
} else {
"not_ready"
};
let execution_admission = if checks.execution_admission {
"ready"
} else {
"not_ready"
};
if checks.is_ready() {
(
StatusCode::OK,
Json(json!({
"service": "admin-api",
"status": "ready",
"checks": { "postgres": postgres, "artifact_storage": artifact_storage }
"checks": {
"postgres": postgres,
"artifact_storage": artifact_storage,
"execution_admission": execution_admission
}
})),
)
} else {
@@ -52,7 +61,11 @@ pub async fn readiness(State(state): State<AppState>) -> impl IntoResponse {
Json(json!({
"service": "admin-api",
"status": "not_ready",
"checks": { "postgres": postgres, "artifact_storage": artifact_storage }
"checks": {
"postgres": postgres,
"artifact_storage": artifact_storage,
"execution_admission": execution_admission
}
})),
)
}
@@ -67,6 +80,7 @@ mod tests {
let checks = ReadinessChecks {
postgres: false,
artifact_storage: true,
execution_admission: true,
};
assert!(!checks.is_ready());
assert_eq!(
@@ -92,6 +106,7 @@ mod tests {
let checks = ReadinessChecks {
postgres: true,
artifact_storage: false,
execution_admission: true,
};
assert!(!checks.is_ready());
assert_eq!(
@@ -111,4 +126,14 @@ mod tests {
"not_ready"
);
}
#[test]
fn readiness_checks_identify_an_admission_state_failure() {
let checks = ReadinessChecks {
postgres: true,
artifact_storage: true,
execution_admission: false,
};
assert!(!checks.is_ready());
}
}
+9 -3
View File
@@ -55,8 +55,8 @@ use crate::{
storage::LocalArtifactStorage,
};
use operation_validation::{
validate_approval_policy, validate_execution_timeout, validate_idempotency_policy,
validate_protocol_target, validate_response_cache_policy,
validate_approval_policy, validate_execution_policy, validate_execution_timeout,
validate_idempotency_policy, validate_protocol_target, validate_response_cache_policy,
};
#[derive(Clone)]
@@ -104,11 +104,12 @@ pub use crate::dto::*;
pub struct ReadinessChecks {
pub postgres: bool,
pub artifact_storage: bool,
pub execution_admission: bool,
}
impl ReadinessChecks {
pub const fn is_ready(self) -> bool {
self.postgres && self.artifact_storage
self.postgres && self.artifact_storage && self.execution_admission
}
}
@@ -150,6 +151,8 @@ pub(super) struct CredentialAuditRecord<'a> {
impl AdminService {
pub async fn readiness(&self) -> ReadinessChecks {
let postgres = self.registry.ping().await.is_ok();
// Story 4.1: the mandatory admission state is a readiness dependency of every execution.
let execution_admission = self.registry.execution_admission_health().await.is_ok();
let store = Arc::clone(&self.artifact_store);
let artifact_storage = tokio::task::spawn_blocking(move || store.check_health())
.await
@@ -157,6 +160,7 @@ impl AdminService {
ReadinessChecks {
postgres,
artifact_storage,
execution_admission,
}
}
@@ -725,6 +729,7 @@ impl AdminService {
validate_response_cache_policy(&payload.target, &payload.execution_config)?;
validate_idempotency_policy(&payload.target, &payload.execution_config)?;
validate_approval_policy(&payload.execution_config)?;
validate_execution_policy(&payload.execution_config)?;
payload.input_mapping.validate_paths()?;
payload.output_mapping.validate_paths()?;
Ok(())
@@ -738,6 +743,7 @@ impl AdminService {
validate_response_cache_policy(&operation.target, &operation.execution_config)?;
validate_idempotency_policy(&operation.target, &operation.execution_config)?;
validate_approval_policy(&operation.execution_config)?;
validate_execution_policy(&operation.execution_config)?;
operation.input_mapping.validate_paths()?;
operation.output_mapping.validate_paths()?;
Ok(())
+1
View File
@@ -441,6 +441,7 @@ fn demo_rest_operation_payload() -> OperationPayload {
idempotency: None,
safety: None,
approval_policy: None,
execution_policy: None,
auth_profile_ref: None,
headers: BTreeMap::new(),
},
+1
View File
@@ -635,6 +635,7 @@ impl AdminService {
idempotency: None,
safety: None,
approval_policy: None,
execution_policy: None,
auth_profile_ref: None,
headers: BTreeMap::new(),
},
@@ -610,6 +610,7 @@ impl AdminService {
idempotency: None,
safety: None,
approval_policy: None,
execution_policy: None,
auth_profile_ref,
headers: BTreeMap::new(),
},
@@ -160,6 +160,23 @@ pub(super) fn validate_approval_policy(
Ok(())
}
pub(super) fn validate_execution_policy(
execution_config: &crank_core::ExecutionConfig,
) -> Result<(), ApiError> {
let Some(policy) = execution_config.execution_policy.as_ref() else {
return Ok(());
};
if let Err(error) = policy.validate() {
return Err(ApiError::validation_with_context(
error.to_string(),
json!({
"field": "execution_config.execution_policy.deadline_ms",
}),
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
@@ -171,8 +188,8 @@ mod tests {
};
use super::{
validate_approval_policy, validate_execution_timeout, validate_idempotency_policy,
validate_response_cache_policy,
validate_approval_policy, validate_execution_policy, validate_execution_timeout,
validate_idempotency_policy, validate_response_cache_policy,
};
fn cacheable_execution_config() -> ExecutionConfig {
@@ -183,6 +200,7 @@ mod tests {
idempotency: None,
safety: None,
approval_policy: None,
execution_policy: None,
auth_profile_ref: None,
headers: BTreeMap::new(),
}
@@ -201,6 +219,7 @@ mod tests {
}),
safety: None,
approval_policy: None,
execution_policy: None,
auth_profile_ref: None,
headers: BTreeMap::new(),
}
@@ -365,4 +384,36 @@ mod tests {
"approval ttl must be between 1 and 300 seconds"
);
}
#[test]
fn accepts_published_execution_policy_and_absent_legacy_policy() {
let mut config = cacheable_execution_config();
validate_execution_policy(&config).unwrap();
config.execution_policy = Some(crank_core::ToolExecutionPolicy {
mode: crank_core::ToolExecutionMode::Adaptive,
deadline_ms: 120_000,
no_tasks_fallback: crank_core::NoTasksFallback::Unsupported,
});
validate_execution_policy(&config).unwrap();
}
#[test]
fn rejects_zero_and_overflowing_execution_policy_deadline() {
let mut config = cacheable_execution_config();
config.execution_policy = Some(crank_core::ToolExecutionPolicy {
mode: crank_core::ToolExecutionMode::Synchronous,
deadline_ms: 0,
no_tasks_fallback: crank_core::NoTasksFallback::Synchronous,
});
let error = validate_execution_policy(&config).unwrap_err();
assert!(matches!(error, crate::error::ApiError::Validation { .. }));
config.execution_policy = Some(crank_core::ToolExecutionPolicy {
mode: crank_core::ToolExecutionMode::Background,
deadline_ms: crank_core::MAX_TOOL_DEADLINE_MS + 1,
no_tasks_fallback: crank_core::NoTasksFallback::Synchronous,
});
assert!(validate_execution_policy(&config).is_err());
}
}
@@ -495,6 +495,7 @@ pub(super) fn test_operation_payload(base_url: &str, name: &str) -> OperationPay
idempotency: None,
safety: None,
approval_policy: None,
execution_policy: None,
auth_profile_ref: None,
headers: BTreeMap::new(),
},
+2 -1
View File
@@ -29,7 +29,7 @@ sha2.workspace = true
sqlx.workspace = true
thiserror.workspace = true
time.workspace = true
tokio = { workspace = true, features = ["sync"] }
tokio = { workspace = true, features = ["sync", "signal"] }
tracing.workspace = true
tracing-subscriber.workspace = true
uuid.workspace = true
@@ -45,5 +45,6 @@ opentelemetry_sdk.workspace = true
prost.workspace = true
reqwest.workspace = true
tower.workspace = true
tokio = { workspace = true, features = ["net", "time"] }
tracing-opentelemetry.workspace = true
metrics-util = "0.20.4"
+193 -11
View File
@@ -1,7 +1,7 @@
use std::{io, process::ExitCode, time::Duration};
use crank_community_mcp::{
auth::CommunityMachineCredentialVerifier, build_app_with_artifacts,
auth::CommunityMachineCredentialVerifier, build_app_with_artifacts_and_tasks,
session::PostgresTransportSessionStore,
};
use crank_config::{
@@ -14,13 +14,14 @@ use crank_observability::{
OtlpTraceConfig, RedactionLimits, SentryConfig, ServiceIdentity, capture_critical_error,
};
use crank_registry::{
MASTER_KEY_CIPHER_CONTRACT, MasterKeyIdentityCandidate, PostgresPoolConfig, PostgresRegistry,
AdmissionLimits, MASTER_KEY_CIPHER_CONTRACT, MasterKeyIdentityCandidate,
PostgresExecutionAdmission, PostgresPoolConfig, PostgresRegistry, PostgresTaskStore,
};
use crank_runtime::{
RequestRateLimitConfig, RequestRateLimiter, RuntimeCacheConfig, RuntimeCacheStores,
RuntimeLimits, SecretCrypto,
};
use mcp_server::pool_metrics::spawn_postgres_pool_metrics;
use mcp_server::pool_metrics::{spawn_admission_metrics, spawn_postgres_pool_metrics};
use sqlx::postgres::PgConnectOptions;
use tokio::net::TcpListener;
use tracing::info;
@@ -175,6 +176,7 @@ async fn run(
};
if metrics_enabled {
spawn_postgres_pool_metrics(registry.pool().clone());
spawn_admission_metrics(registry.pool().clone());
}
let session_store = PostgresTransportSessionStore::from_pool(registry.pool().clone()).await?;
let secret_crypto =
@@ -190,12 +192,55 @@ async fn run(
.with_limits(runtime_limits)
.with_response_cache(cache_stores.response.clone())
.with_coordination_store(cache_stores.coordination.clone())
.with_admission(execution_admission(
registry.pool().clone(),
runtime_limits,
)?)
.build();
// Published static Resources are read from the same immutable content-addressed root the
// admin-api publishes into. The compatibility constructor keeps an unprovisioned/unsafe root
// fail-closed per read instead of preventing startup; nothing is ever written from this process.
let artifact_store = crank_artifacts::ArtifactStore::new(config.storage_root.clone());
let app = build_app_with_artifacts(
// Story 4.2/4.3: the durable Task application entry shares the registry pool, the published
// catalog refresh interval and the exact admission limits of the runtime; the public wire stays
// synchronous/unsupported while a fenced in-process worker executes accepted Tasks.
let task_store = std::sync::Arc::new(PostgresTaskStore::new(
registry.pool().clone(),
admission_limits(runtime_limits),
crank_core::AdmissionSchedulerConfig::default(),
)?);
let tasks = std::sync::Arc::new(crank_community_mcp::TaskApplication::new(
registry.clone(),
Duration::from_millis(config.refresh_ms),
cache_stores.coordination.clone(),
crank_runtime::TaskService::with_secret_crypto(
task_store.clone(),
std::sync::Arc::new(secret_crypto.clone()),
),
));
// Story 4.3: the durable Task worker runs in this process over the same PostgreSQL store,
// published catalog, executor and encrypted-input authority as the accepting application.
let task_handler =
std::sync::Arc::new(crank_community_mcp::TaskWorkerHandler::from_application(
&tasks,
registry.clone(),
runtime.clone(),
secret_crypto.clone(),
));
let task_worker = std::sync::Arc::new(crank_runtime::TaskWorker::new(
task_store,
task_handler,
crank_runtime::TaskWorkerConfig {
owner: format!("mcp-server-{}", std::process::id()),
..crank_runtime::TaskWorkerConfig::default()
},
)?);
let (task_shutdown_tx, task_shutdown_rx) = tokio::sync::watch::channel(false);
let task_worker_handle = tokio::spawn({
let worker = task_worker.clone();
async move { worker.run_until_shutdown(task_shutdown_rx).await }
});
let app = build_app_with_artifacts_and_tasks(
registry,
Duration::from_millis(config.refresh_ms),
config.runtime.base_url.clone(),
@@ -211,6 +256,7 @@ async fn run(
std::sync::Arc::new(CommunityMachineCredentialVerifier),
runtime_limits.max_concurrent_sessions,
artifact_store,
tasks,
);
let listener = TcpListener::bind(config.bind_addr).await?;
@@ -231,15 +277,77 @@ async fn run(
info!(name: "mcp.server.listening", bind_address = %config.bind_addr, "mcp-server listening");
*startup_completed = true;
if let Some(metrics_server) = metrics_server {
tokio::select! {
result = axum::serve(listener, app) => result?,
result = metrics_server.serve() => result?,
let serve_result: Result<(), Box<dyn std::error::Error>> = async {
if let Some(metrics_server) = metrics_server {
tokio::select! {
result = serve_with_worker_shutdown(app, listener, shutdown_signal(), task_shutdown_tx.clone()) => result?,
result = metrics_server.serve() => result?,
}
} else {
serve_with_worker_shutdown(app, listener, shutdown_signal(), task_shutdown_tx.clone())
.await?;
}
} else {
axum::serve(listener, app).await?;
Ok(())
}
.await;
// Stop new claims; active attempts checkpoint/drain or are cancelled inside the bounded worker
// shutdown timeout (<=30s), and queued PostgreSQL work is left for recovery.
task_shutdown_tx.send(true).ok();
if tokio::time::timeout(Duration::from_secs(30), task_worker_handle)
.await
.is_err()
{
tracing::warn!(
name: "mcp.task_worker.shutdown_timeout",
"durable Task worker did not stop within the story shutdown budget"
);
}
serve_result
}
/// Gracefully stop the durable Task worker as soon as shutdown is requested, while still
/// draining in-flight HTTP connections before returning.
async fn serve_with_worker_shutdown<S>(
app: axum::Router,
listener: tokio::net::TcpListener,
signal: S,
worker_stop: tokio::sync::watch::Sender<bool>,
) -> std::io::Result<()>
where
S: std::future::Future<Output = ()> + Send + 'static,
{
let worker_stop_graceful = worker_stop.clone();
let graceful = async move {
signal.await;
let _ = worker_stop_graceful.send(true);
};
let result = axum::serve(listener, app)
.with_graceful_shutdown(graceful)
.await;
let _ = worker_stop.send(true);
result
}
/// Process shutdown trigger for the HTTP surface and the durable Task worker.
async fn shutdown_signal() {
let ctrl_c = async {
let _ = tokio::signal::ctrl_c().await;
};
#[cfg(unix)]
let terminate = async {
if let Ok(mut signal) =
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
{
signal.recv().await;
}
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {}
_ = terminate => {}
}
Ok(())
}
fn init_observability(
@@ -287,6 +395,25 @@ fn init_observability(
)?)
}
fn admission_limits(runtime_limits: RuntimeLimits) -> AdmissionLimits {
AdmissionLimits {
global_limit: runtime_limits.max_concurrent_unary,
..AdmissionLimits::default()
}
}
fn execution_admission(
pool: sqlx::PgPool,
runtime_limits: RuntimeLimits,
) -> Result<std::sync::Arc<dyn crank_core::ExecutionAdmission>, Box<dyn std::error::Error>> {
let store = PostgresExecutionAdmission::new(
pool,
admission_limits(runtime_limits),
crank_core::AdmissionSchedulerConfig::default(),
)?;
Ok(std::sync::Arc::new(store))
}
fn preflight_config(config: &McpProcessConfig) -> Result<(), crank_config::ConfigError> {
let invalid = |field| crank_config::ConfigError::single(DiagnosticCode::InvalidType, field);
database_options(&config.database).map_err(|_| invalid("database.source"))?;
@@ -307,6 +434,12 @@ fn preflight_config(config: &McpProcessConfig) -> Result<(), crank_config::Confi
config.runtime.max_concurrent_sessions,
)
.map_err(|_| invalid("runtime.limits"))?;
crank_registry::AdmissionLimits {
global_limit: config.runtime.max_concurrent_unary,
..crank_registry::AdmissionLimits::default()
}
.validate()
.map_err(|_| invalid("runtime.admission"))?;
runtime_cache_config(config).map_err(|_| invalid("cache"))?;
RequestRateLimitConfig::new(
config.rate_limit.requests_per_second,
@@ -460,3 +593,52 @@ fn runtime_cache_config(
.map(|value| value.expose_secret().to_owned()),
)
}
#[cfg(test)]
mod tests {
use super::*;
use axum::{Router, routing::get};
use std::time::Duration;
use tokio::sync::{oneshot, watch};
async fn slow_handler() -> &'static str {
tokio::time::sleep(Duration::from_millis(600)).await;
"ok"
}
#[tokio::test]
async fn worker_stops_before_http_drain_completes() {
let app = Router::new().route("/slow", get(slow_handler));
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let (stop_tx, mut stop_rx) = watch::channel(false);
let (signal_tx, signal_rx) = oneshot::channel::<()>();
let handle = tokio::spawn(serve_with_worker_shutdown(
app,
listener,
async {
let _ = signal_rx.await;
},
stop_tx,
));
let client = reqwest::Client::new();
let request = client.get(format!("http://{}/slow", addr));
let in_flight = tokio::spawn(async move { request.send().await });
tokio::time::sleep(Duration::from_millis(150)).await;
let _ = signal_tx.send(());
tokio::time::timeout(Duration::from_millis(250), stop_rx.changed())
.await
.expect("worker stop signal should arrive before HTTP drain")
.expect("watch changed");
assert!(*stop_rx.borrow());
let response = in_flight.await.unwrap().unwrap();
assert!(response.status().is_success());
let serve_result = handle.await.unwrap();
assert!(serve_result.is_ok());
}
}
+14
View File
@@ -11,3 +11,17 @@ pub fn spawn_postgres_pool_metrics(pool: PgPool) -> tokio::task::JoinHandle<()>
}
})
}
/// Story 4.1: samples the authoritative admission queue so the oldest-waiting age grows (and returns
/// to zero) without waiting for a new request.
pub fn spawn_admission_metrics(pool: PgPool) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(5));
loop {
interval.tick().await;
if let Ok(snapshot) = crank_registry::admission_snapshot(&pool).await {
crank_observability::record_admission_oldest_waiting(snapshot.oldest_waiting_ms);
}
}
})
}
+874
View File
@@ -0,0 +1,874 @@
//! ATDD Story 4.1 (FR-21/FR-24): the runtime asks one authoritative store for admission.
//!
//! Real PostgreSQL state, two independent `RuntimeExecutor` instances (the multi-process shape) and a
//! counted adapter: one shared global budget, typed overload with retry guidance, typed unsupported
//! fallback, deadline that includes queue waiting and zero outbound calls on every refusal.
//!
//! Lives in the `mcp-server` app crate because the runtime must not depend on the registry crate; this
//! app already wires both and this is the real deployment shape.
use std::{
collections::BTreeMap,
sync::{
Arc, Mutex,
atomic::{AtomicUsize, Ordering},
},
time::{Duration, Instant},
};
use async_trait::async_trait;
use crank_core::{
AdapterResponse, AdmissionMode, AdmissionRequest, AdmissionScope, ApprovalEvidence,
ConfirmationPolicy, ExecutionAdmission, ExecutionConfig, ExecutionErrorCode, ExecutionMode,
ExecutionOrigin, ExecutionStage, HttpMethod, InvocationSource, NoTasksFallback, Operation,
OperationId, OperationSafetyClass, OperationSafetyPolicy, OperationSecurityLevel,
OperationStatus, OutcomeCertainty, Protocol, ProtocolAdapter, RestTarget,
RuntimeRequestContext, Target, ToolDescription, ToolExecutionMode, ToolExecutionPolicy,
WorkspaceId,
};
use crank_mapping::MappingSet;
use crank_registry::{AdmissionLimits, PostgresExecutionAdmission};
use crank_runtime::{RuntimeExecutor, RuntimeExecutorBuilder, RuntimeLimits};
use crank_schema::{Schema, SchemaKind};
use serde_json::json;
use sqlx::PgPool;
use time::OffsetDateTime;
struct CountingAdapter {
calls: Arc<AtomicUsize>,
current: Arc<AtomicUsize>,
peak: Arc<AtomicUsize>,
}
#[async_trait]
impl ProtocolAdapter for CountingAdapter {
fn protocol(&self) -> Protocol {
Protocol::Rest
}
fn supports_mode(&self, mode: ExecutionMode) -> bool {
mode == ExecutionMode::Unary
}
async fn invoke_unary(
&self,
_target: &Target,
_prepared: &crank_core::PreparedRequest,
context: &RuntimeRequestContext,
) -> Result<AdapterResponse, crank_core::ProtocolAdapterError> {
context.mark_dispatch_started();
self.calls.fetch_add(1, Ordering::SeqCst);
let current = self.current.fetch_add(1, Ordering::SeqCst) + 1;
self.peak.fetch_max(current, Ordering::SeqCst);
tokio::time::sleep(Duration::from_millis(150)).await;
self.current.fetch_sub(1, Ordering::SeqCst);
Ok(AdapterResponse {
status_code: 200,
headers: BTreeMap::new(),
body: json!({"ok": true}),
data: json!({"ok": true}),
})
}
}
fn schema(kind: SchemaKind) -> Schema {
Schema {
kind,
format: None,
description: None,
required: false,
nullable: false,
default_value: None,
fields: BTreeMap::new(),
items: None,
enum_values: Vec::new(),
variants: Vec::new(),
}
}
fn operation(policy: Option<ToolExecutionPolicy>) -> Operation<Schema, MappingSet> {
Operation {
id: OperationId::new("op_admission_budget"),
name: "admission_budget".to_owned(),
display_name: "Admission budget".to_owned(),
category: "general".to_owned(),
protocol: Protocol::Rest,
security_level: OperationSecurityLevel::Standard,
status: OperationStatus::Published,
version: 7,
target: Target::Rest(RestTarget {
base_url: "https://api.example.com".to_owned(),
method: HttpMethod::Get,
path_template: "/v1/items".to_owned(),
static_headers: BTreeMap::new(),
request_contract: None,
response_contract: Vec::new(),
multipart_field_mime_types: BTreeMap::new(),
}),
input_schema: schema(SchemaKind::Object),
output_schema: schema(SchemaKind::Object),
input_mapping: MappingSet::default(),
output_mapping: MappingSet::default(),
execution_config: ExecutionConfig {
timeout_ms: 5_000,
execution_policy: policy,
..ExecutionConfig::default()
},
tool_description: ToolDescription {
title: "Admission".to_owned(),
description: "Admission budget probe".to_owned(),
tags: Vec::new(),
examples: Vec::new(),
},
samples: None,
generated_draft: None,
config_export: None,
wizard_state: None,
created_at: OffsetDateTime::now_utc(),
updated_at: OffsetDateTime::now_utc(),
published_at: None,
}
}
async fn execute(
executor: &RuntimeExecutor,
operation: &crank_runtime::RuntimeOperation,
input: &serde_json::Value,
) -> Result<serde_json::Value, crank_core::ExecutionFailure> {
let workspace = WorkspaceId::new("ws_test");
let context = crank_runtime::RuntimeRequestContext::from_correlation(
&crank_core::CorrelationContext::generate(),
)
.with_metering_context(workspace.clone(), None, InvocationSource::AdminTestRun);
execute_in(executor, operation, input, &context).await
}
async fn execute_in(
executor: &RuntimeExecutor,
operation: &crank_runtime::RuntimeOperation,
input: &serde_json::Value,
context: &crank_runtime::RuntimeRequestContext,
) -> Result<serde_json::Value, crank_core::ExecutionFailure> {
let workspace = context
.metering_context()
.map(|metering| metering.workspace_id.clone())
.unwrap_or_else(|| WorkspaceId::new("ws_test"));
let request = crank_runtime::RuntimeExecutionRequest::try_new(
&workspace,
ExecutionOrigin::AdminDraft,
None,
operation,
input,
crank_runtime::ExecutionAuthorization::Authorized,
None,
context,
Instant::now() + Duration::from_secs(30),
)
.map_err(|_| {
crank_core::ExecutionFailure::new(
ExecutionErrorCode::RuntimeInternal,
crank_core::CorrelationContext::new(
context.request_id.clone(),
context.trace_context.clone(),
),
)
})?;
executor
.execute_outcome(request)
.await
.map(|success| success.output)
}
async fn schema_pool(schema_url: &str) -> PgPool {
let pool = PgPool::connect(schema_url)
.await
.expect("admission schema pool");
crank_registry::MigrationAuthority::apply(&pool)
.await
.expect("migration authority applies the admission schema");
pool
}
fn admission_store(pool: PgPool, global: usize, queue: usize) -> Arc<PostgresExecutionAdmission> {
Arc::new(
PostgresExecutionAdmission::new(
pool,
AdmissionLimits {
global_limit: global,
agent_limit: 8,
tool_limit: 8,
queue_limit: queue,
},
crank_core::AdmissionSchedulerConfig::default(),
)
.expect("valid admission limits"),
)
}
fn executor(
store: Arc<PostgresExecutionAdmission>,
calls: Arc<AtomicUsize>,
current: Arc<AtomicUsize>,
peak: Arc<AtomicUsize>,
) -> RuntimeExecutor {
RuntimeExecutorBuilder::new()
.register_adapter(Arc::new(CountingAdapter {
calls,
current,
peak,
}))
.with_limits(RuntimeLimits::try_new(8, 2).expect("local limits"))
.with_admission(store)
.build()
}
#[tokio::test]
async fn two_executors_share_one_global_budget() {
let schema_url = crank_test_support::postgres_schema_url("admission_budget").await;
let pool = schema_pool(&schema_url).await;
let store = admission_store(pool.clone(), 2, 8);
let calls = Arc::new(AtomicUsize::new(0));
let current = Arc::new(AtomicUsize::new(0));
let peak = Arc::new(AtomicUsize::new(0));
let first = executor(
Arc::clone(&store),
Arc::clone(&calls),
Arc::clone(&current),
Arc::clone(&peak),
);
let second = executor(
Arc::clone(&store),
Arc::clone(&calls),
Arc::clone(&current),
Arc::clone(&peak),
);
let operation: crank_runtime::RuntimeOperation = operation(None).into();
let mut tasks = Vec::new();
for index in 0..4 {
let executor = if index % 2 == 0 {
first.clone()
} else {
second.clone()
};
let operation = operation.clone();
tasks.push(tokio::spawn(async move {
execute(&executor, &operation, &json!({})).await
}));
}
let mut completed = 0;
for task in tasks {
match task.await.expect("execution task") {
Ok(_) => completed += 1,
Err(failure) => panic!("admission must grant every request: {failure:?}"),
}
}
assert_eq!(completed, 4);
assert_eq!(calls.load(Ordering::SeqCst), 4);
assert!(
peak.load(Ordering::SeqCst) <= 2,
"peak concurrency {} exceeded the shared global limit",
peak.load(Ordering::SeqCst)
);
assert_eq!(store.snapshot().await.unwrap().executing, 0);
}
#[tokio::test]
async fn saturation_returns_typed_overload_without_an_outbound_call() {
let schema_url = crank_test_support::postgres_schema_url("admission_overload").await;
let pool = schema_pool(&schema_url).await;
let store = admission_store(pool, 1, 1);
let calls = Arc::new(AtomicUsize::new(0));
let current = Arc::new(AtomicUsize::new(0));
let peak = Arc::new(AtomicUsize::new(0));
let runtime = executor(
Arc::clone(&store),
Arc::clone(&calls),
Arc::clone(&current),
Arc::clone(&peak),
);
// Another process already holds the only global permit.
let held = match store
.admit(AdmissionRequest {
scope: AdmissionScope {
workspace_id: "ws_test".to_owned(),
agent_key: "admin".to_owned(),
operation_id: "op_admission_budget".to_owned(),
operation_version: 7,
},
mode: AdmissionMode::Synchronous,
policy_mode: ToolExecutionMode::Synchronous,
no_tasks_fallback: NoTasksFallback::Synchronous,
approval: ApprovalEvidence::NotRequired,
fingerprint: "fp_holder".to_owned(),
expires_at_unix_ms: crank_core::current_unix_ms() + 10_000,
})
.await
.expect("admission store is reachable")
{
crank_core::AdmissionOutcome::Permitted(permit) => permit,
other => panic!("expected a permit, got {other:?}"),
};
let waiting_operation: crank_runtime::RuntimeOperation = operation(Some(ToolExecutionPolicy {
mode: ToolExecutionMode::Synchronous,
deadline_ms: 250,
no_tasks_fallback: NoTasksFallback::Synchronous,
}))
.into();
let waiting_runtime = runtime.clone();
let first_waiting_operation = waiting_operation.clone();
let waiting = tokio::spawn(async move {
execute(&waiting_runtime, &first_waiting_operation, &json!({})).await
});
// Let the first request occupy the bounded queue before the next one arrives.
while store.snapshot().await.unwrap().waiting == 0 {
tokio::time::sleep(Duration::from_millis(5)).await;
}
let overloaded = execute(&runtime, &waiting_operation, &json!({})).await;
let failure = overloaded.expect_err("a full queue must refuse the request");
assert_eq!(
failure.error_code(),
ExecutionErrorCode::ExecutionOverloaded
);
assert_eq!(failure.stage(), ExecutionStage::Admission);
assert_eq!(failure.retry_after_ms(), Some(250));
// The queued request hits its absolute deadline instead of dispatching. That refusal happened
// inside admission (nothing was ever dispatched), so the client must see an admission-stage
// overload with retry guidance — not an `upstream_timeout`.
let expired = waiting
.await
.expect("waiting task")
.expect_err("deadline must refuse");
assert_eq!(
expired.error_code(),
ExecutionErrorCode::ExecutionOverloaded,
"queue waiting counts against the published deadline"
);
assert_eq!(expired.stage(), ExecutionStage::Admission);
assert_eq!(expired.retry_after_ms(), Some(250));
assert_eq!(expired.outcome_certainty(), OutcomeCertainty::Certain);
assert_eq!(
calls.load(Ordering::SeqCst),
0,
"no outbound call on refusal"
);
assert!(store.release(&held).await.unwrap());
}
#[tokio::test]
async fn background_policy_without_a_task_transport_is_typed_unsupported() {
let schema_url = crank_test_support::postgres_schema_url("admission_unsupported").await;
let pool = schema_pool(&schema_url).await;
let store = admission_store(pool, 2, 4);
let calls = Arc::new(AtomicUsize::new(0));
let current = Arc::new(AtomicUsize::new(0));
let peak = Arc::new(AtomicUsize::new(0));
let runtime = executor(
Arc::clone(&store),
Arc::clone(&calls),
Arc::clone(&current),
Arc::clone(&peak),
);
let operation: crank_runtime::RuntimeOperation = operation(Some(ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 2_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
}))
.into();
let failure = execute(&runtime, &operation, &json!({}))
.await
.expect_err("no Task transport exists in Story 4.1");
assert_eq!(
failure.error_code(),
ExecutionErrorCode::ExecutionModeUnsupported
);
assert_eq!(calls.load(Ordering::SeqCst), 0);
let snapshot = store.snapshot().await.unwrap();
assert_eq!(snapshot.waiting, 0);
assert_eq!(snapshot.executing, 0);
}
#[tokio::test]
async fn background_policy_with_a_synchronous_fallback_keeps_the_sync_response_shape() {
let schema_url = crank_test_support::postgres_schema_url("admission_fallback").await;
let pool = schema_pool(&schema_url).await;
let store = admission_store(pool, 2, 4);
let calls = Arc::new(AtomicUsize::new(0));
let current = Arc::new(AtomicUsize::new(0));
let peak = Arc::new(AtomicUsize::new(0));
let runtime = executor(
Arc::clone(&store),
Arc::clone(&calls),
Arc::clone(&current),
Arc::clone(&peak),
);
let operation: crank_runtime::RuntimeOperation = operation(Some(ToolExecutionPolicy {
mode: ToolExecutionMode::Adaptive,
deadline_ms: 2_000,
no_tasks_fallback: NoTasksFallback::Synchronous,
}))
.into();
let output = execute(&runtime, &operation, &json!({}))
.await
.expect("adaptive resolves through the published synchronous fallback");
// The response shape is unchanged by admission: an operation without output mapping still
// returns the same mapped output object it returned before Story 4.1.
assert_eq!(output, json!({}));
assert_eq!(calls.load(Ordering::SeqCst), 1);
assert_eq!(store.snapshot().await.unwrap().executing, 0);
}
#[tokio::test]
async fn admission_store_outage_fails_closed_before_dispatch() {
let schema_url = crank_test_support::postgres_schema_url("admission_outage").await;
let pool = schema_pool(&schema_url).await;
let store = admission_store(pool.clone(), 2, 4);
let calls = Arc::new(AtomicUsize::new(0));
let current = Arc::new(AtomicUsize::new(0));
let peak = Arc::new(AtomicUsize::new(0));
let runtime = executor(
Arc::clone(&store),
Arc::clone(&calls),
Arc::clone(&current),
Arc::clone(&peak),
);
// The mandatory state disappears after the store was built: every execution must fail closed.
sqlx::query("drop table execution_admission_entries")
.execute(&pool)
.await
.expect("drop the mandatory admission state");
let operation: crank_runtime::RuntimeOperation = operation(None).into();
let failure = execute(&runtime, &operation, &json!({}))
.await
.expect_err("a missing mandatory store must fail closed");
assert_eq!(
failure.error_code(),
ExecutionErrorCode::SafetyStoreUnavailable
);
assert_eq!(calls.load(Ordering::SeqCst), 0);
assert!(store.health().await.is_err());
}
/// Admission store that records the snapshot it was asked to decide on (Story 4.1, AC2/F2).
struct RecordingAdmission {
inner: Arc<PostgresExecutionAdmission>,
requests: Mutex<Vec<AdmissionRequest>>,
permits: AtomicUsize,
releases: AtomicUsize,
}
impl RecordingAdmission {
fn new(inner: Arc<PostgresExecutionAdmission>) -> Self {
Self {
inner,
requests: Mutex::new(Vec::new()),
permits: AtomicUsize::new(0),
releases: AtomicUsize::new(0),
}
}
fn requests(&self) -> Vec<AdmissionRequest> {
self.requests.lock().expect("admission log").clone()
}
fn permits(&self) -> usize {
self.permits.load(Ordering::SeqCst)
}
fn releases(&self) -> usize {
self.releases.load(Ordering::SeqCst)
}
}
#[async_trait]
impl ExecutionAdmission for RecordingAdmission {
async fn health(&self) -> Result<(), crank_core::AdmissionStoreError> {
self.inner.health().await
}
async fn admit(
&self,
request: AdmissionRequest,
) -> Result<crank_core::AdmissionOutcome, crank_core::AdmissionStoreError> {
self.requests
.lock()
.expect("admission log")
.push(request.clone());
let outcome = self.inner.admit(request).await?;
if matches!(outcome, crank_core::AdmissionOutcome::Permitted(_)) {
self.permits.fetch_add(1, Ordering::SeqCst);
}
Ok(outcome)
}
async fn release(
&self,
permit: &crank_core::AdmissionPermit,
) -> Result<bool, crank_core::AdmissionStoreError> {
let released = self.inner.release(permit).await?;
if released {
self.releases.fetch_add(1, Ordering::SeqCst);
}
Ok(released)
}
async fn cancel(
&self,
ticket_id: u64,
owner_token: &str,
) -> Result<bool, crank_core::AdmissionStoreError> {
self.inner.cancel(ticket_id, owner_token).await
}
async fn snapshot(
&self,
) -> Result<crank_core::AdmissionQueueSnapshot, crank_core::AdmissionStoreError> {
self.inner.snapshot().await
}
}
fn confirmation_operation(version: u32) -> crank_runtime::RuntimeOperation {
let mut source = operation(None);
source.id = OperationId::new("op_admission_confirmation");
source.name = "admission_confirmation".to_owned();
source.version = version;
let Target::Rest(target) = &mut source.target;
target.method = HttpMethod::Delete;
source.execution_config.safety = Some(OperationSafetyPolicy {
class: OperationSafetyClass::Destructive,
confirmation: Some(ConfirmationPolicy { ttl_ms: 60_000 }),
});
source.into()
}
fn confirmation_runtime(
admission: Arc<RecordingAdmission>,
coordination: Option<Arc<dyn crank_core::CoordinationStateStore>>,
calls: Arc<AtomicUsize>,
) -> RuntimeExecutor {
let store: Arc<dyn ExecutionAdmission> = admission;
let builder = RuntimeExecutorBuilder::new()
.register_adapter(Arc::new(CountingAdapter {
calls,
current: Arc::new(AtomicUsize::new(0)),
peak: Arc::new(AtomicUsize::new(0)),
}))
.with_limits(RuntimeLimits::try_new(8, 2).expect("local limits"))
.with_admission(store);
match coordination {
Some(coordination) => builder.with_coordination_store(coordination).build(),
None => builder.build(),
}
}
fn confirmation_context() -> crank_runtime::RuntimeRequestContext {
crank_runtime::RuntimeRequestContext::from_correlation(
&crank_core::CorrelationContext::generate(),
)
.with_response_cache_scope("ws_test", "agent_a")
.with_metering_context(
WorkspaceId::new("ws_test"),
None,
InvocationSource::AdminTestRun,
)
}
#[tokio::test]
async fn confirmation_requiring_call_takes_a_permit_only_with_verified_approval() {
let schema_url = crank_test_support::postgres_schema_url("admission_confirmation").await;
let pool = schema_pool(&schema_url).await;
let recording = Arc::new(RecordingAdmission::new(Arc::new(
PostgresExecutionAdmission::new(
pool,
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
crank_core::AdmissionSchedulerConfig::default(),
)
.expect("valid admission limits"),
)));
let coordination: Arc<dyn crank_core::CoordinationStateStore> =
Arc::new(crank_runtime::InMemoryCoordinationStateStore::default());
let calls = Arc::new(AtomicUsize::new(0));
let runtime = confirmation_runtime(
Arc::clone(&recording),
Some(Arc::clone(&coordination)),
Arc::clone(&calls),
);
let operation = confirmation_operation(3);
let context = confirmation_context();
let input = json!({ "reason": "remove the stale item" });
// 1. No approval yet: the existing challenge is returned and no execution capacity is taken.
let challenge = execute_in(&runtime, &operation, &input, &context)
.await
.expect_err("an unapproved destructive call must only stage confirmation");
assert_eq!(
challenge.error_code(),
ExecutionErrorCode::ConfirmationRequired
);
let token = challenge
.confirmation()
.expect("expected the confirmation challenge")
.token()
.to_owned();
assert_eq!(
recording.permits(),
0,
"an unapproved request must not take an execution permit"
);
assert_eq!(calls.load(Ordering::SeqCst), 0);
let requests = recording.requests();
assert_eq!(
requests.len(),
1,
"the one admission snapshot is consulted once"
);
assert_eq!(
requests[0].approval,
ApprovalEvidence::NotGranted,
"admission must see the real approval state, not a fixed NotRequired"
);
assert_eq!(requests[0].scope.operation_version, 3);
let snapshot = recording.snapshot().await.expect("admission snapshot");
assert_eq!((snapshot.executing, snapshot.waiting), (0, 0));
// 2. An approval issued for another published version never opens this execution.
let other_version = confirmation_operation(4);
let foreign = execute_in(
&runtime,
&other_version,
&input,
&context.clone().with_confirmation_token(token.clone()),
)
.await
.expect_err("a foreign approval must not authorise this version");
assert_eq!(
foreign.error_code(),
ExecutionErrorCode::ConfirmationInvalid
);
assert_eq!(recording.permits(), 0);
assert_eq!(
recording.requests().len(),
1,
"an unusable approval is refused before any capacity request"
);
// 3. An approval is bound to its exact input as well.
let mismatched_input = json!({ "reason": "remove another item" });
let mismatched = execute_in(
&runtime,
&operation,
&mismatched_input,
&context.clone().with_confirmation_token(token.clone()),
)
.await
.expect_err("a confirmation token must not authorise different input");
assert_eq!(
mismatched.error_code(),
ExecutionErrorCode::ConfirmationInvalid
);
assert_eq!(recording.permits(), 0);
assert_eq!(recording.requests().len(), 1);
// 4. The matching approval passes admission with truthful evidence and executes exactly once.
let executed = execute_in(
&runtime,
&operation,
&input,
&context.clone().with_confirmation_token(token.clone()),
)
.await
.expect("a verified approval executes");
assert!(executed.is_object());
assert_eq!(calls.load(Ordering::SeqCst), 1);
let requests = recording.requests();
assert_eq!(requests.len(), 2);
assert_eq!(requests[1].approval, ApprovalEvidence::Granted);
assert_eq!(requests[1].scope.operation_version, 3);
assert_eq!(recording.permits(), 1);
assert_eq!(
recording.releases(),
1,
"the permit is released after execution"
);
assert_eq!(recording.snapshot().await.expect("snapshot").executing, 0);
// 5. Replay of the consumed confirmation is refused before capacity and without a side effect.
let replay = execute_in(
&runtime,
&operation,
&input,
&context.clone().with_confirmation_token(token),
)
.await
.expect_err("a confirmation token is single-use");
assert_eq!(replay.error_code(), ExecutionErrorCode::ConfirmationInvalid);
assert_eq!(calls.load(Ordering::SeqCst), 1);
assert_eq!(recording.permits(), 1);
assert_eq!(
recording.requests().len(),
2,
"a replayed approval never reaches admission"
);
// 6. The trusted approved-recovery path still passes the same gate with real evidence.
let approved = execute_in(
&runtime,
&operation,
&json!({ "reason": "approved by an operator" }),
&context.with_approval_granted(),
)
.await;
assert!(approved.is_ok());
assert_eq!(calls.load(Ordering::SeqCst), 2);
let requests = recording.requests();
assert_eq!(requests.len(), 3);
assert_eq!(requests[2].approval, ApprovalEvidence::Granted);
assert_eq!(recording.permits(), 2);
assert_eq!(recording.snapshot().await.expect("snapshot").executing, 0);
}
#[tokio::test]
async fn mandatory_safety_store_outage_fails_closed_before_admission() {
let schema_url = crank_test_support::postgres_schema_url("admission_confirmation_outage").await;
let pool = schema_pool(&schema_url).await;
let recording = Arc::new(RecordingAdmission::new(Arc::new(
PostgresExecutionAdmission::new(
pool,
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
crank_core::AdmissionSchedulerConfig::default(),
)
.expect("valid admission limits"),
)));
let calls = Arc::new(AtomicUsize::new(0));
// Confirmation is mandatory for this Tool, but no safety store is wired: the request must fail
// closed instead of reserving capacity and hoping for an in-memory fallback.
let runtime = confirmation_runtime(Arc::clone(&recording), None, Arc::clone(&calls));
let operation = confirmation_operation(3);
let failure = execute_in(
&runtime,
&operation,
&json!({ "reason": "no store" }),
&confirmation_context(),
)
.await
.expect_err("a mandatory safety store outage must fail closed");
assert_eq!(
failure.error_code(),
ExecutionErrorCode::SafetyStoreUnavailable
);
assert_eq!(calls.load(Ordering::SeqCst), 0);
assert_eq!(recording.permits(), 0);
let after_missing_store = recording.requests().len();
// The same Tool with a claimed approval and a *failing* safety store never reaches the capacity
// store at all: the approval claim stays unverified, so no permit and no outbound call exist.
let failing = confirmation_runtime(
Arc::clone(&recording),
Some(Arc::new(UnavailableSafetyStore)),
Arc::clone(&calls),
);
let outage = execute_in(
&failing,
&operation,
&json!({ "reason": "failing store" }),
&confirmation_context().with_confirmation_token("ct_unavailable"),
)
.await
.expect_err("an unreachable safety store must refuse the claimed approval");
assert_eq!(
outage.error_code(),
ExecutionErrorCode::SafetyStoreUnavailable
);
assert_eq!(
recording.requests().len(),
after_missing_store,
"an unverified approval never becomes a capacity request"
);
assert_eq!(recording.permits(), 0);
assert_eq!(calls.load(Ordering::SeqCst), 0);
}
/// Coordination store whose mandatory backend is down: every operation fails closed.
struct UnavailableSafetyStore;
impl UnavailableSafetyStore {
fn error() -> crank_core::CacheStoreError {
crank_core::CacheStoreError::Unavailable {
message: "test backend unavailable".to_owned(),
}
}
}
#[async_trait]
impl crank_core::CoordinationStateStore for UnavailableSafetyStore {
async fn get_value(
&self,
_scope: crank_core::CacheScope,
_key: &str,
) -> Result<Option<crank_core::CoordinationStateValue>, crank_core::CacheStoreError> {
Err(Self::error())
}
async fn put_value(
&self,
_scope: crank_core::CacheScope,
_key: &str,
_value: crank_core::CoordinationStateValue,
_ttl: Duration,
) -> Result<(), crank_core::CacheStoreError> {
Err(Self::error())
}
async fn delete_value(
&self,
_scope: crank_core::CacheScope,
_key: &str,
) -> Result<(), crank_core::CacheStoreError> {
Err(Self::error())
}
async fn take_value(
&self,
_scope: crank_core::CacheScope,
_key: &str,
) -> Result<Option<crank_core::CoordinationStateValue>, crank_core::CacheStoreError> {
Err(Self::error())
}
async fn reserve_value(
&self,
_scope: crank_core::CacheScope,
_key: &str,
_value: crank_core::CoordinationStateValue,
_ttl: Duration,
) -> Result<crank_core::CoordinationStateReservation, crank_core::CacheStoreError> {
Err(Self::error())
}
async fn compare_and_set_value(
&self,
_scope: crank_core::CacheScope,
_key: &str,
_expected: &crank_core::CoordinationStateValue,
_value: crank_core::CoordinationStateValue,
_ttl: Duration,
) -> Result<bool, crank_core::CacheStoreError> {
Err(Self::error())
}
}
+4
View File
@@ -1,16 +1,20 @@
mod integration {
mod catalog_access;
mod common;
mod durable_tasks;
mod era_isolation;
mod execution_certainty;
mod execution_stages;
mod inventory_projection;
mod jsonrpc_correlation;
mod mcp_completion;
mod mcp_logging;
mod mcp_prompts;
mod mcp_signals;
mod request_context;
mod revision_matrix;
mod static_resources;
mod tool_search;
mod tools_lifecycle;
mod transport_protocol;
}
@@ -60,6 +60,31 @@ impl SharedLogWriter {
}
}
/// Story 3.4: a capturing writer plus the subscriber that records into it, so a test can prove that a
/// request-scoped progress token never reaches the log projection. Install it with
/// `tracing::subscriber::set_default` around the request on the `current_thread` runtime.
pub(super) fn capturing_subscriber() -> (
CapturedLogs,
impl tracing::Subscriber + Send + Sync + 'static,
) {
let writer = SharedLogWriter::default();
let subscriber = tracing_subscriber::fmt()
.with_writer(writer.clone())
.with_max_level(LevelFilter::TRACE)
.with_ansi(false)
.finish();
(CapturedLogs(writer), subscriber)
}
/// The captured log text of one scoped subscriber.
pub(super) struct CapturedLogs(SharedLogWriter);
impl CapturedLogs {
pub(super) fn output(&self) -> String {
self.0.output()
}
}
impl<'a> MakeWriter<'a> for SharedLogWriter {
type Writer = SharedLogGuard;
@@ -596,6 +621,7 @@ pub(super) fn test_operation(base_url: &str, name: &str) -> Operation<Schema, Ma
idempotency: None,
safety: None,
approval_policy: None,
execution_policy: None,
auth_profile_ref: None,
headers: BTreeMap::new(),
},
@@ -0,0 +1,555 @@
//! Story 4.4 ATDD: no outbound send exists without a committed durable attempt, and an elapsed
//! accepted deadline fails before dispatch.
//!
//! Both oracles drive the production composition (application entry → PG store → runtime executor →
//! real local HTTP upstream); only the upstream and the checkpoint failure are test-controlled.
use super::*;
use axum::Router;
use axum::http::StatusCode;
use axum::routing::post;
use crank_community_mcp::TaskWorkerHandler;
use crank_core::{
AgentOperationBinding, PlatformApiKeyScope, TaskState, ToolExecutionMode, ToolExecutionPolicy,
};
use crank_runtime::{SecretCrypto, TaskWorker, TaskWorkerConfig};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use time::OffsetDateTime;
async fn composition(
registry: &PostgresRegistry,
upstream: &str,
policy: ToolExecutionPolicy,
caller_name: &str,
scoped_key: &str,
) -> (
Arc<PostgresTaskStore>,
crank_community_mcp::TaskApplication,
SecretCrypto,
crank_community_mcp::TaskPrincipal,
) {
let mut operation = common::test_operation(upstream, TOOL_NAME);
operation.execution_config.execution_policy = Some(policy);
registry
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
.await
.unwrap();
publish(registry, &operation.id, 1).await;
common::publish_agent_with_bindings(
registry,
AGENT_SLUG,
vec![AgentOperationBinding {
operation_id: operation.id.clone(),
operation_version: 1,
tool_name: operation.name.clone(),
tool_title: operation.tool_description.title.clone(),
tool_description_override: None,
enabled: true,
}],
)
.await;
let caller = principal(
registry,
caller_name,
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let crypto = SecretCrypto::new(TEST_MASTER_KEY).unwrap();
registry
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
epoch: crypto.master_key_epoch(),
fingerprint: crypto.master_key_fingerprint(),
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
observed_at: &OffsetDateTime::now_utc(),
})
.await
.expect("test master-key identity registered");
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("valid task store configuration"),
);
let tasks = TaskApplication::new(
registry.clone(),
Duration::ZERO,
Arc::new(InMemoryCoordinationStateStore::default()),
TaskService::with_secret_crypto(store.clone(), Arc::new(crypto.clone())),
);
let _ = scoped_key;
(store, tasks, crypto, caller)
}
fn worker_for(
registry: &PostgresRegistry,
store: &Arc<PostgresTaskStore>,
tasks: &TaskApplication,
crypto: &SecretCrypto,
owner: &str,
) -> TaskWorker {
let runtime = crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build();
let handler = Arc::new(TaskWorkerHandler::from_application(
tasks,
registry.clone(),
runtime,
crypto.clone(),
));
let config = TaskWorkerConfig {
owner: owner.to_owned(),
batch_limit: 1,
lease_ms: 10_000,
poll_interval: Duration::from_millis(50),
max_inflight: 1,
shutdown_timeout: Duration::from_secs(5),
..TaskWorkerConfig::default()
};
TaskWorker::new(store.clone(), handler, config).expect("valid worker")
}
async fn state_of(registry: &PostgresRegistry, task_id: crank_core::TaskId) -> Option<String> {
sqlx::query_scalar("select state from tasks where task_id = $1")
.bind(task_id.as_uuid())
.fetch_optional(registry.pool())
.await
.unwrap()
}
/// AC1: a dispatch checkpoint that cannot commit produces zero outbound calls and no attempt row.
#[tokio::test]
async fn checkpoint_commit_failure_never_reaches_upstream() {
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let calls = Arc::clone(&calls);
move || {
let calls = Arc::clone(&calls);
async move {
calls.fetch_add(1, Ordering::SeqCst);
(StatusCode::OK, axum::Json(json!({"id": "lead-1"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let upstream = format!("http://{address}");
let (store, tasks, crypto, caller) = composition(
&registry,
&upstream,
background_policy(),
"attempt-guards-checkpoint",
"guards-checkpoint-key",
)
.await;
let acceptance = tasks
.accept_background(
&caller,
TOOL_NAME,
None,
"guards-checkpoint-key",
&json!({"email": "user@example.com"}),
&unsampled_correlation(),
)
.await
.expect("durable acceptance");
// The checkpoint is the only statement that commits an attempt; it is refused here.
sqlx::raw_sql(sqlx::AssertSqlSafe(
"create function refuse_task_checkpoint() returns trigger language plpgsql as $$
begin
if new.current_attempt_id is not null and old.current_attempt_id is null then
raise exception 'durable checkpoint refused for test';
end if;
return new;
end $$;
create trigger refuse_task_checkpoint before update on tasks
for each row execute function refuse_task_checkpoint();",
))
.execute(registry.pool())
.await
.expect("checkpoint refusal trigger");
let worker = worker_for(
&registry,
&store,
&tasks,
&crypto,
"worker-guards-checkpoint",
);
let stop_flag = worker.stop_flag();
let worker_task = tokio::spawn(async move { worker.run().await });
tokio::time::sleep(Duration::from_secs(2)).await;
stop_flag.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
assert_eq!(
calls.load(Ordering::SeqCst),
0,
"an uncommitted durable checkpoint must never produce an outbound call"
);
let attempts: i64 =
sqlx::query_scalar("select count(*)::bigint from task_attempts where task_id = $1")
.bind(acceptance.task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(
attempts, 0,
"no attempt history without a committed checkpoint"
);
assert_ne!(
state_of(&registry, acceptance.task_id).await.as_deref(),
Some("completed"),
"the refused attempt cannot complete the Task"
);
sqlx::raw_sql(sqlx::AssertSqlSafe(
"drop trigger refuse_task_checkpoint on tasks; drop function refuse_task_checkpoint();",
))
.execute(registry.pool())
.await
.expect("checkpoint refusal trigger removed");
server.abort();
}
/// AC6: an accepted absolute deadline that already elapsed fails the Task before any dispatch.
#[tokio::test]
async fn elapsed_deadline_fails_before_dispatch_without_upstream_call() {
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let calls = Arc::clone(&calls);
move || {
let calls = Arc::clone(&calls);
async move {
calls.fetch_add(1, Ordering::SeqCst);
(StatusCode::OK, axum::Json(json!({"id": "lead-1"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let upstream = format!("http://{address}");
let short_deadline = ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 300,
no_tasks_fallback: crank_core::NoTasksFallback::Unsupported,
};
let (store, tasks, crypto, caller) = composition(
&registry,
&upstream,
short_deadline,
"attempt-guards-deadline",
"guards-deadline-key",
)
.await;
let acceptance = tasks
.accept_background(
&caller,
TOOL_NAME,
None,
"guards-deadline-key",
&json!({"email": "user@example.com"}),
&unsampled_correlation(),
)
.await
.expect("durable acceptance");
// The accepted deadline elapses before any worker claims the Task.
tokio::time::sleep(Duration::from_millis(500)).await;
let worker = worker_for(&registry, &store, &tasks, &crypto, "worker-guards-deadline");
let stop_flag = worker.stop_flag();
let worker_task = tokio::spawn(async move { worker.run().await });
let mut state = None;
for _ in 0..100 {
tokio::time::sleep(Duration::from_millis(50)).await;
state = state_of(&registry, acceptance.task_id).await;
if state.as_deref() == Some("failed") {
break;
}
}
stop_flag.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
assert_eq!(state.as_deref(), Some("failed"));
assert_eq!(
calls.load(Ordering::SeqCst),
0,
"an elapsed deadline must fail before dispatch"
);
let attempts: i64 =
sqlx::query_scalar("select count(*)::bigint from task_attempts where task_id = $1")
.bind(acceptance.task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(attempts, 0, "no attempt is checkpointed after the deadline");
let code: Option<String> =
sqlx::query_scalar("select error_envelope ->> 'code' from tasks where task_id = $1")
.bind(acceptance.task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(code.as_deref(), Some("task_deadline_elapsed"));
assert_eq!(
sqlx::query_scalar::<_, String>("select state from tasks where task_id = $1")
.bind(acceptance.task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap(),
TaskState::Failed.as_str()
);
server.abort();
}
/// AC2: a write whose upstream idempotency is backed by server-owned evidence is retried with the
/// same stable key, in a bounded number of distinct durable attempts, and applies exactly one effect.
#[tokio::test]
async fn verified_idempotent_write_retries_with_stable_key_and_one_effect() {
use crank_community_mcp::VerifiedIdempotentWrite;
use crank_core::{IdempotencyMode, IdempotencyPolicy, RetryPolicy};
use std::collections::HashSet;
use std::sync::Mutex;
let registry = common::test_registry().await;
let requests = Arc::new(AtomicUsize::new(0));
let effects = Arc::new(AtomicUsize::new(0));
let keys = Arc::new(Mutex::new(Vec::<String>::new()));
let seen = Arc::new(Mutex::new(HashSet::<String>::new()));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let requests = Arc::clone(&requests);
let effects = Arc::clone(&effects);
let keys = Arc::clone(&keys);
let seen = Arc::clone(&seen);
move |headers: axum::http::HeaderMap| {
let requests = Arc::clone(&requests);
let effects = Arc::clone(&effects);
let keys = Arc::clone(&keys);
let seen = Arc::clone(&seen);
async move {
let key = headers
.get("idempotency-key")
.and_then(|value| value.to_str().ok())
.unwrap_or_default()
.to_owned();
keys.lock().expect("keys").push(key.clone());
let first_application = seen.lock().expect("seen").insert(key);
if first_application {
effects.fetch_add(1, Ordering::SeqCst);
}
let attempt = requests.fetch_add(1, Ordering::SeqCst);
if attempt == 0 {
// The first application is stored but its acknowledgement is lost.
return (
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(json!({"error": "acknowledgement lost"})),
);
}
(
StatusCode::OK,
axum::Json(json!({"id": "lead-1", "deduped": !first_application})),
)
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let upstream = format!("http://{address}");
let mut operation = common::test_operation(&upstream, TOOL_NAME);
operation.execution_config.execution_policy = Some(background_policy());
operation.execution_config.retry_policy = Some(RetryPolicy { max_attempts: 2 });
operation.execution_config.idempotency = Some(IdempotencyPolicy {
mode: IdempotencyMode::Required,
ttl_ms: 60_000,
input_field: Some("idempotency_key".to_owned()),
header_name: Some("Idempotency-Key".to_owned()),
});
registry
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
.await
.unwrap();
publish(&registry, &operation.id, 1).await;
common::publish_agent_with_bindings(
&registry,
AGENT_SLUG,
vec![AgentOperationBinding {
operation_id: operation.id.clone(),
operation_version: 1,
tool_name: operation.name.clone(),
tool_title: operation.tool_description.title.clone(),
tool_description_override: None,
enabled: true,
}],
)
.await;
let caller = principal(
&registry,
"attempt-guards-verified",
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let crypto = SecretCrypto::new(TEST_MASTER_KEY).unwrap();
registry
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
epoch: crypto.master_key_epoch(),
fingerprint: crypto.master_key_fingerprint(),
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
observed_at: &OffsetDateTime::now_utc(),
})
.await
.expect("test master-key identity registered");
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("valid task store configuration"),
);
let tasks = TaskApplication::new(
registry.clone(),
Duration::ZERO,
Arc::new(InMemoryCoordinationStateStore::default()),
TaskService::with_secret_crypto(store.clone(), Arc::new(crypto.clone())),
);
let acceptance = tasks
.accept_background(
&caller,
TOOL_NAME,
None,
"guards-verified-key",
&json!({"email": "user@example.com", "idempotency_key": "k-1"}),
&unsampled_correlation(),
)
.await
.expect("durable acceptance");
let runtime = crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.with_coordination_store(Arc::new(InMemoryCoordinationStateStore::default()))
.build();
let handler = Arc::new(
TaskWorkerHandler::from_application(&tasks, registry.clone(), runtime, crypto.clone())
.with_verified_writes(vec![VerifiedIdempotentWrite {
operation_id: operation.id.as_str().to_owned(),
operation_version: 1,
key_header: "Idempotency-Key".to_owned(),
validity_ms: 60_000,
}]),
);
let config = TaskWorkerConfig {
owner: "worker-guards-verified".to_owned(),
batch_limit: 1,
lease_ms: 10_000,
poll_interval: Duration::from_millis(50),
max_inflight: 1,
shutdown_timeout: Duration::from_secs(5),
..TaskWorkerConfig::default()
};
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
let stop_flag = worker.stop_flag();
let worker_task = tokio::spawn(async move { worker.run().await });
let mut state = None;
for _ in 0..200 {
tokio::time::sleep(Duration::from_millis(50)).await;
state = state_of(&registry, acceptance.task_id).await;
if state.as_deref() == Some("completed") {
break;
}
}
stop_flag.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
server.abort();
assert_eq!(state.as_deref(), Some("completed"));
assert_eq!(
requests.load(Ordering::SeqCst),
2,
"exactly one retry of the write"
);
assert_eq!(
effects.load(Ordering::SeqCst),
1,
"the upstream applied the write exactly once"
);
assert_eq!(
keys.lock().expect("keys").as_slice(),
["k-1".to_owned(), "k-1".to_owned()],
"the same stable key is delivered on both attempts"
);
let attempts = store
.attempts(
&crank_core::TaskAccess {
workspace_id: common::test_workspace_id().to_string(),
owner_kind: crank_core::TaskOwnerKind::Agent,
owner_id: "agent_attempt_guards".to_owned(),
authorization_scope: String::new(),
},
acceptance.task_id,
)
.await;
// Owner scope of this principal is the agent of the published binding; read the history through
// the application entry instead of guessing the authorization scope string.
assert!(attempts.is_err(), "a foreign scope never reads the history");
let attempts = tasks
.attempts(&caller, TOOL_NAME, acceptance.task_id)
.await
.expect("owner-scoped attempt history");
assert_eq!(attempts.len(), 2, "two distinct durable attempts");
assert_eq!(attempts[0].ordinal, 1);
assert_eq!(attempts[1].ordinal, 2);
assert!(attempts.iter().all(|attempt| attempt.retry_allowed));
assert_eq!(
attempts[0].outcome,
Some(crank_core::AttemptOutcomeClass::RetryScheduled)
);
assert_eq!(
attempts[1].outcome,
Some(crank_core::AttemptOutcomeClass::Completed)
);
assert_eq!(
attempts[0].upstream_key_digest, attempts[1].upstream_key_digest,
"the same stable key digest is recorded on both attempts"
);
assert!(attempts[0].upstream_key_digest.is_some());
let record = tasks
.status(&caller, TOOL_NAME, acceptance.task_id)
.await
.expect("status");
assert_eq!(record.pins.retry.max_attempts, 2);
}
@@ -0,0 +1,432 @@
//! Story 4.3 ATDD: durable Attempt ID reaches the adapter attempt span (PF-1).
//!
//! End-to-end: a real durable Task worker dispatches a retrying GET through a real local
//! upstream, and the `adapter_attempt` spans carry the same durable Attempt ID that is
//! persisted in `tasks.current_attempt_id` on both attempts.
use super::super::common;
use axum::Router;
use axum::http::StatusCode;
use axum::routing::get;
use crank_community_mcp::TaskWorkerHandler;
use crank_core::{
AgentOperationBinding, CorrelationContext, RetryPolicy, Target, ToolExecutionPolicy,
};
use crank_runtime::{
OutboundHttpPolicy, SecretCrypto, TaskWorker, TaskWorkerConfig, community_with_outbound_policy,
};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use time::OffsetDateTime;
use tracing::{Id, Subscriber, field::Visit};
use tracing_subscriber::{Layer, layer::SubscriberExt, registry::LookupSpan};
const AGENT_SLUG: &str = "reports";
const TOOL_NAME: &str = "attempt_trace";
const KEY: &str = "attempt-trace-key-1";
async fn publish(
registry: &crank_registry::PostgresRegistry,
operation_id: &crank_core::OperationId,
version: u32,
) {
registry
.publish_operation(crank_registry::PublishRequest {
workspace_id: &common::test_workspace_id(),
operation_id,
version,
published_at: &OffsetDateTime::parse(
"2026-03-26T10:00:00Z",
&time::format_description::well_known::Rfc3339,
)
.unwrap(),
published_by: Some("alice"),
})
.await
.unwrap();
}
async fn application_with_store(
registry: &crank_registry::PostgresRegistry,
) -> (
crank_community_mcp::TaskApplication,
std::sync::Arc<crank_registry::PostgresTaskStore>,
SecretCrypto,
) {
let crypto = SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap();
let observed_at = OffsetDateTime::now_utc();
registry
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
epoch: crypto.master_key_epoch(),
fingerprint: crypto.master_key_fingerprint(),
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
observed_at: &observed_at,
})
.await
.expect("test master-key identity registered");
let store = std::sync::Arc::new(
crank_registry::PostgresTaskStore::new(
registry.pool().clone(),
crank_registry::AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("valid task store configuration"),
);
let tasks = crank_community_mcp::TaskApplication::new(
registry.clone(),
std::time::Duration::ZERO,
std::sync::Arc::new(crank_runtime::InMemoryCoordinationStateStore::default()),
crank_runtime::TaskService::with_secret_crypto(
store.clone(),
std::sync::Arc::new(crypto.clone()),
),
);
(tasks, store, crypto)
}
async fn principal(
registry: &crank_registry::PostgresRegistry,
name: &str,
scopes: &[crank_core::PlatformApiKeyScope],
) -> crank_community_mcp::TaskPrincipal {
let token = common::create_platform_api_key(registry, AGENT_SLUG, name, scopes).await;
let secret_hash = common::hash_access_secret(&token);
let api_key = registry
.get_platform_api_key_by_secret_for_agent_slug(
common::test_workspace_slug(),
AGENT_SLUG,
&secret_hash,
)
.await
.unwrap()
.expect("an active platform key resolves");
registry
.touch_platform_api_key(
&api_key.api_key.workspace_id,
&api_key.api_key.id,
&OffsetDateTime::now_utc(),
)
.await
.unwrap();
crank_community_mcp::TaskPrincipal::Agent {
workspace_slug: common::test_workspace_slug().to_owned(),
agent_slug: AGENT_SLUG.to_owned(),
credential: crank_core::VerifiedMachineCredential {
machine_access_mode: crank_core::MachineAccessMode::StaticAgentKey,
max_security_level: crank_core::OperationSecurityLevel::Standard,
scopes: api_key.api_key.scopes.clone(),
platform_api_key_id: Some(api_key.api_key.id.clone()),
},
}
}
#[tokio::test]
async fn adapter_attempt_span_carries_durable_attempt_id() {
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
get({
let calls = Arc::clone(&calls);
move || {
let calls = Arc::clone(&calls);
async move {
let n = calls.fetch_add(1, Ordering::SeqCst);
if n == 0 {
return (
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(serde_json::json!({"error":"boom"})),
);
}
(
StatusCode::OK,
axum::Json(serde_json::json!({"id":"lead-1"})),
)
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let upstream = format!("http://{address}");
let mut operation = common::test_operation(&upstream, TOOL_NAME);
let Target::Rest(target) = &mut operation.target;
target.method = crank_core::HttpMethod::Get;
operation.execution_config.execution_policy = Some(ToolExecutionPolicy {
mode: crank_core::ToolExecutionMode::Background,
deadline_ms: 60_000,
no_tasks_fallback: crank_core::NoTasksFallback::Unsupported,
});
operation.execution_config.retry_policy = Some(RetryPolicy { max_attempts: 2 });
registry
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
.await
.unwrap();
publish(&registry, &operation.id, 1).await;
common::publish_agent_with_bindings(
&registry,
AGENT_SLUG,
vec![AgentOperationBinding {
operation_id: operation.id.clone(),
operation_version: 1,
tool_name: operation.name.clone(),
tool_title: operation.tool_description.title.clone(),
tool_description_override: None,
enabled: true,
}],
)
.await;
let caller = principal(
&registry,
"attempt-trace",
&[
crank_core::PlatformApiKeyScope::Read,
crank_core::PlatformApiKeyScope::Write,
],
)
.await;
let (tasks, store, crypto) = application_with_store(&registry).await;
let acceptance = tasks
.accept_background(
&caller,
TOOL_NAME,
None,
KEY,
&serde_json::json!({"email": "user@example.com"}),
&CorrelationContext::generate(),
)
.await
.expect("durable acceptance");
let runtime =
community_with_outbound_policy(OutboundHttpPolicy::allowing_hosts(["127.0.0.1"])).build();
let handler = Arc::new(TaskWorkerHandler::from_application(
&tasks,
registry.clone(),
runtime,
crypto,
));
let config = TaskWorkerConfig {
owner: "runtime-worker-attempt-trace".to_owned(),
batch_limit: 1,
lease_ms: 10_000,
poll_interval: std::time::Duration::from_millis(50),
max_inflight: 1,
shutdown_timeout: std::time::Duration::from_secs(5),
..TaskWorkerConfig::default()
};
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
let stop_flag = worker.stop_flag();
let capture = TraceCapture::default();
let subscriber = tracing_subscriber::registry().with(capture.clone());
let _guard = tracing::subscriber::set_default(subscriber);
let worker_task = tokio::spawn(async move { worker.run().await });
let mut state = None;
for _ in 0..200 {
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
let current: Option<String> =
sqlx::query_scalar("select state from tasks where task_id = $1")
.bind(acceptance.task_id.as_uuid())
.fetch_optional(registry.pool())
.await
.unwrap();
state = current;
if state.as_deref() == Some("completed") {
break;
}
}
stop_flag.store(true, Ordering::SeqCst);
tokio::time::timeout(std::time::Duration::from_secs(10), worker_task)
.await
.expect("worker stops inside the budget")
.expect("worker exits cleanly")
.expect("worker reports no error");
server.abort();
let durable_attempt_id: Option<String> = sqlx::query_scalar::<_, Option<String>>(
"select current_attempt_id from tasks where task_id = $1",
)
.bind(acceptance.task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(state.as_deref(), Some("completed"));
// Story 4.4: every outbound send is its own durable attempt, so the two retries of one lease carry
// two distinct attempt identities, both recorded in the immutable attempt history.
let attempt_rows: Vec<(String, i32)> = sqlx::query_as(
"select attempt_id, ordinal from task_attempts where task_id = $1 order by ordinal",
)
.bind(acceptance.task_id.as_uuid())
.fetch_all(registry.pool())
.await
.unwrap();
assert_eq!(attempt_rows.len(), 2, "two durable attempts were committed");
assert_eq!(attempt_rows[0].1, 1);
assert_eq!(attempt_rows[1].1, 2);
assert_eq!(
Some(attempt_rows[1].0.as_str()),
durable_attempt_id.as_deref(),
"the Task points at its latest durable attempt"
);
let spans = capture.snapshot();
let attempt_spans: Vec<_> = spans
.iter()
.filter(|s| s.name == "adapter_attempt")
.collect();
assert_eq!(attempt_spans.len(), 2, "two adapter_attempt spans");
for span in &attempt_spans {
let aid = span.fields.get("attempt_id").cloned().unwrap_or_default();
assert!(!aid.is_empty(), "attempt_id must not be empty");
assert!(
!aid.contains("traceparent"),
"attempt_id must not contain traceparent"
);
assert!(
attempt_rows.iter().any(|(id, _)| id == &aid),
"every attempt span names an attempt that was durably committed"
);
}
let ids: Vec<String> = attempt_spans
.iter()
.map(|s| s.fields.get("attempt_id").cloned().unwrap_or_default())
.collect();
assert_ne!(ids[0], ids[1], "each retry has its own durable attempt id");
assert_eq!(
spans
.iter()
.filter(|span| span.name == "durable_task_attempt")
.count(),
1,
"one durable attempt span per dispatch"
);
let runtime_span = spans
.iter()
.find(|span| span.name == "runtime.execute")
.expect("runtime.execute span");
assert_eq!(
runtime_span.parent_name,
Some("durable_task_attempt"),
"the execution continues the durable attempt trace"
);
for span in &attempt_spans {
assert_eq!(
span.parent_name,
Some("runtime.execute"),
"each retry is a child attempt span under the durable execution"
);
}
let attempts: Vec<String> = attempt_spans
.iter()
.map(|s| s.fields.get("attempt").cloned().unwrap_or_default())
.collect();
assert!(attempts.contains(&"1".to_string()));
assert!(attempts.contains(&"2".to_string()));
for span in &attempt_spans {
assert_eq!(
span.fields.get("max_attempts").cloned().unwrap_or_default(),
"2"
);
}
}
// TraceCapture minimal port from crates/crank-runtime/tests/stages.rs
#[derive(Clone, Default)]
struct TraceCapture {
spans: Arc<Mutex<Vec<CapturedSpan>>>,
}
impl TraceCapture {
fn snapshot(&self) -> Vec<CapturedSpan> {
self.spans.lock().expect("span lock").clone()
}
}
#[derive(Clone, Debug)]
struct CapturedSpan {
name: &'static str,
parent_name: Option<&'static str>,
fields: std::collections::BTreeMap<String, String>,
}
impl<S> Layer<S> for TraceCapture
where
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
{
fn on_new_span(
&self,
attributes: &tracing::span::Attributes<'_>,
id: &Id,
context: tracing_subscriber::layer::Context<'_, S>,
) {
let parent = attributes
.parent()
.and_then(|parent| context.span(parent))
.or_else(|| {
attributes
.is_contextual()
.then(|| context.lookup_current())
.flatten()
});
let mut visitor = FieldVisitor::default();
attributes.record(&mut visitor);
let mut spans = self.spans.lock().expect("span lock");
let index = spans.len();
spans.push(CapturedSpan {
name: attributes.metadata().name(),
parent_name: parent.map(|span| span.metadata().name()),
fields: visitor.fields,
});
context
.span(id)
.expect("span exists")
.extensions_mut()
.insert(index);
}
fn on_record(
&self,
id: &Id,
values: &tracing::span::Record<'_>,
context: tracing_subscriber::layer::Context<'_, S>,
) {
let mut visitor = FieldVisitor::default();
values.record(&mut visitor);
let span = context.span(id).expect("span exists");
let index = *span.extensions().get::<usize>().expect("capture index");
self.spans.lock().expect("span lock")[index]
.fields
.extend(visitor.fields);
}
}
#[derive(Default)]
struct FieldVisitor {
fields: std::collections::BTreeMap<String, String>,
}
impl Visit for FieldVisitor {
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
self.fields
.insert(field.name().to_owned(), value.to_owned());
}
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
self.fields
.insert(field.name().to_owned(), format!("{value:?}"));
}
}
@@ -0,0 +1,846 @@
//! ATDD Story 4.4 (Beam): cancel through TaskApplication -> PG -> worker -> upstream.
use super::common;
use super::unsampled_correlation;
use axum::Router;
use axum::http::StatusCode;
use axum::routing::post;
use crank_community_mcp::{TaskApplication, TaskWorkerHandler};
use crank_core::{
AgentOperationBinding, NoTasksFallback, PlatformApiKeyId, PlatformApiKeyScope, TaskState,
TaskStore, ToolExecutionMode, ToolExecutionPolicy, VerifiedMachineCredential,
};
use crank_registry::{AdmissionLimits, PostgresRegistry, PostgresTaskStore, PublishRequest};
use crank_runtime::{
InMemoryCoordinationStateStore, SecretCrypto, TaskService, TaskWorker, TaskWorkerConfig,
};
use serde_json::json;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use time::OffsetDateTime;
const AGENT_SLUG: &str = "reports";
const TOOL_NAME: &str = "report_gen";
const TEST_MASTER_KEY: &str = "test-master-key-00000000000000000000000000000000";
async fn state_of(registry: &PostgresRegistry, task_id: crank_core::TaskId) -> Option<String> {
sqlx::query_scalar("select state from tasks where task_id = $1")
.bind(task_id.as_uuid())
.fetch_optional(registry.pool())
.await
.unwrap()
}
async fn seed_tool(registry: &PostgresRegistry, upstream: &str, policy: ToolExecutionPolicy) {
let mut operation = common::test_operation(upstream, TOOL_NAME);
operation.execution_config.execution_policy = Some(policy);
registry
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
.await
.unwrap();
registry
.publish_operation(PublishRequest {
workspace_id: &common::test_workspace_id(),
operation_id: &operation.id,
version: 1,
published_at: &OffsetDateTime::parse(
"2026-03-26T10:00:00Z",
&time::format_description::well_known::Rfc3339,
)
.unwrap(),
published_by: Some("alice"),
})
.await
.unwrap();
common::publish_agent_with_bindings(
registry,
AGENT_SLUG,
vec![AgentOperationBinding {
operation_id: operation.id.clone(),
operation_version: 1,
tool_name: operation.name.clone(),
tool_title: operation.tool_description.title.clone(),
tool_description_override: None,
enabled: true,
}],
)
.await;
}
async fn principal(registry: &PostgresRegistry, name: &str) -> crank_community_mcp::TaskPrincipal {
let token = common::create_platform_api_key(
registry,
AGENT_SLUG,
name,
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let secret_hash = common::hash_access_secret(&token);
let api_key = registry
.get_platform_api_key_by_secret_for_agent_slug(
common::test_workspace_slug(),
AGENT_SLUG,
&secret_hash,
)
.await
.unwrap()
.expect("active key");
registry
.touch_platform_api_key(
&api_key.api_key.workspace_id,
&api_key.api_key.id,
&OffsetDateTime::now_utc(),
)
.await
.unwrap();
crank_community_mcp::TaskPrincipal::Agent {
workspace_slug: common::test_workspace_slug().to_owned(),
agent_slug: AGENT_SLUG.to_owned(),
credential: VerifiedMachineCredential {
machine_access_mode: crank_core::MachineAccessMode::StaticAgentKey,
max_security_level: crank_core::OperationSecurityLevel::Standard,
scopes: api_key.api_key.scopes.clone(),
platform_api_key_id: Some(api_key.api_key.id.clone()),
},
}
}
async fn application(registry: &PostgresRegistry) -> TaskApplication {
let crypto = SecretCrypto::new(TEST_MASTER_KEY).unwrap();
let observed_at = OffsetDateTime::now_utc();
registry
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
epoch: crypto.master_key_epoch(),
fingerprint: crypto.master_key_fingerprint(),
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
observed_at: &observed_at,
})
.await
.expect("master key");
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("store"),
);
TaskApplication::new(
registry.clone(),
Duration::ZERO,
Arc::new(InMemoryCoordinationStateStore::default()),
TaskService::with_secret_crypto(store, Arc::new(crypto)),
)
}
#[tokio::test]
async fn cancel_during_active_request_is_observed_and_leaves_one_unknown_outcome() {
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
let signal = Arc::new(std::sync::Mutex::new(Some(tx)));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let calls = Arc::clone(&calls);
let signal = Arc::clone(&signal);
move || {
let calls = Arc::clone(&calls);
let signal = Arc::clone(&signal);
async move {
calls.fetch_add(1, Ordering::SeqCst);
if let Some(tx) = signal.lock().unwrap().take() {
let _ = tx.send(());
}
tokio::time::sleep(Duration::from_millis(1500)).await;
(StatusCode::OK, axum::Json(json!({"id":"lead"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let upstream = format!("http://{address}");
let policy = ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 30_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
};
seed_tool(&registry, &upstream, policy).await;
let caller = principal(&registry, "cancel-s1").await;
let foreign = principal(&registry, "cancel-s1-foreign").await;
let app = application(&registry).await;
let acceptance = app
.accept_background(
&caller,
TOOL_NAME,
None,
"s1-key",
&json!({"email":"u"}),
&unsampled_correlation(),
)
.await
.expect("accept");
let crypto = SecretCrypto::new(TEST_MASTER_KEY).unwrap();
registry
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
epoch: crypto.master_key_epoch(),
fingerprint: crypto.master_key_fingerprint(),
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
observed_at: &OffsetDateTime::now_utc(),
})
.await
.expect("master key");
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("store"),
);
let runtime = crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build();
let handler = Arc::new(TaskWorkerHandler::from_application(
&app,
registry.clone(),
runtime,
crypto.clone(),
));
let worker = TaskWorker::new(
store,
handler,
TaskWorkerConfig {
owner: "worker-s1".to_owned(),
batch_limit: 1,
lease_ms: 10_000,
poll_interval: Duration::from_millis(50),
max_inflight: 1,
renewal_interval: Duration::from_millis(100),
shutdown_timeout: Duration::from_secs(5),
},
)
.expect("worker");
let stop_flag = worker.stop_flag();
let worker_task = tokio::spawn(async move { worker.run().await });
let _ = tokio::time::timeout(Duration::from_secs(5), rx).await;
let cancel = app
.cancel(&caller, TOOL_NAME, acceptance.task_id, None)
.await
.expect("cancel");
match cancel {
crank_core::TaskCancelOutcome::OutcomeUnknown(record) => {
assert_eq!(record.state, TaskState::OutcomeUnknown);
}
other => panic!("expected OutcomeUnknown got {other:?}"),
}
let foreign_cancel = app
.cancel(&foreign, TOOL_NAME, acceptance.task_id, None)
.await;
assert!(matches!(
foreign_cancel,
Err(crank_community_mcp::TaskApplicationError::NotFound)
));
tokio::time::sleep(Duration::from_millis(1600)).await;
assert_eq!(calls.load(Ordering::SeqCst), 1);
assert_eq!(
state_of(&registry, acceptance.task_id).await.as_deref(),
Some("outcome_unknown")
);
let certainty: Option<String> = sqlx::query_scalar(
"select certainty from task_attempts where task_id = $1 order by ordinal desc limit 1",
)
.bind(acceptance.task_id.as_uuid())
.fetch_optional(registry.pool())
.await
.unwrap();
assert_eq!(certainty.as_deref(), Some("unknown"));
let code: Option<String> =
sqlx::query_scalar("select error_envelope ->> 'code' from tasks where task_id = $1")
.bind(acceptance.task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(code.as_deref(), Some("cancelled_after_dispatch"));
let attempts_count: i64 =
sqlx::query_scalar("select count(*)::bigint from task_attempts where task_id = $1")
.bind(acceptance.task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(attempts_count, 1);
let cancel2 = app
.cancel(&caller, TOOL_NAME, acceptance.task_id, None)
.await
.expect("cancel2");
assert!(matches!(
cancel2,
crank_core::TaskCancelOutcome::OutcomeUnknown(_)
));
stop_flag.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
server.abort();
}
#[tokio::test]
async fn cancel_before_any_dispatch_is_cancelled_without_upstream_call() {
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let calls = Arc::clone(&calls);
move || {
let calls = Arc::clone(&calls);
async move {
calls.fetch_add(1, Ordering::SeqCst);
(StatusCode::OK, axum::Json(json!({"id":"lead"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let upstream = format!("http://{address}");
let policy = ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 30_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
};
seed_tool(&registry, &upstream, policy).await;
let caller = principal(&registry, "cancel-s3").await;
let app = application(&registry).await;
let acceptance = app
.accept_background(
&caller,
TOOL_NAME,
None,
"s3-key",
&json!({"email":"u"}),
&unsampled_correlation(),
)
.await
.expect("accept");
let cancel = app
.cancel(&caller, TOOL_NAME, acceptance.task_id, None)
.await
.expect("cancel");
match cancel {
crank_core::TaskCancelOutcome::Cancelled(record) => {
assert_eq!(record.state, TaskState::Cancelled)
}
other => panic!("expected Cancelled got {other:?}"),
}
assert_eq!(
state_of(&registry, acceptance.task_id).await.as_deref(),
Some("cancelled")
);
let attempts = app
.attempts(&caller, TOOL_NAME, acceptance.task_id)
.await
.expect("attempts");
assert!(attempts.is_empty());
assert_eq!(calls.load(Ordering::SeqCst), 0);
server.abort();
}
#[tokio::test]
async fn cancel_during_pinned_backoff_never_redispatches_and_is_outcome_unknown() {
use crank_community_mcp::VerifiedIdempotentWrite;
use crank_core::{IdempotencyMode, IdempotencyPolicy, RetryPolicy};
let registry = common::test_registry().await;
let requests = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let requests = Arc::clone(&requests);
move |headers: axum::http::HeaderMap| {
let requests = Arc::clone(&requests);
async move {
let _ = headers.get("idempotency-key");
let attempt = requests.fetch_add(1, Ordering::SeqCst);
if attempt == 0 {
// Applied upstream, acknowledgement lost after the caller had time to observe that
// the request really left the worker.
tokio::time::sleep(Duration::from_millis(600)).await;
return (
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(json!({"error":"acknowledgement lost"})),
);
}
(StatusCode::OK, axum::Json(json!({"id":"lead-1"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let upstream = format!("http://{address}");
let mut operation = common::test_operation(&upstream, TOOL_NAME);
operation.execution_config.execution_policy = Some(ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 30_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
});
operation.execution_config.retry_policy = Some(RetryPolicy { max_attempts: 2 });
operation.execution_config.idempotency = Some(IdempotencyPolicy {
mode: IdempotencyMode::Required,
ttl_ms: 60_000,
input_field: Some("idempotency_key".to_owned()),
header_name: Some("Idempotency-Key".to_owned()),
});
registry
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
.await
.unwrap();
registry
.publish_operation(PublishRequest {
workspace_id: &common::test_workspace_id(),
operation_id: &operation.id,
version: 1,
published_at: &OffsetDateTime::parse(
"2026-03-26T10:00:00Z",
&time::format_description::well_known::Rfc3339,
)
.unwrap(),
published_by: Some("alice"),
})
.await
.unwrap();
common::publish_agent_with_bindings(
&registry,
AGENT_SLUG,
vec![AgentOperationBinding {
operation_id: operation.id.clone(),
operation_version: 1,
tool_name: operation.name.clone(),
tool_title: operation.tool_description.title.clone(),
tool_description_override: None,
enabled: true,
}],
)
.await;
let caller = principal(&registry, "cancel-s2").await;
let app = application(&registry).await;
let acceptance = app
.accept_background(
&caller,
TOOL_NAME,
None,
"s2-key",
&json!({"email":"u","idempotency_key":"k-2"}),
&unsampled_correlation(),
)
.await
.expect("accept");
let crypto = SecretCrypto::new(TEST_MASTER_KEY).unwrap();
registry
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
epoch: crypto.master_key_epoch(),
fingerprint: crypto.master_key_fingerprint(),
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
observed_at: &OffsetDateTime::now_utc(),
})
.await
.expect("master key");
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("store"),
);
let runtime = crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.with_coordination_store(Arc::new(InMemoryCoordinationStateStore::default()))
.build();
let handler = Arc::new(
TaskWorkerHandler::from_application(&app, registry.clone(), runtime, crypto.clone())
.with_verified_writes(vec![VerifiedIdempotentWrite {
operation_id: operation.id.as_str().to_owned(),
operation_version: 1,
key_header: "Idempotency-Key".to_owned(),
validity_ms: 60_000,
}]),
);
// Reproduce deterministically the durable state the executor commits after an ambiguous attempt
// whose bounded retry is allowed: one attempt resolved with certainty=unknown, the retry armed and
// `current_attempt_id` cleared. The real retry window is a pinned millisecond backoff, so racing a
// running executor against the cancel would not be a repeatable oracle.
let lease = store
.claim_batch("worker-s2-prep", 1, 10_000)
.await
.expect("claim")
.pop()
.expect("one claimed lease");
let attempt = store
.checkpoint_dispatch(crank_core::BeginAttempt {
task_id: acceptance.task_id,
owner: lease.owner.clone(),
fence: lease.fence,
expected_version: lease.record.version,
attempt_id: format!("attempt-{}", uuid::Uuid::now_v7()),
input_fingerprint: lease.record.input_fingerprint.clone(),
upstream_key_digest: None,
retry_allowed: true,
})
.await
.expect("durable checkpoint");
let resolution = store
.resolve_attempt(
&attempt,
crank_core::AttemptOutcome::Uncertain {
error: crank_core::TaskEnvelope::try_redacted(
&json!({"code": "upstream_ack_lost", "phase": "dispatched"}),
)
.expect("bounded attempt error"),
repeatable: true,
phase: crank_core::AttemptPhase::Dispatched,
},
)
.await
.expect("attempt resolution");
assert!(
matches!(resolution, crank_core::AttemptResolution::RetryAt { .. }),
"an ambiguous but safely repeatable attempt arms the pinned retry"
);
let cancel = app
.cancel(&caller, TOOL_NAME, acceptance.task_id, None)
.await
.expect("cancel");
match cancel {
crank_core::TaskCancelOutcome::OutcomeUnknown(record) => {
assert_eq!(record.state, crank_core::TaskState::OutcomeUnknown)
}
other => panic!("cancel after an ambiguous attempt must stay uncertain, got {other:?}"),
}
// A real worker must never redispatch or terminate the Task afterwards: the uncertain state is not
// claimable and no second terminal write is possible.
let worker = TaskWorker::new(
store.clone(),
handler,
TaskWorkerConfig {
owner: "worker-s2".to_owned(),
batch_limit: 1,
lease_ms: 10_000,
poll_interval: Duration::from_millis(50),
max_inflight: 1,
shutdown_timeout: Duration::from_secs(5),
..TaskWorkerConfig::default()
},
)
.expect("worker");
let stop_flag = worker.stop_flag();
let worker_task = tokio::spawn(async move { worker.run().await });
tokio::time::sleep(Duration::from_secs(1)).await;
stop_flag.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
server.abort();
assert_eq!(
requests.load(Ordering::SeqCst),
0,
"no outbound send after the cancel"
);
assert_eq!(
state_of(&registry, acceptance.task_id).await.as_deref(),
Some("outcome_unknown")
);
let attempts_count: i64 =
sqlx::query_scalar("select count(*)::bigint from task_attempts where task_id = $1")
.bind(acceptance.task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(attempts_count, 1, "no second durable attempt");
let certainty: Option<String> = sqlx::query_scalar(
"select certainty from task_attempts where task_id = $1 order by ordinal desc limit 1",
)
.bind(acceptance.task_id.as_uuid())
.fetch_optional(registry.pool())
.await
.unwrap();
assert_eq!(certainty.as_deref(), Some("unknown"));
let code: Option<String> =
sqlx::query_scalar("select error_envelope ->> 'code' from tasks where task_id = $1")
.bind(acceptance.task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(code.as_deref(), Some("cancelled_after_dispatch"));
}
#[tokio::test]
async fn refused_attempt_after_lost_ack_keeps_outcome_unknown() {
use crank_community_mcp::VerifiedIdempotentWrite;
use crank_core::{IdempotencyMode, IdempotencyPolicy, RetryPolicy};
let registry = common::test_registry().await;
let requests = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let requests = Arc::clone(&requests);
move |headers: axum::http::HeaderMap| {
let requests = Arc::clone(&requests);
async move {
let _ = headers.get("idempotency-key");
let attempt = requests.fetch_add(1, Ordering::SeqCst);
if attempt == 0 {
// Applied upstream, acknowledgement lost after the caller had time to observe that
// the request really left the worker.
tokio::time::sleep(Duration::from_millis(600)).await;
return (
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(json!({"error":"acknowledgement lost"})),
);
}
(StatusCode::OK, axum::Json(json!({"id":"lead-1"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let upstream = format!("http://{address}");
let mut operation = common::test_operation(&upstream, TOOL_NAME);
operation.execution_config.execution_policy = Some(ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 30_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
});
operation.execution_config.retry_policy = Some(RetryPolicy { max_attempts: 2 });
operation.execution_config.idempotency = Some(IdempotencyPolicy {
mode: IdempotencyMode::Required,
ttl_ms: 60_000,
input_field: Some("idempotency_key".to_owned()),
header_name: Some("Idempotency-Key".to_owned()),
});
registry
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
.await
.unwrap();
registry
.publish_operation(PublishRequest {
workspace_id: &common::test_workspace_id(),
operation_id: &operation.id,
version: 1,
published_at: &OffsetDateTime::parse(
"2026-03-26T10:00:00Z",
&time::format_description::well_known::Rfc3339,
)
.unwrap(),
published_by: Some("alice"),
})
.await
.unwrap();
common::publish_agent_with_bindings(
&registry,
AGENT_SLUG,
vec![AgentOperationBinding {
operation_id: operation.id.clone(),
operation_version: 1,
tool_name: operation.name.clone(),
tool_title: operation.tool_description.title.clone(),
tool_description_override: None,
enabled: true,
}],
)
.await;
let caller = principal(&registry, "cancel-s4").await;
let app = application(&registry).await;
let acceptance = app
.accept_background(
&caller,
TOOL_NAME,
None,
"s4-key",
&json!({"email":"u","idempotency_key":"k-4"}),
&unsampled_correlation(),
)
.await
.expect("accept");
let crypto = SecretCrypto::new(TEST_MASTER_KEY).unwrap();
registry
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
epoch: crypto.master_key_epoch(),
fingerprint: crypto.master_key_fingerprint(),
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
observed_at: &OffsetDateTime::now_utc(),
})
.await
.expect("master key");
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("store"),
);
let runtime = crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.with_coordination_store(Arc::new(InMemoryCoordinationStateStore::default()))
.build();
let handler = Arc::new(
TaskWorkerHandler::from_application(&app, registry.clone(), runtime, crypto.clone())
.with_verified_writes(vec![VerifiedIdempotentWrite {
operation_id: operation.id.as_str().to_owned(),
operation_version: 1,
key_header: "Idempotency-Key".to_owned(),
validity_ms: 60_000,
}]),
);
let worker = TaskWorker::new(
store.clone(),
handler,
TaskWorkerConfig {
owner: "worker-s4".to_owned(),
batch_limit: 1,
lease_ms: 10_000,
poll_interval: Duration::from_millis(50),
max_inflight: 1,
shutdown_timeout: Duration::from_secs(5),
..TaskWorkerConfig::default()
},
)
.expect("worker");
let stop_flag = worker.stop_flag();
let worker_task = tokio::spawn(async move { worker.run().await });
// Revoke the key the Task was accepted with while the first outbound write is provably in flight:
// that attempt still resolves as an uncertain write, and the next attempt is refused by the
// authority before its checkpoint.
for _ in 0..200 {
if requests.load(Ordering::SeqCst) >= 1 {
break;
}
tokio::time::sleep(Duration::from_millis(20)).await;
}
assert_eq!(
requests.load(Ordering::SeqCst),
1,
"the first attempt is dispatched before the refusal"
);
registry
.revoke_platform_api_key_for_agent(
&common::test_workspace_id(),
&common::test_agent_id(AGENT_SLUG),
&PlatformApiKeyId::new("pk_cancel-s4".to_owned()),
&OffsetDateTime::now_utc(),
)
.await
.expect("revoke");
let mut state = None;
for _ in 0..200 {
tokio::time::sleep(Duration::from_millis(50)).await;
state = state_of(&registry, acceptance.task_id).await;
if state.as_deref() == Some("outcome_unknown") {
break;
}
}
stop_flag.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
server.abort();
assert_eq!(
state.as_deref(),
Some("outcome_unknown"),
"a refused attempt after a lost acknowledgement must stay uncertain"
);
assert_eq!(
requests.load(Ordering::SeqCst),
1,
"the refused attempt is never dispatched"
);
let attempts_count: i64 =
sqlx::query_scalar("select count(*)::bigint from task_attempts where task_id = $1")
.bind(acceptance.task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(attempts_count, 1, "no second durable attempt");
let certainty: Option<String> = sqlx::query_scalar(
"select certainty from task_attempts where task_id = $1 order by ordinal desc limit 1",
)
.bind(acceptance.task_id.as_uuid())
.fetch_optional(registry.pool())
.await
.unwrap();
assert_eq!(certainty.as_deref(), Some("unknown"));
let phase: Option<String> = sqlx::query_scalar(
"select phase from task_attempts where task_id = $1 order by ordinal desc limit 1",
)
.bind(acceptance.task_id.as_uuid())
.fetch_optional(registry.pool())
.await
.unwrap();
assert_eq!(phase.as_deref(), Some("dispatched"));
let error: Option<(Option<String>, Option<String>)> = sqlx::query_as(
"select error_envelope ->> 'code', error_envelope ->> 'phase' from tasks where task_id = $1",
)
.bind(acceptance.task_id.as_uuid())
.fetch_optional(registry.pool())
.await
.unwrap();
let (code, error_phase) = error.expect("the refused Task carries its bounded error");
assert_eq!(code.as_deref(), Some("task_authority_rejected"));
assert_eq!(
error_phase.as_deref(),
Some("dispatched"),
"the error keeps the phase of the attempt that may have reached the upstream"
);
}
@@ -0,0 +1,965 @@
//! ATDD Story 4.5 (Beam): durable input continuation through TaskApplication -> PG -> worker -> upstream.
use super::common;
use super::unsampled_correlation;
use axum::Router;
use axum::http::StatusCode;
use axum::routing::post;
use crank_community_mcp::{TaskApplication, TaskWorkerHandler};
use crank_core::{
NoTasksFallback, PlatformApiKeyScope, TaskId, TaskState, ToolExecutionMode, ToolExecutionPolicy,
};
use crank_registry::{AdmissionLimits, PostgresRegistry, PostgresTaskStore, PublishRequest};
use crank_runtime::{
InMemoryCoordinationStateStore, SecretCrypto, TaskService, TaskWorker, TaskWorkerConfig,
};
use serde_json::json;
use sqlx::Row;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use time::OffsetDateTime;
const AGENT_SLUG: &str = "reports";
pub(crate) const TOOL_NAME: &str = "report_gen";
const TEST_MASTER_KEY: &str = "test-master-key-00000000000000000000000000000000";
pub(crate) async fn state_of(registry: &PostgresRegistry, task_id: TaskId) -> Option<String> {
sqlx::query_scalar("select state from tasks where task_id = $1")
.bind(task_id.as_uuid())
.fetch_optional(registry.pool())
.await
.unwrap()
}
pub(crate) async fn wait_for_state(
registry: &PostgresRegistry,
task_id: TaskId,
desired: &str,
limit: Duration,
) -> bool {
let deadline = tokio::time::Instant::now() + limit;
while tokio::time::Instant::now() < deadline {
if state_of(registry, task_id).await.as_deref() == Some(desired) {
return true;
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
state_of(registry, task_id).await.as_deref() == Some(desired)
}
pub(crate) async fn seed_tool(
registry: &PostgresRegistry,
upstream: &str,
policy: ToolExecutionPolicy,
) {
let mut operation = common::test_operation(upstream, TOOL_NAME);
operation.execution_config.execution_policy = Some(policy);
registry
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
.await
.unwrap();
registry
.publish_operation(PublishRequest {
workspace_id: &common::test_workspace_id(),
operation_id: &operation.id,
version: 1,
published_at: &OffsetDateTime::parse(
"2026-03-26T10:00:00Z",
&time::format_description::well_known::Rfc3339,
)
.unwrap(),
published_by: Some("alice"),
})
.await
.unwrap();
common::publish_agent_with_bindings(
registry,
AGENT_SLUG,
vec![crank_core::AgentOperationBinding {
operation_id: operation.id.clone(),
operation_version: 1,
tool_name: operation.name.clone(),
tool_title: operation.tool_description.title.clone(),
tool_description_override: None,
enabled: true,
}],
)
.await;
}
pub(crate) async fn principal(
registry: &PostgresRegistry,
name: &str,
) -> crank_community_mcp::TaskPrincipal {
let token = common::create_platform_api_key(
registry,
AGENT_SLUG,
name,
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let secret_hash = common::hash_access_secret(&token);
let api_key = registry
.get_platform_api_key_by_secret_for_agent_slug(
common::test_workspace_slug(),
AGENT_SLUG,
&secret_hash,
)
.await
.unwrap()
.expect("active key");
registry
.touch_platform_api_key(
&api_key.api_key.workspace_id,
&api_key.api_key.id,
&OffsetDateTime::now_utc(),
)
.await
.unwrap();
crank_community_mcp::TaskPrincipal::Agent {
workspace_slug: common::test_workspace_slug().to_owned(),
agent_slug: AGENT_SLUG.to_owned(),
credential: crank_core::VerifiedMachineCredential {
machine_access_mode: crank_core::MachineAccessMode::StaticAgentKey,
max_security_level: crank_core::OperationSecurityLevel::Standard,
scopes: api_key.api_key.scopes.clone(),
platform_api_key_id: Some(api_key.api_key.id.clone()),
},
}
}
pub(crate) async fn application(registry: &PostgresRegistry) -> TaskApplication {
let crypto = SecretCrypto::new(TEST_MASTER_KEY).unwrap();
let observed_at = OffsetDateTime::now_utc();
registry
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
epoch: crypto.master_key_epoch(),
fingerprint: crypto.master_key_fingerprint(),
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
observed_at: &observed_at,
})
.await
.expect("master key");
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("store"),
);
TaskApplication::new(
registry.clone(),
Duration::ZERO,
Arc::new(InMemoryCoordinationStateStore::default()),
TaskService::with_secret_crypto(store, Arc::new(crypto)),
)
}
pub(crate) fn spawn_worker(
registry: &PostgresRegistry,
app: &TaskApplication,
) -> (
Arc<std::sync::atomic::AtomicBool>,
tokio::task::JoinHandle<()>,
) {
let crypto = SecretCrypto::new(TEST_MASTER_KEY).unwrap();
let pool = registry.pool().clone();
let store = Arc::new(
PostgresTaskStore::new(
pool,
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("store"),
);
let runtime = crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build();
let handler = Arc::new(TaskWorkerHandler::from_application(
app,
registry.clone(),
runtime,
crypto,
));
let worker = TaskWorker::new(
store,
handler,
TaskWorkerConfig {
owner: "worker-input".to_owned(),
batch_limit: 1,
lease_ms: 10_000,
poll_interval: Duration::from_millis(50),
max_inflight: 1,
renewal_interval: Duration::from_millis(100),
shutdown_timeout: Duration::from_secs(5),
},
)
.expect("worker");
let stop_flag = worker.stop_flag();
let jh = tokio::spawn(async move {
let _ = worker.run().await;
});
(stop_flag, jh)
}
#[tokio::test]
async fn input_continuation_happy_path_accept_update_resume_replay() {
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let calls = Arc::clone(&calls);
move || {
let calls = Arc::clone(&calls);
async move {
calls.fetch_add(1, Ordering::SeqCst);
(StatusCode::OK, axum::Json(json!({"id":"lead"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let upstream = format!("http://{address}");
let policy = ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 60_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
};
seed_tool(&registry, &upstream, policy).await;
let caller = principal(&registry, "input-happy").await;
let app = application(&registry).await;
let acceptance = app
.accept_background(
&caller,
TOOL_NAME,
None,
"input-key-1",
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.expect("acceptance");
assert_eq!(acceptance.state, TaskState::Queued);
let task_id = acceptance.task_id;
let (stop, worker_task) = spawn_worker(&registry, &app);
assert!(
wait_for_state(
&registry,
task_id,
"input_required",
Duration::from_secs(10)
)
.await,
"worker must suspend for missing email"
);
// positive control: before update no outbound request
assert_eq!(calls.load(Ordering::SeqCst), 0);
// persisted request fields
let row = sqlx::query(
"select wait_id, expected_version, schema_snapshot, projected_schema, prompt_code, expires_at from task_input_requests where task_id = $1",
)
.bind(task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
let wait_id: String = row.get("wait_id");
let expected_version: i32 = row.get("expected_version");
let schema_snapshot: serde_json::Value = row.get("schema_snapshot");
let projected_schema: serde_json::Value = row.get("projected_schema");
let prompt_code: String = row.get("prompt_code");
let _expires_at: OffsetDateTime = row.get("expires_at");
assert!(!wait_id.is_empty());
assert!(expected_version > 0);
assert!(schema_snapshot.is_object());
assert!(projected_schema.is_object());
assert_eq!(prompt_code, "required_input_missing");
let audit_count_before: i64 = sqlx::query_scalar(
"select count(*)::bigint from task_input_audit where task_id = $1 and event = 'input_required'",
)
.bind(task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert!(audit_count_before >= 1);
// stop worker before update to avoid races
stop.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
let outcome = app
.update_input(
&caller,
TOOL_NAME,
task_id,
&wait_id,
expected_version as u32,
"update-key-1",
&json!({"email": "user@example.com"}),
)
.await
.expect("valid update");
assert_eq!(outcome.state, TaskState::Working);
assert!(!outcome.replayed);
assert_eq!(outcome.task_id, task_id);
assert_eq!(outcome.wait_id, wait_id);
let (stop2, worker_task2) = spawn_worker(&registry, &app);
assert!(
wait_for_state(&registry, task_id, "completed", Duration::from_secs(10)).await,
"worker must complete after update"
);
stop2.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task2).await;
assert_eq!(calls.load(Ordering::SeqCst), 1);
let attempts = app
.attempts(&caller, TOOL_NAME, task_id)
.await
.expect("attempts");
assert_eq!(attempts.len(), 1);
let effective_fp: String =
sqlx::query_scalar("select effective_input_fingerprint from tasks where task_id = $1")
.bind(task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(attempts[0].input_fingerprint, effective_fp);
let input_fp: String =
sqlx::query_scalar("select input_fingerprint from tasks where task_id = $1")
.bind(task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_ne!(effective_fp, input_fp);
// An identical answer replay still returns the committed outcome after the Task completed:
// no second transition, audit row or outbound call.
let completed_replay = app
.update_input(
&caller,
TOOL_NAME,
task_id,
&wait_id,
expected_version as u32,
"update-key-1",
&json!({"email": "user@example.com"}),
)
.await
.expect("identical replay after completion");
assert!(completed_replay.replayed);
assert_eq!(completed_replay.version, outcome.version);
assert_eq!(calls.load(Ordering::SeqCst), 1);
// replay with original scoped key/partial input: same Task, no fingerprint conflict
let replay = app
.accept_background(
&caller,
TOOL_NAME,
None,
"input-key-1",
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.expect("replay accepted");
assert_eq!(replay.task_id, task_id);
server.abort();
}
pub(crate) async fn start_and_suspend(
registry: &PostgresRegistry,
app: &TaskApplication,
caller: &crank_community_mcp::TaskPrincipal,
scoped_key: &str,
) -> (TaskId, String, u32) {
let acceptance = app
.accept_background(
caller,
TOOL_NAME,
None,
scoped_key,
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.expect("acceptance");
let (stop, worker_task) = spawn_worker(registry, app);
assert!(
wait_for_state(
registry,
acceptance.task_id,
"input_required",
Duration::from_secs(10)
)
.await,
"worker must suspend for missing email"
);
stop.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
let (wait_id, expected_version) = wait_row(registry, acceptance.task_id).await;
(acceptance.task_id, wait_id, expected_version)
}
async fn wait_row(registry: &PostgresRegistry, task_id: TaskId) -> (String, u32) {
let row =
sqlx::query("select wait_id, expected_version from task_input_requests where task_id = $1")
.bind(task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
(
row.get::<String, _>("wait_id"),
u32::try_from(row.get::<i32, _>("expected_version")).unwrap_or_default(),
)
}
pub(crate) async fn audit_events(
registry: &PostgresRegistry,
task_id: TaskId,
) -> Vec<(String, Option<String>)> {
sqlx::query("select event, reason_code from task_input_audit where task_id = $1 order by id")
.bind(task_id.as_uuid())
.fetch_all(registry.pool())
.await
.unwrap()
.into_iter()
.map(|row| (row.get("event"), row.get("reason_code")))
.collect()
}
pub(crate) async fn admission_entries(
registry: &PostgresRegistry,
task_id: TaskId,
) -> Vec<(String, i64)> {
sqlx::query(
"select state, count(*)::bigint as count from execution_admission_entries
where task_id = $1 group by state order by state",
)
.bind(task_id.as_uuid())
.fetch_all(registry.pool())
.await
.unwrap()
.into_iter()
.map(|row| (row.get("state"), row.get("count")))
.collect()
}
fn key_id(principal: &crank_community_mcp::TaskPrincipal) -> crank_core::PlatformApiKeyId {
match principal {
crank_community_mcp::TaskPrincipal::Agent { credential, .. } => credential
.platform_api_key_id
.clone()
.expect("agent principal carries a key id"),
crank_community_mcp::TaskPrincipal::Admin { .. } => panic!("agent principal expected"),
}
}
#[tokio::test]
async fn invalid_schema_foreign_scope_and_revoked_key_never_transition_or_dispatch() {
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let calls = Arc::clone(&calls);
move || {
let calls = Arc::clone(&calls);
async move {
calls.fetch_add(1, Ordering::SeqCst);
(StatusCode::OK, axum::Json(json!({"id": "lead"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let policy = ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 60_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
};
seed_tool(&registry, &format!("http://{address}"), policy).await;
let caller = principal(&registry, "input-scope").await;
let app = application(&registry).await;
let (task_id, wait_id, expected_version) =
start_and_suspend(&registry, &app, &caller, "input-key-scope").await;
// Invalid answer: server-side schema validation refuses it, nothing transitions and nothing leaks.
let invalid = app
.update_input(
&caller,
TOOL_NAME,
task_id,
&wait_id,
expected_version,
"update-invalid",
&json!({}),
)
.await
.expect_err("the pinned schema requires email");
assert!(
matches!(
invalid,
crank_community_mcp::TaskApplicationError::Task(crank_core::TaskError::Invalid { .. })
),
"unexpected error: {invalid:?}"
);
assert_eq!(
state_of(&registry, task_id).await.as_deref(),
Some("input_required")
);
assert_eq!(calls.load(Ordering::SeqCst), 0);
assert!(
audit_events(&registry, task_id)
.await
.iter()
.all(|(event, _)| event == "input_required"),
"a rejected answer must not be audited as accepted"
);
let stored_snapshot: serde_json::Value =
sqlx::query_scalar("select schema_snapshot from task_input_requests where task_id = $1")
.bind(task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert!(!stored_snapshot.to_string().contains("user@example.com"));
// A different key of the same agent carries a different authorization scope: opaque not-found.
let foreign = principal(&registry, "input-foreign").await;
let foreign_result = app
.update_input(
&foreign,
TOOL_NAME,
task_id,
&wait_id,
expected_version,
"update-foreign",
&json!({"email": "user@example.com"}),
)
.await
.expect_err("a foreign scope cannot continue the Task");
assert_eq!(
foreign_result,
crank_community_mcp::TaskApplicationError::NotFound
);
assert_eq!(
state_of(&registry, task_id).await.as_deref(),
Some("input_required")
);
// Positive control: the accepting principal (same key/scope) completes the transition.
let accepted = app
.update_input(
&caller,
TOOL_NAME,
task_id,
&wait_id,
expected_version,
"update-valid",
&json!({"email": "user@example.com"}),
)
.await
.expect("valid answer for the accepting scope");
assert_eq!(accepted.state, TaskState::Working);
assert!(!accepted.replayed);
// Finish the positive-control Task through a real worker so the revocation case starts from a
// known dispatch count instead of leaving resumable work behind.
let (finish_stop, finish_worker) = spawn_worker(&registry, &app);
assert!(
wait_for_state(&registry, task_id, "completed", Duration::from_secs(10)).await,
"the positive-control Task completes"
);
finish_stop.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), finish_worker).await;
assert_eq!(calls.load(Ordering::SeqCst), 1);
// Revocation race: the accepting key is revoked after suspension. The same waiting scope must
// fail closed inside the update transaction and never revive or dispatch the Task.
let revocable = principal(&registry, "input-revoked").await;
let (revoked_task, revoked_wait, revoked_version) =
start_and_suspend(&registry, &app, &revocable, "input-key-revoked").await;
registry
.revoke_platform_api_key(
&common::test_workspace_id(),
&key_id(&revocable),
&OffsetDateTime::now_utc(),
)
.await
.unwrap();
let revoked_result = app
.update_input(
&revocable,
TOOL_NAME,
revoked_task,
&revoked_wait,
revoked_version,
"update-revoked",
&json!({"email": "revoked@example.com"}),
)
.await
.expect_err("a revoked key cannot continue the Task");
assert_eq!(
revoked_result,
crank_community_mcp::TaskApplicationError::NotFound
);
assert_eq!(
state_of(&registry, revoked_task).await.as_deref(),
Some("input_required")
);
assert_eq!(
calls.load(Ordering::SeqCst),
1,
"the revoked answer must not add an outbound call"
);
server.abort();
}
#[tokio::test]
async fn replay_conflict_and_stale_answers_have_one_typed_outcome() {
let registry = common::test_registry().await;
let policy = ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 60_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
};
seed_tool(&registry, "http://127.0.0.1:9", policy).await;
let caller = principal(&registry, "input-replay").await;
let app = application(&registry).await;
let (task_id, wait_id, expected_version) =
start_and_suspend(&registry, &app, &caller, "input-key-replay").await;
let first = app
.update_input(
&caller,
TOOL_NAME,
task_id,
&wait_id,
expected_version,
"update-key",
&json!({"email": "user@example.com"}),
)
.await
.expect("first valid answer");
assert!(!first.replayed);
assert_eq!(first.state, TaskState::Working);
// Identical replay returns the stored outcome without a second CAS/audit.
let replay = app
.update_input(
&caller,
TOOL_NAME,
task_id,
&wait_id,
expected_version,
"update-key",
&json!({"email": "user@example.com"}),
)
.await
.expect("identical replay");
assert!(replay.replayed);
assert_eq!(replay.version, first.version);
assert_eq!(
state_of(&registry, task_id).await.as_deref(),
Some("working")
);
let accepted_audit = audit_events(&registry, task_id)
.await
.into_iter()
.filter(|(event, _)| event == "input_accepted")
.count();
assert_eq!(accepted_audit, 1);
// Same key with another payload is a fingerprint conflict.
let conflict = app
.update_input(
&caller,
TOOL_NAME,
task_id,
&wait_id,
expected_version,
"update-key",
&json!({"email": "other@example.com"}),
)
.await
.expect_err("same key with another payload conflicts");
assert_eq!(
conflict,
crank_community_mcp::TaskApplicationError::Task(crank_core::TaskError::FingerprintConflict)
);
// Another key after a committed answer, and a foreign wait id, are state conflicts.
let other_key = app
.update_input(
&caller,
TOOL_NAME,
task_id,
&wait_id,
first.version,
"update-key-2",
&json!({"email": "user@example.com"}),
)
.await
.expect_err("another key after commit conflicts");
assert_eq!(
other_key,
crank_community_mcp::TaskApplicationError::Task(crank_core::TaskError::StateConflict)
);
let foreign_wait = app
.update_input(
&caller,
TOOL_NAME,
task_id,
"wait-does-not-exist",
first.version,
"update-key-3",
&json!({"email": "user@example.com"}),
)
.await
.expect_err("a foreign wait id conflicts");
assert_eq!(
foreign_wait,
crank_community_mcp::TaskApplicationError::Task(crank_core::TaskError::StateConflict)
);
// A stale generation on a fresh wait is refused; the valid generation still works.
let (stale_task, stale_wait, stale_version) =
start_and_suspend(&registry, &app, &caller, "input-key-stale").await;
let stale = app
.update_input(
&caller,
TOOL_NAME,
stale_task,
&stale_wait,
stale_version.saturating_sub(1).max(1),
"update-stale",
&json!({"email": "user@example.com"}),
)
.await
.expect_err("a stale generation cannot be continued");
assert_eq!(
stale,
crank_community_mcp::TaskApplicationError::Task(crank_core::TaskError::StateConflict)
);
let repaired = app
.update_input(
&caller,
TOOL_NAME,
stale_task,
&stale_wait,
stale_version,
"update-stale",
&json!({"email": "user@example.com"}),
)
.await
.expect("the current generation still accepts the answer");
assert!(!repaired.replayed);
}
#[tokio::test]
async fn ttl_expiry_fails_with_audit_and_releases_capacity_without_dispatch() {
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let calls = Arc::clone(&calls);
move || {
let calls = Arc::clone(&calls);
async move {
calls.fetch_add(1, Ordering::SeqCst);
(StatusCode::OK, axum::Json(json!({"id": "lead"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
// The wait TTL is the remaining pinned absolute execution deadline.
let policy = ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 1_200,
no_tasks_fallback: NoTasksFallback::Unsupported,
};
seed_tool(&registry, &format!("http://{address}"), policy).await;
let caller = principal(&registry, "input-ttl").await;
let app = application(&registry).await;
let acceptance = app
.accept_background(
&caller,
TOOL_NAME,
None,
"input-key-ttl",
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.expect("acceptance");
let task_id = acceptance.task_id;
let (stop, worker_task) = spawn_worker(&registry, &app);
assert!(
wait_for_state(&registry, task_id, "input_required", Duration::from_secs(5)).await,
"worker must suspend"
);
assert!(
wait_for_state(&registry, task_id, "failed", Duration::from_secs(6)).await,
"the per-poll sweeper must fail the expired wait"
);
stop.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
let (wait_id, expected_version) = wait_row(&registry, task_id).await;
let events = audit_events(&registry, task_id).await;
assert!(
events.iter().any(|(event, reason)| event == "input_expired"
&& reason.as_deref() == Some("input_ttl_expired")),
"expiry reason must be audited: {events:?}"
);
assert_eq!(admission_entries(&registry, task_id).await, Vec::new());
assert_eq!(calls.load(Ordering::SeqCst), 0);
// A late answer cannot revive the failed Task.
let late = app
.update_input(
&caller,
TOOL_NAME,
task_id,
&wait_id,
expected_version,
"update-late",
&json!({"email": "user@example.com"}),
)
.await
.expect_err("an expired wait never revives");
assert!(
matches!(
late,
crank_community_mcp::TaskApplicationError::Task(
crank_core::TaskError::InputExpired | crank_core::TaskError::StateConflict
)
),
"unexpected late answer error: {late:?}"
);
assert_eq!(
state_of(&registry, task_id).await.as_deref(),
Some("failed")
);
assert_eq!(calls.load(Ordering::SeqCst), 0);
server.abort();
}
#[tokio::test]
async fn competing_identical_updates_commit_once_and_keep_one_permit() {
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let calls = Arc::clone(&calls);
move || {
let calls = Arc::clone(&calls);
async move {
calls.fetch_add(1, Ordering::SeqCst);
(StatusCode::OK, axum::Json(json!({"id": "lead"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let policy = ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 60_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
};
seed_tool(&registry, &format!("http://{address}"), policy).await;
let caller = principal(&registry, "input-race").await;
let app = application(&registry).await;
let (task_id, wait_id, expected_version) =
start_and_suspend(&registry, &app, &caller, "input-key-race").await;
assert_eq!(
admission_entries(&registry, task_id).await,
vec![("executing".to_owned(), 1)],
"the waiting Task keeps exactly one executing permit"
);
let payload = json!({"email": "user@example.com"});
let first = app.update_input(
&caller,
TOOL_NAME,
task_id,
&wait_id,
expected_version,
"update-race",
&payload,
);
let second = app.update_input(
&caller,
TOOL_NAME,
task_id,
&wait_id,
expected_version,
"update-race",
&payload,
);
let (first, second) = tokio::join!(first, second);
let mut outcomes = [
first.expect("competing answer"),
second.expect("competing answer"),
];
outcomes.sort_by_key(|outcome| outcome.replayed);
assert!(!outcomes[0].replayed);
assert!(outcomes[1].replayed);
assert_eq!(outcomes[0].version, outcomes[1].version);
assert_eq!(
audit_events(&registry, task_id)
.await
.into_iter()
.filter(|(event, _)| event == "input_accepted")
.count(),
1
);
assert_eq!(
admission_entries(&registry, task_id).await,
vec![("executing".to_owned(), 1)],
"the update must not grant a second permit"
);
assert_eq!(calls.load(Ordering::SeqCst), 0);
let (stop, worker_task) = spawn_worker(&registry, &app);
assert!(
wait_for_state(&registry, task_id, "completed", Duration::from_secs(10)).await,
"the resumed Task completes"
);
stop.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
assert_eq!(calls.load(Ordering::SeqCst), 1);
assert_eq!(admission_entries(&registry, task_id).await, Vec::new());
server.abort();
}
@@ -0,0 +1,457 @@
//! ATDD Story 4.5 QA1 guards (F1/F3): one pinned deadline for several waiting Tasks, cancellation
//! while the Task waits for input and refusal of a stale lease/fence around the continuation seam.
use super::common;
use super::input_continuation::{
TOOL_NAME, admission_entries, application, audit_events, principal, seed_tool, spawn_worker,
start_and_suspend, state_of, wait_for_state,
};
use super::unsampled_correlation;
use axum::Router;
use axum::http::StatusCode;
use axum::routing::post;
use crank_core::{
BeginAttempt, NoTasksFallback, TASK_INPUT_SCHEMA_FORMAT_VERSION, TaskInputRequest,
TaskInputStore, TaskState, TaskStore, ToolExecutionMode, ToolExecutionPolicy,
};
use crank_registry::{AdmissionLimits, PostgresTaskStore};
use serde_json::json;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use time::OffsetDateTime;
#[tokio::test]
async fn two_waits_with_one_pinned_deadline_and_cancelled_leftover_do_not_block_suspension() {
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let calls = Arc::clone(&calls);
move || {
let calls = Arc::clone(&calls);
async move {
calls.fetch_add(1, Ordering::SeqCst);
(StatusCode::OK, axum::Json(json!({"id": "lead"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let policy = ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 60_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
};
seed_tool(&registry, &format!("http://{address}"), policy).await;
let caller = principal(&registry, "deadline-cancel").await;
let app = application(&registry).await;
let acc_a = app
.accept_background(
&caller,
TOOL_NAME,
None,
"deadline-key-a",
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.expect("accept a");
let acc_b = app
.accept_background(
&caller,
TOOL_NAME,
None,
"deadline-key-b",
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.expect("accept b");
let task_a = acc_a.task_id;
let task_b = acc_b.task_id;
let pinned_ms = (OffsetDateTime::now_utc().unix_timestamp() as f64 * 1000.0) + 60_000.0;
for tid in [task_a, task_b] {
sqlx::query("update tasks set execution_deadline = to_timestamp($1::double precision/1000.0) where task_id = $2")
.bind(pinned_ms)
.bind(tid.as_uuid())
.execute(registry.pool())
.await
.unwrap();
}
let (stop, worker_task) = spawn_worker(&registry, &app);
assert!(
wait_for_state(&registry, task_a, "input_required", Duration::from_secs(10)).await,
"a must suspend"
);
assert!(
wait_for_state(&registry, task_b, "input_required", Duration::from_secs(10)).await,
"b must suspend"
);
stop.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
assert_eq!(calls.load(Ordering::SeqCst), 0);
// Both active waits carry the same absolute TTL value: the per-Task uniqueness above is the only
// uniqueness, so this must not be a UNIQUE index on expires_at.
let same_deadline_wait: i64 = sqlx::query_scalar(
"select count(*)::bigint from task_input_requests a
join task_input_requests b on b.task_id = $2
where a.task_id = $1 and a.accepted_at is null and b.accepted_at is null
and a.expires_at = b.expires_at",
)
.bind(task_a.as_uuid())
.bind(task_b.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(
same_deadline_wait, 1,
"both waits share the pinned deadline"
);
let outcome = app
.cancel(&caller, TOOL_NAME, task_a, None)
.await
.expect("cancel a");
assert!(matches!(
outcome,
crank_core::TaskCancelOutcome::Cancelled(_)
));
assert_eq!(
state_of(&registry, task_a).await.as_deref(),
Some("cancelled")
);
assert_eq!(admission_entries(&registry, task_a).await, Vec::new());
// accept C with same pinned deadline
let acc_c = app
.accept_background(
&caller,
TOOL_NAME,
None,
"deadline-key-c",
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.expect("accept c");
let task_c = acc_c.task_id;
sqlx::query("update tasks set execution_deadline = to_timestamp($1::double precision/1000.0) where task_id = $2")
.bind(pinned_ms)
.bind(task_c.as_uuid())
.execute(registry.pool())
.await
.unwrap();
let (stop2, worker_task2) = spawn_worker(&registry, &app);
assert!(
wait_for_state(&registry, task_c, "input_required", Duration::from_secs(10)).await,
"c must suspend despite leftover a"
);
stop2.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task2).await;
assert_eq!(
state_of(&registry, task_b).await.as_deref(),
Some("input_required")
);
let c_deadline_ms: i64 = sqlx::query_scalar(
"select (extract(epoch from expires_at) * 1000)::bigint
from task_input_requests where task_id = $1",
)
.bind(task_c.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(
c_deadline_ms, pinned_ms as i64,
"the third wait reuses the same pinned deadline value"
);
let a_accepted: Option<OffsetDateTime> =
sqlx::query_scalar("select accepted_at from task_input_requests where task_id = $1")
.bind(task_a.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert!(
a_accepted.is_none(),
"cancellation leaves the wait row without an accepted receipt"
);
assert_eq!(calls.load(Ordering::SeqCst), 0);
server.abort();
}
#[tokio::test]
async fn cancel_while_input_required_never_revives_or_dispatches() {
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let calls = Arc::clone(&calls);
move || {
let calls = Arc::clone(&calls);
async move {
calls.fetch_add(1, Ordering::SeqCst);
(StatusCode::OK, axum::Json(json!({"id": "lead"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let policy = ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 60_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
};
seed_tool(&registry, &format!("http://{address}"), policy).await;
let caller = principal(&registry, "cancel-wait").await;
let app = application(&registry).await;
let (task_id, wait_id, expected_version) =
start_and_suspend(&registry, &app, &caller, "input-key-cancel").await;
assert_eq!(
admission_entries(&registry, task_id).await,
vec![("executing".to_owned(), 1)]
);
let outcome = app
.cancel(&caller, TOOL_NAME, task_id, None)
.await
.expect("cancel");
assert!(matches!(
outcome,
crank_core::TaskCancelOutcome::Cancelled(_)
));
assert_eq!(
state_of(&registry, task_id).await.as_deref(),
Some("cancelled")
);
assert_eq!(admission_entries(&registry, task_id).await, Vec::new());
let late = app
.update_input(
&caller,
TOOL_NAME,
task_id,
&wait_id,
expected_version,
"update-late",
&json!({"email": "user@example.com"}),
)
.await
.expect_err("late answer must not revive");
assert!(
matches!(
late,
crank_community_mcp::TaskApplicationError::Task(
crank_core::TaskError::StateConflict | crank_core::TaskError::InputExpired
)
),
"unexpected: {late:?}"
);
assert_eq!(
state_of(&registry, task_id).await.as_deref(),
Some("cancelled")
);
assert_eq!(calls.load(Ordering::SeqCst), 0);
let events = audit_events(&registry, task_id).await;
assert!(events.iter().any(|(e, _)| e == "input_required"));
assert!(!events.iter().any(|(e, _)| e == "input_accepted"));
let attempts = app
.attempts(&caller, TOOL_NAME, task_id)
.await
.expect("attempts");
assert!(attempts.is_empty());
server.abort();
}
#[tokio::test]
async fn stale_lease_fence_cannot_suspend_or_dispatch() {
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let upstream_app = Router::new().route(
"/crm/leads",
post({
let calls = Arc::clone(&calls);
move || {
let calls = Arc::clone(&calls);
async move {
calls.fetch_add(1, Ordering::SeqCst);
(StatusCode::OK, axum::Json(json!({"id": "lead"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let policy = ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 60_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
};
seed_tool(&registry, &format!("http://{address}"), policy).await;
let caller = principal(&registry, "stale-fence").await;
let app = application(&registry).await;
let acceptance = app
.accept_background(
&caller,
TOOL_NAME,
None,
"stale-key",
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.expect("accept");
let task_id = acceptance.task_id;
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("store"),
);
let mut old_claims = store
.claim_batch("worker-old", 1, 50)
.await
.expect("old claim");
assert!(!old_claims.is_empty());
let old = old_claims.remove(0);
tokio::time::sleep(Duration::from_millis(120)).await;
let mut new_claims = store
.claim_batch("worker-new", 1, 10_000)
.await
.expect("new claim");
assert!(!new_claims.is_empty());
let new = new_claims.remove(0);
assert!(new.fence > old.fence);
assert_eq!(new.record.task_id, task_id);
let request = TaskInputRequest {
wait_id: "stale-wait".to_string(),
expected_version: old.record.version + 1,
schema_revision: 1,
schema_format_version: TASK_INPUT_SCHEMA_FORMAT_VERSION,
schema_snapshot: json!({}),
projected_schema: json!({}),
prompt_code: "required_input_missing".to_string(),
missing_fields: vec!["$.email".to_string()],
expires_at_unix_ms: OffsetDateTime::now_utc().unix_timestamp() * 1000 + 30_000,
fingerprint_format_version: TASK_INPUT_SCHEMA_FORMAT_VERSION,
};
let suspended =
<PostgresTaskStore as TaskInputStore>::suspend_for_input(&*store, &old, request).await;
assert!(
suspended.is_err(),
"stale fence must not suspend: {suspended:?}"
);
let attempt_id = "attempt-stale".to_string();
let checkpoint = store
.checkpoint_dispatch(BeginAttempt {
task_id,
owner: old.owner.clone(),
fence: old.fence,
expected_version: old.record.version,
attempt_id,
input_fingerprint: old.record.input_fingerprint.clone(),
upstream_key_digest: None,
retry_allowed: false,
})
.await;
assert!(
checkpoint.is_err(),
"stale fence must not checkpoint: {checkpoint:?}"
);
assert_ne!(
state_of(&registry, task_id).await.as_deref(),
Some("input_required")
);
let attempt_count: i64 =
sqlx::query_scalar("select count(*)::bigint from task_attempts where task_id = $1")
.bind(task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(attempt_count, 0);
let req_count: i64 =
sqlx::query_scalar("select count(*)::bigint from task_input_requests where task_id = $1")
.bind(task_id.as_uuid())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(req_count, 0);
assert_eq!(calls.load(Ordering::SeqCst), 0);
// Positive control: the same request under the current lease suspends the Task exactly once, so
// the refusal above is the stale fence and not an invalid request shape.
let request = TaskInputRequest {
wait_id: "current-wait".to_string(),
expected_version: new.record.version + 1,
schema_revision: 1,
schema_format_version: TASK_INPUT_SCHEMA_FORMAT_VERSION,
fingerprint_format_version: TASK_INPUT_SCHEMA_FORMAT_VERSION,
schema_snapshot: json!({}),
projected_schema: json!({}),
prompt_code: "required_input_missing".to_string(),
missing_fields: vec!["$.email".to_string()],
expires_at_unix_ms: new.record.pins.execution_deadline_unix_ms,
};
let suspended =
<PostgresTaskStore as TaskInputStore>::suspend_for_input(&*store, &new, request)
.await
.expect("the current lease suspends");
assert_eq!(suspended.state, TaskState::InputRequired);
assert_eq!(
state_of(&registry, task_id).await.as_deref(),
Some("input_required")
);
assert_eq!(
admission_entries(&registry, task_id).await,
vec![("executing".to_owned(), 1)]
);
// The stale fence still cannot checkpoint a dispatch of this generation.
let checkpoint = store
.checkpoint_dispatch(BeginAttempt {
task_id,
owner: old.owner.clone(),
fence: old.fence,
expected_version: old.record.version,
attempt_id: "attempt-stale-after-suspend".to_string(),
input_fingerprint: old.record.input_fingerprint.clone(),
upstream_key_digest: None,
retry_allowed: false,
})
.await;
assert!(
checkpoint.is_err(),
"stale fence must not checkpoint: {checkpoint:?}"
);
assert_eq!(calls.load(Ordering::SeqCst), 0);
server.abort();
}
@@ -0,0 +1,659 @@
//! ATDD Story 4.2 (FR-22/FR-23): the durable Task application entry over a real published catalog
//! and a real PostgreSQL schema.
//!
//! These oracles drive the production application entry (`TaskApplication`) that the mcp-server
//! composition root constructs: a verified machine credential is checked against the published Tool,
//! the Task is accepted durably, a freshly constructed application instance reads the same identity
//! and pins, replay/version conflicts never rebind, foreign callers receive no metadata, and a missing
//! Task store fails closed and reports readiness as not ready. The public `tools/call` wire stays
//! synchronous/unsupported: this suite never calls it.
use super::common;
use std::time::Duration;
use crank_community_mcp::{TaskApplication, TaskApplicationError, TaskPrincipal};
use crank_core::{
AgentOperationBinding, CorrelationContext, NoTasksFallback, PlatformApiKeyScope, RequestId,
TaskAcceptRequest, TaskEnvelope, TaskId, TaskOwnerKind, TaskPins, TaskState, TaskStore,
TaskSupportIds, ToolExecutionMode, ToolExecutionPolicy,
};
use crank_registry::{
AdmissionLimits, CreateVersionRequest, PostgresRegistry, PostgresTaskStore, PublishRequest,
};
use crank_runtime::{InMemoryCoordinationStateStore, SecretCrypto, TaskService};
use serde_json::json;
use sqlx::{PgPool, Row};
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
const AGENT_SLUG: &str = "reports";
const TOOL_NAME: &str = "report_gen";
const KEY: &str = "retry-key-1";
fn operation(policy: ToolExecutionPolicy) -> crank_registry::RegistryOperation {
let mut operation = common::test_operation("https://api.example.com", TOOL_NAME);
operation.execution_config.execution_policy = Some(policy);
operation
}
fn background_policy() -> ToolExecutionPolicy {
ToolExecutionPolicy {
mode: ToolExecutionMode::Background,
deadline_ms: 60_000,
no_tasks_fallback: NoTasksFallback::Unsupported,
}
}
async fn seed_tool(registry: &PostgresRegistry, policy: ToolExecutionPolicy) {
let operation = operation(policy);
registry
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
.await
.unwrap();
publish(registry, &operation.id, 1).await;
// A second, explicitly synchronous Tool of the same Agent: the durable entry must refuse it.
let synchronous = common::test_operation("https://api.example.com", "report_sync");
registry
.create_operation(&common::test_workspace_id(), &synchronous, Some("alice"))
.await
.unwrap();
publish(registry, &synchronous.id, 1).await;
let binding = AgentOperationBinding {
operation_id: operation.id.clone(),
operation_version: 1,
tool_name: operation.name.clone(),
tool_title: operation.tool_description.title.clone(),
tool_description_override: None,
enabled: true,
};
let synchronous_binding = AgentOperationBinding {
operation_id: synchronous.id.clone(),
operation_version: 1,
tool_name: synchronous.name.clone(),
tool_title: synchronous.tool_description.title.clone(),
tool_description_override: None,
enabled: true,
};
common::publish_agent_with_bindings(registry, AGENT_SLUG, vec![binding, synchronous_binding])
.await;
}
async fn publish(
registry: &PostgresRegistry,
operation_id: &crank_core::OperationId,
version: u32,
) {
registry
.publish_operation(PublishRequest {
workspace_id: &common::test_workspace_id(),
operation_id,
version,
published_at: &OffsetDateTime::parse("2026-03-26T10:00:00Z", &Rfc3339).unwrap(),
published_by: Some("alice"),
})
.await
.unwrap();
}
async fn publish_tool_version(registry: &PostgresRegistry, version: u32) {
let mut next = operation(background_policy());
next.version = version;
next.status = crank_core::OperationStatus::Draft;
next.published_at = None;
registry
.create_version(CreateVersionRequest {
workspace_id: &common::test_workspace_id(),
snapshot: &next,
change_note: Some("next publication"),
created_by: Some("alice"),
})
.await
.unwrap();
publish(registry, &next.id, version).await;
// The agent catalog must bind the published Tool version for the new publication to be addressed.
let agent_id = common::test_agent_id(AGENT_SLUG);
let draft = crank_core::AgentVersion {
agent_id: agent_id.clone(),
version: 2,
status: crank_core::AgentStatus::Draft,
instructions: json!({}),
tool_selection_policy: Default::default(),
created_at: OffsetDateTime::parse("2026-03-26T10:01:00Z", &Rfc3339).unwrap(),
};
registry
.create_agent_draft_version(crank_registry::CreateAgentDraftVersionRequest {
workspace_id: &common::test_workspace_id(),
agent_id: &agent_id,
version: &draft,
bindings: &[AgentOperationBinding {
operation_id: next.id.clone(),
operation_version: version,
tool_name: next.name.clone(),
tool_title: next.tool_description.title.clone(),
tool_description_override: None,
enabled: true,
}],
updated_at: &OffsetDateTime::parse("2026-03-26T10:01:00Z", &Rfc3339).unwrap(),
})
.await
.unwrap();
registry
.publish_agent(crank_registry::PublishAgentRequest {
workspace_id: &common::test_workspace_id(),
agent_id: &agent_id,
version: 2,
published_at: &OffsetDateTime::parse("2026-03-26T10:02:00Z", &Rfc3339).unwrap(),
published_by: Some("alice"),
expected_state: None,
})
.await
.unwrap();
}
const TEST_MASTER_KEY: &str = "test-master-key-00000000000000000000000000000000";
fn task_service(pool: PgPool, crypto: SecretCrypto) -> TaskService {
let store = PostgresTaskStore::new(
pool,
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("valid task store configuration");
TaskService::with_secret_crypto(std::sync::Arc::new(store), std::sync::Arc::new(crypto))
}
/// The application entry under the same encrypted-input authority the production process uses.
async fn application(registry: &PostgresRegistry) -> TaskApplication {
let crypto = SecretCrypto::new(TEST_MASTER_KEY).expect("test master key");
let observed_at = OffsetDateTime::now_utc();
registry
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
epoch: crypto.master_key_epoch(),
fingerprint: crypto.master_key_fingerprint(),
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
observed_at: &observed_at,
})
.await
.expect("test master-key identity registered");
TaskApplication::new(
registry.clone(),
Duration::from_millis(0),
std::sync::Arc::new(InMemoryCoordinationStateStore::default()),
task_service(registry.pool().clone(), crypto),
)
}
async fn principal(
registry: &PostgresRegistry,
name: &str,
scopes: &[PlatformApiKeyScope],
) -> TaskPrincipal {
let token = common::create_platform_api_key(registry, AGENT_SLUG, name, scopes).await;
// The production machine-access path resolves the presented secret against the active platform
// key of the addressed Agent and derives the scope identity from that verified record.
let secret_hash = common::hash_access_secret(&token);
let api_key = registry
.get_platform_api_key_by_secret_for_agent_slug(
common::test_workspace_slug(),
AGENT_SLUG,
&secret_hash,
)
.await
.unwrap()
.expect("an active platform key resolves");
registry
.touch_platform_api_key(
&api_key.api_key.workspace_id,
&api_key.api_key.id,
&OffsetDateTime::now_utc(),
)
.await
.unwrap();
TaskPrincipal::Agent {
workspace_slug: common::test_workspace_slug().to_owned(),
agent_slug: AGENT_SLUG.to_owned(),
credential: crank_core::VerifiedMachineCredential {
machine_access_mode: crank_core::MachineAccessMode::StaticAgentKey,
max_security_level: crank_core::OperationSecurityLevel::Standard,
scopes: api_key.api_key.scopes.clone(),
platform_api_key_id: Some(api_key.api_key.id.clone()),
},
}
}
fn unsampled_correlation() -> CorrelationContext {
CorrelationContext::new(
crank_core::RequestId::generate(),
crank_core::TraceContext::parse("00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00")
.unwrap(),
)
}
async fn counts(pool: &PgPool) -> (i64, i64) {
let row = sqlx::query(
"select
(select count(*)::bigint from tasks) as tasks,
(select count(*)::bigint from execution_admission_entries where task_id is not null)
as intents",
)
.fetch_one(pool)
.await
.unwrap();
(row.get("tasks"), row.get("intents"))
}
#[tokio::test]
async fn a_verified_caller_receives_a_durable_task_that_a_new_instance_reads() {
let registry = common::test_registry().await;
seed_tool(&registry, background_policy()).await;
let caller = principal(
&registry,
"task-accept",
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let correlation = unsampled_correlation();
let app = application(&registry).await;
let acceptance = app
.accept_background(
&caller,
TOOL_NAME,
None,
KEY,
&json!({"report": "q3"}),
&correlation,
)
.await
.expect("acceptance over the real publication");
assert_eq!(acceptance.state, TaskState::Queued);
assert!(!acceptance.replayed);
assert_eq!(acceptance.operation_version, 1);
assert_eq!(acceptance.support.request_id, *correlation.request_id());
assert_eq!(acceptance.support.trace_id, *correlation.trace_id());
assert!(!acceptance.support.trace_sampled);
assert_eq!(
uuid::Uuid::parse_str(&acceptance.task_id.to_string())
.unwrap()
.get_version_num(),
7,
"the durable Task id is UUIDv7"
);
// Task, dispatch intent and queue slot committed together.
assert_eq!(counts(registry.pool()).await, (1, 1));
// A freshly constructed application + service reads the same durable identity and pins.
let reader = application(&registry).await;
let record = reader
.status(&caller, TOOL_NAME, acceptance.task_id)
.await
.unwrap();
assert_eq!(record.state, TaskState::Queued);
assert_eq!(record.pins.operation_version, 1);
assert_eq!(record.pins.policy_mode, ToolExecutionMode::Background);
assert_eq!(record.support.request_id, acceptance.support.request_id);
assert_eq!(record.support.traceparent, acceptance.support.traceparent);
assert!(!record.support.trace_sampled);
let outcome = reader
.result(&caller, TOOL_NAME, acceptance.task_id)
.await
.unwrap();
assert!(outcome.result.is_none() && outcome.error.is_none());
// Replay of the same scoped key and input returns the accepted Task instead of a new one.
let replay = app
.accept_background(
&caller,
TOOL_NAME,
None,
KEY,
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.unwrap();
assert_eq!(replay.task_id, acceptance.task_id);
assert!(replay.replayed);
assert_eq!(counts(registry.pool()).await, (1, 1));
}
#[tokio::test]
async fn a_new_publication_never_rebinds_an_accepted_task() {
let registry = common::test_registry().await;
seed_tool(&registry, background_policy()).await;
let caller = principal(
&registry,
"task-rebind",
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let app = application(&registry).await;
let accepted = app
.accept_background(
&caller,
TOOL_NAME,
None,
KEY,
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.unwrap();
let before = app
.status(&caller, TOOL_NAME, accepted.task_id)
.await
.unwrap();
publish_tool_version(&registry, 2).await;
// The same retry keeps the accepted Task and its v1 pin; it never becomes a v2 Task.
let replay = app
.accept_background(
&caller,
TOOL_NAME,
None,
KEY,
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.unwrap();
assert_eq!(replay.task_id, accepted.task_id);
assert_eq!(replay.operation_version, 1);
let after = app
.status(&caller, TOOL_NAME, accepted.task_id)
.await
.unwrap();
assert_eq!(after.pins.operation_version, 1);
assert_eq!(after.support.request_id, before.support.request_id);
assert_eq!(counts(registry.pool()).await, (1, 1));
// An explicit other exact version is a typed conflict, not a rebind.
let explicit = app
.accept_background(
&caller,
TOOL_NAME,
Some(2),
KEY,
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.expect_err("a different explicit pin cannot rebind the accepted Task");
assert_eq!(explicit, TaskApplicationError::VersionUnavailable);
// The original explicit pin still resolves to the accepted Task.
let pinned = app
.accept_background(
&caller,
TOOL_NAME,
Some(1),
KEY,
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.unwrap();
assert_eq!(pinned.task_id, accepted.task_id);
assert_eq!(counts(registry.pool()).await, (1, 1));
}
#[tokio::test]
async fn foreign_and_insufficient_callers_receive_no_task_metadata() {
let registry = common::test_registry().await;
seed_tool(&registry, background_policy()).await;
let owner = principal(
&registry,
"task-owner",
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let app = application(&registry).await;
let accepted = app
.accept_background(
&owner,
TOOL_NAME,
None,
KEY,
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.unwrap();
// Positive control: the owner reads its Task.
assert!(
app.status(&owner, TOOL_NAME, accepted.task_id)
.await
.is_ok()
);
// A second verified key of the same Agent is another authorization scope: no payload, no metadata.
let foreign_scope =
principal(&registry, "task-other-scope", &[PlatformApiKeyScope::Read]).await;
match app
.status(&foreign_scope, TOOL_NAME, accepted.task_id)
.await
{
Err(TaskApplicationError::NotFound) => {}
other => panic!("foreign scope must be not-found without metadata, got {other:?}"),
}
assert!(matches!(
app.result(&foreign_scope, TOOL_NAME, accepted.task_id)
.await,
Err(TaskApplicationError::NotFound)
));
// A synchronous publication is not accepted as background work at all.
let synchronous_app = application(&registry).await;
let refused = synchronous_app
.accept_background(
&owner,
"report_sync",
None,
"sync-key",
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.expect_err("a synchronous policy has no durable acceptance");
assert_eq!(refused, TaskApplicationError::NotBackground);
assert_eq!(counts(registry.pool()).await, (1, 1));
}
#[tokio::test]
async fn admin_owned_tasks_are_scoped_to_that_admin_and_workspace() {
let registry = common::test_registry().await;
seed_tool(&registry, background_policy()).await;
let app = application(&registry).await;
let admin = TaskPrincipal::Admin {
workspace_slug: common::test_workspace_slug().to_owned(),
agent_slug: AGENT_SLUG.to_owned(),
user_id: "user_admin_1".to_owned(),
};
let accepted = app
.accept_background(
&admin,
TOOL_NAME,
None,
KEY,
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.expect("an authenticated administrator accepts durable work");
assert_eq!(accepted.state, TaskState::Queued);
// Positive control: the same administrator reads its own Task.
assert!(
app.status(&admin, TOOL_NAME, accepted.task_id)
.await
.is_ok()
);
// An administrator is never a wildcard owner: another admin user and the owning Agent of the
// same workspace both see nothing.
let other_admin = TaskPrincipal::Admin {
workspace_slug: common::test_workspace_slug().to_owned(),
agent_slug: AGENT_SLUG.to_owned(),
user_id: "user_admin_2".to_owned(),
};
assert_eq!(
app.status(&other_admin, TOOL_NAME, accepted.task_id)
.await
.unwrap_err(),
TaskApplicationError::NotFound
);
assert_eq!(
app.result(&other_admin, TOOL_NAME, accepted.task_id)
.await
.unwrap_err(),
TaskApplicationError::NotFound
);
let agent = principal(&registry, "task-admin-cross", &[PlatformApiKeyScope::Read]).await;
assert_eq!(
app.status(&agent, TOOL_NAME, accepted.task_id)
.await
.unwrap_err(),
TaskApplicationError::NotFound
);
// An admin identity addressed to another workspace cannot resolve the publication at all.
let foreign_workspace = TaskPrincipal::Admin {
workspace_slug: "other-workspace".to_owned(),
agent_slug: AGENT_SLUG.to_owned(),
user_id: "user_admin_1".to_owned(),
};
assert_eq!(
app.accept_background(
&foreign_workspace,
TOOL_NAME,
None,
"foreign-key",
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.unwrap_err(),
TaskApplicationError::NotFound
);
assert_eq!(counts(registry.pool()).await, (1, 1));
}
#[tokio::test]
async fn a_missing_task_store_fails_closed_and_readiness_reports_not_ready() {
let database_url = crank_test_support::postgres_schema_url("durable_tasks_outage").await;
let pool = PgPool::connect(&database_url).await.unwrap();
// No migration: the durable Task state does not exist.
let outage_registry = PostgresRegistry::connect(&database_url).await;
assert!(
outage_registry.is_err(),
"an unmigrated registry cannot be used"
);
crank_registry::MigrationAuthority::apply(&pool)
.await
.unwrap();
let registry = PostgresRegistry::connect(&database_url).await.unwrap();
seed_tool(&registry, background_policy()).await;
let caller = principal(
&registry,
"task-outage",
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let app = application(&registry).await;
assert!(app.health().await.is_ok());
// Fail closed: without the Task schema the entry accepts nothing and reports dependency failure.
sqlx::query("drop table tasks cascade")
.execute(&pool)
.await
.unwrap();
assert!(app.health().await.is_err());
let error = app
.accept_background(
&caller,
TOOL_NAME,
None,
KEY,
&json!({"report": "q3"}),
&unsampled_correlation(),
)
.await
.expect_err("no durable Task store, no acceptance");
assert!(matches!(
error,
TaskApplicationError::Task(crank_core::TaskError::Unavailable { .. })
));
// Readiness of the app that wires the same store reports the durable Task dependency as not ready.
let router = common::build_test_app(registry.clone(), Duration::from_millis(0), None);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move {
axum::serve(listener, router).await.unwrap();
});
let response = reqwest::get(format!("http://{address}/ready"))
.await
.unwrap();
assert_eq!(response.status(), reqwest::StatusCode::SERVICE_UNAVAILABLE);
let body: serde_json::Value = response.json().await.unwrap();
assert_eq!(body["checks"]["durable_tasks"], json!("not_ready"));
server.abort();
}
/// Commits and resolves exactly one durable attempt of the current lease through the real store.
///
/// Story 4.4 moved the dispatch checkpoint and the attempt resolution into the runtime seam, so a
/// worker-level test double persists exactly one attempt instead of inventing a second retry loop.
pub(crate) async fn commit_attempt(
context: &crank_runtime::TaskAttemptContext,
outcome: crank_core::AttemptOutcome,
) -> crank_runtime::TaskAttemptOutcome {
use crank_core::{AttemptResolution, BeginAttempt, TaskState};
let lease = context.lease();
let begin = BeginAttempt {
task_id: lease.record.task_id,
owner: lease.owner.clone(),
fence: lease.fence,
expected_version: lease.record.version,
attempt_id: format!("attempt-{}", uuid::Uuid::now_v7()),
input_fingerprint: lease.record.input_fingerprint.clone(),
upstream_key_digest: None,
retry_allowed: lease.record.pins.retry.max_attempts > 1,
};
match context.store().checkpoint_dispatch(begin).await {
Ok(attempt) => match context.store().resolve_attempt(&attempt, outcome).await {
Ok(AttemptResolution::Finished(task)) if task.state == TaskState::Completed => {
crank_runtime::TaskAttemptOutcome::Completed
}
Ok(AttemptResolution::Finished(_)) => crank_runtime::TaskAttemptOutcome::Failed {
code: "test_failure".to_owned(),
},
Ok(AttemptResolution::Unknown(_)) => crank_runtime::TaskAttemptOutcome::OutcomeUnknown,
Ok(AttemptResolution::RetryAt { .. }) | Err(_) => {
crank_runtime::TaskAttemptOutcome::Stopped
}
},
Err(_) => crank_runtime::TaskAttemptOutcome::Stopped,
}
}
/// Bounded success outcome used by the worker-level test doubles.
pub(crate) fn completed_outcome(value: serde_json::Value) -> crank_core::AttemptOutcome {
crank_core::AttemptOutcome::Completed(
crank_core::TaskEnvelope::try_redacted(&value).expect("test result is bounded"),
)
}
mod attempt_guards;
mod attempt_trace;
mod cancel_chain;
mod input_continuation;
mod input_continuation_guards;
mod shutdown_window;
mod task_worker;
@@ -0,0 +1,220 @@
//! Story 4.3 QA caveat proof: the narrow post-checkpoint / pre-permit stop window.
//!
//! Split from `task_worker.rs` to keep both integration files inside the code-health limit.
use super::*;
/// A stop that lands after the durable dispatch checkpoint but before the sealed dispatch permit
/// exists finalizes the Task as failed(`shutdown_before_dispatch`) without any outbound call, and
/// does not consume work that is still queued.
///
/// The window is made deterministic instead of raced: a `BEFORE UPDATE` trigger delays only the
/// durable dispatch checkpoint, so the stop flag is set while that checkpoint statement is still in
/// flight (before `TaskDispatchPermit`/`TaskHandler::execute` can exist).
#[tokio::test]
async fn task_worker_stop_between_checkpoint_and_dispatch_never_reaches_upstream() {
use crank_core::{TaskAccess, TaskLease, TaskOwnerKind};
use crank_runtime::{
TaskAttemptOutcome, TaskHandler, TaskWorker, TaskWorkerConfig, TaskWorkerError,
};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
let registry = common::test_registry().await;
// The test registry exposes a single pooled connection: delaying the checkpoint inside its own
// statement keeps the window deterministic without competing for a second connection.
sqlx::raw_sql(sqlx::AssertSqlSafe(
"create function delay_task_dispatch_checkpoint() returns trigger language plpgsql as $$
begin
if new.current_attempt_id is not null and old.current_attempt_id is null then
perform pg_sleep(1.5);
end if;
return new;
end $$;
create trigger delay_task_dispatch_checkpoint before update on tasks
for each row execute function delay_task_dispatch_checkpoint();",
))
.execute(registry.pool())
.await
.expect("checkpoint delay trigger");
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("valid task store configuration"),
);
let mut accepted = Vec::new();
for (seed, scoped_key) in [("q", "a"), ("r", "c")] {
let request = TaskAcceptRequest {
task_id: TaskId::generate(),
workspace_id: common::test_workspace_id().to_string(),
owner_kind: TaskOwnerKind::Agent,
owner_id: "agent_a".to_owned(),
authorization_scope: "credential:pk_stop_window".to_owned(),
platform_api_key_id: None,
operation_id: "op_report".to_owned(),
pins: TaskPins {
operation_version: 1,
policy_mode: ToolExecutionMode::Background,
execution_deadline_unix_ms: 1_900_000_000_000,
retain_until_unix_ms: 1_900_000_000_000 + 60_000,
approval_reference: None,
retry: crank_core::TaskRetryPolicy::single_attempt(),
},
scoped_key: scoped_key.repeat(64),
input_fingerprint: scoped_key.repeat(64),
input: TaskEnvelope::try_redacted(&json!({ "report": seed })).unwrap(),
support: TaskSupportIds::new(
RequestId::generate(),
&crank_core::TraceContext::parse(
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
)
.unwrap(),
),
executable_input_ciphertext: None,
executable_input_key_version: None,
executable_input_master_key_epoch: None,
};
accepted.push(store.accept(request).await.expect("PG accepts"));
}
// The first revalidation waits for the test; the second Task is never claimed (batch_limit = 1).
let revalidate_entered = Arc::new(tokio::sync::Semaphore::new(0));
let revalidate_release = Arc::new(tokio::sync::Semaphore::new(0));
let executed = Arc::new(AtomicUsize::new(0));
struct GatedHandler {
entered: Arc<tokio::sync::Semaphore>,
release: Arc<tokio::sync::Semaphore>,
executed: Arc<AtomicUsize>,
}
#[async_trait::async_trait]
impl TaskHandler for GatedHandler {
async fn revalidate(&self, _lease: &TaskLease) -> Result<(), TaskWorkerError> {
self.entered.add_permits(1);
let _ = self.release.acquire().await;
Ok(())
}
async fn execute(&self, context: crank_runtime::TaskAttemptContext) -> TaskAttemptOutcome {
// The trigger delays the durable checkpoint; a stop that lands while it is in flight must
// finalize failed(shutdown_before_dispatch) without an outbound send.
let begin = crank_core::BeginAttempt {
task_id: context.lease().record.task_id,
owner: context.lease().owner.clone(),
fence: context.lease().fence,
expected_version: context.lease().record.version,
attempt_id: "attempt-stop-window".to_owned(),
input_fingerprint: context.lease().record.input_fingerprint.clone(),
upstream_key_digest: None,
retry_allowed: false,
};
let Ok(attempt) = context.store().checkpoint_dispatch(begin).await else {
return TaskAttemptOutcome::Stopped;
};
if context.is_stopping() {
let _ = context
.store()
.resolve_attempt(
&attempt,
crank_core::AttemptOutcome::Failed {
error: crank_core::TaskEnvelope::try_redacted(&json!({
"code": "shutdown_before_dispatch",
"phase": "before_dispatch",
}))
.expect("bounded error envelope"),
retryable: false,
phase: crank_core::AttemptPhase::BeforeDispatch,
},
)
.await;
return TaskAttemptOutcome::Failed {
code: "shutdown_before_dispatch".to_owned(),
};
}
self.executed.fetch_add(1, Ordering::SeqCst);
commit_attempt(&context, completed_outcome(json!({ "ok": true }))).await
}
}
let handler: Arc<dyn TaskHandler> = Arc::new(GatedHandler {
entered: revalidate_entered.clone(),
release: revalidate_release.clone(),
executed: executed.clone(),
});
let config = TaskWorkerConfig {
owner: "runtime-worker-stop-window".to_owned(),
batch_limit: 1,
lease_ms: 30_000,
poll_interval: Duration::from_millis(50),
renewal_interval: Duration::from_secs(20),
max_inflight: 1,
shutdown_timeout: Duration::from_secs(20),
};
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
let stop_flag = worker.stop_flag();
let worker_task = tokio::spawn(async move { worker.run().await });
tokio::time::timeout(Duration::from_secs(5), revalidate_entered.acquire())
.await
.expect("worker must claim the first Task and enter revalidation")
.expect("revalidation gate is open")
.forget();
// Let revalidation continue; the checkpoint now runs while the trigger holds it for 1.5s.
revalidate_release.add_permits(1);
tokio::time::sleep(Duration::from_millis(300)).await;
// The stop now lands while the durable checkpoint statement is still in flight: the worker may
// finalize the Task, but no permit exists yet, so no outbound call can start.
let stop_requested = std::time::Instant::now();
stop_flag.store(true, Ordering::SeqCst);
tokio::time::timeout(Duration::from_secs(30), worker_task)
.await
.expect("worker stops inside the 30s budget")
.expect("worker exits cleanly")
.expect("worker reports no error");
assert!(
stop_requested.elapsed() >= Duration::from_millis(500),
"the checkpoint must have still been in flight when the stop arrived"
);
assert_eq!(
executed.load(Ordering::SeqCst),
0,
"a stop between checkpoint and permit must never reach the upstream adapter"
);
let access = TaskAccess {
workspace_id: common::test_workspace_id().to_string(),
owner_kind: TaskOwnerKind::Agent,
owner_id: "agent_a".to_owned(),
authorization_scope: "credential:pk_stop_window".to_owned(),
};
let record = store.read(&access, accepted[0].task_id).await.unwrap();
assert_eq!(record.state, TaskState::Failed);
assert_eq!(record.attempts, 1, "the durable checkpoint did commit");
assert!(record.current_attempt_id.is_some());
assert_eq!(
record
.error
.as_ref()
.and_then(|error| error.value().get("code"))
.and_then(|code| code.as_str()),
Some("shutdown_before_dispatch")
);
// Work that was still queued is not consumed by the stopped worker.
let queued = store.read(&access, accepted[1].task_id).await.unwrap();
assert_eq!(queued.state, TaskState::Queued);
let recovered = store
.claim_batch("worker_after_stop", 4, 10_000)
.await
.expect("queued work stays claimable");
assert_eq!(recovered.len(), 1);
assert_eq!(recovered[0].record.task_id, accepted[1].task_id);
}
@@ -0,0 +1,821 @@
//! Story 4.3 ATDD: fenced durable Task worker leases, recovery and real upstream dispatch.
//!
//! Split from `durable_tasks.rs` to keep both integration files inside the code-health limit.
use super::*;
/// Store-level positive control only: PostgreSQL leases/fencing/recovery work without Valkey or a
/// task-worker process. This does not exercise TaskWorker startup, a bounded authoritative scan,
/// graceful shutdown, outbound dispatch, or runtime recovery timing, which remain unimplemented.
#[tokio::test]
async fn task_worker_store_lease_recovery_contract() {
let registry = common::test_registry().await;
let store = PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("valid task store configuration");
// Positive control: no Valkey or worker process is involved; PostgreSQL alone accepts and
// leases durable work, preserving the same admission-backed intent.
let request = TaskAcceptRequest {
task_id: TaskId::generate(),
workspace_id: common::test_workspace_id().to_string(),
owner_kind: TaskOwnerKind::Agent,
owner_id: "agent_a".to_owned(),
authorization_scope: "credential:pk_1".to_owned(),
platform_api_key_id: None,
operation_id: "op_report".to_owned(),
pins: TaskPins {
operation_version: 1,
policy_mode: ToolExecutionMode::Background,
execution_deadline_unix_ms: 1_900_000_000_000,
retain_until_unix_ms: 1_900_000_000_000 + 60_000,
approval_reference: None,
retry: crank_core::TaskRetryPolicy::single_attempt(),
},
scoped_key: "a".repeat(64),
input_fingerprint: "b".repeat(64),
input: TaskEnvelope::try_redacted(&json!({"report": "q3"})).unwrap(),
support: TaskSupportIds::new(
RequestId::generate(),
&crank_core::TraceContext::parse(
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
)
.unwrap(),
),
// This store-level control exercises lease/fence behavior, not the encrypted acceptance path.
executable_input_ciphertext: None,
executable_input_key_version: None,
executable_input_master_key_epoch: None,
};
let accepted = store.accept(request).await.expect("PG accepts");
let leases = store
.claim_batch("worker_1", 4, 10_000)
.await
.expect("claim");
assert_eq!(leases.len(), 1);
assert_eq!(leases[0].record.task_id, accepted.task_id);
let lease = leases[0].clone();
// A stale or competing owner cannot win CAS with the issued fencing token.
assert!(
store
.renew_lease("worker_2", lease.record.task_id, lease.fence, 10_000)
.await
.is_err()
);
assert!(
store
.write_progress(
"worker_1",
lease.record.task_id,
lease.fence + 1,
TaskEnvelope::try_redacted(&json!({"progress": 1})).unwrap(),
)
.await
.is_err()
);
// A stale fencing token can never checkpoint dispatch, even under the issuing owner.
assert!(
store
.checkpoint_dispatch(crank_core::BeginAttempt {
task_id: lease.record.task_id,
owner: "worker_1".to_owned(),
fence: lease.fence + 1,
expected_version: lease.record.version,
attempt_id: "attempt-stale".to_owned(),
input_fingerprint: lease.record.input_fingerprint.clone(),
upstream_key_digest: None,
retry_allowed: false,
})
.await
.is_err()
);
// The valid owner still commits the dispatch checkpoint before any outbound work.
let committed = store
.checkpoint_dispatch(crank_core::BeginAttempt {
task_id: lease.record.task_id,
owner: "worker_1".to_owned(),
fence: lease.fence,
expected_version: lease.record.version,
attempt_id: "attempt-1".to_owned(),
input_fingerprint: lease.record.input_fingerprint.clone(),
upstream_key_digest: None,
retry_allowed: false,
})
.await
.unwrap();
assert_eq!(committed.attempt.attempt_id, "attempt-1");
assert_eq!(
committed.task.current_attempt_id.as_deref(),
Some("attempt-1")
);
}
#[tokio::test]
async fn task_worker_runtime_dispatch_and_recovery_contract() {
use async_trait::async_trait;
use crank_core::{TaskAccess, TaskLease, TaskOwnerKind};
use crank_runtime::{
TaskAttemptOutcome, TaskHandler, TaskWorker, TaskWorkerConfig, TaskWorkerError,
};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
let registry = common::test_registry().await;
let pool = registry.pool().clone();
let store = Arc::new(
PostgresTaskStore::new(
pool.clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("valid task store configuration"),
);
let observed = Arc::new(AtomicUsize::new(0));
// Positive control: PostgreSQL alone accepts the durable Task.
let request = TaskAcceptRequest {
task_id: TaskId::generate(),
workspace_id: common::test_workspace_id().to_string(),
owner_kind: TaskOwnerKind::Agent,
owner_id: "agent_a".to_owned(),
authorization_scope: "credential:pk_runtime".to_owned(),
platform_api_key_id: None,
operation_id: "op_report".to_owned(),
pins: TaskPins {
operation_version: 1,
policy_mode: ToolExecutionMode::Background,
execution_deadline_unix_ms: 1_900_000_000_000,
retain_until_unix_ms: 1_900_000_000_000 + 60_000,
approval_reference: None,
retry: crank_core::TaskRetryPolicy::single_attempt(),
},
scoped_key: "r".repeat(64),
input_fingerprint: "b".repeat(64),
input: TaskEnvelope::try_redacted(&json!({"email": "q3"})).unwrap(),
support: TaskSupportIds::new(
RequestId::generate(),
&crank_core::TraceContext::parse(
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
)
.unwrap(),
),
executable_input_ciphertext: None,
executable_input_key_version: None,
executable_input_master_key_epoch: None,
};
let accepted = store.accept(request).await.expect("PG accepts");
let task_id = accepted.task_id;
struct RuntimeHandler {
observed: Arc<AtomicUsize>,
}
#[async_trait]
impl TaskHandler for RuntimeHandler {
async fn revalidate(&self, _lease: &TaskLease) -> Result<(), TaskWorkerError> {
Ok(())
}
async fn execute(&self, context: crank_runtime::TaskAttemptContext) -> TaskAttemptOutcome {
self.observed.fetch_add(1, Ordering::SeqCst);
commit_attempt(&context, completed_outcome(serde_json::json!({"ok": true}))).await
}
}
let handler: Arc<dyn TaskHandler> = Arc::new(RuntimeHandler {
observed: observed.clone(),
});
// PG-only authoritative dispatch: no Valkey or upstream process is involved.
let config = TaskWorkerConfig {
owner: "runtime-worker-1".to_owned(),
batch_limit: 1,
lease_ms: 10_000,
poll_interval: Duration::from_millis(100),
renewal_interval: Duration::from_millis(1_000),
max_inflight: 1,
shutdown_timeout: Duration::from_secs(5),
};
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
let stop_flag = worker.stop_flag();
let worker_task = tokio::spawn(async move { worker.run().await });
let access = TaskAccess {
workspace_id: common::test_workspace_id().to_string(),
owner_kind: TaskOwnerKind::Agent,
owner_id: "agent_a".to_owned(),
authorization_scope: "credential:pk_runtime".to_owned(),
};
let mut state = None;
for _ in 0..100 {
tokio::time::sleep(Duration::from_millis(100)).await;
if let Ok(record) = store.read(&access, task_id).await {
state = Some(record.state);
if record.state == TaskState::Completed {
break;
}
}
}
stop_flag.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
assert_eq!(state, Some(TaskState::Completed));
assert_eq!(
observed.load(Ordering::SeqCst),
1,
"exactly one outbound attempt"
);
// Terminal state is not duplicated.
let record = store.read(&access, task_id).await.expect("terminal read");
assert_eq!(record.state, TaskState::Completed);
// Bounded shutdown with no fresh work returns promptly.
let idle_config = TaskWorkerConfig {
owner: "runtime-worker-idle".to_owned(),
poll_interval: Duration::from_millis(50),
shutdown_timeout: Duration::from_secs(2),
..TaskWorkerConfig::default()
};
let idle_worker = TaskWorker::new(
store.clone(),
handler_for_no_work(store.clone()),
idle_config,
)
.expect("valid idle worker");
idle_worker.stop();
let started = std::time::Instant::now();
idle_worker.run().await.expect("idle worker stops");
assert!(started.elapsed() <= Duration::from_secs(30));
}
/// Story 4.3 ATDD: failed pre-dispatch revalidation finishes the attempt without any outbound call.
#[tokio::test]
async fn task_worker_revalidation_denial_never_dispatches() {
use crank_core::{TaskAccess, TaskError, TaskLease, TaskOwnerKind};
use crank_runtime::{
TaskAttemptOutcome, TaskHandler, TaskWorker, TaskWorkerConfig, TaskWorkerError,
};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
let registry = common::test_registry().await;
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("valid task store configuration"),
);
let request = TaskAcceptRequest {
task_id: TaskId::generate(),
workspace_id: common::test_workspace_id().to_string(),
owner_kind: TaskOwnerKind::Agent,
owner_id: "agent_a".to_owned(),
authorization_scope: "credential:pk_denied".to_owned(),
platform_api_key_id: None,
operation_id: "op_report".to_owned(),
pins: TaskPins {
operation_version: 1,
policy_mode: ToolExecutionMode::Background,
execution_deadline_unix_ms: 1_900_000_000_000,
retain_until_unix_ms: 1_900_000_000_000 + 60_000,
approval_reference: None,
retry: crank_core::TaskRetryPolicy::single_attempt(),
},
scoped_key: "d".repeat(64),
input_fingerprint: "b".repeat(64),
input: TaskEnvelope::try_redacted(&json!({"report": "q3"})).unwrap(),
support: TaskSupportIds::new(
RequestId::generate(),
&crank_core::TraceContext::parse(
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
)
.unwrap(),
),
executable_input_ciphertext: None,
executable_input_key_version: None,
executable_input_master_key_epoch: None,
};
let accepted = store.accept(request).await.expect("PG accepts");
struct DeniedHandler {
observed: Arc<AtomicUsize>,
}
#[async_trait::async_trait]
impl TaskHandler for DeniedHandler {
async fn revalidate(&self, _lease: &TaskLease) -> Result<(), TaskWorkerError> {
Err(TaskWorkerError::Store(TaskError::invalid(
"publication or credential revoked",
)))
}
async fn execute(&self, context: crank_runtime::TaskAttemptContext) -> TaskAttemptOutcome {
self.observed.fetch_add(1, Ordering::SeqCst);
commit_attempt(&context, completed_outcome(json!({"ok": true}))).await
}
}
let observed = Arc::new(AtomicUsize::new(0));
let handler: Arc<dyn TaskHandler> = Arc::new(DeniedHandler {
observed: observed.clone(),
});
let config = TaskWorkerConfig {
owner: "runtime-worker-denied".to_owned(),
batch_limit: 1,
lease_ms: 10_000,
poll_interval: Duration::from_millis(50),
max_inflight: 1,
shutdown_timeout: Duration::from_secs(5),
..TaskWorkerConfig::default()
};
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
let stop_flag = worker.stop_flag();
let worker_task = tokio::spawn(async move { worker.run().await });
let access = TaskAccess {
workspace_id: common::test_workspace_id().to_string(),
owner_kind: TaskOwnerKind::Agent,
owner_id: "agent_a".to_owned(),
authorization_scope: "credential:pk_denied".to_owned(),
};
let mut state = None;
for _ in 0..100 {
tokio::time::sleep(Duration::from_millis(50)).await;
if let Ok(record) = store.read(&access, accepted.task_id).await {
state = Some(record.state);
if record.state == TaskState::Failed {
break;
}
}
}
stop_flag.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
assert_eq!(state, Some(TaskState::Failed));
assert_eq!(
observed.load(Ordering::SeqCst),
0,
"invalidated work never reaches the upstream"
);
}
fn handler_for_no_work(
_store: std::sync::Arc<PostgresTaskStore>,
) -> std::sync::Arc<dyn crank_runtime::TaskHandler> {
struct NoWorkHandler;
#[async_trait::async_trait]
impl crank_runtime::TaskHandler for NoWorkHandler {
async fn revalidate(
&self,
_lease: &crank_core::TaskLease,
) -> Result<(), crank_runtime::TaskWorkerError> {
Ok(())
}
async fn execute(
&self,
context: crank_runtime::TaskAttemptContext,
) -> crank_runtime::TaskAttemptOutcome {
commit_attempt(&context, completed_outcome(serde_json::json!({"ok": true}))).await
}
}
std::sync::Arc::new(NoWorkHandler)
}
/// Story 4.3 end-to-end ATDD: the production durable-Task handler, the real runtime executor and a
/// real local HTTP upstream observe exactly one dispatch after the fenced checkpoint, while the
/// accepted parent trace is continued on the wire.
#[tokio::test]
async fn task_worker_dispatches_one_real_upstream_attempt_with_parent_trace() {
use axum::Router;
use axum::http::{HeaderMap, StatusCode};
use axum::routing::post;
use crank_community_mcp::TaskWorkerHandler;
use crank_core::{AgentOperationBinding, PlatformApiKeyScope};
use crank_runtime::{SecretCrypto, TaskWorker, TaskWorkerConfig};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
let registry = common::test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let seen_traceparent = Arc::new(Mutex::new(None::<String>));
let app = Router::new().route(
"/crm/leads",
post({
let calls = Arc::clone(&calls);
let seen = Arc::clone(&seen_traceparent);
move |headers: HeaderMap| {
let calls = Arc::clone(&calls);
let seen = Arc::clone(&seen);
async move {
calls.fetch_add(1, Ordering::SeqCst);
if let Some(value) = headers
.get("traceparent")
.and_then(|value| value.to_str().ok())
{
*seen.lock().unwrap() = Some(value.to_owned());
}
(StatusCode::OK, axum::Json(json!({"id": "lead-1"})))
}
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
let upstream = format!("http://{address}");
let mut operation = common::test_operation(&upstream, TOOL_NAME);
operation.execution_config.execution_policy = Some(background_policy());
registry
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
.await
.unwrap();
publish(&registry, &operation.id, 1).await;
common::publish_agent_with_bindings(
&registry,
AGENT_SLUG,
vec![AgentOperationBinding {
operation_id: operation.id.clone(),
operation_version: 1,
tool_name: operation.name.clone(),
tool_title: operation.tool_description.title.clone(),
tool_description_override: None,
enabled: true,
}],
)
.await;
let caller = principal(
&registry,
"task-worker-e2e",
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let correlation = unsampled_correlation();
let crypto = SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap();
let observed_at = OffsetDateTime::now_utc();
registry
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
epoch: crypto.master_key_epoch(),
fingerprint: crypto.master_key_fingerprint(),
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
observed_at: &observed_at,
})
.await
.expect("test master-key identity registered");
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("valid task store configuration"),
);
let tasks = TaskApplication::new(
registry.clone(),
Duration::ZERO,
Arc::new(InMemoryCoordinationStateStore::default()),
TaskService::with_secret_crypto(store.clone(), Arc::new(crypto.clone())),
);
let acceptance = tasks
.accept_background(
&caller,
TOOL_NAME,
None,
"worker-e2e-key",
&json!({"email": "user@example.com"}),
&correlation,
)
.await
.expect("durable acceptance with executable input");
let runtime = crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build();
let handler = Arc::new(TaskWorkerHandler::from_application(
&tasks,
registry.clone(),
runtime,
crypto,
));
let config = TaskWorkerConfig {
owner: "runtime-worker-e2e".to_owned(),
batch_limit: 1,
lease_ms: 10_000,
poll_interval: Duration::from_millis(50),
max_inflight: 1,
shutdown_timeout: Duration::from_secs(5),
..TaskWorkerConfig::default()
};
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
let stop_flag = worker.stop_flag();
let worker_task = tokio::spawn(async move { worker.run().await });
let mut state = None;
for _ in 0..200 {
tokio::time::sleep(Duration::from_millis(50)).await;
let current: Option<String> =
sqlx::query_scalar("select state from tasks where task_id = $1")
.bind(acceptance.task_id.as_uuid())
.fetch_optional(registry.pool())
.await
.unwrap();
state = current;
if state.as_deref() == Some("completed") {
break;
}
}
stop_flag.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
server.abort();
assert_eq!(state.as_deref(), Some("completed"));
assert_eq!(calls.load(Ordering::SeqCst), 1, "exactly one upstream call");
assert_eq!(
seen_traceparent.lock().unwrap().as_deref(),
Some(correlation.trace_context().traceparent()),
"the durable attempt continues the accepted parent trace"
);
}
/// Story 4.3 end-to-end ATDD: an ambiguous post-checkpoint upstream failure is persisted as
/// `outcome_unknown` (not a retryable failure) and is not redispatched automatically.
#[tokio::test]
async fn task_worker_ambiguous_upstream_error_becomes_outcome_unknown() {
use axum::Router;
use axum::http::StatusCode;
use axum::routing::post;
use crank_community_mcp::TaskWorkerHandler;
use crank_core::{AgentOperationBinding, PlatformApiKeyScope};
use crank_runtime::{SecretCrypto, TaskWorker, TaskWorkerConfig};
use std::sync::Arc;
use std::sync::atomic::Ordering;
let registry = common::test_registry().await;
let upstream_app = Router::new().route(
"/crm/leads",
post(|| async { (StatusCode::SERVICE_UNAVAILABLE, "upstream unavailable") }),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, upstream_app).await.unwrap() });
let upstream = format!("http://{address}");
let mut operation = common::test_operation(&upstream, TOOL_NAME);
operation.execution_config.execution_policy = Some(background_policy());
registry
.create_operation(&common::test_workspace_id(), &operation, Some("alice"))
.await
.unwrap();
publish(&registry, &operation.id, 1).await;
common::publish_agent_with_bindings(
&registry,
AGENT_SLUG,
vec![AgentOperationBinding {
operation_id: operation.id.clone(),
operation_version: 1,
tool_name: operation.name.clone(),
tool_title: operation.tool_description.title.clone(),
tool_description_override: None,
enabled: true,
}],
)
.await;
let caller = principal(
&registry,
"task-worker-uncertain",
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let correlation = unsampled_correlation();
let crypto = SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap();
let observed_at = OffsetDateTime::now_utc();
registry
.verify_or_register_master_key_identity(crank_registry::MasterKeyIdentityCandidate {
epoch: crypto.master_key_epoch(),
fingerprint: crypto.master_key_fingerprint(),
cipher_contract: crank_registry::MASTER_KEY_CIPHER_CONTRACT,
observed_at: &observed_at,
})
.await
.expect("test master-key identity registered");
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("valid task store configuration"),
);
let tasks = TaskApplication::new(
registry.clone(),
Duration::ZERO,
Arc::new(InMemoryCoordinationStateStore::default()),
TaskService::with_secret_crypto(store.clone(), Arc::new(crypto.clone())),
);
let acceptance = tasks
.accept_background(
&caller,
TOOL_NAME,
None,
"worker-uncertain-key",
&json!({"email": "user@example.com"}),
&correlation,
)
.await
.expect("durable acceptance with executable input");
let runtime = crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build();
let handler = Arc::new(TaskWorkerHandler::from_application(
&tasks,
registry.clone(),
runtime,
crypto,
));
let config = TaskWorkerConfig {
owner: "runtime-worker-uncertain".to_owned(),
batch_limit: 1,
lease_ms: 10_000,
poll_interval: Duration::from_millis(50),
max_inflight: 1,
shutdown_timeout: Duration::from_secs(5),
..TaskWorkerConfig::default()
};
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
let stop_flag = worker.stop_flag();
let worker_task = tokio::spawn(async move { worker.run().await });
let mut state = None;
for _ in 0..200 {
tokio::time::sleep(Duration::from_millis(50)).await;
let current: Option<String> =
sqlx::query_scalar("select state from tasks where task_id = $1")
.bind(acceptance.task_id.as_uuid())
.fetch_optional(registry.pool())
.await
.unwrap();
state = current;
if state.as_deref() == Some("outcome_unknown") {
break;
}
}
stop_flag.store(true, Ordering::SeqCst);
let _ = tokio::time::timeout(Duration::from_secs(10), worker_task).await;
server.abort();
assert_eq!(state.as_deref(), Some("outcome_unknown"));
// No automatic retry: another authoritative scan never redispaches the uncertain attempt.
assert!(
store
.claim_batch("worker_after", 4, 10_000)
.await
.unwrap()
.is_empty()
);
}
/// Story 4.3 ATDD: graceful stop stops new claims but lets an active attempt reach its fenced
/// terminal commit within the 30s budget.
#[tokio::test]
async fn task_worker_graceful_stop_drains_active_attempt() {
use crank_core::{TaskAccess, TaskLease, TaskOwnerKind};
use crank_runtime::{
TaskAttemptOutcome, TaskHandler, TaskWorker, TaskWorkerConfig, TaskWorkerError,
};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
let registry = common::test_registry().await;
let store = Arc::new(
PostgresTaskStore::new(
registry.pool().clone(),
AdmissionLimits {
global_limit: 4,
agent_limit: 4,
tool_limit: 4,
queue_limit: 8,
},
Default::default(),
)
.expect("valid task store configuration"),
);
let request = TaskAcceptRequest {
task_id: TaskId::generate(),
workspace_id: common::test_workspace_id().to_string(),
owner_kind: TaskOwnerKind::Agent,
owner_id: "agent_a".to_owned(),
authorization_scope: "credential:pk_drain".to_owned(),
platform_api_key_id: None,
operation_id: "op_report".to_owned(),
pins: TaskPins {
operation_version: 1,
policy_mode: ToolExecutionMode::Background,
execution_deadline_unix_ms: 1_900_000_000_000,
retain_until_unix_ms: 1_900_000_000_000 + 60_000,
approval_reference: None,
retry: crank_core::TaskRetryPolicy::single_attempt(),
},
scoped_key: "s".repeat(64),
input_fingerprint: "d".repeat(64),
input: TaskEnvelope::try_redacted(&json!({"report": "q3"})).unwrap(),
support: TaskSupportIds::new(
RequestId::generate(),
&crank_core::TraceContext::parse(
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
)
.unwrap(),
),
executable_input_ciphertext: None,
executable_input_key_version: None,
executable_input_master_key_epoch: None,
};
let accepted = store.accept(request).await.expect("PG accepts");
struct SlowHandler {
started: Arc<AtomicUsize>,
}
#[async_trait::async_trait]
impl TaskHandler for SlowHandler {
async fn revalidate(&self, _lease: &TaskLease) -> Result<(), TaskWorkerError> {
Ok(())
}
async fn execute(&self, context: crank_runtime::TaskAttemptContext) -> TaskAttemptOutcome {
self.started.fetch_add(1, Ordering::SeqCst);
tokio::time::sleep(Duration::from_millis(500)).await;
commit_attempt(&context, completed_outcome(json!({"ok": true}))).await
}
}
let started = Arc::new(AtomicUsize::new(0));
let handler: Arc<dyn TaskHandler> = Arc::new(SlowHandler {
started: started.clone(),
});
let config = TaskWorkerConfig {
owner: "runtime-worker-drain".to_owned(),
batch_limit: 1,
lease_ms: 10_000,
poll_interval: Duration::from_millis(50),
max_inflight: 1,
shutdown_timeout: Duration::from_secs(20),
..TaskWorkerConfig::default()
};
let worker = TaskWorker::new(store.clone(), handler, config).expect("valid worker");
let stop_flag = worker.stop_flag();
let worker_task = tokio::spawn(async move { worker.run().await });
for _ in 0..100 {
if started.load(Ordering::SeqCst) > 0 {
break;
}
tokio::time::sleep(Duration::from_millis(20)).await;
}
assert_eq!(started.load(Ordering::SeqCst), 1, "attempt started");
let stop_requested = std::time::Instant::now();
stop_flag.store(true, Ordering::SeqCst);
tokio::time::timeout(Duration::from_secs(30), worker_task)
.await
.expect("worker stops inside the 30s budget")
.expect("worker exits cleanly")
.expect("worker reports no error");
assert!(stop_requested.elapsed() <= Duration::from_secs(30));
let access = TaskAccess {
workspace_id: common::test_workspace_id().to_string(),
owner_kind: TaskOwnerKind::Agent,
owner_id: "agent_a".to_owned(),
authorization_scope: "credential:pk_drain".to_owned(),
};
let record = store.read(&access, accepted.task_id).await.unwrap();
assert_eq!(record.state, TaskState::Completed);
}
@@ -241,6 +241,11 @@ async fn inventory_projection_sidecar_is_provenance_only_and_names_the_projectio
assert_eq!(
triples,
vec![
(
"server".to_owned(),
"capability".to_owned(),
"logging".to_owned()
),
(
"server".to_owned(),
"capability".to_owned(),
@@ -262,7 +267,7 @@ async fn inventory_projection_sidecar_is_provenance_only_and_names_the_projectio
"tools".to_owned()
)
],
"the advertised server capabilities of the current scope are `completions`, `prompts`, `resources` and `tools`"
"the advertised server capabilities of the current scope are `logging`, `completions`, `prompts`, `resources` and `tools`"
);
// The sidecar is not the payload: byte comparison is against the frozen file only.
@@ -0,0 +1,856 @@
//! Story 3.18 acceptance tests: safe MCP server logging (AT-3.18-01..03, FR-19).
//!
//! Oracles: `docs/acceptance-test-plan.md` AT-3.18-01..03, `epics.md` Story 3.18 and the pinned
//! `logging`/`notifications/message`/`LoggingLevel` schemas of both supported revisions
//! (`2026-07-28` carries the level per request in `_meta`; `2025-11-25` serves `logging/setLevel`).
//! Every request travels through the real HTTP endpoint built by `build_test_app` over a real
//! PostgreSQL registry; the level is set through the production request path.
//!
//! The delivered events are observed on the SSE response stream, which is the only carrier the
//! pinned transport gives a server notification: a JSON body has no place for one. Where a pending
//! notification exists and the client offered `text/event-stream`, the negotiated mode is the stream.
//!
//! No external MCP client is exercised here: these are local harness results against the pinned wire
//! shapes, and the story handoff states that honestly.
use std::time::Duration;
use crank_core::PlatformApiKeyScope;
use crank_registry::PostgresRegistry;
use serde_json::{Value, json};
use super::common::{
agent_mcp_url, build_test_app, create_platform_api_key, initialize_session,
publish_agent_with_bindings, spawn_mcp_server, test_registry,
};
const MODERN_REVISION: &str = "2026-07-28";
const LEGACY_REVISION: &str = "2025-11-25";
const META_PROTOCOL_VERSION: &str = "io.modelcontextprotocol/protocolVersion";
const META_LOG_LEVEL: &str = "io.modelcontextprotocol/logLevel";
const META_CLIENT_CAPABILITIES: &str = "io.modelcontextprotocol/clientCapabilities";
const META_CLIENT_INFO: &str = "io.modelcontextprotocol/clientInfo";
const AGENT_SLUG: &str = "logging-agent";
const OTHER_AGENT_SLUG: &str = "logging-other";
/// The one safe wire detail of a malformed logging level.
const INVALID_LEVEL: &str = "invalid logging level";
/// The fixed logger name of a delivered notification.
const LOGGER_NAME: &str = "crank-mcp-server";
/// A value that must never appear in a delivered notification. It is distinctive on purpose.
const CANARY: &str = "crank-canary-9f7620e5-aa41-4b0d";
struct LoggingEndpoint {
client: reqwest::Client,
base_url: String,
api_key: String,
other_api_key: String,
registry: PostgresRegistry,
}
impl LoggingEndpoint {
fn url(&self, agent_slug: &str) -> String {
agent_mcp_url(&self.base_url, agent_slug)
}
/// One request whose response is negotiated as the SSE stream, so a pending notification really
/// has a carrier. The body is split into its notifications and the final JSON-RPC response.
async fn post_stream(
&self,
agent_slug: &str,
api_key: &str,
headers: &[(&str, &str)],
payload: Value,
) -> StreamReply {
let mut request = self
.client
.post(self.url(agent_slug))
.header(reqwest::header::ACCEPT, "text/event-stream")
.header(reqwest::header::AUTHORIZATION, format!("Bearer {api_key}"));
for (name, value) in headers {
request = request.header(*name, *value);
}
let response = request
.json(&payload)
.send()
.await
.expect("the MCP endpoint answers");
let status = response.status();
let content_type = response
.headers()
.get(reqwest::header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.unwrap_or_default()
.to_owned();
let body = response.text().await.expect("the stream body is readable");
StreamReply::parse(status, content_type, body)
}
/// One legacy request inside the initialized session of `agent_slug`.
async fn legacy_post(
&self,
agent_slug: &str,
api_key: &str,
session_id: &str,
payload: Value,
) -> StreamReply {
self.post_stream(
agent_slug,
api_key,
&[
("MCP-Protocol-Version", LEGACY_REVISION),
("MCP-Session-Id", session_id),
],
payload,
)
.await
}
/// One `logging/setLevel` request of the legacy era.
async fn set_level(&self, session_id: &str, level: Value) -> StreamReply {
self.legacy_post(
AGENT_SLUG,
&self.api_key,
session_id,
json!({
"jsonrpc": "2.0",
"id": 41,
"method": "logging/setLevel",
"params": {"level": level}
}),
)
.await
}
/// One modern request of an arbitrary method with the required per-request identity.
async fn modern_stream(&self, method: &str, params: Value) -> StreamReply {
let mut params = params;
params["_meta"] = json!({
META_PROTOCOL_VERSION: MODERN_REVISION,
META_CLIENT_CAPABILITIES: {},
META_CLIENT_INFO: {"name": "crank-logging-atdd", "version": "0.1.0"}
});
self.modern_stream_with_params(method, params).await
}
/// The same, with the caller's `params` (including a chosen `_meta`).
async fn modern_stream_with_params(&self, method: &str, params: Value) -> StreamReply {
self.post_stream(
AGENT_SLUG,
&self.api_key,
&[
("MCP-Protocol-Version", MODERN_REVISION),
("Mcp-Method", method),
],
json!({"jsonrpc": "2.0", "id": 51, "method": method, "params": params}),
)
.await
}
async fn legacy_session(&self, agent_slug: &str, api_key: &str) -> String {
initialize_session(&self.client, &self.url(agent_slug), api_key).await
}
}
struct StreamReply {
status: reqwest::StatusCode,
content_type: String,
/// The JSON-RPC response, which is the last event of the stream.
response: Value,
/// The `notifications/message` bodies delivered before it.
notifications: Vec<Value>,
/// The raw body, so a test can search it for a canary value.
raw: String,
}
impl StreamReply {
/// Parse a reply that may arrive either as an SSE stream (a pending notification needs the
/// stream) or as the plain JSON body of a method this era does not serve.
fn parse(status: reqwest::StatusCode, content_type: String, body: String) -> Self {
let mut messages = Vec::new();
for block in body.split("\n\n") {
for line in block.lines() {
let Some(data) = line.strip_prefix("data:") else {
continue;
};
let data = data.trim_start();
if data.is_empty() {
continue;
}
let value: Value = serde_json::from_str(data)
.unwrap_or_else(|error| panic!("every stream event is JSON: {data:?} {error}"));
messages.push(value);
}
}
if messages.is_empty() {
// An era-correct rejection of an unserved method is answered as a plain JSON body: it
// never carries a notification, so there is nothing to stream.
let response: Value = serde_json::from_str(&body)
.unwrap_or_else(|error| panic!("expected a JSON-RPC body, got {body:?}: {error}"));
return Self {
status,
content_type,
response,
notifications: Vec::new(),
raw: body,
};
}
let response = messages
.pop()
.unwrap_or_else(|| panic!("the stream must end with the JSON-RPC response: {body:?}"));
Self {
status,
content_type,
response,
notifications: messages,
raw: body,
}
}
fn result(&self) -> &Value {
self.response
.get("result")
.unwrap_or_else(|| panic!("expected a JSON-RPC result, got {}", self.response))
}
fn error(&self) -> (i64, String) {
let code = self
.response
.pointer("/error/code")
.and_then(Value::as_i64)
.unwrap_or_else(|| panic!("expected a JSON-RPC error, got {}", self.response));
let message = self
.response
.pointer("/error/message")
.and_then(Value::as_str)
.unwrap_or_default()
.to_owned();
(code, message)
}
fn assert_stream(&self) {
assert_eq!(self.status, reqwest::StatusCode::OK);
assert!(
self.content_type.starts_with("text/event-stream"),
"a streamed answer must carry the SSE content type, got {:?}",
self.content_type
);
}
}
/// The exact closed body of one delivered event. Nothing else is allowed to be carried.
fn expected_notification(level: &str, event: &str, outcome: &str) -> Value {
json!({
"jsonrpc": "2.0",
"method": "notifications/message",
"params": {
"level": level,
"logger": LOGGER_NAME,
"data": {"event": event, "outcome": outcome}
}
})
}
async fn logging_endpoint() -> LoggingEndpoint {
let registry = test_registry().await;
publish_agent_with_bindings(&registry, AGENT_SLUG, Vec::new()).await;
publish_agent_with_bindings(&registry, OTHER_AGENT_SLUG, Vec::new()).await;
let api_key = create_platform_api_key(
&registry,
AGENT_SLUG,
"mcp-logging",
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let other_api_key = create_platform_api_key(
&registry,
OTHER_AGENT_SLUG,
"mcp-logging-other",
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let base_url = spawn_mcp_server(build_test_app(
registry.clone(),
Duration::from_millis(0),
Some("https://crank.example.com".to_owned()),
))
.await;
LoggingEndpoint {
client: reqwest::Client::new(),
base_url,
api_key,
other_api_key,
registry,
}
}
/// AT-3.18-01/03: the advertisement equals the implemented surface on both revisions.
#[tokio::test]
async fn advertisement_declares_the_logging_capability_on_both_revisions() {
let endpoint = logging_endpoint().await;
let discover = endpoint
.client
.post(endpoint.url(AGENT_SLUG))
.header(
reqwest::header::ACCEPT,
"application/json, text/event-stream",
)
.header(
reqwest::header::AUTHORIZATION,
format!("Bearer {}", endpoint.api_key),
)
.header("MCP-Protocol-Version", MODERN_REVISION)
.header("Mcp-Method", "server/discover")
.json(&json!({
"jsonrpc": "2.0",
"id": 1,
"method": "server/discover",
"params": {"_meta": {
META_PROTOCOL_VERSION: MODERN_REVISION,
META_CLIENT_CAPABILITIES: {},
META_CLIENT_INFO: {"name": "crank-logging-atdd", "version": "0.1.0"}
}}
}))
.send()
.await
.expect("the endpoint answers");
let discover: Value = discover.json().await.expect("a JSON body");
assert_eq!(
discover["result"]["capabilities"]["logging"],
json!({}),
"the modern advertisement must declare the implemented logging capability: {discover}"
);
let initialize = endpoint
.client
.post(endpoint.url(AGENT_SLUG))
.header(
reqwest::header::ACCEPT,
"application/json, text/event-stream",
)
.header(
reqwest::header::AUTHORIZATION,
format!("Bearer {}", endpoint.api_key),
)
.json(&json!({
"jsonrpc": "2.0",
"id": 2,
"method": "initialize",
"params": {"protocolVersion": LEGACY_REVISION, "capabilities": {}}
}))
.send()
.await
.expect("the endpoint answers");
let initialize: Value = initialize.json().await.expect("a JSON body");
assert_eq!(
initialize["result"]["capabilities"]["logging"],
json!({}),
"the legacy advertisement must declare the implemented logging capability: {initialize}"
);
// Control: a capability this story does not implement is still absent, so the assertion above is
// not a blanket "advertise everything" change.
assert!(
initialize["result"]["capabilities"]
.get("io.modelcontextprotocol/tasks")
.is_none(),
"an unimplemented extension must not appear: {initialize}"
);
endpoint.registry.ping().await.expect("the registry is up");
}
/// AT-3.18-01: setting a valid legacy level is acknowledged with one safe structured event.
#[tokio::test]
async fn legacy_set_level_acknowledges_with_a_closed_safe_event() {
let endpoint = logging_endpoint().await;
let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await;
let reply = endpoint.set_level(&session, json!("debug")).await;
reply.assert_stream();
assert_eq!(
reply.result(),
&json!({}),
"the pinned `logging/setLevel` result is empty: {}",
reply.response
);
assert_eq!(
reply.notifications,
vec![
expected_notification("notice", "mcp.logging.level_applied", "applied"),
expected_notification("info", "mcp.request.served", "ok"),
],
"the acknowledgment carries the applied level and the served outcome, nothing else"
);
assert!(
reply.raw.len() < 4_096,
"one notification is a few hundred bytes, never a payload echo"
);
}
/// AT-3.18-01: after the opt-in, a served request delivers its own safe event only.
#[tokio::test]
async fn legacy_served_request_delivers_only_the_closed_safe_event() {
let endpoint = logging_endpoint().await;
let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await;
endpoint.set_level(&session, json!("debug")).await;
let reply = endpoint
.legacy_post(
AGENT_SLUG,
&endpoint.api_key,
&session,
json!({"jsonrpc": "2.0", "id": 61, "method": "tools/list", "params": {}}),
)
.await;
reply.assert_stream();
assert!(
reply.result().get("tools").is_some(),
"the business result is unchanged: {}",
reply.response
);
assert_eq!(
reply.notifications,
vec![expected_notification("info", "mcp.request.served", "ok")],
"a served request reports its outcome and nothing about the request"
);
}
/// AT-3.18-01 (redaction): a value the request carries can never reach a notification, while the
/// positive control proves the same search does find a reflected value elsewhere in the body.
#[tokio::test]
async fn a_request_value_can_never_reach_a_notification() {
let endpoint = logging_endpoint().await;
let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await;
endpoint.set_level(&session, json!("debug")).await;
let reply = endpoint
.legacy_post(
AGENT_SLUG,
&endpoint.api_key,
&session,
json!({
"jsonrpc": "2.0",
"id": 62,
"method": "tools/call",
"params": {
"name": CANARY,
"arguments": {"secret": CANARY},
"_meta": {"progressToken": CANARY}
}
}),
)
.await;
reply.assert_stream();
// Positive control: the existing product answer echoes the unknown tool name verbatim, so the
// canary is provably present in this very body — a "not found" search here would be meaningless.
let (code, message) = reply.error();
assert_eq!(code, -32602, "an unknown tool is a safe client error");
assert!(
message.contains(CANARY),
"the positive control must reflect the canary in the error message, got {message:?}"
);
// Positive control on delivery: the event is really delivered and its body is exactly the
// closed one for a rejected request, so the canary assertions below cannot pass vacuously.
assert_eq!(
reply.notifications,
vec![expected_notification(
"warning",
"mcp.request.rejected",
"invalid_params"
)],
"a rejected request must deliver exactly one safe warning event"
);
// The delivered notification is exactly the closed safe body: no canary, no argument value.
for notification in &reply.notifications {
assert_eq!(
notification["params"]["data"]
.as_object()
.map(|data| {
let mut keys: Vec<&str> = data.keys().map(String::as_str).collect();
keys.sort_unstable();
keys
})
.unwrap_or_default(),
vec!["event", "outcome"],
"a notification data object carries exactly the two closed members: {notification}"
);
assert!(
!notification.to_string().contains(CANARY),
"a request value must never reach a notification: {notification}"
);
assert!(
notification["params"].get("id").is_none(),
"a notification is not a response: {notification}"
);
}
}
/// AT-3.18-02/03: the modern era opts in per request, and only from the pinned `_meta` key.
#[tokio::test]
async fn modern_level_is_per_request_and_requires_an_explicit_opt_in() {
let endpoint = logging_endpoint().await;
let opted_in = endpoint
.modern_stream_with_params(
"tools/list",
json!({"_meta": {
META_PROTOCOL_VERSION: MODERN_REVISION,
META_CLIENT_CAPABILITIES: {},
META_CLIENT_INFO: {"name": "crank-logging-atdd", "version": "0.1.0"},
META_LOG_LEVEL: "debug"
}}),
)
.await;
opted_in.assert_stream();
assert!(opted_in.result().get("tools").is_some());
assert_eq!(
opted_in.notifications,
vec![expected_notification("info", "mcp.request.served", "ok")],
"a request that opted in receives its safe event"
);
// The pinned modern schema: without the key the server MUST NOT send any notification.
let not_opted_in = endpoint.modern_stream("tools/list", json!({})).await;
assert_eq!(not_opted_in.status, reqwest::StatusCode::OK);
assert!(
not_opted_in.result().get("tools").is_some(),
"the business answer is unchanged without an opt-in: {}",
not_opted_in.response
);
assert!(
not_opted_in.notifications.is_empty(),
"no notification without an explicit opt-in, got {:?}",
not_opted_in.notifications
);
// An unknown level is treated as "not opted in" rather than as an error of the business request,
// and it never widens the accepted vocabulary.
let unknown = endpoint
.modern_stream_with_params(
"tools/list",
json!({"_meta": {
META_PROTOCOL_VERSION: MODERN_REVISION,
META_CLIENT_CAPABILITIES: {},
META_CLIENT_INFO: {"name": "crank-logging-atdd", "version": "0.1.0"},
META_LOG_LEVEL: "verbose"
}}),
)
.await;
assert!(unknown.result().get("tools").is_some());
assert!(
unknown.notifications.is_empty(),
"an unpinned level must not opt in, got {:?}",
unknown.notifications
);
}
/// AT-3.18-03: the delivered severities follow the ranged level of the legacy session.
#[tokio::test]
async fn legacy_severity_filter_is_honoured() {
let endpoint = logging_endpoint().await;
let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await;
// `error` is more severe than both a served request and a rejected one: neither is delivered.
endpoint.set_level(&session, json!("error")).await;
let served = endpoint
.legacy_post(
AGENT_SLUG,
&endpoint.api_key,
&session,
json!({"jsonrpc": "2.0", "id": 71, "method": "tools/list", "params": {}}),
)
.await;
assert!(
served.notifications.is_empty(),
"a served event is below the `error` level, got {:?}",
served.notifications
);
let rejected = endpoint
.legacy_post(
AGENT_SLUG,
&endpoint.api_key,
&session,
json!({"jsonrpc": "2.0", "id": 72, "method": "prompts/get", "params": {}}),
)
.await;
assert_eq!(
rejected.error().0,
-32602,
"a malformed request is a client error"
);
assert!(
rejected.notifications.is_empty(),
"a warning event is below the `error` level, got {:?}",
rejected.notifications
);
// `warning` admits the rejected event, and still not the served one.
endpoint.set_level(&session, json!("warning")).await;
let rejected = endpoint
.legacy_post(
AGENT_SLUG,
&endpoint.api_key,
&session,
json!({"jsonrpc": "2.0", "id": 73, "method": "prompts/get", "params": {}}),
)
.await;
assert_eq!(
rejected.notifications,
vec![expected_notification(
"warning",
"mcp.request.rejected",
"invalid_params"
)],
"the rejection is delivered at the `warning` level"
);
let served = endpoint
.legacy_post(
AGENT_SLUG,
&endpoint.api_key,
&session,
json!({"jsonrpc": "2.0", "id": 74, "method": "tools/list", "params": {}}),
)
.await;
assert!(
served.notifications.is_empty(),
"an info event is still below the `warning` level, got {:?}",
served.notifications
);
}
/// AT-3.18-02: a malformed level is a revision-correct safe error with bounded work, and it never
/// changes the level of the client or of anybody else.
#[tokio::test]
async fn a_malformed_level_is_a_safe_error_without_state_change() {
let endpoint = logging_endpoint().await;
let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await;
endpoint.set_level(&session, json!("error")).await;
// A value outside the pinned union, a non-string shape, a missing member and an oversized value
// all share one safe detail: the client cannot tell them apart and nothing is echoed back.
for level in [
json!("verbose"),
json!("INFO"),
json!(7),
json!(null),
json!({}),
json!("i".repeat(64)),
] {
let reply = endpoint.set_level(&session, level.clone()).await;
assert_eq!(
reply.error(),
(-32602, INVALID_LEVEL.to_owned()),
"a malformed level is one safe invalid-params detail for {level}"
);
if let Some(text) = level.as_str() {
assert!(
!reply.raw.contains(text),
"the offending value must never be echoed back"
);
}
assert!(
reply.notifications.is_empty(),
"a rejected level is not an opt-in, got {:?}",
reply.notifications
);
}
// An oversized value is refused by the fixed bound: the answer stays a short safe detail and the
// value is not reflected, which is the observable half of "bounded work".
let oversized = "i".repeat(65_536);
let reply = endpoint.set_level(&session, json!(oversized)).await;
assert_eq!(reply.error(), (-32602, INVALID_LEVEL.to_owned()));
assert!(
!reply.raw.contains(&oversized),
"an oversized level must not be reflected"
);
assert!(
reply.raw.len() < 4_096,
"the answer to an oversized level is bounded, got {} bytes",
reply.raw.len()
);
let missing = endpoint
.legacy_post(
AGENT_SLUG,
&endpoint.api_key,
&session,
json!({"jsonrpc": "2.0", "id": 81, "method": "logging/setLevel", "params": {}}),
)
.await;
assert_eq!(missing.error(), (-32602, INVALID_LEVEL.to_owned()));
// The level of the session is unchanged (`error` was set before the malformed attempts), so a
// served request still delivers nothing...
let served = endpoint
.legacy_post(
AGENT_SLUG,
&endpoint.api_key,
&session,
json!({"jsonrpc": "2.0", "id": 82, "method": "tools/list", "params": {}}),
)
.await;
assert!(
served.notifications.is_empty(),
"a malformed level must not change the active level, got {:?}",
served.notifications
);
// ...and a valid level still works, which proves the assertions above are not vacuous.
endpoint.set_level(&session, json!("debug")).await;
let served = endpoint
.legacy_post(
AGENT_SLUG,
&endpoint.api_key,
&session,
json!({"jsonrpc": "2.0", "id": 83, "method": "tools/list", "params": {}}),
)
.await;
assert_eq!(served.notifications.len(), 1);
}
/// AT-3.18-02 (era matrix): the modern schema declares no `logging/setLevel`, so the same wire name
/// is a method-not-found there and never reaches the legacy service.
#[tokio::test]
async fn the_modern_era_has_no_set_level_method() {
let endpoint = logging_endpoint().await;
let reply = endpoint
.modern_stream("logging/setLevel", json!({"level": "debug"}))
.await;
let (code, message) = reply.error();
assert_eq!(
code, -32601,
"the modern revision has no logging/setLevel: {}",
reply.response
);
assert!(
!message.contains("debug"),
"the rejection must not echo the level: {message:?}"
);
// And the level was not applied through a wrong-era route: a modern request stays silent.
let after = endpoint.modern_stream("tools/list", json!({})).await;
assert!(after.notifications.is_empty());
}
/// AT-3.18-01/03: a method this era does not implement is a pinned client error, so the safe event
/// of an opted-in client describes a client-side rejection, never an internal server failure.
#[tokio::test]
async fn an_unknown_method_is_a_rejection_not_an_internal_failure() {
let endpoint = logging_endpoint().await;
let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await;
endpoint.set_level(&session, json!("info")).await;
// `server/discover` is a modern-era method, so the legacy adapter answers it with -32601.
let reply = endpoint
.legacy_post(
AGENT_SLUG,
&endpoint.api_key,
&session,
json!({
"jsonrpc": "2.0",
"id": 71,
"method": "server/discover",
"params": {}
}),
)
.await;
assert_eq!(
reply.error().0,
-32601,
"an unimplemented method is a pinned client error: {}",
reply.response
);
assert_eq!(
reply.notifications,
vec![expected_notification(
"warning",
"mcp.request.rejected",
"invalid_params"
)],
"a pinned client error must not be reported as an internal failure"
);
}
/// AT-3.18-01/03 (independence): a level belongs to one client context, so a second session and a
/// second Agent keep their own (or no) level.
#[tokio::test]
async fn a_level_never_leaks_to_another_client() {
let endpoint = logging_endpoint().await;
let session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await;
endpoint.set_level(&session, json!("debug")).await;
// Another initialized session of the same Agent never opted in.
let other_session = endpoint.legacy_session(AGENT_SLUG, &endpoint.api_key).await;
assert_ne!(
session, other_session,
"each initialize mints its own session"
);
let other = endpoint
.legacy_post(
AGENT_SLUG,
&endpoint.api_key,
&other_session,
json!({"jsonrpc": "2.0", "id": 91, "method": "tools/list", "params": {}}),
)
.await;
assert!(
other.notifications.is_empty(),
"a session that never opted in must stay silent, got {:?}",
other.notifications
);
assert!(other.result().get("tools").is_some());
// The opted-in session still receives its own events...
let opted_in = endpoint
.legacy_post(
AGENT_SLUG,
&endpoint.api_key,
&session,
json!({"jsonrpc": "2.0", "id": 92, "method": "tools/list", "params": {}}),
)
.await;
assert_eq!(opted_in.notifications.len(), 1);
// ...and another Agent's session is unaffected by this Agent's level, both before and after it
// sets its own.
let other_agent_session = endpoint
.legacy_session(OTHER_AGENT_SLUG, &endpoint.other_api_key)
.await;
let other_agent = endpoint
.legacy_post(
OTHER_AGENT_SLUG,
&endpoint.other_api_key,
&other_agent_session,
json!({"jsonrpc": "2.0", "id": 93, "method": "tools/list", "params": {}}),
)
.await;
assert!(
other_agent.notifications.is_empty(),
"another Agent must not inherit a level, got {:?}",
other_agent.notifications
);
endpoint
.legacy_post(
OTHER_AGENT_SLUG,
&endpoint.other_api_key,
&other_agent_session,
json!({"jsonrpc": "2.0", "id": 94, "method": "logging/setLevel", "params": {"level": "error"}}),
)
.await;
let other_agent_error = endpoint
.legacy_post(
OTHER_AGENT_SLUG,
&endpoint.other_api_key,
&other_agent_session,
json!({"jsonrpc": "2.0", "id": 95, "method": "tools/list", "params": {}}),
)
.await;
assert!(
other_agent_error.notifications.is_empty(),
"the other Agent's own `error` level admits no served event"
);
}
@@ -23,7 +23,7 @@ async fn advertisement_declares_prompts_on_both_revisions() {
let discover = endpoint.modern("server/discover", json!({}), None).await;
assert_eq!(
discover["result"]["capabilities"]["prompts"],
json!({"listChanged": false}),
json!({"listChanged": true}),
"the modern advertisement must declare the prompts capability: {discover}"
);
@@ -50,7 +50,7 @@ async fn advertisement_declares_prompts_on_both_revisions() {
let initialize = json_body(initialize_response).await;
assert_eq!(
initialize["result"]["capabilities"]["prompts"],
json!({"listChanged": false}),
json!({"listChanged": true}),
"the legacy advertisement must declare the prompts capability: {initialize}"
);
}
@@ -0,0 +1,928 @@
//! Story 3.19 acceptance tests (AT-3.19-01..03, FR-19): committed Resource/Prompt catalog changes
//! are announced to a client that asked for the signal of its revision, and the ordinary list
//! methods stay authoritative when a notification is lost.
//!
//! Everything here is the production path: the real loopback MCP endpoint, the real PostgreSQL
//! registry, the real immutable artifact store and real commits through the registry API. No stubbed
//! route, no faked notification, no seeded notification table.
#![allow(dead_code)]
mod scenarios;
use std::{
fs,
path::PathBuf,
sync::atomic::{AtomicU64, Ordering},
time::Duration,
};
use axum::http::header;
use crank_artifacts::ArtifactStore;
use crank_core::{
Agent, AgentId, AgentStatus, AgentVersion, PlatformApiKeyScope, ToolAccessMode, ToolGroup,
ToolSearchSettings, ToolSelectionPolicy,
ids::{PromptId, ResourceId},
prompt::{PromptDefinition, PromptSensitivity},
};
use crank_registry::model::{
AgentPromptBinding, CreatePromptDraftRequest, PublishPromptRequest,
SaveAgentCatalogConfigRequest,
resource::{CreateResourceDraftRequest, PublishResourceVersionRequest},
};
use crank_registry::{
AgentResourceBinding, CreateAgentDraftVersionRequest, CreateAgentRequest, PostgresRegistry,
PublishAgentRequest, PublishRequest,
};
use serde_json::{Value, json};
use time::OffsetDateTime;
use super::common::*;
const MODERN_REVISION: &str = "2026-07-28";
const LEGACY_REVISION: &str = "2025-11-25";
const META_PROTOCOL_VERSION: &str = "io.modelcontextprotocol/protocolVersion";
const META_CLIENT_CAPABILITIES: &str = "io.modelcontextprotocol/clientCapabilities";
const META_CLIENT_INFO: &str = "io.modelcontextprotocol/clientInfo";
const RESOURCE_LIST_CHANGED: &str = "notifications/resources/list_changed";
const PROMPT_LIST_CHANGED: &str = "notifications/prompts/list_changed";
const TOOL_LIST_CHANGED: &str = "notifications/tools/list_changed";
const SUBSCRIPTIONS_ACKNOWLEDGED: &str = "notifications/subscriptions/acknowledged";
const SUBSCRIPTION_ID_KEY: &str = "io.modelcontextprotocol/subscriptionId";
const AGENT_SLUG: &str = "sales-signals";
/// A second published Agent of the same workspace: its catalog must never reach another Agent.
const OTHER_AGENT_SLUG: &str = "sales-other";
static NEXT_ROOT: AtomicU64 = AtomicU64::new(0);
/// Test-owned artifact root (0700), removed on drop.
struct TestRoot(PathBuf);
impl TestRoot {
fn new() -> Self {
let path = std::env::temp_dir().join(format!(
"crank-mcp-signals-{}-{}",
std::process::id(),
NEXT_ROOT.fetch_add(1, Ordering::Relaxed)
));
fs::create_dir_all(&path).expect("artifact root must be creatable");
let mut permissions = fs::metadata(&path).unwrap().permissions();
std::os::unix::fs::PermissionsExt::set_mode(&mut permissions, 0o700);
fs::set_permissions(&path, permissions).expect("artifact root must be private");
Self(path)
}
}
impl Drop for TestRoot {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
}
}
/// The published fixture: one Tool, two bound Prompts, one published but initially unbound Resource.
struct SignalsEndpoint {
client: reqwest::Client,
base_url: String,
mcp_url: String,
api_key: String,
registry: PostgresRegistry,
artifact_store: ArtifactStore,
root: TestRoot,
agent_id: AgentId,
operation: crank_core::Operation<crank_schema::Schema, crank_mapping::MappingSet>,
second_operation: crank_core::Operation<crank_schema::Schema, crank_mapping::MappingSet>,
greeting: PromptId,
review: PromptId,
extra_prompt: PromptId,
notice: ResourceId,
}
impl SignalsEndpoint {
fn modern_meta() -> Value {
json!({
META_PROTOCOL_VERSION: MODERN_REVISION,
META_CLIENT_CAPABILITIES: {},
META_CLIENT_INFO: { "name": "crank-signals-atdd", "version": "0.1.0" }
})
}
async fn modern_post(&self, payload: Value) -> reqwest::Response {
self.client
.post(&self.mcp_url)
.header(header::ACCEPT, "application/json, text/event-stream")
.header(header::AUTHORIZATION, format!("Bearer {}", self.api_key))
.json(&payload)
.send()
.await
.unwrap()
}
/// One modern request whose result is a JSON body (never a stream).
async fn modern(&self, method: &str, params: Value) -> Value {
let mut params = params;
params["_meta"] = Self::modern_meta();
let response = self
.client
.post(&self.mcp_url)
.header(header::ACCEPT, "application/json")
.header(header::AUTHORIZATION, format!("Bearer {}", self.api_key))
.header("MCP-Protocol-Version", MODERN_REVISION)
.header("Mcp-Method", method)
.json(&json!({"jsonrpc": "2.0", "id": 5, "method": method, "params": params}))
.send()
.await
.unwrap();
let status = response.status();
let body = response.text().await.unwrap();
serde_json::from_str(&body).unwrap_or_else(|error| {
panic!("expected JSON for {method}, {status} {body:?}: {error}")
})
}
/// One modern request that offers both response modes and is expected to answer a JSON body
/// (an admission or validation rejection, never a stream).
async fn modern_offering_stream(&self, method: &str, params: Value) -> Value {
let mut params = params;
params["_meta"] = Self::modern_meta();
let response = self
.client
.post(&self.mcp_url)
.header(header::ACCEPT, "application/json, text/event-stream")
.header(header::AUTHORIZATION, format!("Bearer {}", self.api_key))
.header("MCP-Protocol-Version", MODERN_REVISION)
.header("Mcp-Method", method)
.json(&json!({"jsonrpc": "2.0", "id": 9, "method": method, "params": params}))
.send()
.await
.unwrap();
assert_eq!(response.status(), reqwest::StatusCode::OK);
let body = response.text().await.unwrap();
serde_json::from_str(&body).unwrap_or_else(|error| {
panic!("expected a JSON rejection for {method}: {body:?} {error}")
})
}
/// One legacy request inside the initialized session of this Agent.
async fn legacy(&self, session: &str, method: &str, params: Value) -> Value {
let response = self
.client
.post(&self.mcp_url)
.header(header::ACCEPT, "application/json, text/event-stream")
.header(header::AUTHORIZATION, format!("Bearer {}", self.api_key))
.header("MCP-Protocol-Version", LEGACY_REVISION)
.header("MCP-Session-Id", session)
.json(&json!({"jsonrpc": "2.0", "id": 6, "method": method, "params": params}))
.send()
.await
.unwrap();
let status = response.status();
let body = response.text().await.unwrap();
serde_json::from_str(&body).unwrap_or_else(|error| {
panic!("expected JSON for {method}, {status} {body:?}: {error}")
})
}
async fn legacy_session(&self) -> String {
initialize_session(&self.client, &self.mcp_url, &self.api_key).await
}
/// The legacy notification stream (`GET`), consumed as an SSE response.
async fn legacy_stream(&self, session: &str) -> SseResponse {
let response = self
.client
.get(&self.mcp_url)
.header(header::ACCEPT, "text/event-stream")
.header(header::AUTHORIZATION, format!("Bearer {}", self.api_key))
.header("MCP-Protocol-Version", LEGACY_REVISION)
.header("MCP-Session-Id", session)
.send()
.await
.unwrap();
SseResponse::assert_open(response).await
}
/// Open the modern `subscriptions/listen` stream with an explicit filter.
async fn listen(&self, request_id: Value, filter: Value) -> SseResponse {
let params = json!({ "notifications": filter, "_meta": Self::modern_meta() });
let response = self
.client
.post(&self.mcp_url)
.header(header::ACCEPT, "text/event-stream")
.header(header::AUTHORIZATION, format!("Bearer {}", self.api_key))
.header("MCP-Protocol-Version", MODERN_REVISION)
.header("Mcp-Method", "subscriptions/listen")
.json(&json!({
"jsonrpc": "2.0",
"id": request_id,
"method": "subscriptions/listen",
"params": params
}))
.send()
.await
.unwrap();
SseResponse::assert_open(response).await
}
fn agent_version(&self, version: u32) -> AgentVersion {
AgentVersion {
agent_id: self.agent_id.clone(),
version,
status: AgentStatus::Draft,
instructions: json!({}),
tool_selection_policy: ToolSelectionPolicy::default(),
created_at: OffsetDateTime::now_utc(),
}
}
async fn open_draft(&self, version: u32) {
self.registry
.create_agent_draft_version(CreateAgentDraftVersionRequest {
workspace_id: &test_workspace_id(),
agent_id: &self.agent_id,
version: &self.agent_version(version),
bindings: &[],
updated_at: &OffsetDateTime::now_utc(),
})
.await
.expect("a new Agent draft version opens");
}
/// Write the exact catalog config of one draft version: Tool bindings, Resource bindings and
/// Prompt bindings in the single accepted transition.
async fn configure_draft(
&self,
version: u32,
tool_bindings: &[crank_core::AgentOperationBinding],
resource_bindings: &[AgentResourceBinding],
prompt_bindings: &[AgentPromptBinding],
) {
self.configure_draft_with_policy(
version,
tool_bindings,
&ToolSelectionPolicy::default(),
resource_bindings,
prompt_bindings,
)
.await;
}
/// The same transition with an explicit Tool selection policy (Story 3.5: the policy is part of
/// the served `tools/list` projection).
async fn configure_draft_with_policy(
&self,
version: u32,
tool_bindings: &[crank_core::AgentOperationBinding],
tool_selection_policy: &ToolSelectionPolicy,
resource_bindings: &[AgentResourceBinding],
prompt_bindings: &[AgentPromptBinding],
) {
self.registry
.save_agent_catalog_config_editable_with_prompts(
SaveAgentCatalogConfigRequest {
workspace_id: &test_workspace_id(),
agent_id: &self.agent_id,
agent_version: version,
bindings: tool_bindings,
tool_selection_policy,
resource_bindings,
expected_state: None,
},
prompt_bindings,
)
.await
.expect("the exact catalog config of the draft version saves");
}
async fn publish_version(&self, version: u32) {
self.registry
.publish_agent(PublishAgentRequest {
workspace_id: &test_workspace_id(),
agent_id: &self.agent_id,
version,
published_at: &OffsetDateTime::now_utc(),
published_by: Some("alice"),
expected_state: None,
})
.await
.expect("the draft Agent version publishes");
}
fn tool_bindings(&self) -> Vec<crank_core::AgentOperationBinding> {
vec![binding_for_operation(&self.operation)]
}
fn both_tool_bindings(&self) -> Vec<crank_core::AgentOperationBinding> {
vec![
binding_for_operation(&self.operation),
binding_for_operation(&self.second_operation),
]
}
fn base_prompts(&self) -> Vec<AgentPromptBinding> {
vec![
AgentPromptBinding {
prompt_id: self.greeting.clone(),
prompt_version: 1,
},
AgentPromptBinding {
prompt_id: self.review.clone(),
prompt_version: 1,
},
]
}
fn extended_prompts(&self) -> Vec<AgentPromptBinding> {
let mut prompts = self.base_prompts();
prompts.push(AgentPromptBinding {
prompt_id: self.extra_prompt.clone(),
prompt_version: 1,
});
prompts
}
fn notice_binding(&self) -> AgentResourceBinding {
AgentResourceBinding {
resource_id: self.notice.clone(),
resource_version: 1,
}
}
/// The draft version this Agent would open next, so the commit helpers do not depend on the
/// order the test calls them in.
async fn next_draft_version(&self) -> u32 {
self.registry
.get_agent_summary(&test_workspace_id(), &self.agent_id)
.await
.expect("the Agent summary reads")
.expect("the fixture Agent exists")
.current_draft_version
+ 1
}
/// The committed catalog change of AT-3.19-01: one Resource binding and one Prompt binding join
/// the published Agent snapshot in a single publication.
async fn commit_resource_and_prompt_change(&self) {
let version = self.next_draft_version().await;
self.open_draft(version).await;
self.configure_draft(
version,
&self.tool_bindings(),
&[self.notice_binding()],
&self.extended_prompts(),
)
.await;
self.publish_version(version).await;
}
/// A commit that only changes the Tool catalog: the visible Resource/Prompt projection is
/// identical, so it must not produce a Resource or Prompt signal.
async fn commit_tool_only_change(&self) {
let version = self.next_draft_version().await;
self.open_draft(version).await;
self.configure_draft(
version,
&self.both_tool_bindings(),
&[self.notice_binding()],
&self.extended_prompts(),
)
.await;
self.publish_version(version).await;
}
/// A commit that only changes the Resource catalog: the visible Prompt projection is identical,
/// so it must produce a Resource signal only.
async fn commit_resource_only_change(&self) {
let version = self.next_draft_version().await;
self.open_draft(version).await;
self.configure_draft(
version,
&self.tool_bindings(),
&[self.notice_binding()],
&self.base_prompts(),
)
.await;
self.publish_version(version).await;
}
/// A commit that only changes the Prompt catalog: the visible Resource projection is identical,
/// so it must produce a Prompt signal only.
async fn commit_prompt_only_change(&self) {
let version = self.next_draft_version().await;
self.open_draft(version).await;
self.configure_draft(
version,
&self.tool_bindings(),
&[self.notice_binding()],
&self.extended_prompts(),
)
.await;
self.publish_version(version).await;
}
/// A draft-only edit: nothing is published, so no committed catalog state changed.
async fn edit_draft_without_publishing(&self) {
let version = self.next_draft_version().await;
self.open_draft(version).await;
self.configure_draft(
version,
&self.both_tool_bindings(),
&[self.notice_binding()],
&self.base_prompts(),
)
.await;
}
/// The reverse Tool-only commit: the second Tool binding leaves the published snapshot while the
/// visible Resource/Prompt projection stays identical.
async fn commit_tool_only_revert(&self) {
let version = self.next_draft_version().await;
self.open_draft(version).await;
self.configure_draft(
version,
&self.tool_bindings(),
&[self.notice_binding()],
&self.extended_prompts(),
)
.await;
self.publish_version(version).await;
}
/// The search-mode Tool selection policy of this Agent: one group over the bound Tool.
fn search_policy(&self) -> ToolSelectionPolicy {
ToolSelectionPolicy {
mode: ToolAccessMode::Search,
groups: vec![ToolGroup {
id: "crm".to_owned(),
name: "CRM".to_owned(),
description: "CRM operations".to_owned(),
tool_names: vec![self.operation.name.clone()],
}],
search: ToolSearchSettings::default(),
}
}
/// A commit that changes only the Tool selection policy: same Tool, Resource and Prompt
/// bindings, direct mode -> search mode. The served `tools/list` changes from the bound Tool to
/// `search_tools`/`call_tool`, so this is a visible Tool catalog change.
async fn commit_policy_only_change(&self) {
let version = self.next_draft_version().await;
self.open_draft(version).await;
self.configure_draft_with_policy(
version,
&self.tool_bindings(),
&self.search_policy(),
&[],
&self.base_prompts(),
)
.await;
self.publish_version(version).await;
}
/// A search-mode commit that only adds a Tool binding: `tools/list` still serves exactly
/// `search_tools`/`call_tool`, so it must not announce a Tools change.
async fn commit_search_mode_binding_change(&self) {
let version = self.next_draft_version().await;
self.open_draft(version).await;
self.configure_draft_with_policy(
version,
&self.both_tool_bindings(),
&self.search_policy(),
&[],
&self.base_prompts(),
)
.await;
self.publish_version(version).await;
}
/// Publish an independent Agent of the same workspace, bound to the first fixture operation, and
/// return its read-only MCP client key.
async fn publish_other_agent(&self, slug: &str) -> String {
let agent_id = test_agent_id(slug);
let now = OffsetDateTime::now_utc();
self.registry
.create_agent(CreateAgentRequest {
agent: &Agent {
id: agent_id.clone(),
workspace_id: test_workspace_id(),
slug: slug.to_owned(),
display_name: slug.to_owned(),
description: "Story 3.5 isolation fixture".to_owned(),
status: AgentStatus::Draft,
current_draft_version: 1,
latest_published_version: None,
created_at: now,
updated_at: now,
published_at: None,
},
version: &AgentVersion {
agent_id: agent_id.clone(),
version: 1,
status: AgentStatus::Draft,
instructions: json!({}),
tool_selection_policy: ToolSelectionPolicy::default(),
created_at: now,
},
bindings: &[binding_for_operation(&self.operation)],
})
.await
.expect("the second Agent is created");
self.registry
.publish_agent(PublishAgentRequest {
workspace_id: &test_workspace_id(),
agent_id: &agent_id,
version: 1,
published_at: &OffsetDateTime::now_utc(),
published_by: Some("alice"),
expected_state: None,
})
.await
.expect("the second Agent publishes");
create_platform_api_key(
&self.registry,
slug,
"mcp-signals-other",
&[PlatformApiKeyScope::Read],
)
.await
}
/// A committed Tool-only change of the independent Agent: it binds the second fixture Tool.
async fn commit_other_agent_tool_change(&self, slug: &str) {
let agent_id = test_agent_id(slug);
let version = AgentVersion {
agent_id: agent_id.clone(),
version: 2,
status: AgentStatus::Draft,
instructions: json!({}),
tool_selection_policy: ToolSelectionPolicy::default(),
created_at: OffsetDateTime::now_utc(),
};
self.registry
.create_agent_draft_version(CreateAgentDraftVersionRequest {
workspace_id: &test_workspace_id(),
agent_id: &agent_id,
version: &version,
bindings: &[],
updated_at: &OffsetDateTime::now_utc(),
})
.await
.expect("a second draft version of the other Agent opens");
self.registry
.save_agent_catalog_config_editable_with_prompts(
SaveAgentCatalogConfigRequest {
workspace_id: &test_workspace_id(),
agent_id: &agent_id,
agent_version: 2,
bindings: &self.both_tool_bindings(),
tool_selection_policy: &ToolSelectionPolicy::default(),
resource_bindings: &[],
expected_state: None,
},
&[],
)
.await
.expect("the other Agent's second catalog config saves");
self.registry
.publish_agent(PublishAgentRequest {
workspace_id: &test_workspace_id(),
agent_id: &agent_id,
version: 2,
published_at: &OffsetDateTime::now_utc(),
published_by: Some("alice"),
expected_state: None,
})
.await
.expect("the other Agent's second version publishes");
}
}
/// A live SSE response, read event by event.
struct SseResponse {
response: reqwest::Response,
buffer: String,
}
impl SseResponse {
async fn assert_open(response: reqwest::Response) -> Self {
assert_eq!(
response.status(),
reqwest::StatusCode::OK,
"the notification stream must be served"
);
let content_type = response
.headers()
.get(header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.unwrap_or_default()
.to_owned();
assert!(
content_type.starts_with("text/event-stream"),
"the notification stream must be an SSE response, got {content_type}"
);
Self {
response,
buffer: String::new(),
}
}
/// The payload of the next `data:` frame, or `None` when nothing arrived within `timeout`.
async fn next_data(&mut self, timeout: Duration) -> Option<Value> {
loop {
if let Some(index) = self.buffer.find("\n\n") {
let block: String = self.buffer.drain(..index + 2).collect();
for line in block.lines() {
if let Some(data) = line.strip_prefix("data:") {
return Some(
serde_json::from_str(data.trim_start())
.unwrap_or_else(|error| panic!("SSE frame must be JSON: {error}")),
);
}
}
continue;
}
let chunk = tokio::time::timeout(timeout, self.response.chunk())
.await
.ok()?
.ok()??;
self.buffer.push_str(&String::from_utf8_lossy(&chunk));
}
}
/// The next frame within the watcher's bound.
async fn expect_frame(&mut self, what: &str) -> Value {
self.next_data(Duration::from_secs(6))
.await
.unwrap_or_else(|| panic!("expected {what} on the notification stream"))
}
/// Exactly one safe negative window: no frame within `wait`.
async fn expect_silence(&mut self, wait: Duration, what: &str) {
if let Some(frame) = self.next_data(wait).await {
panic!("expected no {what}, got {frame}");
}
}
}
async fn publish_prompt(registry: &PostgresRegistry, prompt_id: &PromptId, name: &str) -> PromptId {
let definition = PromptDefinition::parse(&json!({
"name": name,
"description": format!("{name} fixture"),
"arguments": [],
"messages": [{"role": "user", "content": format!("Use {name}.")}]
}))
.expect("the fixture Prompt definition is valid");
let created = registry
.create_prompt_draft(CreatePromptDraftRequest {
workspace_id: test_workspace_id(),
prompt_id: prompt_id.clone(),
sensitivity: PromptSensitivity::Internal,
definition,
created_at: OffsetDateTime::now_utc(),
})
.await
.expect("the Prompt Draft is created");
registry
.publish_prompt_draft(PublishPromptRequest {
workspace_id: test_workspace_id(),
prompt_id: prompt_id.clone(),
expected_updated_at: created.updated_at,
published_at: OffsetDateTime::now_utc(),
})
.await
.expect("the Prompt version publishes");
prompt_id.clone()
}
/// Publish one static Resource through the production 3.6/3.7 registry transition.
async fn publish_resource(
registry: &PostgresRegistry,
store: &ArtifactStore,
resource_id: &ResourceId,
bytes: &[u8],
) {
let artifact = store
.put_registered(bytes)
.expect("the fixture bytes are registered durably");
registry
.create_resource_draft(CreateResourceDraftRequest {
workspace_id: test_workspace_id(),
resource_id: resource_id.clone(),
uri: format!("crank://resources/{}", resource_id.as_str()),
name: resource_id.as_str().to_owned(),
description: "Story 3.19 signal fixture".to_owned(),
kind: "text".to_owned(),
mime_type: "text/plain".to_owned(),
sensitivity: "internal".to_owned(),
artifact,
created_at: OffsetDateTime::now_utc(),
})
.await
.expect("the Resource Draft is created");
let current = registry
.get_resource_draft(test_workspace_id().as_str(), resource_id)
.await
.expect("the Resource Draft reads");
registry
.publish_resource_version(PublishResourceVersionRequest {
workspace_id: test_workspace_id(),
resource_id: resource_id.clone(),
expected_updated_at: current.resource.updated_at,
expected_version: current.version.version,
expected_digest: current.version.digest.clone(),
expected_size_bytes: current.version.size_bytes,
artifact: store
.put_registered(bytes)
.expect("the published bytes are registered durably"),
published_at: OffsetDateTime::now_utc(),
})
.await
.expect("the Resource version publishes");
}
async fn signals_endpoint() -> SignalsEndpoint {
let root = TestRoot::new();
let artifact_store = ArtifactStore::open(root.0.clone()).expect("the artifact store opens");
let registry = test_registry().await;
let upstream_base_url = spawn_upstream_server().await;
let operation = test_operation(&upstream_base_url, "crm_create_lead");
registry
.create_operation(&test_workspace_id(), &operation, Some("alice"))
.await
.unwrap();
registry
.publish_operation(PublishRequest {
workspace_id: &test_workspace_id(),
operation_id: &operation.id,
version: 1,
published_at: &OffsetDateTime::now_utc(),
published_by: Some("alice"),
})
.await
.unwrap();
let second_operation = test_operation(&upstream_base_url, "crm_create_second");
registry
.create_operation(&test_workspace_id(), &second_operation, Some("alice"))
.await
.unwrap();
registry
.publish_operation(PublishRequest {
workspace_id: &test_workspace_id(),
operation_id: &second_operation.id,
version: 1,
published_at: &OffsetDateTime::now_utc(),
published_by: Some("alice"),
})
.await
.unwrap();
let greeting = publish_prompt(&registry, &PromptId::new("prm_a_greeting"), "greeting").await;
let review = publish_prompt(&registry, &PromptId::new("prm_b_review"), "review").await;
let extra_prompt = publish_prompt(&registry, &PromptId::new("prm_c_extra"), "extra").await;
let notice = ResourceId::new("res_a_notice");
publish_resource(&registry, &artifact_store, &notice, b"first notice").await;
let agent_id = AgentId::new(format!("agent_{AGENT_SLUG}"));
let now = OffsetDateTime::now_utc();
registry
.create_agent(CreateAgentRequest {
agent: &Agent {
id: agent_id.clone(),
workspace_id: test_workspace_id(),
slug: AGENT_SLUG.to_owned(),
display_name: AGENT_SLUG.to_owned(),
description: "Story 3.19 signal fixture".to_owned(),
status: AgentStatus::Draft,
current_draft_version: 1,
latest_published_version: None,
created_at: now,
updated_at: now,
published_at: None,
},
version: &AgentVersion {
agent_id: agent_id.clone(),
version: 1,
status: AgentStatus::Draft,
instructions: json!({}),
tool_selection_policy: ToolSelectionPolicy::default(),
created_at: now,
},
bindings: &[binding_for_operation(&operation)],
})
.await
.expect("the draft Agent is created");
registry
.save_agent_catalog_config_editable_with_prompts(
SaveAgentCatalogConfigRequest {
workspace_id: &test_workspace_id(),
agent_id: &agent_id,
agent_version: 1,
bindings: &[binding_for_operation(&operation)],
tool_selection_policy: &ToolSelectionPolicy::default(),
resource_bindings: &[],
expected_state: None,
},
&[
AgentPromptBinding {
prompt_id: greeting.clone(),
prompt_version: 1,
},
AgentPromptBinding {
prompt_id: review.clone(),
prompt_version: 1,
},
],
)
.await
.expect("the exact bound Prompt versions are accepted");
registry
.publish_agent(PublishAgentRequest {
workspace_id: &test_workspace_id(),
agent_id: &agent_id,
version: 1,
published_at: &OffsetDateTime::now_utc(),
published_by: Some("alice"),
expected_state: None,
})
.await
.expect("the bound Agent publishes");
let api_key = create_platform_api_key(
&registry,
AGENT_SLUG,
"mcp-signals",
&[PlatformApiKeyScope::Read, PlatformApiKeyScope::Write],
)
.await;
let base_url = spawn_mcp_server(build_test_app_with_artifacts(
registry.clone(),
Duration::from_millis(0),
None,
artifact_store.clone(),
))
.await;
SignalsEndpoint {
client: reqwest::Client::new(),
mcp_url: agent_mcp_url(&base_url, AGENT_SLUG),
base_url,
api_key,
registry,
artifact_store,
root,
agent_id,
operation,
second_operation,
greeting,
review,
extra_prompt,
notice,
}
}
fn prompt_names(list: &Value) -> Vec<String> {
list["result"]["prompts"]
.as_array()
.unwrap_or_else(|| panic!("prompts/list must return a page: {list}"))
.iter()
.map(|prompt| prompt["name"].as_str().unwrap().to_owned())
.collect()
}
fn resource_uris(list: &Value) -> Vec<String> {
list["result"]["resources"]
.as_array()
.unwrap_or_else(|| panic!("resources/list must return a page: {list}"))
.iter()
.map(|resource| resource["uri"].as_str().unwrap().to_owned())
.collect()
}
fn tool_names(list: &Value) -> Vec<String> {
let mut names: Vec<String> = list["result"]["tools"]
.as_array()
.unwrap_or_else(|| panic!("tools/list must return a page: {list}"))
.iter()
.map(|tool| tool["name"].as_str().unwrap().to_owned())
.collect();
names.sort();
names
}
/// Write a stand file that only the current user may read (credential files live here).
fn write_private(path: &PathBuf, contents: &str) {
use std::io::Write as _;
use std::os::unix::fs::OpenOptionsExt as _;
let mut file = fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.mode(0o600)
.open(path)
.unwrap_or_else(|error| panic!("stand file {path:?} must be writable: {error}"));
file.write_all(contents.as_bytes())
.expect("the stand file must be written");
}
@@ -0,0 +1,791 @@
//! Story 3.19 acceptance scenarios (AT-3.19-01..03, FR-19) on the harness in the parent module.
//!
//! Moved out of `mcp_signals.rs` verbatim (code-health split, 1000-line limit); no scenario,
//! assertion or fixture changed.
use super::*;
/// Loopback disposable stand for the AC-3 representative-client evidence. This is deliberately not
/// part of the automated ATDD: it publishes the fixture catalog, serves the real MCP endpoint on
/// `127.0.0.1`, and then performs one commit per command file so an external MCP client can observe
/// the signals on its own stream.
///
/// Run it explicitly (the credential file is `0600` and never printed):
///
/// ```text
/// MCP_SIGNALS_STAND_DIR=/tmp/crank-signals-stand \
/// cargo test -p mcp-server --test integration mcp_signals::scenarios::representative_client_stand \
/// -- --ignored --exact --nocapture
/// ```
///
/// Commands (create the file, wait for `<command>.done`): `commit-both`, `commit-prompt`,
/// `commit-resource`, `commit-tool`, `draft`, `stop`.
#[tokio::test]
#[ignore = "manual loopback stand for the representative MCP Inspector client evidence"]
async fn representative_client_stand() {
let directory = PathBuf::from(
std::env::var("MCP_SIGNALS_STAND_DIR")
.expect("MCP_SIGNALS_STAND_DIR names the disposable stand directory"),
);
fs::create_dir_all(&directory).expect("the stand directory is creatable");
let endpoint = signals_endpoint().await;
write_private(
&directory.join("stand.json"),
&json!({ "url": endpoint.mcp_url, "token": endpoint.api_key }).to_string(),
);
println!("stand ready: {}", directory.join("stand.json").display());
let deadline = tokio::time::Instant::now() + Duration::from_secs(900);
while tokio::time::Instant::now() < deadline {
for command in [
"commit-both",
"commit-prompt",
"commit-resource",
"commit-tool",
"draft",
"stop",
] {
let marker = directory.join(command);
if !marker.exists() {
continue;
}
fs::remove_file(&marker).expect("the command file is consumed once");
if command == "stop" {
println!("stand stopped: {command}");
return;
}
match command {
"commit-both" => endpoint.commit_resource_and_prompt_change().await,
"commit-prompt" => endpoint.commit_prompt_only_change().await,
"commit-resource" => endpoint.commit_resource_only_change().await,
"commit-tool" => endpoint.commit_tool_only_change().await,
_ => endpoint.edit_draft_without_publishing().await,
}
write_private(&directory.join(format!("{command}.done")), "done");
println!("stand committed: {command}");
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
/// AT-3.19-01 (legacy): an open notification stream receives the Resource and Prompt list-change
/// notifications only after the catalog commit, and `resources/list` / `prompts/list` serve the new
/// state afterwards.
#[tokio::test]
async fn legacy_stream_announces_a_committed_catalog_change_after_the_commit() {
let endpoint = signals_endpoint().await;
let session = endpoint.legacy_session().await;
let mut stream = endpoint.legacy_stream(&session).await;
// The watcher takes its baseline before it can report anything; let it settle before the commit.
tokio::time::sleep(Duration::from_millis(1_500)).await;
stream
.expect_silence(Duration::from_millis(1_200), "signal before any commit")
.await;
// A draft-only edit is not a commit: nothing is announced.
endpoint.edit_draft_without_publishing().await;
stream
.expect_silence(Duration::from_millis(2_500), "signal for a draft-only edit")
.await;
endpoint.commit_resource_and_prompt_change().await;
let mut methods = Vec::new();
for _ in 0..2 {
let frame = stream
.expect_frame("the committed list-change signals")
.await;
assert_eq!(frame["jsonrpc"], json!("2.0"));
assert!(
frame.get("params").is_none(),
"a legacy list-change notification carries no parameters: {frame}"
);
methods.push(frame["method"].as_str().unwrap().to_owned());
}
methods.sort();
assert_eq!(
methods,
vec![
PROMPT_LIST_CHANGED.to_owned(),
RESOURCE_LIST_CHANGED.to_owned()
],
"one signal per changed catalog type"
);
// The ordinary list methods are authoritative for the same committed state.
let prompts = endpoint.legacy(&session, "prompts/list", json!({})).await;
assert!(
prompt_names(&prompts).contains(&"extra".to_owned()),
"prompts/list must serve the newly bound Prompt: {prompts}"
);
let resources = endpoint.legacy(&session, "resources/list", json!({})).await;
assert!(
resource_uris(&resources)
.iter()
.any(|uri| uri == &format!("crank://resources/{}", endpoint.notice.as_str())),
"resources/list must serve the newly bound Resource: {resources}"
);
// A Tool-only commit keeps the Resource/Prompt projection: only a Tools frame follows (Story 3.5).
endpoint.commit_tool_only_change().await;
let frame = stream
.expect_frame("the committed Tools list-change signal")
.await;
assert_eq!(frame["method"], TOOL_LIST_CHANGED);
assert!(
frame.get("params").is_none(),
"a legacy list-change notification carries no parameters: {frame}"
);
stream
.expect_silence(
Duration::from_millis(2_500),
"a Resource or Prompt signal for a Tool-only commit",
)
.await;
}
/// AT-3.19-01/03 (modern): `subscriptions/listen` acknowledges the honoured filter with the request
/// id, then delivers coalesced list-change notifications carrying the subscription id.
#[tokio::test]
async fn modern_listen_stream_acknowledges_then_announces_coalesced_changes() {
let endpoint = signals_endpoint().await;
let mut stream = endpoint
.listen(
json!(41),
json!({
"resourcesListChanged": true,
"promptsListChanged": true,
"toolsListChanged": true,
"resourceSubscriptions": ["crank://resources/res_a_notice"]
}),
)
.await;
let acknowledgement = stream.expect_frame("the acknowledgment").await;
assert_eq!(acknowledgement["method"], SUBSCRIPTIONS_ACKNOWLEDGED);
assert_eq!(
acknowledgement["params"]["_meta"][SUBSCRIPTION_ID_KEY],
json!(41)
);
assert_eq!(
acknowledgement["params"]["notifications"],
json!({
"promptsListChanged": true,
"resourcesListChanged": true,
"toolsListChanged": true
}),
"only the implemented list-change types are acknowledged: {acknowledgement}"
);
tokio::time::sleep(Duration::from_millis(1_500)).await;
// Two commits in quick succession coalesce into one signal per changed type.
endpoint.commit_resource_and_prompt_change().await;
endpoint.commit_tool_only_change().await;
let mut methods = Vec::new();
for _ in 0..3 {
let frame = stream
.expect_frame("the coalesced list-change signal")
.await;
assert_eq!(frame["params"]["_meta"][SUBSCRIPTION_ID_KEY], json!(41));
methods.push(frame["method"].as_str().unwrap().to_owned());
}
methods.sort();
assert_eq!(
methods,
vec![
PROMPT_LIST_CHANGED.to_owned(),
RESOURCE_LIST_CHANGED.to_owned(),
TOOL_LIST_CHANGED.to_owned()
]
);
stream
.expect_silence(
Duration::from_millis(2_500),
"a duplicate or unrequested signal",
)
.await;
// A client that missed every notification still recovers the authoritative catalog.
let prompts = endpoint.modern("prompts/list", json!({})).await;
assert!(
prompt_names(&prompts).contains(&"extra".to_owned()),
"{prompts}"
);
}
/// AT-3.19-01/03 (modern): the acknowledged filter is the delivery contract. A prompts-only stream
/// stays silent for a Resource-only commit, delivers exactly one Prompt frame with the subscription
/// id for a Prompt commit, and never delivers an unrequested type.
#[tokio::test]
async fn modern_listen_delivers_only_the_negotiated_signal_types() {
let endpoint = signals_endpoint().await;
let mut stream = endpoint
.listen(
json!("prompts-only"),
json!({
"promptsListChanged": true,
"toolsListChanged": true,
"resourceSubscriptions": ["crank://resources/res_a_notice"]
}),
)
.await;
let acknowledgement = stream.expect_frame("the acknowledgment").await;
assert_eq!(acknowledgement["method"], SUBSCRIPTIONS_ACKNOWLEDGED);
assert_eq!(
acknowledgement["params"]["_meta"][SUBSCRIPTION_ID_KEY],
json!("prompts-only")
);
assert_eq!(
acknowledgement["params"]["notifications"],
json!({ "promptsListChanged": true, "toolsListChanged": true }),
"only the implemented and requested types are acknowledged: {acknowledgement}"
);
// The watcher takes its baseline before it can report anything.
tokio::time::sleep(Duration::from_millis(1_500)).await;
// A Resource-only commit is outside the negotiated filter: a server that ignored the filter would
// emit a Resource frame here.
endpoint.commit_resource_only_change().await;
stream
.expect_silence(
Duration::from_millis(2_500),
"signal for a Resource-only commit on a prompts-only stream",
)
.await;
// A Prompt-only commit is inside it: exactly one frame, correlated by the subscription id.
endpoint.commit_prompt_only_change().await;
let frame = stream
.expect_frame("the negotiated Prompt list-change signal")
.await;
assert_eq!(frame["method"], PROMPT_LIST_CHANGED);
assert_eq!(
frame["params"]["_meta"][SUBSCRIPTION_ID_KEY],
json!("prompts-only")
);
// No Tool commit happened, so no Tools frame follows; `resourceSubscriptions` was not honoured.
stream
.expect_silence(
Duration::from_millis(2_500),
"a duplicate, Tools or URI-content signal",
)
.await;
}
/// AT-3.19-03: the unsupported URI-content subscription surface is never acknowledged, and the
/// legacy revision has no `subscriptions/listen` request at all.
#[tokio::test]
async fn unsupported_subscription_surfaces_are_refused_rather_than_faked() {
let endpoint = signals_endpoint().await;
for method in ["resources/subscribe", "resources/unsubscribe"] {
let reply = endpoint
.modern(method, json!({ "uri": "crank://resources/res_a_notice" }))
.await;
assert_eq!(
reply["error"]["code"],
json!(-32601),
"{method} must stay unimplemented: {reply}"
);
}
// The legacy revision opens its notification stream with GET only.
let session = endpoint.legacy_session().await;
let legacy_listen = endpoint
.legacy(
&session,
"subscriptions/listen",
json!({ "notifications": {} }),
)
.await;
assert_eq!(
legacy_listen["error"]["code"],
json!(-32601),
"subscriptions/listen is a modern-only request: {legacy_listen}"
);
// A malformed filter is a bounded invalid-params answer with no echoed value.
let malformed = endpoint
.modern_offering_stream(
"subscriptions/listen",
json!({ "notifications": ["promptsListChanged"] }),
)
.await;
assert_eq!(malformed["error"]["code"], json!(-32602));
assert_eq!(
malformed["error"]["message"],
json!("invalid subscriptions/listen notification filter")
);
// Without an SSE-capable client the stream cannot be served at all.
let without_sse = endpoint
.client
.post(&endpoint.mcp_url)
.header(header::ACCEPT, "application/json")
.header(
header::AUTHORIZATION,
format!("Bearer {}", endpoint.api_key),
)
.header("MCP-Protocol-Version", MODERN_REVISION)
.header("Mcp-Method", "subscriptions/listen")
.json(&json!({
"jsonrpc": "2.0",
"id": 42,
"method": "subscriptions/listen",
"params": {
"notifications": { "promptsListChanged": true },
"_meta": SignalsEndpoint::modern_meta()
}
}))
.send()
.await
.unwrap();
assert_eq!(without_sse.status(), reqwest::StatusCode::NOT_ACCEPTABLE);
}
/// AT-3.19-02: a missed notification never loses committed state — a legacy client re-initializes
/// (the retired session is a safe `404`) and reads the authoritative catalog, while the modern
/// stateless client recovers through `server/discover` and the list methods.
#[tokio::test]
async fn a_lost_notification_is_recovered_through_the_authoritative_methods() {
let endpoint = signals_endpoint().await;
// Modern recovery: the commit happens with no stream open at all.
endpoint.commit_resource_and_prompt_change().await;
let discover = endpoint.modern("server/discover", json!({})).await;
assert_eq!(
discover["result"]["capabilities"]["prompts"]["listChanged"],
json!(true)
);
assert_eq!(
discover["result"]["capabilities"]["resources"]["listChanged"],
json!(true)
);
assert_eq!(
discover["result"]["capabilities"]["tools"]["listChanged"],
json!(true),
"the Tools list-change capability is advertised: {discover}"
);
let resources = endpoint.modern("resources/list", json!({})).await;
assert!(
resource_uris(&resources)
.iter()
.any(|uri| uri == &format!("crank://resources/{}", endpoint.notice.as_str())),
"the committed Resource is served to a client that missed the signal: {resources}"
);
// Legacy recovery: the negotiated session can be dropped, and the revision-correct answer is a
// safe `404` that makes the client re-initialize instead of resuming a stale stream.
let session = endpoint.legacy_session().await;
let mut stream = endpoint.legacy_stream(&session).await;
tokio::time::sleep(Duration::from_millis(1_500)).await;
let deleted = endpoint
.client
.delete(&endpoint.mcp_url)
.header(
header::AUTHORIZATION,
format!("Bearer {}", endpoint.api_key),
)
.header("MCP-Protocol-Version", LEGACY_REVISION)
.header("MCP-Session-Id", &session)
.send()
.await
.unwrap();
assert_eq!(deleted.status(), reqwest::StatusCode::NO_CONTENT);
stream
.expect_silence(
Duration::from_millis(2_500),
"a signal on the retired session",
)
.await;
let stale_get = endpoint
.client
.get(&endpoint.mcp_url)
.header(header::ACCEPT, "text/event-stream")
.header(
header::AUTHORIZATION,
format!("Bearer {}", endpoint.api_key),
)
.header("MCP-Protocol-Version", LEGACY_REVISION)
.header("MCP-Session-Id", &session)
.send()
.await
.unwrap();
assert_eq!(
stale_get.status(),
reqwest::StatusCode::NOT_FOUND,
"a retired session is never resumed"
);
let fresh = endpoint.legacy_session().await;
let prompts = endpoint.legacy(&fresh, "prompts/list", json!({})).await;
assert!(
prompt_names(&prompts).contains(&"extra".to_owned()),
"re-initialization serves the authoritative catalog: {prompts}"
);
}
// ------------------------------------------------------------------------------------------------
// Story 3.5 (FR-14): Tool catalog change signals. A client that negotiated the Tools list-changed
// notification learns about a *committed* Tool catalog change, and `tools/list` stays the
// authoritative source for a client without the notification or after a reconnect.
// ------------------------------------------------------------------------------------------------
/// AT-3.5-01/02 (modern): the Tools signal is delivered only to the stream that negotiated it, once
/// per committed change, and `tools/list` serves the same committed snapshot.
#[tokio::test]
async fn modern_tools_list_changed_is_delivered_only_to_a_client_that_asks_for_it() {
let endpoint = signals_endpoint().await;
let mut tools = endpoint
.listen(json!(51), json!({ "toolsListChanged": true }))
.await;
let mut prompts = endpoint
.listen(json!(52), json!({ "promptsListChanged": true }))
.await;
let acknowledgement = tools.expect_frame("the Tools acknowledgment").await;
assert_eq!(acknowledgement["method"], SUBSCRIPTIONS_ACKNOWLEDGED);
assert_eq!(
acknowledgement["params"]["notifications"],
json!({ "toolsListChanged": true }),
"the Tools list-change type must be honoured and acknowledged: {acknowledgement}"
);
let other = prompts.expect_frame("the Prompts acknowledgment").await;
assert_eq!(
other["params"]["notifications"],
json!({ "promptsListChanged": true }),
"a stream that did not ask for Tools is acknowledged without it: {other}"
);
// The watchers take their baseline before they can report anything.
tokio::time::sleep(Duration::from_millis(1_500)).await;
// A draft-only edit is not a commit: nothing is announced.
endpoint.edit_draft_without_publishing().await;
tools
.expect_silence(
Duration::from_millis(2_500),
"Tools signal for a draft-only edit",
)
.await;
// A committed Resource/Prompt change is not a Tools change: the Tools stream stays silent while
// the Prompts stream receives exactly its own type.
endpoint.commit_resource_and_prompt_change().await;
let prompt_frame = prompts
.expect_frame("the committed Prompt list-change signal")
.await;
assert_eq!(prompt_frame["method"], PROMPT_LIST_CHANGED);
tools
.expect_silence(
Duration::from_millis(2_500),
"Tools signal for a Resource/Prompt commit",
)
.await;
// One committed Tool catalog change is announced exactly once.
endpoint.commit_tool_only_change().await;
let frame = tools
.expect_frame("the committed Tools list-change signal")
.await;
assert_eq!(frame["jsonrpc"], json!("2.0"));
assert_eq!(frame["method"], TOOL_LIST_CHANGED);
assert_eq!(frame["params"]["_meta"][SUBSCRIPTION_ID_KEY], json!(51));
tools
.expect_silence(Duration::from_millis(2_500), "a repeated Tools signal")
.await;
// The prompts-only stream was never told about Tools, even though the commit happened after it
// opened.
prompts
.expect_silence(Duration::from_millis(1_200), "unrequested Tools signal")
.await;
// `tools/list` is authoritative for the same committed state, notification or not.
let list = endpoint.modern("tools/list", json!({})).await;
assert_eq!(
tool_names(&list),
vec!["crm_create_lead".to_owned(), "crm_create_second".to_owned()]
);
}
/// AT-3.5-01/02 (legacy): the legacy `GET` stream carries the Tools signal after the commit; a
/// Resource/Prompt commit produces no Tools frame; a committed change of another Agent is never
/// disclosed on this stream, and `tools/list` stays authoritative.
#[tokio::test]
async fn legacy_stream_announces_a_committed_tools_change_and_never_another_agents() {
let endpoint = signals_endpoint().await;
let other_key = endpoint.publish_other_agent(OTHER_AGENT_SLUG).await;
let session = endpoint.legacy_session().await;
let mut stream = endpoint.legacy_stream(&session).await;
tokio::time::sleep(Duration::from_millis(1_500)).await;
stream
.expect_silence(Duration::from_millis(1_200), "signal before any commit")
.await;
// A committed Resource/Prompt change keeps the Tool binding set: no Tools frame.
endpoint.commit_resource_and_prompt_change().await;
let mut methods = Vec::new();
for _ in 0..2 {
let frame = stream.expect_frame("the committed catalog signals").await;
assert!(
frame.get("params").is_none(),
"a legacy list-change notification carries no parameters: {frame}"
);
methods.push(frame["method"].as_str().unwrap().to_owned());
}
methods.sort();
assert_eq!(
methods,
vec![
PROMPT_LIST_CHANGED.to_owned(),
RESOURCE_LIST_CHANGED.to_owned()
],
"a Resource/Prompt commit must not announce Tools"
);
// A committed Tool change of another Agent of the same workspace never reaches this stream or
// this Agent's `tools/list`.
endpoint
.commit_other_agent_tool_change(OTHER_AGENT_SLUG)
.await;
stream
.expect_silence(
Duration::from_millis(2_500),
"Tools signal for another Agent's commit",
)
.await;
let isolated = endpoint.legacy(&session, "tools/list", json!({})).await;
assert_eq!(
tool_names(&isolated),
vec!["crm_create_lead".to_owned()],
"another Agent's commit must not change this Agent's catalog: {isolated}"
);
// The commit really happened: the other Agent serves its own two-Tool catalog.
let other_url = agent_mcp_url(&endpoint.base_url, OTHER_AGENT_SLUG);
let other_session = initialize_session(&endpoint.client, &other_url, &other_key).await;
let other_list = endpoint
.client
.post(&other_url)
.header(header::ACCEPT, "application/json, text/event-stream")
.header(header::AUTHORIZATION, format!("Bearer {other_key}"))
.header("MCP-Protocol-Version", LEGACY_REVISION)
.header("MCP-Session-Id", &other_session)
.json(&json!({
"jsonrpc": "2.0",
"id": 7,
"method": "tools/list",
"params": {}
}))
.send()
.await
.unwrap();
let other_list: Value = serde_json::from_str(&other_list.text().await.unwrap()).unwrap();
assert_eq!(
tool_names(&other_list),
vec!["crm_create_lead".to_owned(), "crm_create_second".to_owned()],
"the other Agent's committed catalog changed: {other_list}"
);
// This Agent's committed Tool change is announced after the commit, exactly once.
endpoint.commit_tool_only_change().await;
let frame = stream
.expect_frame("the committed Tools list-change signal")
.await;
assert_eq!(frame["method"], TOOL_LIST_CHANGED);
assert!(
frame.get("params").is_none(),
"a legacy list-change notification carries no parameters: {frame}"
);
stream
.expect_silence(
Duration::from_millis(2_500),
"a repeated or unrequested signal",
)
.await;
let list = endpoint.legacy(&session, "tools/list", json!({})).await;
assert_eq!(
tool_names(&list),
vec!["crm_create_lead".to_owned(), "crm_create_second".to_owned()],
"tools/list must serve the committed catalog without the notification: {list}"
);
}
/// AT-3.5-04: the Tools signal follows the same projection `tools/list` serves, so a committed
/// publication that only changes the Tool selection policy is announced on both revisions, while a
/// committed binding change that leaves `tools/list` unchanged is silent.
#[tokio::test]
async fn a_policy_only_commit_is_announced_on_both_revisions_and_an_invisible_change_is_silent() {
let endpoint = signals_endpoint().await;
let direct_projection = vec!["crm_create_lead".to_owned()];
// `tool_names` sorts the served page.
let search_projection = vec!["call_tool".to_owned(), "search_tools".to_owned()];
// Both revisions negotiate the Tools signal and take their baseline before anything can arrive.
let mut modern = endpoint
.listen(json!(61), json!({ "toolsListChanged": true }))
.await;
let acknowledgement = modern.expect_frame("the Tools acknowledgment").await;
assert_eq!(acknowledgement["method"], SUBSCRIPTIONS_ACKNOWLEDGED);
assert_eq!(
acknowledgement["params"]["notifications"],
json!({ "toolsListChanged": true })
);
let session = endpoint.legacy_session().await;
let mut legacy = endpoint.legacy_stream(&session).await;
tokio::time::sleep(Duration::from_millis(1_500)).await;
// The served projection before the commit is the direct-mode binding itself.
assert_eq!(
tool_names(&endpoint.modern("tools/list", json!({})).await),
direct_projection
);
// A policy-only commit: identical Tool/Resource/Prompt bindings, direct -> search mode. The
// served list changes, so each negotiated stream receives exactly one Tools frame.
endpoint.commit_policy_only_change().await;
let modern_frame = modern
.expect_frame("the committed Tools list-change signal (modern)")
.await;
assert_eq!(modern_frame["method"], TOOL_LIST_CHANGED);
assert_eq!(
modern_frame["params"]["_meta"][SUBSCRIPTION_ID_KEY],
json!(61)
);
let legacy_frame = legacy
.expect_frame("the committed Tools list-change signal (legacy)")
.await;
assert_eq!(legacy_frame["method"], TOOL_LIST_CHANGED);
assert!(
legacy_frame.get("params").is_none(),
"a legacy list-change notification carries no parameters: {legacy_frame}"
);
modern
.expect_silence(
Duration::from_millis(2_500),
"a repeated modern Tools signal",
)
.await;
legacy
.expect_silence(
Duration::from_millis(2_500),
"a repeated legacy Tools signal",
)
.await;
// `tools/list` is authoritative for the new projection on both revisions.
assert_eq!(
tool_names(&endpoint.modern("tools/list", json!({})).await),
search_projection
);
assert_eq!(
tool_names(&endpoint.legacy(&session, "tools/list", json!({})).await),
search_projection
);
// The inverse direction: in search mode a binding-only commit does not change `tools/list`, so
// neither stream may claim a visible Tool change.
endpoint.commit_search_mode_binding_change().await;
modern
.expect_silence(
Duration::from_millis(3_500),
"a Tools signal for an unchanged modern tools/list",
)
.await;
legacy
.expect_silence(
Duration::from_millis(2_500),
"a Tools signal for an unchanged legacy tools/list",
)
.await;
assert_eq!(
tool_names(&endpoint.modern("tools/list", json!({})).await),
search_projection
);
}
/// AT-3.5-02/03: a client that missed the notification recovers Tool discovery from the authoritative
/// state — the modern stateless client after a reconnect, and the legacy client after re-initializing
/// a retired session — and a reconnected stream never replays the missed change.
#[tokio::test]
async fn a_reconnected_client_recovers_tools_discovery_from_the_authoritative_state() {
let endpoint = signals_endpoint().await;
// Modern: the commit happens while no stream is open at all, then a client reconnects.
endpoint.commit_tool_only_change().await;
let mut reconnected = endpoint
.listen(
json!("after-reconnect"),
json!({ "toolsListChanged": true }),
)
.await;
let acknowledgement = reconnected.expect_frame("the Tools acknowledgment").await;
assert_eq!(
acknowledgement["params"]["notifications"],
json!({ "toolsListChanged": true })
);
// The baseline of a new stream is the committed state: the missed change is not replayed.
reconnected
.expect_silence(
Duration::from_millis(2_500),
"a replayed Tools signal after reconnect",
)
.await;
let list = endpoint.modern("tools/list", json!({})).await;
assert_eq!(
tool_names(&list),
vec!["crm_create_lead".to_owned(), "crm_create_second".to_owned()],
"a client without the notification recovers the committed catalog: {list}"
);
// Legacy: one more committed Tool change is announced on the live stream, then the session is
// retired and the client re-initializes.
let session = endpoint.legacy_session().await;
let mut stale = endpoint.legacy_stream(&session).await;
tokio::time::sleep(Duration::from_millis(1_500)).await;
endpoint.commit_tool_only_revert().await;
let frame = stale
.expect_frame("the committed Tools list-change signal")
.await;
assert_eq!(frame["method"], TOOL_LIST_CHANGED);
let deleted = endpoint
.client
.delete(&endpoint.mcp_url)
.header(
header::AUTHORIZATION,
format!("Bearer {}", endpoint.api_key),
)
.header("MCP-Protocol-Version", LEGACY_REVISION)
.header("MCP-Session-Id", &session)
.send()
.await
.unwrap();
assert_eq!(deleted.status(), reqwest::StatusCode::NO_CONTENT);
let fresh = endpoint.legacy_session().await;
let list = endpoint.legacy(&fresh, "tools/list", json!({})).await;
assert_eq!(
tool_names(&list),
vec!["crm_create_lead".to_owned()],
"re-initialization serves the committed catalog: {list}"
);
// A stream reopened after the reconnect starts from the committed baseline, not from a replay.
let mut reopened = endpoint.legacy_stream(&fresh).await;
reopened
.expect_silence(
Duration::from_millis(2_500),
"a replayed Tools signal on a reconnected stream",
)
.await;
}
@@ -0,0 +1,868 @@
//! Story 3.4 (FR-14) ATDD: progress and cancellation of one synchronous `tools/call`.
//!
//! Every case drives the real HTTP endpoint of the real application against a real (counted, slow)
//! upstream and real published catalog/registry state; nothing here is a fake client of the server
//! internals. The oracles are the pinned MCP shapes: `notifications/progress` (monotonic `progress`,
//! the request's own `progressToken`, no invented `total`) and the terminal `tools/call` result.
//!
//! Cancellation is revision-correct: `2025-11-25` cancels with `notifications/cancelled`, and
//! `2026-07-28` cancels by closing the request's own response stream. Both are proven to stop the
//! work, to release the call, and to never produce a `completed` record.
use std::{
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
time::Duration,
};
use axum::{Json, Router, routing::post};
use crank_core::{InvocationStatus, OperationId, PlatformApiKeyScope};
use crank_registry::{ListInvocationLogsQuery, PostgresRegistry, PublishRequest};
use serde_json::{Value, json};
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
use tokio::net::TcpListener;
use super::common::{
agent_mcp_url, build_test_app, capturing_subscriber, create_platform_api_key,
initialize_session, post_jsonrpc, post_jsonrpc_with_headers, publish_agent_for_operation,
spawn_mcp_server, test_operation, test_registry, test_workspace_id,
};
const LEGACY: &str = "2025-11-25";
const MODERN: &str = "2026-07-28";
const META_PROTOCOL_VERSION: &str = "io.modelcontextprotocol/protocolVersion";
const META_CLIENT_CAPABILITIES: &str = "io.modelcontextprotocol/clientCapabilities";
const META_CLIENT_INFO: &str = "io.modelcontextprotocol/clientInfo";
const TOOL: &str = "crm_lifecycle_lead";
/// A call that is long enough for the client to observe real progress, short enough for a suite.
const HOLD_MS: u64 = 250;
/// A call that is still running when the client cancels it.
const LONG_HOLD_MS: u64 = 1_500;
/// The grace period after a cancellation. It must exceed [`LONG_HOLD_MS`]: a call that was *not*
/// stopped completes its upstream request and records the outcome inside this window, so the checks
/// after it prove that the cancellation stopped the call rather than only that it had not finished
/// yet.
const CANCEL_GRACE_MS: u64 = 2_500;
/// The suite depends on that order: the constant must not silently drop back below the hold.
const _: () = assert!(CANCEL_GRACE_MS > LONG_HOLD_MS);
#[derive(Clone, Copy, Debug)]
enum Revision {
Legacy,
Modern,
}
impl Revision {
const fn label(self) -> &'static str {
match self {
Self::Legacy => LEGACY,
Self::Modern => MODERN,
}
}
}
struct Upstream {
base_url: String,
started: Arc<AtomicUsize>,
completed: Arc<AtomicUsize>,
}
/// A real upstream that records when a request starts and when it finishes. A cancelled call must
/// increment `started` exactly once and never `completed`: the request really was dispatched (so the
/// outcome is not `certain`-not-sent) and its response really was abandoned (so it is not retried).
async fn spawn_counted_slow_upstream(hold_ms: u64) -> Upstream {
let started = Arc::new(AtomicUsize::new(0));
let completed = Arc::new(AtomicUsize::new(0));
let handler_started = Arc::clone(&started);
let handler_completed = Arc::clone(&completed);
let app = Router::new().route(
"/crm/lifecycle-leads",
post(move |Json(payload): Json<Value>| {
let started = Arc::clone(&handler_started);
let completed = Arc::clone(&handler_completed);
async move {
started.fetch_add(1, Ordering::SeqCst);
tokio::time::sleep(Duration::from_millis(hold_ms)).await;
completed.fetch_add(1, Ordering::SeqCst);
Json(json!({ "id": "lead_123", "email": payload["email"] }))
}
}),
);
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
Upstream {
base_url: format!("http://{address}"),
started,
completed,
}
}
struct Fixture {
client: reqwest::Client,
mcp_url: String,
api_key: String,
other_api_key: String,
registry: PostgresRegistry,
operation_id: OperationId,
upstream: Upstream,
}
async fn fixture(agent_slug: &str, hold_ms: u64, timeout_ms: u64) -> Fixture {
let upstream = spawn_counted_slow_upstream(hold_ms).await;
let registry = test_registry().await;
let mut operation = test_operation(&upstream.base_url, TOOL);
let crank_core::Target::Rest(target) = &mut operation.target;
target.path_template = "/crm/lifecycle-leads".to_owned();
operation.execution_config.timeout_ms = timeout_ms;
registry
.create_operation(&test_workspace_id(), &operation, Some("alice"))
.await
.unwrap();
registry
.publish_operation(PublishRequest {
workspace_id: &test_workspace_id(),
operation_id: &operation.id,
version: 1,
published_at: &OffsetDateTime::parse("2026-03-26T10:00:00Z", &Rfc3339).unwrap(),
published_by: Some("alice"),
})
.await
.unwrap();
publish_agent_for_operation(&registry, &operation, agent_slug).await;
let scopes = [PlatformApiKeyScope::Read, PlatformApiKeyScope::Write];
let api_key = create_platform_api_key(&registry, agent_slug, "mcp-lifecycle", &scopes).await;
let other_api_key =
create_platform_api_key(&registry, agent_slug, "mcp-lifecycle-other", &scopes).await;
let base_url = spawn_mcp_server(build_test_app(
registry.clone(),
Duration::ZERO,
Some("https://crank.example.com".to_owned()),
))
.await;
Fixture {
client: reqwest::Client::new(),
mcp_url: agent_mcp_url(&base_url, agent_slug),
api_key,
other_api_key,
registry,
operation_id: operation.id,
upstream,
}
}
/// One parsed answer: the HTTP status, the negotiated media type and every JSON-RPC frame in order.
struct Reply {
status: reqwest::StatusCode,
content_type: String,
frames: Vec<Value>,
raw: String,
}
impl Reply {
fn parse(status: reqwest::StatusCode, content_type: String, body: String) -> Self {
let mut frames = Vec::new();
for block in body.split("\n\n") {
for line in block.lines() {
let Some(data) = line.strip_prefix("data:") else {
continue;
};
let data = data.trim_start();
if data.is_empty() {
continue;
}
frames.push(
serde_json::from_str(data)
.unwrap_or_else(|error| panic!("every frame is JSON: {data:?} {error}")),
);
}
}
if frames.is_empty() && !body.trim().is_empty() {
frames.push(
serde_json::from_str(body.trim()).unwrap_or_else(|error| {
panic!("expected a JSON-RPC body, got {body:?}: {error}")
}),
);
}
Self {
status,
content_type,
frames,
raw: body,
}
}
fn assert_stream(&self) {
assert_eq!(self.status, reqwest::StatusCode::OK);
assert!(
self.content_type.starts_with("text/event-stream"),
"a progress answer must be the request's SSE stream, got {:?}: {}",
self.content_type,
self.raw
);
}
fn progress_frames(&self) -> Vec<&Value> {
self.frames
.iter()
.filter(|frame| frame["method"] == json!("notifications/progress"))
.collect()
}
fn terminal(&self) -> &Value {
let terminal = self
.frames
.last()
.unwrap_or_else(|| panic!("the stream must carry a terminal frame: {}", self.raw));
assert!(
terminal.get("method").is_none(),
"the terminal frame must be the JSON-RPC response, got {terminal}"
);
terminal
}
}
impl Fixture {
fn call_params(&self, token: Option<Value>, revision: Revision) -> Value {
let mut params = json!({
"name": TOOL,
"arguments": { "email": "lifecycle@example.com" },
});
let mut meta = match revision {
Revision::Modern => json!({
META_PROTOCOL_VERSION: MODERN,
META_CLIENT_CAPABILITIES: {},
META_CLIENT_INFO: { "name": "crank-3.4-atdd", "version": "0.1.0" },
}),
Revision::Legacy => json!({}),
};
if let Some(token) = token {
meta["progressToken"] = token;
}
if meta.as_object().is_some_and(|object| !object.is_empty()) {
params["_meta"] = meta;
}
params
}
fn headers(&self, revision: Revision, session: Option<&str>) -> Vec<(&'static str, String)> {
let mut headers = vec![("MCP-Protocol-Version", revision.label().to_owned())];
match revision {
Revision::Legacy => {
if let Some(session) = session {
headers.push(("MCP-Session-Id", session.to_owned()));
}
}
Revision::Modern => {
headers.push(("Mcp-Method", "tools/call".to_owned()));
headers.push(("Mcp-Name", TOOL.to_owned()));
}
}
headers
}
fn request(
&self,
accept: &'static str,
key: &str,
headers: &[(&'static str, String)],
payload: Value,
) -> reqwest::RequestBuilder {
let mut request = self
.client
.post(&self.mcp_url)
.header(reqwest::header::ACCEPT, accept)
.header(reqwest::header::AUTHORIZATION, format!("Bearer {key}"));
for (name, value) in headers {
request = request.header(*name, value);
}
request.json(&payload)
}
/// One `tools/call` whose answer is negotiated as the request's own SSE stream.
async fn call_stream(
&self,
revision: Revision,
id: u64,
params: Value,
session: Option<&str>,
request_id: &str,
) -> Reply {
self.call_stream_with_key(revision, &self.api_key, id, params, session, request_id)
.await
}
async fn call_stream_with_key(
&self,
revision: Revision,
key: &str,
id: u64,
params: Value,
session: Option<&str>,
request_id: &str,
) -> Reply {
self.send(
"text/event-stream",
key,
revision,
id,
params,
session,
Some(request_id),
)
.await
}
/// One `tools/call` whose answer is negotiated as a single JSON body.
async fn call_json(
&self,
revision: Revision,
id: u64,
params: Value,
session: Option<&str>,
) -> Reply {
self.send(
"application/json",
&self.api_key,
revision,
id,
params,
session,
None,
)
.await
}
#[allow(clippy::too_many_arguments)]
async fn send(
&self,
accept: &'static str,
key: &str,
revision: Revision,
id: u64,
params: Value,
session: Option<&str>,
request_id: Option<&str>,
) -> Reply {
let headers = self.headers(revision, session);
let payload =
json!({ "jsonrpc": "2.0", "id": id, "method": "tools/call", "params": params });
let mut request = self.request(accept, key, &headers, payload);
if let Some(request_id) = request_id {
request = request.header("x-request-id", request_id);
}
let response = request.send().await.expect("the MCP endpoint answers");
let status = response.status();
let content_type = response
.headers()
.get(reqwest::header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.unwrap_or_default()
.to_owned();
let body = response.text().await.expect("the body is readable");
Reply::parse(status, content_type, body)
}
async fn cancel(&self, key: &str, session: &str, request_id: Value) -> reqwest::StatusCode {
let response = self
.request(
"application/json",
key,
&[
("MCP-Protocol-Version", LEGACY.to_owned()),
("MCP-Session-Id", session.to_owned()),
],
json!({
"jsonrpc": "2.0",
"method": "notifications/cancelled",
"params": { "requestId": request_id, "reason": "user requested cancellation" }
}),
)
.send()
.await
.expect("the cancellation is acknowledged");
response.status()
}
/// Every Invocation History row of this operation and request id, oldest first.
async fn history(&self, request_id: &str) -> Vec<crank_core::InvocationLog> {
self.registry
.list_invocation_logs(ListInvocationLogsQuery {
workspace_id: &test_workspace_id(),
level: None,
status: None,
outcome_group: None,
search_text: None,
source: Some(crank_core::InvocationSource::AgentToolCall),
operation_id: Some(&self.operation_id),
agent_id: None,
created_after: None,
created_before: None,
cursor_created_at: None,
cursor_id: None,
limit: 50,
})
.await
.unwrap()
.into_iter()
.map(|record| record.log)
.filter(|log| log.request_id.as_deref() == Some(request_id))
.collect()
}
/// Wait for the counted upstream to be entered, so a cancellation happens while it really runs.
async fn wait_until_dispatched(&self) {
for _ in 0..200 {
if self.upstream.started.load(Ordering::SeqCst) > 0 {
return;
}
tokio::time::sleep(Duration::from_millis(5)).await;
}
panic!("the upstream was never dispatched");
}
}
fn progress_values(frames: &[&Value]) -> Vec<u64> {
frames
.iter()
.map(|frame| {
frame["params"]["progress"]
.as_u64()
.unwrap_or_else(|| panic!("progress must be a number, got {frame}"))
})
.collect()
}
// ---------------------------------------------------------------------------------------------
// AT-3.4-01: a negotiated stream carries bounded monotonic progress of the real execution, and a
// missing token (or a missing carrier) never changes the execution result.
// ---------------------------------------------------------------------------------------------
#[tokio::test]
async fn at_3_4_01_legacy_progress_stream_is_monotonic_and_ends_with_the_real_result() {
let fixture = fixture("lifecycle-legacy-progress", HOLD_MS, 5_000).await;
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
let reply = fixture
.call_stream(
Revision::Legacy,
501,
fixture.call_params(Some(json!("tok-legacy-progress")), Revision::Legacy),
Some(&session),
"req-legacy-progress",
)
.await;
reply.assert_stream();
let progress = reply.progress_frames();
assert!(
!progress.is_empty(),
"a token with a stream carrier must produce progress, got {}",
reply.raw
);
for frame in &progress {
assert_eq!(frame["jsonrpc"], json!("2.0"));
assert_eq!(
frame["params"]["progressToken"],
json!("tok-legacy-progress")
);
assert!(
frame["params"].get("total").is_none(),
"this server knows no total and must not invent one: {frame}"
);
assert!(
frame["params"]["message"].is_string(),
"every real step names itself: {frame}"
);
}
let values = progress_values(&progress);
assert!(
values.windows(2).all(|pair| pair[0] < pair[1]),
"progress must be strictly monotonic, got {values:?}"
);
// One event per real execution step this server actually crosses: resolved, authorized,
// dispatched. Never a timer-driven percentage and never an unbounded stream.
assert_eq!(values, vec![1, 2, 3], "got {values:?}");
let terminal = reply.terminal();
assert_eq!(terminal["id"], json!(501));
assert_eq!(terminal["error"], Value::Null);
assert_eq!(terminal["result"]["isError"], json!(false));
assert_eq!(
terminal["result"]["structuredContent"],
json!({ "id": "lead_123" })
);
assert_eq!(fixture.upstream.started.load(Ordering::SeqCst), 1);
assert_eq!(fixture.upstream.completed.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn at_3_4_01_modern_progress_stream_is_monotonic_and_ends_with_the_real_result() {
let fixture = fixture("lifecycle-modern-progress", HOLD_MS, 5_000).await;
let reply = fixture
.call_stream(
Revision::Modern,
502,
fixture.call_params(Some(json!(77)), Revision::Modern),
None,
"req-modern-progress",
)
.await;
reply.assert_stream();
let progress = reply.progress_frames();
assert!(!progress.is_empty(), "got {}", reply.raw);
for frame in &progress {
assert_eq!(frame["params"]["progressToken"], json!(77));
}
let values = progress_values(&progress);
assert!(
values.windows(2).all(|pair| pair[0] < pair[1]),
"{values:?}"
);
let terminal = reply.terminal();
assert_eq!(terminal["result"]["isError"], json!(false));
assert_eq!(
terminal["result"]["structuredContent"],
json!({ "id": "lead_123" })
);
}
// ---------------------------------------------------------------------------------------------
// AT-3.4-01 (TL correction): the official SDK sends the dual `Accept` JSON-first. A `tools/call`
// that carries a validated token and offers `text/event-stream` in that same header must still be
// answered on its own stream, wherever the media types are ordered.
// ---------------------------------------------------------------------------------------------
#[tokio::test]
async fn at_3_4_01_json_first_dual_accept_keeps_progress_for_a_valid_token() {
/// Exactly what the official MCP SDK sends when it can read a stream.
const SDK_ACCEPT: &str = "application/json, text/event-stream";
let fixture = fixture("lifecycle-json-first", HOLD_MS, 5_000).await;
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
for revision in [Revision::Legacy, Revision::Modern] {
let carrier = match revision {
Revision::Legacy => Some(session.as_str()),
Revision::Modern => None,
};
let token = json!("tok-json-first");
let reply = fixture
.send(
SDK_ACCEPT,
&fixture.api_key,
revision,
550,
fixture.call_params(Some(token.clone()), revision),
carrier,
Some("req-json-first"),
)
.await;
reply.assert_stream();
let progress = reply.progress_frames();
assert!(
!progress.is_empty(),
"{revision:?}: a valid token with an acceptable stream must produce progress, got {}",
reply.raw
);
for frame in &progress {
assert_eq!(frame["params"]["progressToken"], token, "{frame}");
assert!(
frame["params"].get("total").is_none(),
"this server knows no total: {frame}"
);
}
let values = progress_values(&progress);
assert!(
values.windows(2).all(|pair| pair[0] < pair[1]),
"{revision:?}: progress must be strictly monotonic, got {values:?}"
);
assert_eq!(values, vec![1, 2, 3], "{revision:?}: got {values:?}");
let terminal = reply.terminal();
assert_eq!(terminal["id"], json!(550), "{revision:?}");
assert_eq!(terminal["result"]["isError"], json!(false), "{revision:?}");
assert_eq!(
terminal["result"]["structuredContent"],
json!({ "id": "lead_123" }),
"{revision:?}"
);
}
// The correction is scoped to a call that can actually carry progress: the same JSON-first
// header without a token, and with an unusable token, keeps the negotiated single JSON body.
for token in [None, Some(json!("x".repeat(4096)))] {
let reply = fixture
.send(
SDK_ACCEPT,
&fixture.api_key,
Revision::Legacy,
551,
fixture.call_params(token.clone(), Revision::Legacy),
Some(&session),
None,
)
.await;
assert_eq!(reply.status, reqwest::StatusCode::OK);
assert!(
reply.content_type.starts_with("application/json"),
"token {token:?} must keep the negotiated JSON body, got {:?}: {}",
reply.content_type,
reply.raw
);
assert!(reply.progress_frames().is_empty(), "{token:?}");
assert_eq!(
reply.terminal()["result"]["structuredContent"],
json!({ "id": "lead_123" }),
"token {token:?}"
);
}
}
#[tokio::test]
async fn at_3_4_01_no_token_and_no_carrier_keep_the_result_identical() {
let fixture = fixture("lifecycle-no-token", HOLD_MS, 5_000).await;
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
// Without a token the very same call is one plain JSON body: no notification exists.
let without_token = fixture
.call_json(
Revision::Legacy,
510,
fixture.call_params(None, Revision::Legacy),
Some(&session),
)
.await;
assert_eq!(without_token.status, reqwest::StatusCode::OK);
assert!(without_token.progress_frames().is_empty());
let plain = without_token.terminal()["result"].clone();
assert_eq!(plain["isError"], json!(false));
// With a token but only the `application/json` carrier the result is the same and progress is
// simply not sent (the receiver is never obligated to send it).
let token_without_carrier = fixture
.call_json(
Revision::Legacy,
511,
fixture.call_params(Some(json!("tok-no-carrier")), Revision::Legacy),
Some(&session),
)
.await;
assert_eq!(token_without_carrier.status, reqwest::StatusCode::OK);
assert!(token_without_carrier.progress_frames().is_empty());
assert_eq!(token_without_carrier.terminal()["result"], plain);
// In the stream mode without a token the only frame is the terminal response itself.
let stream_without_token = fixture
.call_stream(
Revision::Legacy,
512,
fixture.call_params(None, Revision::Legacy),
Some(&session),
"req-no-token-stream",
)
.await;
assert!(stream_without_token.progress_frames().is_empty());
assert_eq!(stream_without_token.frames.len(), 1);
assert_eq!(stream_without_token.terminal()["result"], plain);
}
/// The streamed answer keeps the Story 3.18 projection: a client that opted in to logging gets the
/// same single closed `notifications/message` of this call before the terminal frame, next to the
/// progress events of Story 3.4.
#[tokio::test]
async fn at_3_4_01_a_progress_stream_keeps_the_logging_projection() {
let fixture = fixture("lifecycle-progress-logging", HOLD_MS, 5_000).await;
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
let set_level = fixture
.request(
"application/json",
&fixture.api_key,
&[
("MCP-Protocol-Version", LEGACY.to_owned()),
("MCP-Session-Id", session.clone()),
],
json!({
"jsonrpc": "2.0", "id": 900, "method": "logging/setLevel",
"params": { "level": "debug" }
}),
)
.send()
.await
.expect("the level is accepted");
assert_eq!(set_level.status(), reqwest::StatusCode::OK);
let reply = fixture
.call_stream(
Revision::Legacy,
901,
fixture.call_params(Some(json!("tok-logging")), Revision::Legacy),
Some(&session),
"req-progress-logging",
)
.await;
reply.assert_stream();
assert!(!reply.progress_frames().is_empty(), "got {}", reply.raw);
let logged: Vec<&Value> = reply
.frames
.iter()
.filter(|frame| frame["method"] == json!("notifications/message"))
.collect();
assert_eq!(
logged.len(),
1,
"exactly one closed log event per response, got {}",
reply.raw
);
assert_eq!(
logged[0]["params"]["data"]["event"],
json!("mcp.request.served")
);
assert_eq!(logged[0]["params"]["data"]["outcome"], json!("ok"));
assert_eq!(reply.terminal()["result"]["isError"], json!(false));
}
// ---------------------------------------------------------------------------------------------
// AT-3.4-02: a malformed, reused-in-another-scope or oversized token disables progress, and the
// token never becomes a metric label, a log field or an authorization scope.
// ---------------------------------------------------------------------------------------------
#[tokio::test]
async fn at_3_4_02_malformed_and_oversized_tokens_disable_progress_without_changing_the_result() {
let fixture = fixture("lifecycle-bad-token", HOLD_MS, 5_000).await;
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
// Both supported revisions read the token from the same pinned `RequestMetaObject` field, so an
// unusable token must behave identically in each of them: no progress, unchanged result.
for revision in [Revision::Legacy, Revision::Modern] {
let carrier = match revision {
Revision::Legacy => Some(session.as_str()),
Revision::Modern => None,
};
let tokens = [
json!(""),
json!({ "not": "a token" }),
json!([1, 2]),
json!(true),
json!("x".repeat(4096)),
];
let mut observed = Vec::new();
for token in tokens {
let reply = fixture
.call_stream(
revision,
520,
fixture.call_params(Some(token.clone()), revision),
carrier,
"req-bad-token",
)
.await;
observed.push((token, reply));
}
for (token, reply) in observed {
assert!(
reply.progress_frames().is_empty(),
"{revision:?}: token {token} must not produce progress, got {}",
reply.raw
);
let terminal = reply.terminal();
assert_eq!(
terminal["result"]["structuredContent"],
json!({ "id": "lead_123" }),
"{revision:?}: token {token} must not change the execution result, got {terminal}"
);
assert_eq!(terminal["result"]["isError"], json!(false));
assert!(terminal["error"].is_null() || terminal.get("error").is_none());
}
}
}
#[tokio::test]
async fn at_3_4_02_a_reused_token_is_scoped_to_its_own_request() {
let fixture = fixture("lifecycle-reused-token", HOLD_MS, 5_000).await;
let first = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
let second = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
let token = json!("tok-shared");
let (first_reply, second_reply) = tokio::join!(
fixture.call_stream(
Revision::Legacy,
530,
fixture.call_params(Some(token.clone()), Revision::Legacy),
Some(&first),
"req-first",
),
fixture.call_stream(
Revision::Legacy,
531,
fixture.call_params(Some(token.clone()), Revision::Legacy),
Some(&second),
"req-second",
)
);
for (reply, id) in [(&first_reply, 530), (&second_reply, 531)] {
reply.assert_stream();
let values = progress_values(&reply.progress_frames());
// Each request counts its own real steps: a reused token never inherits another call's count.
assert_eq!(values, vec![1, 2, 3], "request {id} got {values:?}");
assert_eq!(reply.terminal()["id"], json!(id));
}
}
#[tokio::test]
async fn at_3_4_02_a_progress_token_never_reaches_logs_or_invocation_history() {
const CANARY: &str = "crank-progress-canary-4f1c2d";
let fixture = fixture("lifecycle-token-leak", HOLD_MS, 5_000).await;
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
let (writer, subscriber) = capturing_subscriber();
let reply = {
let _guard = tracing::subscriber::set_default(subscriber);
fixture
.call_stream(
Revision::Legacy,
540,
fixture.call_params(Some(json!(CANARY)), Revision::Legacy),
Some(&session),
"req-token-leak",
)
.await
};
reply.assert_stream();
let logs = writer.output();
assert!(
logs.contains("mcp request received"),
"the capture must really see the endpoint's own log projection, got {logs}"
);
assert!(
!logs.contains(CANARY),
"the progress token is client data and must never be logged, got {logs}"
);
let history = fixture.history("req-token-leak").await;
assert_eq!(history.len(), 1, "got {history:?}");
let row = &history[0];
let serialized = serde_json::to_string(row).unwrap();
assert!(
!serialized.contains(CANARY),
"the progress token must never reach Invocation History, got {serialized}"
);
assert_eq!(row.status, InvocationStatus::Ok);
}
// AT-3.4-03: a cancellation stops a call that can still be stopped, releases it and never yields a
// `completed` result or record.
mod cancellation;
// AT-3.4-05: the client-visible smoke of both supported revisions.
mod smoke;
@@ -0,0 +1,294 @@
//! AT-3.4-03: a cancellation stops a call that can still be stopped, releases it, and never yields a
//! `completed` result or history row. The harness of the suite lives in `super`.
use super::*;
// ---------------------------------------------------------------------------------------------
// AT-3.4-03: a cancellation stops a call that can still be stopped, releases it, and never yields a
// `completed` result or history row.
// ---------------------------------------------------------------------------------------------
#[tokio::test]
async fn at_3_4_03_legacy_cancellation_stops_the_call_and_never_completes_it() {
let fixture = fixture("lifecycle-legacy-cancel", LONG_HOLD_MS, 30_000).await;
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
let call_session = session.clone();
let call = tokio::spawn({
let fixture_client = fixture.client.clone();
let mcp_url = fixture.mcp_url.clone();
let api_key = fixture.api_key.clone();
let params = fixture.call_params(Some(json!("tok-cancel")), Revision::Legacy);
async move {
let response = fixture_client
.post(mcp_url)
.header(reqwest::header::ACCEPT, "text/event-stream")
.header(reqwest::header::AUTHORIZATION, format!("Bearer {api_key}"))
.header("MCP-Protocol-Version", LEGACY)
.header("MCP-Session-Id", call_session)
.header("x-request-id", "req-legacy-cancel")
.json(&json!({
"jsonrpc": "2.0",
"id": 601,
"method": "tools/call",
"params": params,
}))
.send()
.await
.unwrap();
(response.status(), response.text().await.unwrap())
}
});
fixture.wait_until_dispatched().await;
assert_eq!(
fixture.cancel(&fixture.api_key, &session, json!(601)).await,
reqwest::StatusCode::ACCEPTED,
"an accepted cancellation is acknowledged without a body"
);
let (status, body) = tokio::time::timeout(Duration::from_secs(10), call)
.await
.expect("the cancelled call is released, it does not hang")
.expect("the request task joins");
assert_eq!(status, reqwest::StatusCode::OK);
let reply = Reply::parse(status, String::new(), body);
assert!(
reply
.frames
.iter()
.all(|frame| frame.get("method").is_some()),
"a cancelled request gets no terminal message, only the progress it already saw: {}",
reply.raw
);
assert_eq!(
reply.progress_frames().len(),
3,
"the client really saw the real steps before the stop: {}",
reply.raw
);
tokio::time::sleep(Duration::from_millis(CANCEL_GRACE_MS)).await;
assert_eq!(
fixture.upstream.completed.load(Ordering::SeqCst),
0,
"the abandoned upstream response must never complete the call"
);
assert_eq!(
fixture.upstream.started.load(Ordering::SeqCst),
1,
"the write really was dispatched and must not be retried automatically"
);
let history = fixture.history("req-legacy-cancel").await;
assert!(
history.iter().all(|row| row.status != InvocationStatus::Ok),
"a cancelled call must never be recorded as completed, got {history:?}"
);
// The record contract admits a non-`ok` row only with a closed execution error code, and a
// cancellation is not one of them: the honest terminal state of this call is "no record" instead
// of an invented error code. Either way nothing claims that the call completed.
assert!(
history.is_empty(),
"a stopped call has no terminal outcome to record, got {history:?}"
);
}
#[tokio::test]
async fn at_3_4_03_a_foreign_or_unknown_cancellation_never_touches_the_active_call() {
let fixture = fixture("lifecycle-foreign-cancel", HOLD_MS, 5_000).await;
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
let foreign_session =
initialize_session(&fixture.client, &fixture.mcp_url, &fixture.other_api_key).await;
let call = tokio::spawn({
let client = fixture.client.clone();
let mcp_url = fixture.mcp_url.clone();
let api_key = fixture.api_key.clone();
let call_session = session.clone();
let params = fixture.call_params(Some(json!("tok-owner")), Revision::Legacy);
async move {
let response = client
.post(mcp_url)
.header(reqwest::header::ACCEPT, "text/event-stream")
.header(reqwest::header::AUTHORIZATION, format!("Bearer {api_key}"))
.header("MCP-Protocol-Version", LEGACY)
.header("MCP-Session-Id", call_session)
.header("x-request-id", "req-owner")
.json(&json!({
"jsonrpc": "2.0", "id": 602, "method": "tools/call", "params": params,
}))
.send()
.await
.unwrap();
response.text().await.unwrap()
}
});
fixture.wait_until_dispatched().await;
// A foreign credential of the same agent, and an unknown request id: neither may cancel this call.
assert_eq!(
fixture
.cancel(&fixture.other_api_key, &session, json!(602))
.await,
reqwest::StatusCode::ACCEPTED
);
assert_eq!(
fixture
.cancel(&fixture.api_key, &session, json!(999_999))
.await,
reqwest::StatusCode::ACCEPTED
);
// A foreign session of the same credential cannot reach this call either.
assert_eq!(
fixture
.cancel(&fixture.api_key, &foreign_session, json!(602))
.await,
reqwest::StatusCode::ACCEPTED
);
let body = tokio::time::timeout(Duration::from_secs(10), call)
.await
.expect("the untouched call completes")
.expect("the request task joins");
let reply = Reply::parse(reqwest::StatusCode::OK, String::new(), body);
assert_eq!(
reply.terminal()["result"]["structuredContent"],
json!({ "id": "lead_123" }),
"a foreign cancellation must not stop the owner's call, got {}",
reply.raw
);
let history = fixture.history("req-owner").await;
assert_eq!(history.len(), 1, "got {history:?}");
assert_eq!(history[0].status, InvocationStatus::Ok);
}
#[tokio::test]
async fn at_3_4_03_a_late_cancellation_after_the_result_is_ignored() {
let fixture = fixture("lifecycle-late-cancel", HOLD_MS, 5_000).await;
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
let reply = fixture
.call_stream(
Revision::Legacy,
603,
fixture.call_params(Some(json!("tok-late")), Revision::Legacy),
Some(&session),
"req-late-cancel",
)
.await;
reply.assert_stream();
assert_eq!(reply.terminal()["result"]["isError"], json!(false));
assert_eq!(
fixture.cancel(&fixture.api_key, &session, json!(603)).await,
reqwest::StatusCode::ACCEPTED
);
tokio::time::sleep(Duration::from_millis(200)).await;
let history = fixture.history("req-late-cancel").await;
assert_eq!(
history.len(),
1,
"a cancellation of a finished call must not add a second record, got {history:?}"
);
assert_eq!(history[0].status, InvocationStatus::Ok);
}
#[tokio::test]
async fn at_3_4_03_a_plain_json_call_is_cancellable_too() {
let fixture = fixture("lifecycle-json-cancel", LONG_HOLD_MS, 30_000).await;
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
let call = tokio::spawn({
let client = fixture.client.clone();
let mcp_url = fixture.mcp_url.clone();
let api_key = fixture.api_key.clone();
let call_session = session.clone();
let params = fixture.call_params(None, Revision::Legacy);
async move {
let response = client
.post(mcp_url)
.header(reqwest::header::ACCEPT, "application/json")
.header(reqwest::header::AUTHORIZATION, format!("Bearer {api_key}"))
.header("MCP-Protocol-Version", LEGACY)
.header("MCP-Session-Id", call_session)
.header("x-request-id", "req-json-cancel")
.json(&json!({
"jsonrpc": "2.0", "id": 605, "method": "tools/call", "params": params,
}))
.send()
.await
.unwrap();
let status = response.status();
(status, response.text().await.unwrap())
}
});
fixture.wait_until_dispatched().await;
assert_eq!(
fixture.cancel(&fixture.api_key, &session, json!(605)).await,
reqwest::StatusCode::ACCEPTED
);
let (status, body) = tokio::time::timeout(Duration::from_secs(10), call)
.await
.expect("the cancelled call is released")
.expect("the request task joins");
assert_eq!(status, reqwest::StatusCode::OK);
let reply = Reply::parse(status, String::new(), body);
assert!(
reply.frames.is_empty(),
"a cancelled request gets no terminal message, got {}",
reply.raw
);
tokio::time::sleep(Duration::from_millis(CANCEL_GRACE_MS)).await;
assert_eq!(fixture.upstream.completed.load(Ordering::SeqCst), 0);
let history = fixture.history("req-json-cancel").await;
assert!(
history.iter().all(|row| row.status != InvocationStatus::Ok),
"a cancelled call must never be recorded as completed, got {history:?}"
);
}
#[tokio::test]
async fn at_3_4_03_closing_the_modern_response_stream_cancels_the_call() {
let fixture = fixture("lifecycle-modern-disconnect", LONG_HOLD_MS, 30_000).await;
let response = fixture
.request(
"text/event-stream",
&fixture.api_key,
&fixture.headers(Revision::Modern, None),
json!({
"jsonrpc": "2.0",
"id": 604,
"method": "tools/call",
"params": fixture.call_params(Some(json!("tok-disconnect")), Revision::Modern),
}),
)
.header("x-request-id", "req-modern-disconnect")
.send()
.await
.expect("the stream opens before the call finishes");
assert_eq!(response.status(), reqwest::StatusCode::OK);
fixture.wait_until_dispatched().await;
// The pinned `2026-07-28` cancellation signal: closing the request's own response stream.
drop(response);
tokio::time::sleep(Duration::from_millis(CANCEL_GRACE_MS)).await;
assert_eq!(
fixture.upstream.completed.load(Ordering::SeqCst),
0,
"the closed stream must stop the dispatched call instead of letting it finish"
);
assert_eq!(fixture.upstream.started.load(Ordering::SeqCst), 1);
let history = fixture.history("req-modern-disconnect").await;
assert!(
history.iter().all(|row| row.status != InvocationStatus::Ok),
"a cancelled call must never be recorded as completed, got {history:?}"
);
}
@@ -0,0 +1,79 @@
//! AT-3.4-05: the client-visible smoke of both supported revisions.
//!
//! A representative client of each revision performs the whole flow a real MCP client performs —
//! discovery, then a `tools/call` that asks for progress and reads the request's own stream to the
//! terminal response — against the real endpoint. The harness of the suite lives in `super`.
use super::*;
#[tokio::test]
async fn at_3_4_05_representative_client_smoke_of_both_revisions() {
let fixture = fixture("lifecycle-client-smoke", HOLD_MS, 5_000).await;
// Legacy client: initialize → tools/list → tools/call with progress.
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
let tools = post_jsonrpc(
&fixture.client,
&fixture.mcp_url,
&fixture.api_key,
Some(&session),
json!({ "jsonrpc": "2.0", "id": 700, "method": "tools/list", "params": {} }),
)
.await;
assert_eq!(tools["result"]["tools"][0]["name"], json!(TOOL));
let legacy_call = fixture
.call_stream(
Revision::Legacy,
701,
fixture.call_params(Some(json!("tok-smoke-legacy")), Revision::Legacy),
Some(&session),
"req-smoke-legacy",
)
.await;
legacy_call.assert_stream();
assert_eq!(
legacy_call.terminal()["result"]["structuredContent"],
json!({ "id": "lead_123" })
);
// Modern client: `tools/list` and `tools/call` of the stateless revision, progress on the stream.
let modern_list = post_jsonrpc_with_headers(
&fixture.client,
&fixture.mcp_url,
&fixture.api_key,
&[
("MCP-Protocol-Version", MODERN),
("Mcp-Method", "tools/list"),
],
json!({
"jsonrpc": "2.0",
"id": 710,
"method": "tools/list",
"params": { "_meta": {
META_PROTOCOL_VERSION: MODERN,
META_CLIENT_CAPABILITIES: {},
META_CLIENT_INFO: { "name": "crank-3.4-smoke", "version": "0.1.0" },
}}
}),
)
.await;
let modern_list = modern_list.json::<Value>().await.unwrap();
assert_eq!(modern_list["result"]["tools"][0]["name"], json!(TOOL));
let modern_call = fixture
.call_stream(
Revision::Modern,
711,
fixture.call_params(Some(json!("tok-smoke-modern")), Revision::Modern),
None,
"req-smoke-modern",
)
.await;
modern_call.assert_stream();
assert!(!modern_call.progress_frames().is_empty());
assert_eq!(
modern_call.terminal()["result"]["structuredContent"],
json!({ "id": "lead_123" })
);
}
+5
View File
@@ -1,5 +1,10 @@
FROM node:22-alpine AS build
# The CI host uplink drops TLS 1.3 ClientHello carrying the hybrid
# X25519MLKEM768 key share; pin the npm stage to TLS 1.2 until that network
# defect is fixed. Certificates, SRI and the lockfile are unaffected.
ENV NODE_OPTIONS=--tls-max-v1.2
WORKDIR /app
COPY apps/ui/package.json apps/ui/package-lock.json ./
+2
View File
@@ -1036,6 +1036,7 @@ var TRANSLATIONS = {
'wizard.error.secret_name_required': 'Secret name is required.',
'wizard.error.secret_value_required': 'Secret value is required.',
'wizard.error.save_upstream_first': 'Save the API host after creating a new authorization profile before continuing.',
'wizard.error.execution_policy_invalid': 'Execution policy is invalid: mode, deadline from 1 to 3600000 ms and no-Tasks fallback are required.',
'wizard.toast.auth_profile_created_title': 'Authorization profile created',
'wizard.toast.auth_profile_created_body': 'New authorization profile is ready and selected.',
'wizard.toast.auth_profile_error_title': 'Authorization profile creation failed',
@@ -2353,6 +2354,7 @@ var TRANSLATIONS = {
'wizard.error.secret_name_required': 'Нужно указать имя секрета.',
'wizard.error.secret_value_required': 'Нужно указать значение секрета.',
'wizard.error.save_upstream_first': 'Сначала сохраните API-хост после создания нового профиля авторизации.',
'wizard.error.execution_policy_invalid': 'Политика выполнения некорректна: нужны режим, deadline от 1 до 3600000 мс и поведение без Tasks.',
'wizard.toast.auth_profile_created_title': 'Профиль авторизации создан',
'wizard.toast.auth_profile_created_body': 'Новый профиль авторизации готов и уже выбран.',
'wizard.toast.auth_profile_error_title': 'Не удалось создать профиль авторизации',
+30
View File
@@ -214,10 +214,33 @@ function parseExecutionConfig(text) {
auth_profile_ref: authProfileRef,
headers: headers,
};
var executionPolicy = parseExecutionPolicy(value.execution_policy);
if (executionPolicy) {
config.execution_policy = executionPolicy;
}
return config;
}
// Story 4.1: the published execution policy must survive a wizard save. The editor is a free-form
// document, so unknown/legacy shapes stay untouched: only a fully valid policy is projected, and an
// explicit invalid value is reported instead of being silently replaced by defaults.
function parseExecutionPolicy(raw) {
if (raw === undefined || raw === null) return null;
if (typeof raw !== 'object' || Array.isArray(raw)) {
throw new Error(tKey('wizard.error.execution_policy_invalid'));
}
var mode = raw.mode === undefined ? 'synchronous' : raw.mode;
var fallback = raw.no_tasks_fallback === undefined ? 'synchronous' : raw.no_tasks_fallback;
var deadline = Number(raw.deadline_ms);
if (['synchronous', 'background', 'adaptive'].indexOf(mode) < 0
|| ['synchronous', 'unsupported'].indexOf(fallback) < 0
|| !Number.isInteger(deadline) || deadline < 1 || deadline > 3600000) {
throw new Error(tKey('wizard.error.execution_policy_invalid'));
}
return { mode: mode, deadline_ms: deadline, no_tasks_fallback: fallback };
}
function currentUpstream() {
var selected = upstreams.find(function(item) { return item.id === selectedUpstreamId; });
if (selected) return selected;
@@ -415,6 +438,13 @@ function executionConfigToEditorValue(config) {
var value = {
timeout_ms: config.timeout_ms,
};
if (config.execution_policy) {
value.execution_policy = {
mode: config.execution_policy.mode,
deadline_ms: config.execution_policy.deadline_ms,
no_tasks_fallback: config.execution_policy.no_tasks_fallback,
};
}
if (config.retry_policy) {
value.retry = { max_attempts: config.retry_policy.max_attempts };
}
@@ -0,0 +1,139 @@
const { test, expect } = require('@playwright/test');
const { getCurrentWorkspace, login } = require('./helpers');
// Story 4.1 (FR-21): the published execution policy is part of the Tool version and must survive a
// wizard save. The editor is a free-form document, so a no-op edit must not erase it and an explicit
// invalid value must be reported instead of being silently replaced by defaults.
function draftSnapshot(operationId) {
return {
id: operationId,
name: operationId,
display_name: 'Policy rates',
category: 'general',
protocol: 'rest',
target: {
kind: 'rest',
base_url: 'https://api.example.test',
method: 'GET',
path_template: '/rates',
static_headers: {},
},
input_schema: {
type: 'object',
description: null,
required: false,
nullable: false,
default_value: null,
fields: {},
items: null,
enum_values: [],
variants: [],
},
output_schema: {
type: 'object',
description: null,
required: false,
nullable: false,
default_value: null,
fields: {},
items: null,
enum_values: [],
variants: [],
},
input_mapping: { rules: [] },
output_mapping: { rules: [{ source: '$.response.body.rate', target: '$.output.rate', required: true }] },
execution_config: {
timeout_ms: 10000,
retry_policy: null,
auth_profile_ref: null,
headers: {},
approval_policy: null,
execution_policy: {
mode: 'adaptive',
deadline_ms: 45000,
no_tasks_fallback: 'unsupported',
},
},
tool_description: { title: 'Policy rates', description: 'Get rates.', tags: [], examples: [] },
wizard_state: { input_sample: {}, output_sample: {}, test_input: {}, import_findings: [] },
};
}
test('wizard round-trips the published execution policy and rejects an invalid edit', async ({ page }) => {
await login(page);
const workspace = await getCurrentWorkspace(page);
const operationId = 'policy_rates_41';
const operationPath = `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/operations/${operationId}`;
const savedPayloads = [];
await page.route(operationPath, async (route) => {
if (route.request().method() === 'PATCH') {
savedPayloads.push(route.request().postDataJSON());
await route.fulfill({
contentType: 'application/json',
body: JSON.stringify({ operation_id: operationId, workspace_id: workspace.id, version: 2, status: 'draft' }),
});
return;
}
await route.fulfill({
contentType: 'application/json',
body: JSON.stringify({
id: operationId,
workspace_id: workspace.id,
name: operationId,
display_name: 'Policy rates',
category: 'general',
protocol: 'rest',
status: 'draft',
current_draft_version: 1,
draft_version_ref: { version: 1, status: 'draft' },
published_version_ref: null,
agent_refs: [],
}),
});
});
await page.route(`${operationPath}/versions/*`, async (route) => {
await route.fulfill({
contentType: 'application/json',
body: JSON.stringify({
operation_id: operationId,
workspace_id: workspace.id,
version: 1,
status: 'draft',
snapshot: draftSnapshot(operationId),
}),
});
});
await page.route('**/operations/analyze-quality', async (route) => {
await route.fulfill({ contentType: 'application/json', body: JSON.stringify({ blocking: false, findings: [] }) });
});
page.on('dialog', (dialog) => dialog.accept());
await page.goto(`/wizard/?mode=edit&operationId=${operationId}`);
await expect(page.locator('#tool-display-name')).toHaveValue('Policy rates');
const editorValue = await page.locator('#tool-exec-config').inputValue();
expect(editorValue).toContain('execution_policy');
expect(editorValue).toContain('adaptive');
expect(editorValue).toContain('45000');
expect(editorValue).toContain('unsupported');
// A real edit still saves the pinned policy unchanged.
await page.evaluate(() => window.CrankWizardShell.doGoToStep(4));
await page.locator('#tool-display-name').fill('Policy rates edited');
await page.locator('.btn-save-draft').click();
await expect.poll(() => savedPayloads.length).toBe(1);
expect(savedPayloads[0].execution_config.execution_policy).toEqual({
mode: 'adaptive',
deadline_ms: 45000,
no_tasks_fallback: 'unsupported',
});
// An explicit invalid policy is refused instead of being silently rewritten to defaults.
await page.evaluate(() => window.CrankWizardShell.doGoToStep(5));
await expect(page.locator('#tool-exec-config')).toBeVisible();
await page.locator('#tool-exec-config').fill('timeout_ms: 10000\nexecution_policy:\n mode: background\n deadline_ms: 0\n');
await page.locator('.btn-save-draft').click();
await expect(page.locator('#tool-exec-config')).toHaveValue(/deadline_ms: 0/);
expect(savedPayloads.length).toBe(1);
});
+5
View File
@@ -21,6 +21,7 @@ crank-schema = { path = "../crank-schema" }
crank-trace = { path = "../crank-trace" }
futures-util = "0.3"
reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
sha2.workspace = true
@@ -31,7 +32,11 @@ tokio = { workspace = true, features = ["sync"] }
tracing.workspace = true
uuid.workspace = true
# Typed Tools dispatch uses the SDK; its HTTP transport remains test-only.
rmcp = { version = "=3.9.0" }
[dev-dependencies]
rmcp = { version = "=3.9.0", features = ["transport-streamable-http-server"] }
crank-mapping = { path = "../crank-mapping" }
crank-test-support = { path = "../crank-test-support" }
metrics-util = "0.20.4"
+4 -1
View File
@@ -272,7 +272,10 @@ fn record_unknown_approval_access_denial(reason: &'static str) {
);
}
fn allows_scope(scopes: &[PlatformApiKeyScope], required_scope: PlatformApiKeyScope) -> bool {
pub(super) fn allows_scope(
scopes: &[PlatformApiKeyScope],
required_scope: PlatformApiKeyScope,
) -> bool {
match required_scope {
PlatformApiKeyScope::Read => scopes.iter().any(|scope| {
matches!(
+134 -16
View File
@@ -30,9 +30,24 @@ mod approval_policy;
#[cfg(test)]
pub(crate) mod dispatch_observer;
mod invocation_history;
pub(crate) mod lifecycle;
mod metrics;
mod request;
mod response;
mod rmcp_dispatch;
#[cfg(test)]
mod rmcp_http;
#[cfg(test)]
#[path = "rmcp_http_tests.rs"]
mod rmcp_http_tests;
mod rmcp_progress_cancel;
#[cfg(test)]
mod rmcp_session_tests;
#[cfg(test)]
mod rmcp_tools;
#[cfg(test)]
#[path = "rmcp_tools_tests.rs"]
mod rmcp_tools_tests;
mod stages;
mod tool_resolution;
pub(crate) use self::approval_policy::{
@@ -49,13 +64,16 @@ mod approvals;
mod auth_resolution;
mod dispatch;
mod endpoint;
mod legacy_dispatch;
mod task_worker;
mod tasks;
use self::approvals::{
approve_request, deny_request, get_approval_request, list_pending_approvals,
};
pub(super) use self::auth_resolution::resolve_operation_auth;
use self::endpoint::{mcp_delete, mcp_get, mcp_post};
pub use self::task_worker::{TaskWorkerHandler, VerifiedIdempotentWrite};
pub use self::tasks::{TaskApplication, TaskApplicationError, TaskPrincipal};
// Test-only import surface: the BUILD-internal `app/tests.rs` module reaches these two
// through `super`, and the plain lib build does not use them (same pattern as
// `#[cfg(test)] use invocation_history::observe_invocation_history_outcome;` above).
@@ -82,6 +100,17 @@ pub(super) struct AppState {
session_slots: Arc<Semaphore>,
pub(super) credential_verifier: SharedMachineCredentialVerifier,
allowed_origins: AllowedOrigins,
/// Story 3.18: the bounded in-memory `logging/setLevel` opt-in of the legacy sessions. It is a
/// client-scoped level, never a process-global one, and it needs no schema migration.
pub(super) logging_levels: Arc<crate::protocol::LogLevelStore>,
/// Story 3.19: the bounded table of the legacy session that currently owns the notification
/// stream, so one session never broadcasts the same list-change signal into two streams.
pub(super) signal_streams: Arc<crate::protocol::StreamRegistry>,
/// Story 3.4: the bounded table of the `tools/call` requests a legacy client can still cancel.
pub(super) in_flight: Arc<self::lifecycle::InFlightCalls>,
/// Story 4.2: durable background Tasks over the same registry pool. The public wire stays
/// synchronous/unsupported; readiness depends on this mandatory store.
tasks: Arc<TaskApplication>,
}
#[derive(Clone)]
@@ -127,6 +156,7 @@ pub fn build_app(
DEFAULT_MAX_CONCURRENT_SESSIONS,
false,
None,
None,
)
}
@@ -182,6 +212,7 @@ pub fn build_app_with_background_workers_and_limits(
max_concurrent_sessions,
true,
None,
None,
)
}
@@ -217,6 +248,41 @@ pub fn build_app_with_artifacts(
max_concurrent_sessions,
true,
Some(artifact_store),
None,
)
}
/// Production launcher entry point with the durable Task application entry constructed by the
/// process composition root from the same registry pool and admission limits as the runtime.
#[allow(clippy::too_many_arguments)]
pub fn build_app_with_artifacts_and_tasks(
registry: PostgresRegistry,
refresh_interval: Duration,
public_base_url: Option<String>,
secret_crypto: SecretCrypto,
runtime: RuntimeExecutor,
api_rate_limiter: RequestRateLimiter,
coordination_store: Arc<dyn CoordinationStateStore>,
sessions: SharedSessionStore,
credential_verifier: SharedMachineCredentialVerifier,
max_concurrent_sessions: usize,
artifact_store: crank_artifacts::ArtifactStore,
tasks: Arc<TaskApplication>,
) -> Router {
build_app_inner(
registry,
refresh_interval,
public_base_url,
secret_crypto,
runtime,
api_rate_limiter,
coordination_store,
sessions,
credential_verifier,
max_concurrent_sessions,
true,
Some(artifact_store),
Some(tasks),
)
}
@@ -234,8 +300,23 @@ fn build_app_inner(
max_concurrent_sessions: usize,
start_background_workers: bool,
artifact_store: Option<crank_artifacts::ArtifactStore>,
tasks: Option<Arc<TaskApplication>>,
) -> Router {
let session_metrics = ActiveSessionMetrics::start(Arc::clone(&sessions));
let tasks = tasks.unwrap_or_else(|| {
let store = crank_registry::PostgresTaskStore::new(
registry.pool().clone(),
crank_registry::AdmissionLimits::default(),
crank_core::AdmissionSchedulerConfig::default(),
)
.expect("default admission limits and scheduler weights are valid");
Arc::new(TaskApplication::new(
registry.clone(),
refresh_interval,
Arc::clone(&coordination_store),
crank_runtime::TaskService::new(Arc::new(store)),
))
});
let state = Arc::new(AppState {
registry: registry.clone(),
catalog: PublishedToolCatalog::new(registry, refresh_interval, coordination_store),
@@ -248,6 +329,12 @@ fn build_app_inner(
session_slots: Arc::new(Semaphore::new(max_concurrent_sessions)),
credential_verifier,
allowed_origins: AllowedOrigins::new(public_base_url),
logging_levels: Arc::new(crate::protocol::LogLevelStore::new()),
signal_streams: Arc::new(crate::protocol::StreamRegistry::new()),
in_flight: Arc::new(lifecycle::InFlightCalls::new(
lifecycle::MAX_IN_FLIGHT_CALLS,
)),
tasks,
});
if start_background_workers {
spawn_approval_recovery(Arc::clone(&state));
@@ -296,24 +383,55 @@ async fn health() -> Json<Value> {
}
async fn readiness(State(state): State<Arc<AppState>>) -> Response {
match state.registry.ping().await {
Ok(()) => Json(json!({
let postgres_ready = state.registry.ping().await.is_ok();
// Story 4.1: admission state is a mandatory dependency of every execution, so readiness must
// reflect it rather than reporting ready on a bare `select 1`.
let admission_ready = state.registry.execution_admission_health().await.is_ok();
// Story 4.2: the durable Task store is mandatory for a durable acceptance acknowledgement.
let durable_tasks_ready = state.tasks.health().await.is_ok();
if postgres_ready && admission_ready && durable_tasks_ready {
return Json(json!({
"service": "mcp-server",
"status": "ready",
"checks": { "postgres": "ready" }
"checks": {
"postgres": "ready",
"execution_admission": "ready",
"durable_tasks": "ready"
}
}))
.into_response(),
Err(_) => (
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({
"service": "mcp-server",
"status": "not_ready",
"checks": { "postgres": "not_ready" },
"error": "database is unavailable"
})),
)
.into_response(),
.into_response();
}
let mut checks = serde_json::Map::new();
checks.insert(
"postgres".to_owned(),
json!(if postgres_ready { "ready" } else { "not_ready" }),
);
checks.insert(
"execution_admission".to_owned(),
json!(if admission_ready {
"ready"
} else {
"not_ready"
}),
);
checks.insert(
"durable_tasks".to_owned(),
json!(if durable_tasks_ready {
"ready"
} else {
"not_ready"
}),
);
(
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({
"service": "mcp-server",
"status": "not_ready",
"checks": checks,
"error": "a mandatory dependency is unavailable"
})),
)
.into_response()
}
#[cfg(test)]
@@ -19,18 +19,31 @@ pub(crate) async fn resolve_operation_auth(
workspace_id: &crank_core::WorkspaceId,
execution_config: &crank_core::ExecutionConfig,
) -> Result<Option<ResolvedAuth>, RuntimeError> {
if execution_config.auth_profile_ref.is_none() {
return Ok(None);
}
let span = Stage::AuthResolve.span();
let result = resolve_runtime_auth_for_task(
resolve_operation_auth_with(
&state.registry,
&state.secret_crypto,
workspace_id,
execution_config,
)
.instrument(span.clone())
.await;
.await
}
/// Same resolution for callers that hold the authoritative registry/secret-crypto pair without the
/// HTTP application state (the durable Task worker).
pub(crate) async fn resolve_operation_auth_with(
registry: &PostgresRegistry,
secret_crypto: &SecretCrypto,
workspace_id: &crank_core::WorkspaceId,
execution_config: &crank_core::ExecutionConfig,
) -> Result<Option<ResolvedAuth>, RuntimeError> {
if execution_config.auth_profile_ref.is_none() {
return Ok(None);
}
let span = Stage::AuthResolve.span();
let result =
resolve_runtime_auth_for_task(registry, secret_crypto, workspace_id, execution_config)
.instrument(span.clone())
.await;
match &result {
Ok(_) => StageOutcome::Success.record(&span),
Err(_) => {
+186 -75
View File
@@ -5,25 +5,26 @@
//! Moved verbatim out of `app.rs` (Story 3.6, kanban t_987bf309); it is called from
//! `app/endpoint.rs`, so the entry points are `pub(super)`. No behaviour changed.
use super::request::{InitializeParams, ToolCallParams};
use super::request::ToolCallParams;
use super::response::take_confirmation_token;
use super::rmcp_dispatch::{
handle_resources_command, handle_tools_command, list_resource_templates,
};
use crate::{
auth::VerifiedMachineCredential,
jsonrpc::{
is_notification, jsonrpc_error, jsonrpc_result, method_name, negotiated_protocol_version,
params, request_id,
},
jsonrpc::{jsonrpc_error, jsonrpc_result, negotiated_protocol_version, params, request_id},
protocol::{
CompletionCommand, CompletionOutcome, ContinuationSupport, DispatchDecision, MethodHandler,
PromptGetCommand, PromptsCommand, PromptsListCommand, PromptsOutcome, RequestIdentity,
ResourceReadCommand, ResourcesCommand, ResourcesListCommand, ResourcesOutcome,
ServiceContext, ToolCallCommand, ToolsCommand, ToolsListCommand, ToolsOutcome,
declared_mcp_method, discover_result, handle_completion_command, handle_prompts_command,
handle_resources_command, handle_tools_command, validate_modern_request_name,
declared_mcp_method, discover_result, validate_modern_request_name,
},
session::SessionState,
tool_error::execution_locale,
transport::{ResponseMode, json_response, session_id_from_headers, transport_response},
transport::{
ResponseMode, json_response, session_id_from_headers, sse_response, transport_response,
},
};
use axum::{
http::{HeaderMap, StatusCode},
@@ -37,11 +38,10 @@ use time::OffsetDateTime;
use super::response::{
completion_result, internal_jsonrpc_error, prompt_arguments_error_response, prompt_get_result,
prompts_list_result, resource_error_response, resource_read_result,
resource_templates_list_result, resources_list_result, tool_command_response,
tools_list_result,
resource_templates_list_result, resources_list_result, tools_list_result,
};
use super::{AgentRoutePath, AppState, TRANSPORT_SESSION_TTL_MS};
use crate::protocol::{ResourceTemplatesListError, list_resource_templates};
use crate::protocol::ResourceTemplatesListError;
const SERVER_NAME: &str = "crank-mcp-server";
const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION");
@@ -67,12 +67,31 @@ pub(super) async fn handle_modern_request(
.as_deref()
.unwrap_or(crate::jsonrpc::MODERN_PROTOCOL_VERSION);
// Story 3.18: the modern revision carries the log level of this request in
// `_meta["io.modelcontextprotocol/logLevel"]` (SEP-2577); the former `logging/setLevel` request
// does not exist here. Absent, non-string or unknown values mean "not opted in", so no
// `notifications/message` is emitted for this request.
crate::protocol::set_active_level(crate::protocol::modern_request_level(message));
// A notification carries no `id`. It is acknowledged before the request-only header rules: the
// mandatory `Mcp-Method`/`Mcp-Name` pair applies to requests, not to notifications (protocol era
// matrix row 18). The envelope, identity and mixed-carrier checks — and authentication — have
// already run at this point, so this early return cannot admit a malformed identity or a session
// conflict, and the notification is never dispatched.
if is_notification(message) {
let envelope = match crate::protocol::decode_dispatch_envelope(message) {
Ok(Some(envelope)) => envelope,
_ => {
return internal_jsonrpc_error(
message,
"admitted dispatch envelope could not be decoded",
);
}
};
let method = envelope.method();
let notification = envelope.is_notification();
#[cfg(test)]
super::dispatch_observer::sdk_dispatch_envelope_consumed();
if notification {
return crate::protocol::modern_notification_response();
}
@@ -80,15 +99,14 @@ pub(super) async fn handle_modern_request(
Ok(declared_method) => declared_method,
Err(response) => return *response,
};
if declared_method != method_name(message) {
if declared_method != method {
return crate::protocol::modern_header_mismatch_response(message);
}
// R5/F5: the same registration-backed dispatch as the legacy adapter. The handler is decided by
// the owning registration (not by a literal method list inside this adapter), and a method whose
// capability is not eligible in this revision is never served here.
let dispatch =
crate::protocol::dispatch_decision(method_name(message).unwrap_or_default(), version);
let dispatch = crate::protocol::dispatch_decision(method.unwrap_or_default(), version);
match dispatch {
DispatchDecision::Allowed {
handler: MethodHandler::Discover,
@@ -112,7 +130,7 @@ pub(super) async fn handle_modern_request(
request_id(message),
crate::protocol::project_modern_result(
version,
json!({ "protocolVersion": version }),
crate::protocol::ping_result(&json!({"method": "ping"}), version),
),
),
response_mode,
@@ -148,22 +166,23 @@ pub(super) async fn handle_modern_request(
locale: execution_locale(message),
},
ToolsCommand::List(list_command),
// A list command has no progress or cancellation lifecycle (Story 3.4).
None,
)
.await;
match outcome {
ToolsOutcome::List { tools, next_cursor } => transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
crate::protocol::project_modern_cacheable_result(
version,
tools_list_result(tools, next_cursor),
ToolsOutcome::List { tools, next_cursor } => {
match tools_list_result(tools, next_cursor, version) {
Ok(result) => transport_response(
StatusCode::OK,
jsonrpc_result(request_id(message), result),
response_mode,
None,
Some(version),
),
),
response_mode,
None,
Some(version),
),
Err(error) => internal_jsonrpc_error(message, error),
}
}
ToolsOutcome::InvalidParams(message_text) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, message_text),
@@ -212,30 +231,35 @@ pub(super) async fn handle_modern_request(
// session-less, so its calls cannot advertise elicitation support.
let mut arguments = tool_call_params.arguments.unwrap_or_else(|| json!({}));
let confirmation_token = take_confirmation_token(&mut arguments);
let outcome = handle_tools_command(
state,
ServiceContext {
workspace_slug: &path.workspace_slug,
agent_slug: &path.agent_slug,
credential,
correlation: transport_correlation,
// Story 3.4: the client's own `_meta.progressToken` of this request, validated and bounded.
let progress = crate::app::lifecycle::token_from_meta(tool_call_params.meta.as_ref());
super::lifecycle::run_tool_call(
Arc::clone(state),
crate::app::lifecycle::ToolCallRequest {
workspace_slug: path.workspace_slug.clone(),
agent_slug: path.agent_slug.clone(),
credential: credential.clone(),
correlation: transport_correlation.clone(),
continuation: ContinuationSupport::ElicitationUnsupported,
locale: execution_locale(message),
},
ToolsCommand::Call(ToolCallCommand {
crate::app::lifecycle::ToolCallWire {
message: message.clone(),
protocol_version: version.to_owned(),
response_mode,
progress,
session_id: None,
// The modern revision cancels by closing this request's response stream, so no
// session-scoped cancellation table is involved.
in_flight: None,
},
ToolCallCommand {
tool_name: tool_call_params.name,
arguments,
confirmation_token,
}),
)
.await;
tool_command_response(
message,
response_mode,
version,
transport_correlation,
outcome,
},
)
.await
}
DispatchDecision::Allowed {
handler: MethodHandler::ResourceList,
@@ -272,10 +296,10 @@ pub(super) async fn handle_modern_request(
StatusCode::OK,
jsonrpc_result(
request_id(message),
crate::protocol::project_modern_cacheable_result(
version,
resources_list_result(resources, next_cursor),
),
match resources_list_result(resources, next_cursor, version) {
Ok(result) => result,
Err(error) => return internal_jsonrpc_error(message, error),
},
),
response_mode,
None,
@@ -331,10 +355,10 @@ pub(super) async fn handle_modern_request(
StatusCode::OK,
jsonrpc_result(
request_id(message),
crate::protocol::project_modern_cacheable_result(
version,
resource_read_result(contents),
),
match resource_read_result(contents, version) {
Ok(result) => result,
Err(error) => return internal_jsonrpc_error(message, error),
},
),
response_mode,
None,
@@ -420,10 +444,10 @@ pub(super) async fn handle_modern_request(
StatusCode::OK,
jsonrpc_result(
request_id(message),
crate::protocol::project_modern_cacheable_result(
version,
resource_templates_list_result(templates, next_cursor),
),
match resource_templates_list_result(templates, next_cursor, version) {
Ok(result) => result,
Err(error) => return internal_jsonrpc_error(message, error),
},
),
response_mode,
None,
@@ -452,7 +476,7 @@ pub(super) async fn handle_modern_request(
);
}
};
let outcome = handle_prompts_command(
let outcome = super::rmcp_dispatch::handle_prompts_command(
state,
resource_service_context(path, credential, transport_correlation, message),
version,
@@ -469,7 +493,10 @@ pub(super) async fn handle_modern_request(
request_id(message),
crate::protocol::project_modern_cacheable_result(
version,
prompts_list_result(prompts, next_cursor),
match prompts_list_result(prompts, next_cursor) {
Ok(result) => result,
Err(error) => return internal_jsonrpc_error(message, error),
},
),
),
response_mode,
@@ -511,7 +538,7 @@ pub(super) async fn handle_modern_request(
if let Err(response) = validate_modern_request_name(message, headers, &command.name) {
return *response;
}
let outcome = handle_prompts_command(
let outcome = super::rmcp_dispatch::handle_prompts_command(
state,
resource_service_context(path, credential, transport_correlation, message),
version,
@@ -528,7 +555,10 @@ pub(super) async fn handle_modern_request(
request_id(message),
crate::protocol::project_modern_result(
version,
prompt_get_result(description, messages),
match prompt_get_result(description, messages) {
Ok(result) => result,
Err(error) => return internal_jsonrpc_error(message, error),
},
),
),
response_mode,
@@ -602,7 +632,7 @@ pub(super) async fn handle_modern_request(
);
}
};
let outcome = handle_completion_command(
let outcome = super::rmcp_dispatch::handle_completion_command(
state,
resource_service_context(path, credential, transport_correlation, message),
version,
@@ -652,6 +682,80 @@ pub(super) async fn handle_modern_request(
}
}
}
// Story 3.19: the modern long-lived notification stream. The response is itself the SSE
// stream that stays open, so it is built here instead of going through the one-shot
// response helpers: the acknowledgment is its first frame, then the coalesced list-change
// signals, and closing the HTTP stream ends the producer.
DispatchDecision::Allowed {
handler: MethodHandler::SubscriptionsListen,
..
} => {
// A notification stream is an SSE body by construction: a client that did not offer
// `text/event-stream` could not receive it.
if !crate::transport::accepts_event_stream(headers) {
return StatusCode::NOT_ACCEPTABLE.into_response();
}
let filter: crate::protocol::ListenParams =
match serde_json::from_value(params(message)) {
Ok(filter) => filter,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
crate::protocol::INVALID_LISTEN_FILTER,
),
response_mode,
None,
Some(version),
);
}
};
let accepted = match filter.notifications.accepted() {
crate::protocol::FilterOutcome::Accepted(accepted) => accepted,
crate::protocol::FilterOutcome::Invalid(detail) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(version),
);
}
};
// The subscription id is the JSON-RPC id of this request; a value whose encoding is not
// bounded cannot be a frame identity carrier and is refused before the stream opens.
let Some(subscription_id) = crate::protocol::subscription_id(&request_id(message))
else {
return transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32600, "invalid JSON-RPC message"),
response_mode,
None,
Some(version),
);
};
let Ok(permit) = super::metrics::ActiveStreamGuard::try_acquire(&state.session_slots)
else {
return StatusCode::TOO_MANY_REQUESTS.into_response();
};
let acknowledged = crate::protocol::acknowledged_frame(&subscription_id, accepted);
let receiver = crate::protocol::spawn_signal_watch(
Arc::clone(state),
crate::protocol::Watch {
workspace_slug: path.workspace_slug.clone(),
agent_slug: path.agent_slug.clone(),
signals: accepted,
wire: crate::protocol::Wire::Modern(subscription_id),
legacy_stream: None,
deadline: None,
},
);
let stream = crate::protocol::frames_stream(vec![acknowledged], receiver, permit);
sse_response(StatusCode::OK, stream, None, Some(version))
}
// The standard Prompt requests are registered capabilities now; any other
// unregistered/wrong-era method goes straight to the era-correct unsupported answer.
_ => crate::protocol::modern_method_not_found_response(message),
@@ -682,7 +786,8 @@ pub(super) async fn handle_initialize(
message: &Value,
response_mode: ResponseMode,
) -> Response {
let initialize_params: InitializeParams = match serde_json::from_value(params(message)) {
let raw_params = params(message);
let initialize_params = match crate::protocol::initialize_params(&raw_params) {
Ok(value) => value,
Err(_error) => {
return transport_response(
@@ -694,7 +799,10 @@ pub(super) async fn handle_initialize(
);
}
};
let Some(protocol_version) = negotiated_protocol_version(&initialize_params.protocol_version)
#[cfg(test)]
super::dispatch_observer::sdk_initialize_input_consumed(&initialize_params);
let Some(protocol_version) =
negotiated_protocol_version(initialize_params.protocol_version.as_str())
else {
// A revision outside the approved window is rejected without minting a session and without
// echoing any withdrawn revision as the served identity.
@@ -715,9 +823,9 @@ pub(super) async fn handle_initialize(
};
let now = OffsetDateTime::now_utc();
let expires_at = add_millis(now, TRANSPORT_SESSION_TTL_MS);
let supports_elicitation = initialize_params
.capabilities
.get("elicitation")
let supports_elicitation = raw_params
.get("capabilities")
.and_then(|capabilities| capabilities.get("elicitation"))
.is_some_and(Value::is_object);
let session_id = match state
.sessions
@@ -740,14 +848,7 @@ pub(super) async fn handle_initialize(
StatusCode::OK,
jsonrpc_result(
request_id(message),
json!( {
"protocolVersion": protocol_version,
"capabilities": crate::protocol::advertised_server_capabilities(protocol_version),
"serverInfo": {
"name": SERVER_NAME,
"version": SERVER_VERSION
}
}),
crate::protocol::initialize_result(protocol_version, SERVER_NAME, SERVER_VERSION),
),
response_mode,
Some(&session_id),
@@ -759,6 +860,7 @@ pub(super) async fn handle_initialized_notification(
state: Arc<AppState>,
path: &AgentRoutePath,
headers: &HeaderMap,
notification: rmcp::model::ClientNotification,
) -> Response {
match session_id_from_headers(headers) {
Ok(Some(session_id)) => match state.sessions.get(&session_id).await {
@@ -766,6 +868,15 @@ pub(super) async fn handle_initialized_notification(
if session.workspace_slug == path.workspace_slug
&& session.agent_slug == path.agent_slug =>
{
let rmcp::model::ClientNotification::InitializedNotification(initialized) =
notification
else {
unreachable!("initialized handler requires the SDK initialized variant")
};
#[cfg(test)]
super::dispatch_observer::sdk_initialized_notification_consumed(&initialized);
#[cfg(not(test))]
let _ = initialized;
match state
.sessions
.mark_initialized(&session_id, OffsetDateTime::now_utc())
@@ -51,9 +51,153 @@ pub(crate) struct CounterSet {
era_modern: AtomicUsize,
era_legacy: AtomicUsize,
tools_service: AtomicUsize,
sdk_tools_dispatch: AtomicUsize,
sdk_tools_list_result: AtomicUsize,
sdk_tools_call_result: AtomicUsize,
sdk_resources_dispatch: AtomicUsize,
sdk_resources_list_result: AtomicUsize,
sdk_resources_read_result: AtomicUsize,
sdk_resource_templates_list_result: AtomicUsize,
sdk_prompts_dispatch: AtomicUsize,
sdk_prompts_list_result: AtomicUsize,
sdk_prompts_get_result: AtomicUsize,
sdk_completion_dispatch: AtomicUsize,
sdk_completion_result: AtomicUsize,
sdk_logging_dispatch: AtomicUsize,
sdk_logging_level: AtomicUsize,
sdk_logging_notification: AtomicUsize,
sdk_progress_notification: AtomicUsize,
sdk_cancelled_params: AtomicUsize,
sdk_subscriptions_listen: AtomicUsize,
sdk_subscriptions_acknowledged: AtomicUsize,
sdk_signal_notification: AtomicUsize,
sdk_jsonrpc_response: AtomicUsize,
sdk_jsonrpc_error: AtomicUsize,
sdk_ping_request: AtomicUsize,
sdk_ping_result: AtomicUsize,
sdk_dispatch_decoded: AtomicUsize,
sdk_dispatch_consumed: AtomicUsize,
sdk_initialized_decoded: AtomicUsize,
sdk_initialized_consumed: AtomicUsize,
sdk_initialize_input: AtomicUsize,
sdk_initialize_result: AtomicUsize,
sdk_discover_result: AtomicUsize,
}
impl CounterSet {
pub(crate) fn sdk_dispatch_decoded(&self) -> usize {
self.sdk_dispatch_decoded.load(Ordering::SeqCst)
}
pub(crate) fn sdk_dispatch_consumed(&self) -> usize {
self.sdk_dispatch_consumed.load(Ordering::SeqCst)
}
pub(crate) fn sdk_initialized_decoded(&self) -> usize {
self.sdk_initialized_decoded.load(Ordering::SeqCst)
}
pub(crate) fn sdk_initialized_consumed(&self) -> usize {
self.sdk_initialized_consumed.load(Ordering::SeqCst)
}
pub(crate) fn sdk_ping_request(&self) -> usize {
self.sdk_ping_request.load(Ordering::SeqCst)
}
pub(crate) fn sdk_ping_result(&self) -> usize {
self.sdk_ping_result.load(Ordering::SeqCst)
}
pub(crate) fn sdk_initialize_input(&self) -> usize {
self.sdk_initialize_input.load(Ordering::SeqCst)
}
pub(crate) fn sdk_initialize_result(&self) -> usize {
self.sdk_initialize_result.load(Ordering::SeqCst)
}
pub(crate) fn sdk_discover_result(&self) -> usize {
self.sdk_discover_result.load(Ordering::SeqCst)
}
pub(crate) fn sdk_jsonrpc_response(&self) -> usize {
self.sdk_jsonrpc_response.load(Ordering::SeqCst)
}
pub(crate) fn sdk_jsonrpc_error(&self) -> usize {
self.sdk_jsonrpc_error.load(Ordering::SeqCst)
}
pub(crate) fn sdk_subscriptions_listen(&self) -> usize {
self.sdk_subscriptions_listen.load(Ordering::SeqCst)
}
pub(crate) fn sdk_subscriptions_acknowledged(&self) -> usize {
self.sdk_subscriptions_acknowledged.load(Ordering::SeqCst)
}
pub(crate) fn sdk_signal_notification(&self) -> usize {
self.sdk_signal_notification.load(Ordering::SeqCst)
}
pub(crate) fn sdk_progress_notification(&self) -> usize {
self.sdk_progress_notification.load(Ordering::SeqCst)
}
pub(crate) fn sdk_cancelled_params(&self) -> usize {
self.sdk_cancelled_params.load(Ordering::SeqCst)
}
pub(crate) fn sdk_logging_dispatch(&self) -> usize {
self.sdk_logging_dispatch.load(Ordering::SeqCst)
}
pub(crate) fn sdk_logging_level(&self) -> usize {
self.sdk_logging_level.load(Ordering::SeqCst)
}
pub(crate) fn sdk_logging_notification(&self) -> usize {
self.sdk_logging_notification.load(Ordering::SeqCst)
}
pub(crate) fn sdk_completion_dispatch(&self) -> usize {
self.sdk_completion_dispatch.load(Ordering::SeqCst)
}
pub(crate) fn sdk_completion_result(&self) -> usize {
self.sdk_completion_result.load(Ordering::SeqCst)
}
pub(crate) fn sdk_prompts_dispatch(&self) -> usize {
self.sdk_prompts_dispatch.load(Ordering::SeqCst)
}
pub(crate) fn sdk_prompts_list_result(&self) -> usize {
self.sdk_prompts_list_result.load(Ordering::SeqCst)
}
pub(crate) fn sdk_prompts_get_result(&self) -> usize {
self.sdk_prompts_get_result.load(Ordering::SeqCst)
}
pub(crate) fn sdk_resources_dispatch(&self) -> usize {
self.sdk_resources_dispatch.load(Ordering::SeqCst)
}
pub(crate) fn sdk_resources_list_result(&self) -> usize {
self.sdk_resources_list_result.load(Ordering::SeqCst)
}
pub(crate) fn sdk_resources_read_result(&self) -> usize {
self.sdk_resources_read_result.load(Ordering::SeqCst)
}
pub(crate) fn sdk_resource_templates_list_result(&self) -> usize {
self.sdk_resource_templates_list_result
.load(Ordering::SeqCst)
}
/// SDK typed Tools dispatch entries, separate from the existing era/service snapshot.
pub(crate) fn sdk_tools_dispatch(&self) -> usize {
self.sdk_tools_dispatch.load(Ordering::SeqCst)
}
/// Successfully serialized SDK ListToolsResult values, not dispatch entries.
pub(crate) fn sdk_tools_list_result(&self) -> usize {
self.sdk_tools_list_result.load(Ordering::SeqCst)
}
/// Successfully serialized SDK CallToolResult values, not dispatch entries.
pub(crate) fn sdk_tools_call_result(&self) -> usize {
self.sdk_tools_call_result.load(Ordering::SeqCst)
}
/// Read the counters of one observed request as one comparable snapshot.
pub(crate) fn snapshot(&self) -> Snapshot {
Snapshot {
@@ -134,6 +278,205 @@ pub(crate) fn tools_service_enter() {
increment(|counters| &counters.tools_service);
}
/// `SDK_TOOLS_DISPATCH`: entered the real SDK typed Tools dispatch, not just admission.
pub(crate) fn sdk_tools_dispatch_enter() {
increment(|counters| &counters.sdk_tools_dispatch);
}
/// Mark only after serializing a real SDK ListToolsResult successfully.
pub(crate) fn sdk_tools_list_result_serialized() {
increment(|counters| &counters.sdk_tools_list_result);
}
/// Mark only after serializing a real SDK CallToolResult successfully.
pub(crate) fn sdk_tools_call_result_serialized() {
increment(|counters| &counters.sdk_tools_call_result);
}
/// Mark only after converting a Resources request through the real SDK typed path.
pub(crate) fn sdk_resources_dispatch_enter() {
increment(|counters| &counters.sdk_resources_dispatch);
}
/// Mark only after successfully serializing a real SDK ListResourcesResult.
pub(crate) fn sdk_resources_list_result_serialized() {
increment(|counters| &counters.sdk_resources_list_result);
}
/// Mark only after successfully serializing a real SDK ReadResourceResult.
pub(crate) fn sdk_resources_read_result_serialized() {
increment(|counters| &counters.sdk_resources_read_result);
}
/// Mark only after successfully serializing a real SDK ListResourceTemplatesResult.
pub(crate) fn sdk_resource_templates_list_result_serialized() {
increment(|counters| &counters.sdk_resource_templates_list_result);
}
#[cfg(test)]
#[path = "../rmcp_resources_runtime_tests.rs"]
mod rmcp_resources_runtime_tests;
/// Mark only after converting a Prompts request through the real SDK typed path.
pub(crate) fn sdk_prompts_dispatch_enter() {
increment(|counters| &counters.sdk_prompts_dispatch);
}
/// Mark only after successfully serializing a real SDK ListPromptsResult.
pub(crate) fn sdk_prompts_list_result_serialized() {
increment(|counters| &counters.sdk_prompts_list_result);
}
/// Mark only after successfully serializing a real SDK GetPromptResult.
pub(crate) fn sdk_prompts_get_result_serialized() {
increment(|counters| &counters.sdk_prompts_get_result);
}
#[cfg(test)]
#[path = "../rmcp_prompts_runtime_tests.rs"]
mod rmcp_prompts_runtime_tests;
/// Mark only after constructing real SDK CompleteRequestParams.
pub(crate) fn sdk_completion_dispatch_enter() {
increment(|counters| &counters.sdk_completion_dispatch);
}
/// Mark only after successfully serializing real SDK CompleteResult/CompletionInfo.
pub(crate) fn sdk_completion_result_serialized() {
increment(|counters| &counters.sdk_completion_result);
}
#[cfg(test)]
#[path = "../rmcp_completion_runtime_tests.rs"]
mod rmcp_completion_runtime_tests;
/// Mark only after constructing the real SDK typed legacy logging request.
pub(crate) fn sdk_logging_dispatch_enter() {
increment(|counters| &counters.sdk_logging_dispatch);
}
/// Mark only after successfully decoding a real SDK LoggingLevel at either input.
pub(crate) fn sdk_logging_level_parsed() {
increment(|counters| &counters.sdk_logging_level);
}
/// Mark only after serializing real SDK LoggingMessageNotificationParam params, not an envelope.
pub(crate) fn sdk_logging_notification_serialized() {
increment(|counters| &counters.sdk_logging_notification);
}
#[path = "../rmcp_logging_runtime_tests.rs"]
mod rmcp_logging_runtime_tests;
/// Mark only after successfully serializing real SDK ProgressNotificationParam params.
pub(crate) fn sdk_progress_notification_serialized() {
increment(|counters| &counters.sdk_progress_notification);
}
/// Mark successful SDK CancelledNotificationParam decoding, not successful cancellation.
pub(crate) fn sdk_cancelled_params_decoded() {
increment(|counters| &counters.sdk_cancelled_params);
}
#[path = "../rmcp_progress_cancel_runtime_tests.rs"]
mod rmcp_progress_cancel_runtime_tests;
pub(crate) fn installed_observer() -> Option<Arc<CounterSet>> {
OBSERVER.try_with(Arc::clone).ok()
}
pub(crate) fn sdk_subscriptions_listen_decoded() {
increment(|counters| &counters.sdk_subscriptions_listen);
}
pub(crate) fn sdk_subscriptions_acknowledged_serialized() {
increment(|counters| &counters.sdk_subscriptions_acknowledged);
}
pub(crate) fn sdk_signal_notification_serialized() {
increment(|counters| &counters.sdk_signal_notification);
}
#[path = "../rmcp_signals_runtime_tests.rs"]
mod rmcp_signals_runtime_tests;
/// Mark only immediately after successfully serializing a real SDK JsonRpcResponse.
pub(crate) fn sdk_jsonrpc_response_serialized() {
increment(|counters| &counters.sdk_jsonrpc_response);
}
/// Mark only immediately after successfully serializing a real SDK JsonRpcError.
pub(crate) fn sdk_jsonrpc_error_serialized() {
increment(|counters| &counters.sdk_jsonrpc_error);
}
#[path = "../rmcp_envelopes_runtime_tests.rs"]
mod rmcp_envelopes_runtime_tests;
/// Mark only after successfully serializing a real rmcp InitializeResult.
pub(crate) fn sdk_initialize_result_serialized() {
increment(|counters| &counters.sdk_initialize_result);
}
/// Mark only after successfully serializing a real rmcp DiscoverResult.
pub(crate) fn sdk_discover_result_serialized() {
increment(|counters| &counters.sdk_discover_result);
}
#[path = "../rmcp_lifecycle_negotiation_runtime_tests.rs"]
mod rmcp_lifecycle_negotiation_runtime_tests;
/// Mark only where SDK protocol_version is consumed by initialize negotiation, not at DTO creation.
pub(crate) fn sdk_initialize_input_consumed(_params: &rmcp::model::InitializeRequestParams) {
increment(|counters| &counters.sdk_initialize_input);
}
#[path = "../rmcp_initialize_input_runtime_tests.rs"]
mod rmcp_initialize_input_runtime_tests;
/// Mark only where a real SDK PingRequest is consumed by the shared runtime ping seam.
pub(crate) fn sdk_ping_request_consumed(_request: &rmcp::model::PingRequest) {
increment(|counters| &counters.sdk_ping_request);
}
/// Mark only after successfully serializing the real SDK ping result.
pub(crate) fn sdk_ping_result_serialized() {
increment(|counters| &counters.sdk_ping_result);
}
#[path = "../rmcp_ping_runtime_tests.rs"]
mod rmcp_ping_runtime_tests;
/// Mark immediately after typed method-projection decode, not variant consumption.
pub(crate) fn sdk_initialized_notification_decoded(
_notification: &rmcp::model::InitializedNotification,
) {
increment(|counters| &counters.sdk_initialized_decoded);
}
/// Mark inside the matched SDK variant, after guards and immediately before session mutation.
pub(crate) fn sdk_initialized_notification_consumed(
_notification: &rmcp::model::InitializedNotification,
) {
increment(|counters| &counters.sdk_initialized_consumed);
}
#[path = "../rmcp_initialized_runtime_tests.rs"]
mod rmcp_initialized_runtime_tests;
/// Mark after successful SDK envelope decode, independently of routing consumption.
pub(crate) fn sdk_dispatch_envelope_decoded() {
increment(|counters| &counters.sdk_dispatch_decoded);
}
/// Mark only when decoded method/kind selects the existing registry-backed route.
pub(crate) fn sdk_dispatch_envelope_consumed() {
increment(|counters| &counters.sdk_dispatch_consumed);
}
#[path = "../rmcp_dispatch_runtime_tests.rs"]
mod rmcp_dispatch_runtime_tests;
fn increment(counter: impl Fn(&CounterSet) -> &AtomicUsize) {
let _ = OBSERVER.try_with(|counters| {
counter(counters).fetch_add(1, Ordering::SeqCst);
@@ -144,7 +487,7 @@ fn increment(counter: impl Fn(&CounterSet) -> &AtomicUsize) {
/// execution-boundary reason as the tests below).
#[cfg(test)]
#[path = "../dispatch_observer_support.rs"]
mod support;
pub(crate) mod support;
/// The behavioural section 7.2 harness: real app, real PostgreSQL, one counted loopback upstream and
/// one request-scoped observer per request. Its file lives outside `src/app/` only because the
@@ -153,3 +496,7 @@ mod support;
#[cfg(test)]
#[path = "../dispatch_observer_harness.rs"]
mod tests;
#[cfg(test)]
#[path = "../rmcp_runtime_tests.rs"]
mod rmcp_runtime_tests;
+92 -659
View File
@@ -3,55 +3,37 @@
//!
//! Moved verbatim out of `app.rs` (Story 3.6, kanban t_987bf309); `mcp_post_response` is reached from
//! `build_app_inner` via `mcp_post`, hence `pub(super)`. No behaviour changed.
//!
//! The legacy-era dispatch itself lives in `super::legacy_dispatch` (Story 3.18 code-health, kanban
//! t_d1b45ddc) to keep this transport entry file inside the 1000-line limit.
use super::metrics::{ActiveStreamGuard, McpRequestMetrics};
use super::request::ToolCallParams;
use super::response::take_confirmation_token;
use super::stages::{enforce_traced_rate_limit, require_traced_machine_access};
use crate::{
jsonrpc::{
LEGACY_PROTOCOL_VERSION, is_notification, is_request, jsonrpc_error, jsonrpc_result,
method_name, params, request_id,
},
jsonrpc::{jsonrpc_error, method_name, request_id},
protocol::{
CompletionCommand, CompletionOutcome, ContinuationSupport, DispatchDecision, EnvelopeKind,
Kind, MethodHandler, PromptGetCommand, PromptsCommand, PromptsListCommand, PromptsOutcome,
RequestEra, ResourceReadCommand, ResourcesCommand, ResourcesListCommand, ResourcesOutcome,
ServiceContext, ToolCallCommand, ToolsCommand, ToolsListCommand, ToolsOutcome,
ambiguous_session_carrier_response, handle_completion_command, handle_prompts_command,
handle_resources_command, handle_tools_command, resolve_request_identity,
EnvelopeKind, RequestEra, ambiguous_session_carrier_response, resolve_request_identity,
},
rate_limit::{rate_limited_jsonrpc_response, rate_limited_status_response},
request_context::RequestContext,
tool_error::execution_locale,
transport::{
negotiate_post_response_mode, session_id_from_headers, sse_response, transport_response,
validate_get_accept_header, validate_origin, validate_session_protocol_version,
with_request_id_header,
ResponseMode, accepts_event_stream, negotiate_post_response_mode, session_id_from_headers,
sse_response, transport_response, validate_get_accept_header, validate_origin,
validate_session_protocol_version, with_request_id_header,
},
};
use axum::{
Json,
extract::{Extension, Path, State, rejection::JsonRejection},
http::{HeaderMap, StatusCode},
response::{IntoResponse, Response, sse::Event},
response::{IntoResponse, Response},
};
use crank_core::{CorrelationContext, PlatformApiKeyScope};
use futures_util::stream;
use serde_json::{Value, json};
use std::{convert::Infallible, sync::Arc, time::Duration};
use serde_json::Value;
use std::{sync::Arc, time::Duration, time::Instant};
use tracing::info;
use super::dispatch::{
handle_initialize, handle_initialized_notification, handle_modern_request,
require_initialized_session,
};
use super::response::{
completion_result, internal_jsonrpc_error, prompt_arguments_error_response, prompt_get_result,
prompts_list_result, resource_error_response, resource_read_result,
resource_templates_list_result, resources_list_result, tool_command_response,
tools_list_result,
};
use super::dispatch::handle_modern_request;
use super::{AgentRoutePath, AppState, TRANSPORT_SESSION_TTL_MS};
pub(super) async fn mcp_get(
Path(path): Path<AgentRoutePath>,
@@ -120,11 +102,32 @@ pub(super) async fn mcp_get(
return StatusCode::TOO_MANY_REQUESTS.into_response();
};
let stream = stream::unfold(permit, |permit| async move {
tokio::time::sleep(Duration::from_millis(TRANSPORT_SESSION_TTL_MS)).await;
drop(permit);
None::<(Result<Event, Infallible>, _)>
});
// Story 3.19: the pinned legacy transport forbids broadcasting one JSON-RPC message across the
// SSE streams of a session, so exactly one admitted `GET` receives the Resource/Prompt/Tool
// list-change signals. A newer `GET` takes them over and the older stream simply ends; a
// `DELETE` retires the producer, so a stale generation never writes into a reconnect.
let Some(generation) = state.signal_streams.admit(&session_id) else {
// The bounded owner table is contended: refuse this admission instead of waiting.
return StatusCode::TOO_MANY_REQUESTS.into_response();
};
let receiver = crate::protocol::spawn_signal_watch(
Arc::clone(&state),
crate::protocol::Watch {
workspace_slug: path.workspace_slug.clone(),
agent_slug: path.agent_slug.clone(),
signals: crate::protocol::Signals::ALL,
wire: crate::protocol::Wire::Legacy,
legacy_stream: Some((
Arc::clone(&state.signal_streams),
session_id.clone(),
generation,
)),
deadline: Some(Instant::now() + Duration::from_millis(TRANSPORT_SESSION_TTL_MS)),
},
);
// The stream permit lives in the response stream: a client that closes the SSE connection ends
// the producer and releases the slot, and no signal is queued for a reader that is gone.
let stream = crate::protocol::frames_stream(Vec::new(), receiver, permit);
sse_response(
StatusCode::OK,
@@ -177,6 +180,7 @@ pub(super) async fn mcp_delete(
match state.sessions.delete(&session_id).await {
Ok(true) => {
state.signal_streams.retire(&session_id);
state.session_metrics.refresh();
StatusCode::NO_CONTENT.into_response()
}
@@ -224,8 +228,14 @@ pub(super) async fn mcp_post(
"mcp request received"
);
let response =
mcp_post_response(&path, state, &headers, &message, &transport_correlation).await;
let response = crate::protocol::logging_scope(mcp_post_response(
&path,
state,
&headers,
&message,
&transport_correlation,
))
.await;
request_metrics.complete(&response);
with_request_id_header(response, &transport_request_id)
}
@@ -246,10 +256,17 @@ async fn mcp_post_response(
return status.into_response();
}
let response_mode = match negotiate_post_response_mode(headers) {
let mut response_mode = match negotiate_post_response_mode(headers) {
Ok(mode) => mode,
Err(status) => return status.into_response(),
};
// Story 3.18: a server notification is delivered only on the SSE stream, so the logging
// projection records whether this request offered it. This is an admission fact of the same
// `Accept` header the mode above was negotiated from.
if accepts_event_stream(headers) {
crate::protocol::set_sse_acceptable();
}
// A conflicting duplicate session carrier is an ambiguous identity: it must not be resolved to
// one of its values. The carrier itself cannot tell which era the request belongs to, so the
// diagnostic is read from the non-ambiguous part of the request (matrix row 17) — a consumer of
@@ -327,6 +344,17 @@ async fn mcp_post_response(
}
}
// Story 3.18: the legacy logging opt-in belongs to this session, so it is read before dispatch.
// A session that never sent `logging/setLevel` has no level, which is what the pinned schema
// describes as "not opted in" (no notification is delivered).
if matches!(identity.era, RequestEra::Legacy) {
crate::protocol::set_active_level(
request_session_id
.as_deref()
.and_then(|session_id| state.logging_levels.get(session_id)),
);
}
let required_scope = match method_name(message) {
Some("tools/call") => PlatformApiKeyScope::Write,
_ => PlatformApiKeyScope::Read,
@@ -337,6 +365,19 @@ async fn mcp_post_response(
Err(error) => return error.into_response(),
};
// Story 3.4 correction: the official SDK lists the media types JSON-first. For a `tools/call`
// that carries a validated progress token and offered `text/event-stream`, the stream is the only
// carrier that can deliver the progress the client asked for, so the media-type order is not a
// preference for the JSON body here. Every other request — another method, no token, an unusable
// token, a JSON-only `Accept` — keeps the negotiated mode, and no admission check moved.
if matches!(response_mode, ResponseMode::Json)
&& method_name(message) == Some("tools/call")
&& accepts_event_stream(headers)
&& crate::app::lifecycle::token_from_meta(message.pointer("/params/_meta")).is_some()
{
response_mode = ResponseMode::Sse;
}
if matches!(identity.era, RequestEra::Modern) {
// ERA_MODERN_ENTER (spec section 7.2): this request entered the modern era-adapter.
#[cfg(test)]
@@ -358,625 +399,17 @@ async fn mcp_post_response(
#[cfg(test)]
crate::app::dispatch_observer::era_legacy_enter();
// R5/F5: the owning registration decides *which handler* serves this method in the request's
// revision. The adapter holds no method list of its own — the method-to-handler mapping lives in
// `protocol::registry` and is read through `dispatch_decision`, so a newly registered method
// really reaches its handler and removing a descriptor removes the dispatch.
let registration_revision = protocol_version
.as_deref()
.unwrap_or(LEGACY_PROTOCOL_VERSION);
let dispatch = crate::protocol::dispatch_decision(
method_name(message).unwrap_or_default(),
registration_revision,
);
match dispatch {
// G2: the *registered kind* of the matched method, not the envelope shape, decides which
// legacy lifecycle action runs. `initialize` is a request and `notifications/initialized` is a
// notification: the two share one handler but are not interchangeable, so an `initialize`
// notification may not initialize an existing session and a `notifications/initialized`
// request may not create one.
DispatchDecision::Allowed {
handler: MethodHandler::Lifecycle,
kind: Kind::Method,
} if is_request(message) => handle_initialize(state, path, message, response_mode).await,
DispatchDecision::Allowed {
handler: MethodHandler::Lifecycle,
kind: Kind::Notification,
} if is_notification(message) => {
handle_initialized_notification(state, path, headers).await
}
DispatchDecision::Allowed {
handler: MethodHandler::Ping,
..
} if is_request(message) => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
json!({ "protocolVersion": session.protocol_version }),
),
response_mode,
None,
Some(&session.protocol_version),
)
}
DispatchDecision::Allowed {
handler: MethodHandler::ToolList,
..
} if is_request(message) => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
// The adapter parses the wire form and maps the neutral outcome back; the shared Tools
// service never sees the envelope, the response mode or the protocol revision.
let list_command: ToolsListCommand = match serde_json::from_value(params(message)) {
Ok(value) => value,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, "invalid tools/list parameters"),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let outcome = handle_tools_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential: &credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
ToolsCommand::List(list_command),
)
.await;
match outcome {
ToolsOutcome::List { tools, next_cursor } => transport_response(
StatusCode::OK,
jsonrpc_result(request_id(message), tools_list_result(tools, next_cursor)),
response_mode,
None,
Some(&session.protocol_version),
),
ToolsOutcome::InvalidParams(message_text) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, message_text),
response_mode,
None,
Some(&session.protocol_version),
),
ToolsOutcome::CatalogUnavailable => {
internal_jsonrpc_error(message, "published catalog is unavailable")
}
// A list command never produces a call outcome; it is answered with the adapter's
// internal error instead of being silently ignored.
ToolsOutcome::Call(_) => internal_jsonrpc_error(
message,
"unexpected shared tools outcome for a tools/list",
),
}
}
DispatchDecision::Allowed {
handler: MethodHandler::ToolCall,
..
} if is_request(message) => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let tool_call_params: ToolCallParams = match serde_json::from_value(params(message)) {
Ok(value) => value,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, "invalid tool call parameters"),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
// The adapter owns the wire form: it parses the call parameters, removes the reserved
// confirmation marker and the shared Tools service never sees the envelope, the
// response mode or the protocol revision (spec section 6.2).
let mut arguments = tool_call_params.arguments.unwrap_or_else(|| json!({}));
let confirmation_token = take_confirmation_token(&mut arguments);
let outcome = handle_tools_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential: &credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
ToolsCommand::Call(ToolCallCommand {
tool_name: tool_call_params.name,
arguments,
confirmation_token,
}),
)
.await;
tool_command_response(
message,
response_mode,
&session.protocol_version,
transport_correlation,
outcome,
)
}
DispatchDecision::Allowed {
handler: MethodHandler::ResourceList,
..
} if is_request(message) => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let list_command: ResourcesListCommand = match serde_json::from_value(params(message)) {
Ok(value) => value,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid resources/list parameters",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let outcome = handle_resources_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential: &credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
&session.protocol_version,
ResourcesCommand::List(list_command),
)
.await;
match outcome {
ResourcesOutcome::Listed {
resources,
next_cursor,
} => transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
resources_list_result(resources, next_cursor),
),
response_mode,
None,
Some(&session.protocol_version),
),
ResourcesOutcome::InvalidParams(detail) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(&session.protocol_version),
),
_ => internal_jsonrpc_error(message, "published resources are unavailable"),
}
}
DispatchDecision::Allowed {
handler: MethodHandler::ResourceRead,
..
} if is_request(message) => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let read_command: ResourceReadCommand = match serde_json::from_value(params(message)) {
Ok(value) => value,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid resources/read parameters",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let outcome = handle_resources_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential: &credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
&session.protocol_version,
ResourcesCommand::Read(read_command),
)
.await;
match outcome {
ResourcesOutcome::Read { contents } => transport_response(
StatusCode::OK,
jsonrpc_result(request_id(message), resource_read_result(contents)),
response_mode,
None,
Some(&session.protocol_version),
),
ResourcesOutcome::NotAvailable => transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"the requested resource is not available",
),
response_mode,
None,
Some(&session.protocol_version),
),
ResourcesOutcome::InvalidParams(detail) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(&session.protocol_version),
),
ResourcesOutcome::ExecutionError(contract) => resource_error_response(
message,
response_mode,
&session.protocol_version,
contract,
),
_ => internal_jsonrpc_error(message, "published resources are unavailable"),
}
}
// Story 3.11: the shared service returns exact bound Published templates; the legacy adapter
// retains its earlier result shape and still requires the initialized session.
DispatchDecision::Allowed {
handler: MethodHandler::ResourceTemplateList,
..
} if is_request(message) => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let command: ResourcesListCommand = match serde_json::from_value(params(message)) {
Ok(command) => command,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid resources/templates/list parameters",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let (templates, next_cursor) = match crate::protocol::list_resource_templates(
&state,
&path.workspace_slug,
&path.agent_slug,
&session.protocol_version,
command,
)
.await
{
Ok(page) => page,
Err(crate::protocol::ResourceTemplatesListError::InvalidCursor) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid resources/templates/list cursor",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
Err(crate::protocol::ResourceTemplatesListError::Unavailable) => {
return internal_jsonrpc_error(message, "published templates are unavailable");
}
};
transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
resource_templates_list_result(templates, next_cursor),
),
response_mode,
None,
Some(&session.protocol_version),
)
}
// Story 3.14: `prompts/list` / `prompts/get` of the initialized legacy session, served from
// the same exact bound Published Prompt Versions as the modern era.
DispatchDecision::Allowed {
handler: MethodHandler::PromptList,
..
} if is_request(message) => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let command: PromptsListCommand = match serde_json::from_value(params(message)) {
Ok(command) => command,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid prompts/list parameters",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let outcome = handle_prompts_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential: &credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
&session.protocol_version,
PromptsCommand::List(command),
)
.await;
match outcome {
PromptsOutcome::Listed {
prompts,
next_cursor,
} => transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
prompts_list_result(prompts, next_cursor),
),
response_mode,
None,
Some(&session.protocol_version),
),
PromptsOutcome::InvalidParams(detail) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(&session.protocol_version),
),
_ => internal_jsonrpc_error(message, "published prompts are unavailable"),
}
}
DispatchDecision::Allowed {
handler: MethodHandler::PromptGet,
..
} if is_request(message) => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let command: PromptGetCommand = match serde_json::from_value(params(message)) {
Ok(command) => command,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid prompts/get parameters",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let outcome = handle_prompts_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential: &credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
&session.protocol_version,
PromptsCommand::Get(command),
)
.await;
match outcome {
PromptsOutcome::Got {
description,
messages,
} => transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
prompt_get_result(description, messages),
),
response_mode,
None,
Some(&session.protocol_version),
),
PromptsOutcome::NotAvailable => transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"the requested prompt is not available",
),
response_mode,
None,
Some(&session.protocol_version),
),
PromptsOutcome::ArgumentsRejected { reason, argument } => {
prompt_arguments_error_response(
message,
response_mode,
&session.protocol_version,
reason,
argument,
)
}
PromptsOutcome::DependencyUnavailable { position } => transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32603,
format!("the pinned dependency at dependencies[{position}] is unavailable"),
),
response_mode,
None,
Some(&session.protocol_version),
),
PromptsOutcome::InvalidParams(detail) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(&session.protocol_version),
),
_ => internal_jsonrpc_error(message, "published prompts are unavailable"),
}
}
// Story 3.17: `completion/complete` of the initialized legacy session. The shared service is
// the same one the modern era uses; only the result envelope differs (no `resultType`).
DispatchDecision::Allowed {
handler: MethodHandler::Completion,
..
} if is_request(message) => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let command: CompletionCommand = match serde_json::from_value(params(message)) {
Ok(command) => command,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid completion/complete parameters",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let outcome = handle_completion_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential: &credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
&session.protocol_version,
command,
)
.await;
match outcome {
CompletionOutcome::Completed {
values,
total,
has_more,
} => transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
completion_result(values, total, has_more),
),
response_mode,
None,
Some(&session.protocol_version),
),
CompletionOutcome::InvalidParams(detail) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(&session.protocol_version),
),
CompletionOutcome::NotAvailable => transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"the completion reference is not available",
),
response_mode,
None,
Some(&session.protocol_version),
),
CompletionOutcome::CatalogUnavailable => {
internal_jsonrpc_error(message, "published completion sources are unavailable")
}
}
}
// A notification whose method the registration does not map to this adapter's handlers is
// acknowledged without being dispatched, exactly as before.
_ if is_notification(message) => StatusCode::ACCEPTED.into_response(),
// Every remaining decision (unregistered, wrong era, or a handler this adapter does not
// serve) is a method-level rejection: no handler, no service entry, no outbound call.
_ => match method_name(message) {
Some(method) => transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32601,
format!("method {method} is not supported"),
),
response_mode,
None,
protocol_version.as_deref(),
),
None => transport_response(
StatusCode::BAD_REQUEST,
jsonrpc_error(Value::Null, -32600, "invalid JSON-RPC message"),
response_mode,
None,
protocol_version.as_deref(),
),
},
}
// R5/F5: the owning registration decides which handler serves this method in the request's
// revision; that legacy dispatch lives in `super::legacy_dispatch`.
super::legacy_dispatch::handle_legacy_request(
state,
path,
headers,
message,
response_mode,
&identity,
&credential,
transport_correlation,
)
.await
}
@@ -0,0 +1,839 @@
//! Legacy-era JSON-RPC dispatch: the registered method-to-handler mapping, the
//! initialized-session guard and every legacy envelope the transport endpoint answers.
//!
//! Moved verbatim out of `app/endpoint.rs` (Story 3.18 code-health, kanban t_d1b45ddc) so the
//! transport entry file stays inside the 1000-line limit; the entry point is `pub(super)`.
//! No behaviour changed.
use std::sync::Arc;
use axum::{
http::{HeaderMap, StatusCode},
response::{IntoResponse, Response},
};
use crank_core::CorrelationContext;
use serde_json::{Value, json};
use super::dispatch::{
handle_initialize, handle_initialized_notification, require_initialized_session,
};
use super::request::ToolCallParams;
use super::response::{
completion_result, internal_jsonrpc_error, prompt_arguments_error_response, prompt_get_result,
prompts_list_result, resource_error_response, resource_read_result,
resource_templates_list_result, resources_list_result, take_confirmation_token,
tools_list_result,
};
use super::rmcp_dispatch::{
handle_resources_command, handle_tools_command, list_resource_templates,
};
use super::{AgentRoutePath, AppState};
use crate::{
auth::VerifiedMachineCredential,
jsonrpc::{LEGACY_PROTOCOL_VERSION, jsonrpc_error, jsonrpc_result, params, request_id},
protocol::{
CompletionCommand, CompletionOutcome, ContinuationSupport, DispatchDecision,
EVENT_LEVEL_APPLIED, INVALID_LEVEL, Kind, LoggingOutcome, MethodHandler, PromptGetCommand,
PromptsCommand, PromptsListCommand, PromptsOutcome, RequestIdentity, ResourceReadCommand,
ResourcesCommand, ResourcesListCommand, ResourcesOutcome, ServiceContext, SetLevelCommand,
ToolCallCommand, ToolsCommand, ToolsListCommand, ToolsOutcome, handle_logging_command,
},
tool_error::execution_locale,
transport::{ResponseMode, session_id_from_headers, transport_response},
};
// The transport invariants of one request travel together here; a parameter struct would only
// duplicate them (the same shape every other dispatch entry point in this crate uses).
#[allow(clippy::too_many_arguments)]
pub(super) async fn handle_legacy_request(
state: Arc<AppState>,
path: &AgentRoutePath,
headers: &HeaderMap,
message: &Value,
response_mode: ResponseMode,
identity: &RequestIdentity,
credential: &VerifiedMachineCredential,
transport_correlation: &CorrelationContext,
) -> Response {
let protocol_version = identity.version.as_deref();
// R5/F5: the owning registration decides *which handler* serves this method in the request's
// revision. The adapter holds no method list of its own — the method-to-handler mapping lives in
// `protocol::registry` and is read through `dispatch_decision`, so a newly registered method
// really reaches its handler and removing a descriptor removes the dispatch.
let registration_revision = protocol_version.unwrap_or(LEGACY_PROTOCOL_VERSION);
let envelope = match crate::protocol::decode_dispatch_envelope(message) {
Ok(Some(envelope)) => envelope,
_ => {
return internal_jsonrpc_error(
message,
"admitted dispatch envelope could not be decoded",
);
}
};
let method = envelope.method();
let dispatch =
crate::protocol::dispatch_decision(method.unwrap_or_default(), registration_revision);
#[cfg(test)]
super::dispatch_observer::sdk_dispatch_envelope_consumed();
match dispatch {
// G2: the *registered kind* of the matched method, not the envelope shape, decides which
// legacy lifecycle action runs. `initialize` is a request and `notifications/initialized` is a
// notification: the two share one handler but are not interchangeable, so an `initialize`
// notification may not initialize an existing session and a `notifications/initialized`
// request may not create one.
DispatchDecision::Allowed {
handler: MethodHandler::Lifecycle,
kind: Kind::Method,
} if envelope.is_request() => handle_initialize(state, path, message, response_mode).await,
DispatchDecision::Allowed {
handler: MethodHandler::Lifecycle,
kind: Kind::Notification,
} if envelope.is_notification() => {
handle_initialized_notification(
state,
path,
headers,
crate::protocol::initialized_notification(message),
)
.await
}
DispatchDecision::Allowed {
handler: MethodHandler::Ping,
..
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
crate::protocol::ping_result(
&json!({"method": "ping"}),
&session.protocol_version,
),
),
response_mode,
None,
Some(&session.protocol_version),
)
}
DispatchDecision::Allowed {
handler: MethodHandler::ToolList,
..
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
// The adapter parses the wire form and maps the neutral outcome back; the shared Tools
// service never sees the envelope, the response mode or the protocol revision.
let list_command: ToolsListCommand = match serde_json::from_value(params(message)) {
Ok(value) => value,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, "invalid tools/list parameters"),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let outcome = handle_tools_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
ToolsCommand::List(list_command),
// A list command has no progress or cancellation lifecycle (Story 3.4).
None,
)
.await;
match outcome {
ToolsOutcome::List { tools, next_cursor } => {
match tools_list_result(tools, next_cursor, &session.protocol_version) {
Ok(result) => transport_response(
StatusCode::OK,
jsonrpc_result(request_id(message), result),
response_mode,
None,
Some(&session.protocol_version),
),
Err(error) => internal_jsonrpc_error(message, error),
}
}
ToolsOutcome::InvalidParams(message_text) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, message_text),
response_mode,
None,
Some(&session.protocol_version),
),
ToolsOutcome::CatalogUnavailable => {
internal_jsonrpc_error(message, "published catalog is unavailable")
}
// A list command never produces a call outcome; it is answered with the adapter's
// internal error instead of being silently ignored.
ToolsOutcome::Call(_) => internal_jsonrpc_error(
message,
"unexpected shared tools outcome for a tools/list",
),
}
}
DispatchDecision::Allowed {
handler: MethodHandler::ToolCall,
..
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let tool_call_params: ToolCallParams = match serde_json::from_value(params(message)) {
Ok(value) => value,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, "invalid tool call parameters"),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
// The adapter owns the wire form: it parses the call parameters, removes the reserved
// confirmation marker and the shared Tools service never sees the envelope, the
// response mode or the protocol revision (spec section 6.2).
let mut arguments = tool_call_params.arguments.unwrap_or_else(|| json!({}));
let confirmation_token = take_confirmation_token(&mut arguments);
// Story 3.4: the client's own `_meta.progressToken`, validated and bounded, and the
// session/credential ownership of the in-flight cancellation table.
let progress = crate::app::lifecycle::token_from_meta(tool_call_params.meta.as_ref());
super::lifecycle::run_tool_call(
Arc::clone(&state),
crate::app::lifecycle::ToolCallRequest {
workspace_slug: session.workspace_slug.clone(),
agent_slug: session.agent_slug.clone(),
credential: credential.clone(),
correlation: transport_correlation.clone(),
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
crate::app::lifecycle::ToolCallWire {
message: message.clone(),
protocol_version: session.protocol_version.clone(),
response_mode,
progress,
session_id: Some(session.id.clone()),
in_flight: Some(crate::app::lifecycle::InFlightScope {
session_id: session.id.clone(),
owner: in_flight_owner(credential),
}),
},
ToolCallCommand {
tool_name: tool_call_params.name,
arguments,
confirmation_token,
},
)
.await
}
DispatchDecision::Allowed {
handler: MethodHandler::Cancelled,
kind: Kind::Notification,
} if envelope.is_notification() => {
handle_cancelled_notification(&state, path, headers, message, credential).await
}
DispatchDecision::Allowed {
handler: MethodHandler::ResourceList,
..
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let list_command: ResourcesListCommand = match serde_json::from_value(params(message)) {
Ok(value) => value,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid resources/list parameters",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let outcome = handle_resources_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
&session.protocol_version,
ResourcesCommand::List(list_command),
)
.await;
match outcome {
ResourcesOutcome::Listed {
resources,
next_cursor,
} => transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
match resources_list_result(
resources,
next_cursor,
&session.protocol_version,
) {
Ok(result) => result,
Err(error) => return internal_jsonrpc_error(message, error),
},
),
response_mode,
None,
Some(&session.protocol_version),
),
ResourcesOutcome::InvalidParams(detail) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(&session.protocol_version),
),
_ => internal_jsonrpc_error(message, "published resources are unavailable"),
}
}
DispatchDecision::Allowed {
handler: MethodHandler::ResourceRead,
..
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let read_command: ResourceReadCommand = match serde_json::from_value(params(message)) {
Ok(value) => value,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid resources/read parameters",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let outcome = handle_resources_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
&session.protocol_version,
ResourcesCommand::Read(read_command),
)
.await;
match outcome {
ResourcesOutcome::Read { contents } => transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
match resource_read_result(contents, &session.protocol_version) {
Ok(result) => result,
Err(error) => return internal_jsonrpc_error(message, error),
},
),
response_mode,
None,
Some(&session.protocol_version),
),
ResourcesOutcome::NotAvailable => transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"the requested resource is not available",
),
response_mode,
None,
Some(&session.protocol_version),
),
ResourcesOutcome::InvalidParams(detail) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(&session.protocol_version),
),
ResourcesOutcome::ExecutionError(contract) => resource_error_response(
message,
response_mode,
&session.protocol_version,
contract,
),
_ => internal_jsonrpc_error(message, "published resources are unavailable"),
}
}
// Story 3.11: the shared service returns exact bound Published templates; the legacy adapter
// retains its earlier result shape and still requires the initialized session.
DispatchDecision::Allowed {
handler: MethodHandler::ResourceTemplateList,
..
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let command: ResourcesListCommand = match serde_json::from_value(params(message)) {
Ok(command) => command,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid resources/templates/list parameters",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let (templates, next_cursor) = match list_resource_templates(
&state,
&path.workspace_slug,
&path.agent_slug,
&session.protocol_version,
command,
)
.await
{
Ok(page) => page,
Err(crate::protocol::ResourceTemplatesListError::InvalidCursor) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid resources/templates/list cursor",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
Err(crate::protocol::ResourceTemplatesListError::Unavailable) => {
return internal_jsonrpc_error(message, "published templates are unavailable");
}
};
transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
match resource_templates_list_result(
templates,
next_cursor,
&session.protocol_version,
) {
Ok(result) => result,
Err(error) => return internal_jsonrpc_error(message, error),
},
),
response_mode,
None,
Some(&session.protocol_version),
)
}
// Story 3.14: `prompts/list` / `prompts/get` of the initialized legacy session, served from
// the same exact bound Published Prompt Versions as the modern era.
DispatchDecision::Allowed {
handler: MethodHandler::PromptList,
..
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let command: PromptsListCommand = match serde_json::from_value(params(message)) {
Ok(command) => command,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid prompts/list parameters",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let outcome = super::rmcp_dispatch::handle_prompts_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
&session.protocol_version,
PromptsCommand::List(command),
)
.await;
match outcome {
PromptsOutcome::Listed {
prompts,
next_cursor,
} => transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
match prompts_list_result(prompts, next_cursor) {
Ok(result) => result,
Err(error) => return internal_jsonrpc_error(message, error),
},
),
response_mode,
None,
Some(&session.protocol_version),
),
PromptsOutcome::InvalidParams(detail) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(&session.protocol_version),
),
_ => internal_jsonrpc_error(message, "published prompts are unavailable"),
}
}
DispatchDecision::Allowed {
handler: MethodHandler::PromptGet,
..
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let command: PromptGetCommand = match serde_json::from_value(params(message)) {
Ok(command) => command,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid prompts/get parameters",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let outcome = super::rmcp_dispatch::handle_prompts_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
&session.protocol_version,
PromptsCommand::Get(command),
)
.await;
match outcome {
PromptsOutcome::Got {
description,
messages,
} => transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
match prompt_get_result(description, messages) {
Ok(result) => result,
Err(error) => return internal_jsonrpc_error(message, error),
},
),
response_mode,
None,
Some(&session.protocol_version),
),
PromptsOutcome::NotAvailable => transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"the requested prompt is not available",
),
response_mode,
None,
Some(&session.protocol_version),
),
PromptsOutcome::ArgumentsRejected { reason, argument } => {
prompt_arguments_error_response(
message,
response_mode,
&session.protocol_version,
reason,
argument,
)
}
PromptsOutcome::DependencyUnavailable { position } => transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32603,
format!("the pinned dependency at dependencies[{position}] is unavailable"),
),
response_mode,
None,
Some(&session.protocol_version),
),
PromptsOutcome::InvalidParams(detail) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(&session.protocol_version),
),
_ => internal_jsonrpc_error(message, "published prompts are unavailable"),
}
}
// Story 3.17: `completion/complete` of the initialized legacy session. The shared service is
// the same one the modern era uses; only the result envelope differs (no `resultType`).
DispatchDecision::Allowed {
handler: MethodHandler::Completion,
..
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let command: CompletionCommand = match serde_json::from_value(params(message)) {
Ok(command) => command,
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"invalid completion/complete parameters",
),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
let outcome = super::rmcp_dispatch::handle_completion_command(
&state,
ServiceContext {
workspace_slug: &session.workspace_slug,
agent_slug: &session.agent_slug,
credential,
correlation: transport_correlation,
continuation: ContinuationSupport::from_supported(session.supports_elicitation),
locale: execution_locale(message),
},
&session.protocol_version,
command,
)
.await;
match outcome {
CompletionOutcome::Completed {
values,
total,
has_more,
} => transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
completion_result(values, total, has_more),
),
response_mode,
None,
Some(&session.protocol_version),
),
CompletionOutcome::InvalidParams(detail) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(&session.protocol_version),
),
CompletionOutcome::NotAvailable => transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32602,
"the completion reference is not available",
),
response_mode,
None,
Some(&session.protocol_version),
),
CompletionOutcome::CatalogUnavailable => {
internal_jsonrpc_error(message, "published completion sources are unavailable")
}
}
}
// Story 3.18: the pinned legacy `logging/setLevel`. The level belongs to the initialized
// session of this request, so setting it can never change another client's level. The request
// answers an empty result; the acknowledgment event is filtered by the level it just set.
DispatchDecision::Allowed {
handler: MethodHandler::LoggingSetLevel,
..
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
};
let command: SetLevelCommand = match serde_json::from_value(params(message)) {
Ok(command) => command,
// A non-string, missing or otherwise unparsable level shares the one safe detail of a
// value outside the pinned union: nothing the client sent is echoed back.
Err(_) => {
return transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, INVALID_LEVEL),
response_mode,
None,
Some(&session.protocol_version),
);
}
};
match handle_logging_command(&state.logging_levels, &session.id, command) {
LoggingOutcome::Applied(level) => {
crate::protocol::set_active_level(Some(level));
crate::protocol::note(
crate::protocol::LogLevel::Notice,
EVENT_LEVEL_APPLIED,
"applied",
);
transport_response(
StatusCode::OK,
jsonrpc_result(request_id(message), json!({})),
response_mode,
None,
Some(&session.protocol_version),
)
}
LoggingOutcome::InvalidParams(detail) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(&session.protocol_version),
),
}
}
// A notification whose method the registration does not map to this adapter's handlers is
// acknowledged without being dispatched, exactly as before.
_ if envelope.is_notification() => StatusCode::ACCEPTED.into_response(),
// Every remaining decision (unregistered, wrong era, or a handler this adapter does not
// serve) is a method-level rejection: no handler, no service entry, no outbound call.
_ => match method {
Some(method) => transport_response(
StatusCode::OK,
jsonrpc_error(
request_id(message),
-32601,
format!("method {method} is not supported"),
),
response_mode,
None,
protocol_version,
),
None => transport_response(
StatusCode::BAD_REQUEST,
jsonrpc_error(Value::Null, -32600, "invalid JSON-RPC message"),
response_mode,
None,
protocol_version,
),
},
}
}
/// The credential half of the cancellation ownership of one call (Story 3.4). The progress token is
/// deliberately not part of it: a token never authorizes a cancellation.
fn in_flight_owner(credential: &VerifiedMachineCredential) -> String {
credential
.platform_api_key_id
.as_ref()
.map_or_else(String::new, |id| id.as_str().to_owned())
}
/// Route one legacy `notifications/cancelled` to the call it names, then acknowledge it.
///
/// The pinned rules of a cancelled request are all "ignore": an unknown or already finished request,
/// a foreign session or credential, a malformed `requestId` and an unknown session are acknowledged
/// without any effect. That is also why this handler never fails the transport — a client that
/// cancels must not learn anything about another client's calls.
async fn handle_cancelled_notification(
state: &Arc<AppState>,
path: &AgentRoutePath,
headers: &HeaderMap,
message: &Value,
credential: &VerifiedMachineCredential,
) -> Response {
let request_id = super::rmcp_progress_cancel::decode_cancelled_request_id(&params(message));
let session_id = session_id_from_headers(headers).ok().flatten();
if let (Some(request_id), Some(session_id)) = (request_id, session_id) {
let owned_by_session = match state.sessions.get(&session_id).await {
Ok(Some(session)) => {
session.workspace_slug == path.workspace_slug
&& session.agent_slug == path.agent_slug
}
_ => false,
};
if owned_by_session {
state.in_flight.cancel(
&crate::app::lifecycle::InFlightScope {
session_id,
owner: in_flight_owner(credential),
},
&request_id,
);
}
}
StatusCode::ACCEPTED.into_response()
}
@@ -0,0 +1,578 @@
//! Story 3.4 (FR-14): request-scoped progress and cancellation of one synchronous `tools/call`.
//!
//! The pinned `RequestMetaObject` of *both* supported revisions carries an optional opaque
//! `progressToken`; a request that has one asks for out-of-band `notifications/progress` messages
//! about that request. Such a message only has a carrier on the request's own SSE response stream, so
//! a token is honoured only when the client negotiated `text/event-stream`. Without a token — or
//! without the stream — the call runs exactly as before: the receiver of a progress token is never
//! obligated to send progress, so the execution result cannot change.
//!
//! Cancellation follows the revision-specific rule of the pinned specification:
//! * `2025-11-25`: the client POSTs `notifications/cancelled` with the id of the request it issued.
//! The bounded in-flight table routes it to that call. Ownership is the session *and* the platform
//! credential of the call, never the progress token: a foreign session or key cancels nothing.
//! * `2026-07-28`: closing the request's SSE response stream *is* the cancellation signal, so there is
//! no notification to route; dropping the response stream drops the call future.
//!
//! Either way a cancelled call never becomes a false `completed`: no further message is sent for it
//! (the pinned rule for a cancelled request), and the call never reaches its terminal outcome, so no
//! Invocation History row claims a completion. The record contract only admits a *closed* execution
//! error code for a non-`ok` row and a cancellation is not one of them, so the honest state of a
//! stopped call is "no terminal record" rather than an invented error code; a dedicated cancellation
//! code would be a schema/vocabulary decision of its own.
use std::{
collections::{HashMap, VecDeque},
future::Future,
pin::Pin,
sync::{Arc, Mutex},
};
use axum::{
http::StatusCode,
response::{IntoResponse, Response, sse::Event},
};
use crank_core::{CorrelationContext, ExecutionLocale, VerifiedMachineCredential};
use futures_util::stream;
use rmcp::model::{NumberOrString, ProgressNotificationParam};
use serde_json::{Number, Value, json};
use tokio::sync::{mpsc, watch};
use super::rmcp_dispatch::handle_tools_command;
use super::{
AppState,
response::{ToolCommandPayload, tool_command_payload, tool_command_response},
};
use crate::{
jsonrpc::request_id,
protocol::{ContinuationSupport, ServiceContext, ToolCallCommand, ToolsCommand, ToolsOutcome},
transport::{ResponseMode, sse_response},
};
/// The bounded progress queue of one call. A client that reads its stream slowly can never block the
/// call: a full queue drops the event instead of waiting, and the remaining events stay monotonic.
pub(crate) const PROGRESS_CAPACITY: usize = 8;
/// The pinned `ProgressToken` bound this server accepts. A longer token is not rejected as an error:
/// progress is simply disabled for that request, exactly as for a malformed one.
pub(crate) const MAX_PROGRESS_TOKEN_LEN: usize = 128;
/// The bounded table of calls that can still be cancelled. A call that finds the table full runs
/// without cancellation instead of growing the table.
pub(crate) const MAX_IN_FLIGHT_CALLS: usize = 256;
/// A validated `RequestMetaObject.progressToken`: the pinned opaque token is a string or a number.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum ProgressToken {
Text(String),
Number(Number),
}
impl ProgressToken {
/// `None` means "disable progress", never "fail the call": the pinned schema declares the token
/// as an opaque client value the receiver may simply ignore.
fn parse(value: &Value) -> Option<Self> {
match value {
Value::String(text)
if !text.is_empty()
&& text.len() <= MAX_PROGRESS_TOKEN_LEN
&& !text.chars().any(char::is_control) =>
{
Some(Self::Text(text.clone()))
}
Value::Number(number) => Some(Self::Number(number.clone())),
_ => None,
}
}
fn sdk_token(&self) -> NumberOrString {
match self {
Self::Text(text) => NumberOrString::String(text.clone().into()),
Self::Number(number) => number.as_i64().map_or_else(
|| NumberOrString::String(number.to_string().into()),
NumberOrString::Number,
),
}
}
}
/// The progress token of one request parameters object, already validated and bounded. The adapters
/// read the `_meta` object they already parsed ([`token_from_meta`]); this is the test seam of the
/// same rule.
#[cfg(test)]
pub(crate) fn progress_token(params: &Value) -> Option<ProgressToken> {
token_from_meta(params.get("_meta"))
}
/// The validated token of one `RequestMetaObject`. A non-object `_meta` has no token.
pub(crate) fn token_from_meta(meta: Option<&Value>) -> Option<ProgressToken> {
meta.and_then(|meta| meta.get("progressToken"))
.and_then(ProgressToken::parse)
}
/// One real, already reached execution step: the shared Tools service emits these at the stage
/// boundaries it actually crosses, so no percentage is invented and no timer drives them.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Phase {
Resolved,
Authorized,
Dispatching,
}
impl Phase {
const fn message(self) -> &'static str {
match self {
Self::Resolved => "tool_resolved",
Self::Authorized => "authorization_resolved",
Self::Dispatching => "execution_dispatched",
}
}
}
#[derive(Clone, Debug)]
pub(crate) struct ProgressEvent {
progress: u64,
message: &'static str,
}
/// The pinned `notifications/progress` frame. Both supported revisions declare the same parameters,
/// so the projection is shared; the token is echoed verbatim and never validated as a credential.
pub(super) fn progress_frame(token: &ProgressToken, event: &ProgressEvent) -> Value {
let sdk_params = ProgressNotificationParam::new(
rmcp::model::ProgressToken(token.sdk_token()),
event.progress as f64,
)
.with_message(event.message);
let mut params = serde_json::to_value(sdk_params)
.expect("finite progress and string/number token are serializable SDK params");
#[cfg(test)]
super::dispatch_observer::sdk_progress_notification_serialized();
// SDK uses i64 tokens and f64 progress; restore the original JSON numbers before wire delivery.
if let ProgressToken::Number(number) = token {
params["progressToken"] = Value::Number(number.clone());
}
params["progress"] = Value::from(event.progress);
json!({
"jsonrpc": "2.0",
"method": "notifications/progress",
"params": params,
})
}
#[derive(Clone)]
struct ProgressSink {
events: mpsc::Sender<ProgressEvent>,
produced: Arc<std::sync::atomic::AtomicU64>,
}
impl ProgressSink {
fn emit(&self, message: &'static str) {
let progress = self
.produced
.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
+ 1;
let _ = self.events.try_send(ProgressEvent { progress, message });
}
}
/// A cooperative cancellation signal of one call. It is set at most once; a signal whose only sender
/// is gone simply never fires.
#[derive(Clone)]
pub(crate) struct CancelSignal {
receiver: watch::Receiver<bool>,
}
impl CancelSignal {
fn never() -> Self {
let (_sender, receiver) = watch::channel(false);
Self { receiver }
}
pub(crate) async fn cancelled(&mut self) {
if self
.receiver
.wait_for(|cancelled| *cancelled)
.await
.is_err()
{
// The owning call is gone, so nothing can cancel it any more.
std::future::pending::<()>().await;
}
}
}
/// The request-scoped lifecycle of one `tools/call`: its progress sink (when the client asked for
/// progress *and* offered the stream carrier) and its cancellation signal.
#[derive(Clone)]
pub(crate) struct ToolCallLifecycle {
progress: Option<ProgressSink>,
cancel: CancelSignal,
}
impl ToolCallLifecycle {
pub(crate) fn new(
token: Option<ProgressToken>,
) -> (Self, Option<mpsc::Receiver<ProgressEvent>>) {
match token {
Some(_) => {
let (sender, receiver) = mpsc::channel(PROGRESS_CAPACITY);
(
Self {
progress: Some(ProgressSink {
events: sender,
produced: Arc::new(std::sync::atomic::AtomicU64::new(0)),
}),
cancel: CancelSignal::never(),
},
Some(receiver),
)
}
None => (
Self {
progress: None,
cancel: CancelSignal::never(),
},
None,
),
}
}
pub(crate) fn with_cancel(mut self, cancel: CancelSignal) -> Self {
self.cancel = cancel;
self
}
/// Record one real execution step. Without a progress sink this is a no-op, so the shared service
/// does not branch on the presence of a token.
pub(crate) fn advance(&self, phase: Phase) {
if let Some(progress) = &self.progress {
progress.emit(phase.message());
}
}
pub(crate) fn cancelled(&mut self) -> impl Future<Output = ()> + '_ {
self.cancel.cancelled()
}
}
/// The owned identity of one call, so the call future can be driven by the response stream itself.
pub(crate) struct ToolCallRequest {
pub(crate) workspace_slug: String,
pub(crate) agent_slug: String,
pub(crate) credential: VerifiedMachineCredential,
pub(crate) correlation: CorrelationContext,
pub(crate) continuation: ContinuationSupport,
pub(crate) locale: ExecutionLocale,
}
/// The wire facts the adapter owns: the envelope, the negotiated revision and response mode, the
/// validated token and — for the legacy revision — the session-scoped in-flight ownership.
pub(crate) struct ToolCallWire {
pub(crate) message: Value,
pub(crate) protocol_version: String,
pub(crate) response_mode: ResponseMode,
pub(crate) progress: Option<ProgressToken>,
pub(crate) session_id: Option<String>,
pub(crate) in_flight: Option<InFlightScope>,
}
/// Who owns a cancellable call. The session and the platform credential are the ownership of the
/// legacy revision; the progress token is deliberately not part of it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct InFlightScope {
pub(crate) session_id: String,
pub(crate) owner: String,
}
/// The bounded table of the calls a legacy client can still cancel.
pub(crate) struct InFlightCalls {
entries: Mutex<HashMap<String, watch::Sender<bool>>>,
capacity: usize,
}
impl InFlightCalls {
pub(crate) fn new(capacity: usize) -> Self {
Self {
entries: Mutex::new(HashMap::new()),
capacity,
}
}
fn entries(&self) -> std::sync::MutexGuard<'_, HashMap<String, watch::Sender<bool>>> {
// A panic of another request must not make the cancellation table unusable.
self.entries
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
fn key(scope: &InFlightScope, request_id: &Value) -> String {
// The pinned `RequestId` is a string or a number; the two must not collapse into one key.
format!(
"{}\u{1f}{}\u{1f}{request_id}",
scope.session_id, scope.owner
)
}
/// Register one call as cancellable. `None` means the table is full (or the id is already live),
/// and the call then runs without cancellation instead of evicting someone else's entry.
pub(crate) fn register(
self: &Arc<Self>,
scope: &InFlightScope,
request_id: &Value,
) -> Option<InFlightEntry> {
let key = Self::key(scope, request_id);
let (sender, receiver) = watch::channel(false);
{
let mut entries = self.entries();
if entries.len() >= self.capacity || entries.contains_key(&key) {
return None;
}
entries.insert(key.clone(), sender);
}
Some(InFlightEntry {
registry: Arc::clone(self),
key,
cancel: CancelSignal { receiver },
})
}
/// Route one `notifications/cancelled` to the call it names. `false` means there is no such
/// in-flight call of this owner, which the specification requires to be ignored.
pub(crate) fn cancel(&self, scope: &InFlightScope, request_id: &Value) -> bool {
let entries = self.entries();
entries
.get(&Self::key(scope, request_id))
.is_some_and(|sender| sender.send(true).is_ok())
}
}
/// The live registration of one call: dropping it (terminal response, cancellation or a dropped
/// response stream) releases the table slot.
pub(crate) struct InFlightEntry {
registry: Arc<InFlightCalls>,
key: String,
cancel: CancelSignal,
}
impl InFlightEntry {
pub(crate) fn cancel_signal(&self) -> CancelSignal {
self.cancel.clone()
}
}
impl Drop for InFlightEntry {
fn drop(&mut self) {
self.registry.entries().remove(&self.key);
}
}
/// Run one `tools/call` through the shared Tools service with the lifecycle of this request.
///
/// Without a progress token — or without the SSE carrier the client negotiated — the response is the
/// unchanged single JSON (or single-event SSE) response. With both, the response is the request's own
/// bounded stream: the real progress events of this call, then the terminal response as the last
/// event. Dropping that stream (a `2026-07-28` cancellation) or setting the signal of a legacy
/// `notifications/cancelled` drops the call future, which is the cooperative stop.
pub(crate) async fn run_tool_call(
state: Arc<AppState>,
request: ToolCallRequest,
wire: ToolCallWire,
command: ToolCallCommand,
) -> Response {
// A token is only honoured when the client offered the only carrier a notification has: the
// request's own SSE stream. Otherwise the call runs unchanged (a receiver is never obligated).
let token = match (&wire.response_mode, &wire.progress) {
(ResponseMode::Sse, Some(token)) => Some(token.clone()),
_ => None,
};
let (mut lifecycle, progress_events) = ToolCallLifecycle::new(token.clone());
let in_flight = wire.in_flight.as_ref().and_then(|scope| {
let entry = state.in_flight.register(scope, &request_id(&wire.message));
if let Some(entry) = &entry {
lifecycle = lifecycle.clone().with_cancel(entry.cancel_signal());
}
entry
});
let message = wire.message.clone();
let correlation = request.correlation.clone();
let protocol_version = wire.protocol_version.clone();
let response_mode = wire.response_mode;
let mut call = Box::pin(call_future(
Arc::clone(&state),
request,
lifecycle.clone(),
command,
));
let Some(progress_events) = progress_events else {
// No notification carrier for this call: run it as before, still cancellable.
let outcome = tokio::select! {
biased;
outcome = &mut call => outcome,
_ = lifecycle.cancelled() => return cancelled_response(),
};
return tool_command_response(
&message,
response_mode,
&protocol_version,
&correlation,
outcome,
);
};
debug_assert!(matches!(response_mode, ResponseMode::Sse));
let token = token.expect("a progress receiver implies a validated token");
let stream = stream::unfold(
CallStream {
call,
events: progress_events,
cancel: lifecycle.cancel.clone(),
token,
message,
protocol_version: protocol_version.clone(),
correlation,
pending: VecDeque::new(),
finished: false,
logging: crate::protocol::carry_scope(),
_in_flight: in_flight,
},
next_event,
);
sse_response(
StatusCode::OK,
stream,
wire.session_id.as_deref(),
Some(&protocol_version),
)
}
async fn call_future(
state: Arc<AppState>,
request: ToolCallRequest,
lifecycle: ToolCallLifecycle,
command: ToolCallCommand,
) -> ToolsOutcome {
handle_tools_command(
&state,
ServiceContext {
workspace_slug: &request.workspace_slug,
agent_slug: &request.agent_slug,
credential: &request.credential,
correlation: &request.correlation,
continuation: request.continuation,
locale: request.locale,
},
ToolsCommand::Call(command),
Some(lifecycle),
)
.await
}
/// A cancelled call is not answered: the pinned rule for a cancelled request is that no further
/// message is sent for it, and the client that cancelled it ignores a late response anyway. Nothing
/// here claims that an outbound side effect did or did not happen — and nothing else claims the
/// opposite either: the call is stopped before its terminal outcome, so its Invocation History holds
/// no row for this call at all (see the module docs for why there is no invented cancellation code).
fn cancelled_response() -> Response {
StatusCode::OK.into_response()
}
struct CallStream {
call: Pin<Box<dyn Future<Output = ToolsOutcome> + Send>>,
events: mpsc::Receiver<ProgressEvent>,
cancel: CancelSignal,
token: ProgressToken,
message: Value,
protocol_version: String,
correlation: CorrelationContext,
pending: VecDeque<Event>,
finished: bool,
/// The request's logging context, so the terminal event of the stream is projected exactly as the
/// single response of the same call would be (Story 3.18 carried across the stream, Story 3.4).
logging: crate::protocol::CarriedScope,
/// Held for the lifetime of the stream: the slot is released when the call is over, however it ends.
_in_flight: Option<InFlightEntry>,
}
enum Step {
Progress(ProgressEvent),
Outcome(ToolsOutcome),
Cancelled,
}
/// One stream item: a queued frame, the next real progress event, the terminal response, or the end of
/// a cancelled stream. A cancelled call sends no terminal frame at all.
async fn next_event(
call: CallStream,
) -> Option<(Result<Event, std::convert::Infallible>, CallStream)> {
let scope = call.logging;
crate::protocol::in_carried_scope(scope, next_event_in_scope(call)).await
}
async fn next_event_in_scope(
mut call: CallStream,
) -> Option<(Result<Event, std::convert::Infallible>, CallStream)> {
if let Some(frame) = call.pending.pop_front() {
return Some((Ok(frame), call));
}
if call.finished {
return None;
}
let step = {
let CallStream {
call,
events,
cancel,
..
} = &mut call;
tokio::select! {
biased;
Some(event) = events.recv() => Step::Progress(event),
outcome = call => Step::Outcome(outcome),
_ = cancel.cancelled() => Step::Cancelled,
}
};
match step {
Step::Progress(event) => {
let frame = progress_frame(&call.token, &event);
Some((Ok(Event::default().data(frame.to_string())), call))
}
Step::Outcome(outcome) => {
call.finished = true;
let payload = match tool_command_payload(
&call.message,
&call.protocol_version,
&call.correlation,
outcome,
) {
ToolCommandPayload::Ok(payload) => payload,
ToolCommandPayload::Internal(_) => jsonrpc_internal_error(&call.message),
};
// The same projection point the single response uses, so a streamed answer delivers the
// same closed `notifications/message` (Story 3.18) before its terminal frame.
crate::transport::note_logging_outcome(&payload);
for notification in crate::protocol::take_notifications() {
call.pending
.push_back(Event::default().data(notification.to_string()));
}
call.pending
.push_back(Event::default().data(payload.to_string()));
Some((Ok(call.pending.pop_front().expect("queued frame")), call))
}
Step::Cancelled => {
call.finished = true;
call.pending.clear();
None
}
}
}
fn jsonrpc_internal_error(message: &Value) -> Value {
crate::jsonrpc::jsonrpc_error(request_id(message), -32603, "internal server error")
}
#[cfg(test)]
#[path = "lifecycle_tests.rs"]
mod tests;
@@ -0,0 +1,208 @@
//! Story 3.4 bounds that do not need the HTTP surface: token validation, the monotonic bounded
//! progress queue, the pinned frame shape and the ownership of the in-flight cancellation table.
use super::*;
fn token(value: Value) -> Option<ProgressToken> {
progress_token(&json!({ "_meta": { "progressToken": value } }))
}
#[test]
fn rmcp_progress_cancel_frame_preserves_serialized_numbers_and_omissions() {
for wire_token in [
"\"token\"",
"-7",
"1.5",
"1.0",
"-0.0",
"18446744073709551615",
] {
let value: Value = serde_json::from_str(wire_token).unwrap();
for progress in [2, 9_007_199_254_740_993, u64::MAX] {
let frame = progress_frame(
&token(value.clone()).unwrap(),
&ProgressEvent {
progress,
message: "execution_dispatched",
},
);
assert_eq!(frame["params"]["progressToken"].to_string(), wire_token);
assert_eq!(
frame["params"]["progress"].to_string(),
progress.to_string()
);
assert_eq!(
frame["params"],
json!({
"progressToken": value, "progress": progress, "message": "execution_dispatched"
})
);
assert!(frame["params"].get("total").is_none());
assert!(frame["params"].get("_meta").is_none());
assert_eq!(frame["jsonrpc"], "2.0");
assert_eq!(frame["method"], "notifications/progress");
}
}
}
#[test]
fn a_pinned_progress_token_is_a_bounded_string_or_a_number() {
assert_eq!(
token(json!("tok-1")),
Some(ProgressToken::Text("tok-1".to_owned()))
);
assert!(matches!(token(json!(7)), Some(ProgressToken::Number(_))));
assert!(matches!(token(json!(1.5)), Some(ProgressToken::Number(_))));
// Malformed and out-of-bound tokens disable progress instead of failing the call.
assert_eq!(token(json!("")), None);
assert_eq!(token(json!("x".repeat(MAX_PROGRESS_TOKEN_LEN + 1))), None);
assert_eq!(token(json!("line\nbreak")), None);
assert_eq!(token(json!(null)), None);
assert_eq!(token(json!(true)), None);
assert_eq!(token(json!({ "nested": 1 })), None);
assert_eq!(token(json!([1, 2])), None);
// A token at the bound is still accepted.
assert!(token(json!("x".repeat(MAX_PROGRESS_TOKEN_LEN))).is_some());
// Absent `_meta`, absent token and a non-object `_meta` are all "no progress".
assert_eq!(progress_token(&json!({})), None);
assert_eq!(progress_token(&json!({ "_meta": { "other": 1 } })), None);
assert_eq!(progress_token(&json!({ "_meta": "not-an-object" })), None);
}
#[tokio::test]
async fn real_steps_produce_strictly_increasing_bounded_progress_events() {
let (lifecycle, events) = ToolCallLifecycle::new(token(json!("tok-monotonic")));
let mut events = events.expect("a validated token opens the progress queue");
for phase in [Phase::Resolved, Phase::Authorized, Phase::Dispatching] {
lifecycle.advance(phase);
}
drop(lifecycle);
let mut observed = Vec::new();
while let Some(event) = events.recv().await {
observed.push((event.progress, event.message));
}
assert_eq!(
observed,
vec![
(1, "tool_resolved"),
(2, "authorization_resolved"),
(3, "execution_dispatched"),
]
);
assert!(
observed.windows(2).all(|pair| pair[0].0 < pair[1].0),
"progress must increase monotonically, got {observed:?}"
);
}
#[tokio::test]
async fn a_full_progress_queue_drops_events_instead_of_blocking_the_call() {
let (lifecycle, events) = ToolCallLifecycle::new(token(json!(9)));
let mut events = events.expect("a validated token opens the progress queue");
// Many more steps than the queue holds: the call must never wait for the reader.
for _ in 0..(PROGRESS_CAPACITY * 4) {
lifecycle.advance(Phase::Resolved);
}
let mut delivered = 0;
while events.try_recv().is_ok() {
delivered += 1;
}
assert_eq!(delivered, PROGRESS_CAPACITY);
}
#[tokio::test]
async fn without_a_token_no_progress_is_produced_at_all() {
let (lifecycle, events) = ToolCallLifecycle::new(None);
assert!(events.is_none());
lifecycle.advance(Phase::Resolved);
lifecycle.advance(Phase::Dispatching);
}
/// The pinned `notifications/progress` frame (schema `ProgressNotification`, both supported
/// revisions): `progressToken` echoed verbatim, `progress` increasing, `message` optional and
/// `total` absent because this server never invents one.
#[test]
fn the_progress_frame_matches_the_pinned_notification_shape() {
let event = ProgressEvent {
progress: 2,
message: Phase::Authorized.message(),
};
assert_eq!(
progress_frame(&ProgressToken::Text("tok-2".to_owned()), &event),
json!({
"jsonrpc": "2.0",
"method": "notifications/progress",
"params": {
"progressToken": "tok-2",
"progress": 2,
"message": "authorization_resolved"
}
})
);
assert_eq!(
progress_frame(&ProgressToken::Number(7.into()), &event)["params"]["progressToken"],
json!(7)
);
}
#[tokio::test]
async fn a_cancellation_only_reaches_the_call_of_the_same_session_and_credential() {
let registry = Arc::new(InFlightCalls::new(MAX_IN_FLIGHT_CALLS));
let scope = InFlightScope {
session_id: "sess-1".to_owned(),
owner: "pk_a".to_owned(),
};
let entry = registry
.register(&scope, &json!(42))
.expect("the table has room");
let mut signal = entry.cancel_signal();
// A foreign credential, a foreign session and a different request id cancel nothing.
for foreign in [
InFlightScope {
session_id: "sess-1".to_owned(),
owner: "pk_b".to_owned(),
},
InFlightScope {
session_id: "sess-2".to_owned(),
owner: "pk_a".to_owned(),
},
] {
assert!(!registry.cancel(&foreign, &json!(42)));
}
assert!(!registry.cancel(&scope, &json!(43)));
assert!(
tokio::time::timeout(std::time::Duration::from_millis(50), signal.cancelled())
.await
.is_err(),
"a foreign cancellation must not fire the signal"
);
assert!(registry.cancel(&scope, &json!(42)));
tokio::time::timeout(std::time::Duration::from_millis(50), signal.cancelled())
.await
.expect("the owner's cancellation fires the signal");
}
#[tokio::test]
async fn a_released_call_releases_its_slot_and_the_table_is_bounded() {
let registry = Arc::new(InFlightCalls::new(1));
let scope = InFlightScope {
session_id: "sess-1".to_owned(),
owner: "pk_a".to_owned(),
};
let entry = registry.register(&scope, &json!(1)).expect("first slot");
assert!(
registry.register(&scope, &json!(2)).is_none(),
"the table refuses a call beyond its bound instead of evicting the live one"
);
assert!(registry.cancel(&scope, &json!(1)));
drop(entry);
assert!(
!registry.cancel(&scope, &json!(1)),
"a terminal call is no longer cancellable"
);
assert!(registry.register(&scope, &json!(2)).is_some());
}
@@ -121,3 +121,53 @@ impl ActiveStreamGuard {
})
}
}
#[cfg(test)]
mod tests {
use crank_metrics::McpMethod;
use serde_json::json;
use super::normalized_mcp_method;
/// AT-3.18-02: the metric label of the logging request is a member of the closed label enum, so
/// neither the level nor any other client-supplied identifier can reach the label space. There is
/// no mapping from a request value to a label anywhere in this file.
#[test]
fn a_logging_request_can_only_produce_a_fixed_metric_label() {
for level in [
json!("debug"),
json!("verbose"),
json!("crank-canary-9f7620e5-aa41-4b0d"),
json!(7),
json!(null),
] {
let message = json!({
"jsonrpc": "2.0",
"id": 1,
"method": "logging/setLevel",
"params": {"level": level}
});
assert_eq!(
normalized_mcp_method(&message),
McpMethod::Unsupported,
"the method label is a fixed enum member, never the request value"
);
}
assert_eq!(McpMethod::Unsupported.as_str(), "unsupported");
// Every label value of the enum is a pinned literal, so the label domain cannot be widened by
// a request at all.
assert!(
McpMethod::VALUES.iter().all(|value| value
.chars()
.all(|character| character.is_ascii_lowercase() || character == '_')),
"every method label is a fixed lowercase literal: {:?}",
McpMethod::VALUES
);
// A malformed envelope has its own fixed label as well.
assert_eq!(
normalized_mcp_method(&json!({"level": "debug"})),
McpMethod::Invalid
);
}
}
@@ -1,19 +1,16 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Serialize, Deserialize)]
pub(super) struct InitializeParams {
#[serde(rename = "protocolVersion")]
pub(super) protocol_version: String,
#[serde(default)]
pub(super) capabilities: Value,
}
#[derive(Debug, Serialize, Deserialize)]
pub(super) struct ToolCallParams {
pub(super) name: String,
#[serde(default, deserialize_with = "deserialize_arguments_object")]
pub(super) arguments: Option<Value>,
/// Story 3.4: the pinned `RequestMetaObject` of this request. Only the validated
/// `progressToken` of it is read, and only to decide whether the client asked for out-of-band
/// progress; a malformed or foreign `_meta` never fails the call.
#[serde(default, rename = "_meta")]
pub(super) meta: Option<Value>,
}
/// The pinned `CallToolRequest.params.arguments` is an **optional object**. Omitting it stays valid
+511 -116
View File
@@ -1,9 +1,9 @@
use axum::{http::StatusCode, response::Response};
use rmcp::model::{CacheScope, CallToolResult, ContentBlock, ListToolsResult, ResultType, Tool};
use serde_json::{Value, json};
use crate::{
jsonrpc::{jsonrpc_result, request_id},
protocol::project_modern_result,
tool_error::{ToolErrorContract, tool_error_text, tool_error_value},
transport::{ResponseMode, transport_response},
};
@@ -23,127 +23,304 @@ pub(crate) fn take_confirmation_token(arguments: &mut Value) -> Option<String> {
.filter(|value| !value.trim().is_empty())
}
pub(crate) fn success_tool_response(
/// The era-neutral success payload of one `tools/call` (Story 3.4: the streamed response needs the
/// payload itself, because the terminal response is the last event of the request's stream).
pub(super) fn success_tool_payload(
message: &Value,
response_mode: ResponseMode,
protocol_version: &str,
output: Value,
) -> Response {
transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
project_modern_result(
protocol_version,
json!({
"content": [
{
"type": "text",
"text": serde_json::to_string_pretty(&output).unwrap_or_else(|_| "{}".to_owned())
}
],
"structuredContent": output,
"isError": false
}),
),
),
response_mode,
None,
Some(protocol_version),
)
) -> Value {
call_result_payload(message, protocol_version, sdk_success_tool_result(output))
}
#[cfg(test)]
pub(crate) fn tool_error_response(
message: &Value,
response_mode: ResponseMode,
protocol_version: &str,
error: ToolErrorContract,
) -> Response {
let error_message = tool_error_text(&error);
let error_value = tool_error_value(&error);
transport_response(
StatusCode::OK,
jsonrpc_result(
request_id(message),
project_modern_result(
protocol_version,
json!({
"content": [
{
"type": "text",
"text": error_message
}
],
"structuredContent": {
"error": error_value
},
"isError": true
}),
),
),
tool_error_payload(message, protocol_version, error),
response_mode,
None,
Some(protocol_version),
)
}
/// The era-neutral `tools/list` result shape built from the shared command outcome. Object keys end
/// up byte-lexicographically ordered, exactly as before the extraction.
pub(super) fn tools_list_result(tools: Vec<Value>, next_cursor: Option<String>) -> Value {
let mut result = json!({ "tools": tools });
if let Some(next_cursor) = next_cursor {
result["nextCursor"] = Value::String(next_cursor);
}
pub(super) fn tool_error_payload(
message: &Value,
protocol_version: &str,
error: ToolErrorContract,
) -> Value {
call_result_payload(message, protocol_version, sdk_error_tool_result(error))
}
pub(super) fn sdk_success_tool_result(output: Value) -> CallToolResult {
let text = serde_json::to_string_pretty(&output).expect("JSON value serializes");
let mut result = CallToolResult::success(vec![ContentBlock::text(text)]);
result.structured_content = Some(output);
result
}
/// The era-neutral `resources/list` result shape (Story 3.8). Same pagination wire keys as
/// `tools/list`; `nextCursor` is absent on the last page.
pub(super) fn resources_list_result(resources: Vec<Value>, next_cursor: Option<String>) -> Value {
let mut result = json!({ "resources": resources });
if let Some(next_cursor) = next_cursor {
result["nextCursor"] = Value::String(next_cursor);
}
pub(super) fn sdk_error_tool_result(error: ToolErrorContract) -> CallToolResult {
let mut result = CallToolResult::error(vec![ContentBlock::text(tool_error_text(&error))]);
result.structured_content = Some(json!({ "error": tool_error_value(&error) }));
result
}
/// The era-neutral `resources/read` result shape: exactly the typed `contents[]` the revision
/// converter produced.
pub(super) fn resource_read_result(contents: Value) -> Value {
json!({ "contents": contents })
fn call_result_payload(message: &Value, version: &str, mut result: CallToolResult) -> Value {
result.result_type =
(version == crate::jsonrpc::MODERN_PROTOCOL_VERSION).then_some(ResultType::COMPLETE);
let result = serde_json::to_value(result).expect("SDK call result contains JSON values");
#[cfg(test)]
super::dispatch_observer::sdk_tools_call_result_serialized();
jsonrpc_result(request_id(message), result)
}
pub(super) fn sdk_tools_list_result(
tools: &[Value],
next_cursor: Option<String>,
) -> Result<ListToolsResult, serde_json::Error> {
Ok(ListToolsResult {
tools: tools
.iter()
.cloned()
.map(serde_json::from_value::<Tool>)
.collect::<Result<Vec<_>, _>>()?,
next_cursor,
..Default::default()
})
}
/// Serialize the SDK model, preserving catalog extensions that pinned Tool serde cannot represent.
pub(super) fn tools_list_result(
tools: Vec<Value>,
next_cursor: Option<String>,
version: &str,
) -> Result<Value, serde_json::Error> {
let mut result = sdk_tools_list_result(&tools, next_cursor)?;
let modern = version == crate::jsonrpc::MODERN_PROTOCOL_VERSION;
result.result_type = modern.then_some(ResultType::COMPLETE);
result.ttl_ms = modern.then_some(0);
result.cache_scope = modern.then_some(CacheScope::Private);
let mut result = serde_json::to_value(result)?;
#[cfg(test)]
super::dispatch_observer::sdk_tools_list_result_serialized();
for (raw, serialized) in tools
.iter()
.zip(result["tools"].as_array_mut().expect("SDK tools array"))
{
let serialized = serialized.as_object_mut().expect("SDK Tool object");
for (key, value) in raw.as_object().expect("validated Tool object") {
if matches!(key.as_str(), "annotations" | "icons")
|| !matches!(
key.as_str(),
"name" | "title" | "description" | "inputSchema" | "outputSchema" | "_meta"
)
|| value.is_null()
{
serialized.insert(key.clone(), value.clone());
}
}
}
Ok(result)
}
/// SDK results are serialized before the minimal lossless projection of descriptor extensions.
pub(super) fn resources_list_result(
resources: Vec<Value>,
next_cursor: Option<String>,
version: &str,
) -> Result<Value, serde_json::Error> {
let modern = version == crate::jsonrpc::MODERN_PROTOCOL_VERSION;
let result = rmcp::model::ListResourcesResult {
resources: resources
.iter()
.cloned()
.map(serde_json::from_value)
.collect::<Result<_, _>>()?,
next_cursor,
result_type: modern.then_some(ResultType::COMPLETE),
ttl_ms: modern.then_some(0),
cache_scope: modern.then_some(CacheScope::Private),
meta: None,
};
let mut result = serde_json::to_value(result)?;
#[cfg(test)]
super::dispatch_observer::sdk_resources_list_result_serialized();
preserve_resource_fields(
&resources,
&mut result["resources"],
&[
"uri",
"name",
"title",
"description",
"mimeType",
"size",
"_meta",
],
);
Ok(result)
}
pub(super) fn resource_read_result(
contents: Value,
version: &str,
) -> Result<Value, serde_json::Error> {
let mut result =
rmcp::model::ReadResourceResult::new(serde_json::from_value(contents.clone())?);
let modern = version == crate::jsonrpc::MODERN_PROTOCOL_VERSION;
result.result_type = modern.then_some(ResultType::COMPLETE);
result.ttl_ms = modern.then_some(0);
result.cache_scope = modern.then_some(CacheScope::Private);
let mut result = serde_json::to_value(result)?;
#[cfg(test)]
super::dispatch_observer::sdk_resources_read_result_serialized();
preserve_resource_fields(
contents.as_array().expect("validated contents array"),
&mut result["contents"],
&["uri", "mimeType", "text", "blob", "_meta"],
);
Ok(result)
}
/// The era-neutral `resources/templates/list` result shape.
pub(super) fn resource_templates_list_result(
templates: Vec<Value>,
next_cursor: Option<String>,
) -> Value {
let mut result = json!({ "resourceTemplates": templates });
if let Some(cursor) = next_cursor {
result["nextCursor"] = Value::String(cursor);
}
result
version: &str,
) -> Result<Value, serde_json::Error> {
let modern = version == crate::jsonrpc::MODERN_PROTOCOL_VERSION;
let result = rmcp::model::ListResourceTemplatesResult {
resource_templates: templates
.iter()
.cloned()
.map(serde_json::from_value)
.collect::<Result<_, _>>()?,
next_cursor,
result_type: modern.then_some(ResultType::COMPLETE),
ttl_ms: modern.then_some(0),
cache_scope: modern.then_some(CacheScope::Private),
meta: None,
};
let mut result = serde_json::to_value(result)?;
#[cfg(test)]
super::dispatch_observer::sdk_resource_templates_list_result_serialized();
preserve_resource_fields(
&templates,
&mut result["resourceTemplates"],
&[
"uriTemplate",
"name",
"title",
"description",
"mimeType",
"_meta",
],
);
Ok(result)
}
/// The era-neutral `prompts/list` result shape (Story 3.14). Same pagination wire keys as
/// `resources/list`; `nextCursor` is absent on the last page.
pub(super) fn prompts_list_result(prompts: Vec<Value>, next_cursor: Option<String>) -> Value {
let mut result = json!({ "prompts": prompts });
if let Some(cursor) = next_cursor {
result["nextCursor"] = Value::String(cursor);
// Pinned SDK serde drops unknown fields and optional nulls, including Crank template parameters.
// Treat annotations/icons as opaque too: their nested extensions are not represented by the SDK.
fn preserve_resource_fields(raw: &[Value], serialized: &mut Value, sdk_fields: &[&str]) {
for (raw, serialized) in raw
.iter()
.zip(serialized.as_array_mut().expect("SDK array"))
{
let serialized = serialized.as_object_mut().expect("SDK object");
for (key, value) in raw.as_object().expect("validated resource object") {
if !sdk_fields.contains(&key.as_str()) || value.is_null() {
serialized.insert(key.clone(), value.clone());
}
}
}
result
}
/// The era-neutral `prompts/get` result shape: the rendered `messages[]` and the description of the
/// exact bound Published Prompt Version it came from.
pub(super) fn prompt_get_result(description: Option<String>, messages: Vec<Value>) -> Value {
let mut result = json!({ "messages": messages });
if let Some(description) = description {
result["description"] = Value::String(description);
/// SDK pagination with lossless Crank argument schemas/defaults and optional nulls.
pub(super) fn prompts_list_result(
prompts: Vec<Value>,
next_cursor: Option<String>,
) -> Result<Value, serde_json::Error> {
let result = rmcp::model::ListPromptsResult {
prompts: prompts
.iter()
.cloned()
.map(serde_json::from_value)
.collect::<Result<Vec<rmcp::model::Prompt>, _>>()?,
next_cursor,
result_type: None,
ttl_ms: None,
cache_scope: None,
meta: None,
};
let mut result = serde_json::to_value(result)?;
preserve_resource_fields(
&prompts,
&mut result["prompts"],
&["name", "title", "description", "arguments", "_meta"],
);
for (raw, serialized) in prompts
.iter()
.zip(result["prompts"].as_array_mut().expect("SDK prompts"))
{
if let Some(arguments) = raw["arguments"].as_array() {
preserve_resource_fields(
arguments,
&mut serialized["arguments"],
&["name", "title", "description", "required"],
);
}
}
result
#[cfg(test)]
super::dispatch_observer::sdk_prompts_list_result_serialized();
Ok(result)
}
/// Preserve Crank's system role, which the pinned SDK's user/assistant Role cannot express.
pub(super) fn prompt_get_result(
description: Option<String>,
messages: Vec<Value>,
) -> Result<Value, serde_json::Error> {
let sdk_messages = messages
.iter()
.cloned()
.map(|mut message| {
if message["role"] == "system" {
message["role"] = json!("user");
}
serde_json::from_value::<rmcp::model::PromptMessage>(message)
})
.collect::<Result<Vec<_>, _>>()?;
let mut result = rmcp::model::GetPromptResult::new(sdk_messages);
result.description = description;
// Era-specific resultType/cache projection remains at the existing dispatch boundary.
result.result_type = None;
let mut result = serde_json::to_value(result)?;
preserve_resource_fields(&messages, &mut result["messages"], &["role", "content"]);
for (raw, serialized) in messages
.iter()
.zip(result["messages"].as_array_mut().expect("SDK messages"))
{
if raw["role"] == "system" {
serialized["role"] = raw["role"].clone();
}
// Content extensions/optional nulls are opaque to the pinned SDK.
let content = serialized["content"].as_object_mut().expect("SDK content");
for (key, value) in raw["content"].as_object().expect("validated content") {
if !content.contains_key(key)
|| value.is_null()
|| matches!(key.as_str(), "annotations" | "resource")
{
content.insert(key.clone(), value.clone());
}
}
}
#[cfg(test)]
super::dispatch_observer::sdk_prompts_get_result_serialized();
Ok(result)
}
/// The era-neutral `completion/complete` result shape (Story 3.17): the bounded `values[]`, the
@@ -154,14 +331,18 @@ pub(super) fn completion_result(
total: Option<usize>,
has_more: bool,
) -> Value {
let mut completion = json!({ "values": values });
if let Some(total) = total {
completion["total"] = json!(total);
}
if has_more {
completion["hasMore"] = json!(true);
}
json!({ "completion": completion })
let mut completion = rmcp::model::CompletionInfo::default();
completion.values = values;
// The domain scans at most 1024 candidates, so its exact count fits the SDK's u32.
completion.total = total.map(|total| u32::try_from(total).expect("bounded completion count"));
completion.has_more = has_more.then_some(true);
let mut result = rmcp::model::CompleteResult::new(completion);
// Keep the shared result era-neutral; the existing caller projects modern resultType.
result.result_type = None;
let result = serde_json::to_value(result).expect("SDK completion contains strings and counts");
#[cfg(test)]
super::dispatch_observer::sdk_completion_result_serialized();
result
}
/// The structured `prompts/get` argument rejection (AT-3.14-02): a `-32602` error whose `data`
@@ -193,28 +374,35 @@ pub(super) fn prompt_arguments_error_response(
)
}
/// Map one shared Tools-command outcome back to the era-specific wire response (spec section 6.2).
/// One `tools/call` wire frame, or the adapter's own internal-error detail.
///
/// This adapter function is the only place that turns a neutral outcome into a response: the shared
/// Story 3.4: the same projection serves the single response and the terminal event of a streamed
/// response, so both forms of one outcome can never drift apart.
pub(super) enum ToolCommandPayload {
Ok(Value),
Internal(&'static str),
}
/// Map one shared Tools-command outcome back to its era-specific wire payload (spec section 6.2).
///
/// This adapter function is the only place that turns a neutral outcome into a wire value: the shared
/// service never sees `ResponseMode`, the protocol revision, the request envelope or an
/// `axum::Response`. `ApprovalPending` is a success for the client (it still has to answer), while
/// an unavailable elicitation and a failing call keep their era-correct tool-error shape.
pub(super) fn tool_command_response(
pub(super) fn tool_command_payload(
message: &Value,
response_mode: ResponseMode,
protocol_version: &str,
correlation: &CorrelationContext,
outcome: ToolsOutcome,
) -> Response {
) -> ToolCommandPayload {
match outcome {
ToolsOutcome::Call(ToolCallOutcome::Completed(output))
| ToolsOutcome::Call(ToolCallOutcome::ApprovalPending(output)) => {
success_tool_response(message, response_mode, protocol_version, output)
ToolCommandPayload::Ok(success_tool_payload(message, protocol_version, output))
}
ToolsOutcome::Call(ToolCallOutcome::ApprovalUnavailable(control_error)) => {
tool_error_response(
ToolCommandPayload::Ok(tool_error_payload(
message,
response_mode,
protocol_version,
control_tool_error_contract(
control_error,
@@ -222,34 +410,49 @@ pub(super) fn tool_command_response(
correlation.trace_id().as_str(),
execution_locale(message),
),
)
))
}
ToolsOutcome::Call(ToolCallOutcome::InvalidParams(detail)) => {
ToolCommandPayload::Ok(jsonrpc_error(request_id(message), -32602, detail))
}
ToolsOutcome::Call(ToolCallOutcome::InvalidParams(detail)) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(protocol_version),
),
ToolsOutcome::Call(ToolCallOutcome::Error(contract)) => {
tool_error_response(message, response_mode, protocol_version, contract)
ToolCommandPayload::Ok(tool_error_payload(message, protocol_version, contract))
}
// The pending approval could not be persisted: the adapter answers its internal error shape,
// as the previous wire path did.
ToolsOutcome::Call(ToolCallOutcome::InternalFailure) => internal_jsonrpc_error(
message,
"the pending approval request could not be persisted",
),
ToolsOutcome::Call(ToolCallOutcome::InternalFailure) => {
ToolCommandPayload::Internal("the pending approval request could not be persisted")
}
// A call command never produces these variants; they are answered with the adapter's
// internal error instead of being silently ignored.
ToolsOutcome::List { .. }
| ToolsOutcome::InvalidParams(_)
| ToolsOutcome::CatalogUnavailable => {
internal_jsonrpc_error(message, "unexpected shared tools outcome for a tools/call")
ToolCommandPayload::Internal("unexpected shared tools outcome for a tools/call")
}
}
}
/// The single-response form of [`tool_command_payload`].
pub(super) fn tool_command_response(
message: &Value,
response_mode: ResponseMode,
protocol_version: &str,
correlation: &CorrelationContext,
outcome: ToolsOutcome,
) -> Response {
match tool_command_payload(message, protocol_version, correlation, outcome) {
ToolCommandPayload::Ok(payload) => transport_response(
StatusCode::OK,
payload,
response_mode,
None,
Some(protocol_version),
),
ToolCommandPayload::Internal(detail) => internal_jsonrpc_error(message, detail),
}
}
pub(super) fn resource_error_response(
message: &Value,
response_mode: ResponseMode,
@@ -284,3 +487,195 @@ pub(super) fn internal_jsonrpc_error(message: &Value, _error: impl std::fmt::Dis
None,
)
}
#[cfg(test)]
mod sdk_result_tests {
use super::*;
use crate::jsonrpc::{LEGACY_PROTOCOL_VERSION, MODERN_PROTOCOL_VERSION};
#[test]
fn prompts_sdk_projection_preserves_typed_extensions_and_system_content() {
let prompt = json!({"name":"p", "description":null, "icons":null,
"x-prompt":{}, "arguments":[{"name":"count", "description":null,
"required":false, "schema":{"type":"integer"}, "default":7}]});
for cursor in [None, Some(String::new()), Some("opaque".to_owned())] {
let result = prompts_list_result(vec![prompt.clone()], cursor.clone()).unwrap();
assert_eq!(result["prompts"], json!([prompt]));
assert_eq!(result.get("nextCursor"), cursor.map(Value::String).as_ref());
assert!(result.get("resultType").is_none());
}
let messages = vec![
json!({"role":"system", "x-message":null,
"content":{"type":"text", "text":"\0é\n", "annotations":null, "x-content":[]}}),
json!({"role":"assistant", "content":{"type":"text", "text":""}}),
];
assert_eq!(
prompt_get_result(None, messages.clone()).unwrap(),
json!({"messages":messages})
);
assert_eq!(
prompts_list_result(vec![], None).unwrap(),
json!({"prompts":[]})
);
assert_eq!(
prompt_get_result(Some(String::new()), vec![]).unwrap(),
json!({"description":"", "messages":[]})
);
assert!(prompts_list_result(vec![json!({"name":42})], None).is_err());
assert!(
prompt_get_result(
None,
vec![json!({"role":"invalid", "content":{"type":"text", "text":"secret"}})]
)
.is_err()
);
}
#[test]
fn resources_sdk_projection_is_lossless_for_templates_contents_and_catalog() {
for version in [LEGACY_PROTOCOL_VERSION, MODERN_PROTOCOL_VERSION] {
for optional in [Value::Null, json!({}), json!({"x-extension":[null,{}]})] {
let raw = json!({"uri":"crank://r","name":"r","description":null,
"mimeType":"text/plain","_meta":optional,"x-resource":[],
"annotations":{"x-nested":null},"icons":[{"src":"icon","x-icon":{}}]});
for cursor in [None, Some(String::new()), Some("opaque".to_owned())] {
let list =
resources_list_result(vec![raw.clone()], cursor.clone(), version).unwrap();
assert_eq!(list["resources"], json!([raw]));
assert_eq!(
list.get("nextCursor"),
cursor.as_ref().map(|s| json!(s)).as_ref()
);
assert_eq!(
list.get("resultType").is_some(),
version == MODERN_PROTOCOL_VERSION
);
assert_eq!(
list.get("ttlMs").is_some(),
version == MODERN_PROTOCOL_VERSION
);
assert_eq!(
list.get("cacheScope").is_some(),
version == MODERN_PROTOCOL_VERSION
);
assert!(list.get("_meta").is_none());
let template = json!({"uriTemplate":"crank://r/{id}","name":"r",
"description":null,"mimeType":null,"_meta":optional,
"parameters":[{"name":"id","required":true,"opaque":null}],
"annotations":{"x-nested":[]},"icons":null});
let list = resource_templates_list_result(
vec![template.clone()],
cursor.clone(),
version,
)
.unwrap();
assert_eq!(list["resourceTemplates"], json!([template]));
assert_eq!(
list.get("nextCursor"),
cursor.as_ref().map(|s| json!(s)).as_ref()
);
}
let contents = json!([
{"uri":"crank://r","mimeType":null,"text":"\0é\n","_meta":optional,"x-content":[]},
{"uri":"crank://b","mimeType":"application/octet-stream","blob":"AP8=","_meta":null}
]);
assert_eq!(
resource_read_result(contents.clone(), version).unwrap()["contents"],
contents
);
}
assert_eq!(
resources_list_result(vec![], None, version).unwrap()["resources"],
json!([])
);
assert_eq!(
resource_templates_list_result(vec![], None, version).unwrap()["resourceTemplates"],
json!([])
);
assert_eq!(
resource_read_result(json!([]), version).unwrap()["contents"],
json!([])
);
assert!(resources_list_result(vec![json!({"uri":42})], None, version).is_err());
assert!(
resource_templates_list_result(vec![json!({"name":"r"})], None, version).is_err()
);
assert!(resource_read_result(json!([{"uri":"r"}]), version).is_err());
}
}
#[test]
fn catalog_projection_preserves_extensions_null_and_empty() {
let descriptor = json!({
"name":"tool", "title":"", "description":null,
"inputSchema":{"type":"object", "x-schema":{"nested":null}},
"outputSchema":{"type":"object", "x-output":[]},
"_meta":{"custom":{"empty":{}}}, "x-tool":{"nested":[]},
"annotations":{"readOnlyHint":true,"x-annotation":{"nested":null}},
"icons":[{"src":"https://example.test/icon", "x-icon":{"nested":[]}}]
});
for version in [LEGACY_PROTOCOL_VERSION, MODERN_PROTOCOL_VERSION] {
for raw in [
descriptor.clone(),
json!({"name":"tool","inputSchema":{},
"title":null,"description":"","outputSchema":null,"_meta":null,
"annotations":null,"icons":null}),
json!({"name":"tool","inputSchema":{},
"annotations":{},"icons":[]}),
] {
for cursor in [None, Some(String::new()), Some("next".to_owned())] {
let value =
tools_list_result(vec![raw.clone()], cursor.clone(), version).unwrap();
assert_eq!(value["tools"], json!([raw]));
assert_eq!(
value.get("nextCursor"),
cursor.as_ref().map(|s| Value::String(s.clone())).as_ref()
);
assert_eq!(
value.get("resultType").is_some(),
version == MODERN_PROTOCOL_VERSION
);
assert_eq!(
value.get("ttlMs").is_some(),
version == MODERN_PROTOCOL_VERSION
);
assert_eq!(
value.get("cacheScope").is_some(),
version == MODERN_PROTOCOL_VERSION
);
}
}
assert_eq!(
tools_list_result(vec![], None, version).unwrap()["tools"],
json!([])
);
assert!(tools_list_result(vec![json!({"name":42})], None, version).is_err());
}
}
#[test]
fn null_success_and_rpc_failures_keep_their_distinct_shapes() {
for version in [LEGACY_PROTOCOL_VERSION, MODERN_PROTOCOL_VERSION] {
let message = json!({"id":9007199254740993_u64});
let payload = success_tool_payload(&message, version, Value::Null);
assert_eq!(payload["id"], message["id"]);
assert_eq!(
payload["result"]["content"],
json!([{"type":"text","text":"null"}])
);
assert_eq!(
payload["result"].get("structuredContent"),
Some(&Value::Null)
);
assert_eq!(payload["result"]["isError"], false);
let correlation = CorrelationContext::generate();
let internal = tool_command_payload(
&message,
version,
&correlation,
ToolsOutcome::Call(ToolCallOutcome::InternalFailure),
);
assert!(matches!(internal, ToolCommandPayload::Internal(_)));
}
}
}
@@ -0,0 +1,181 @@
//! Runtime SDK dispatch; admission, lifecycle and response projection stay with the caller.
use std::sync::Arc;
use rmcp::model::{CallToolRequestParams, PaginatedRequestParams, ReadResourceRequestParams};
use serde_json::Value;
use super::{AppState, lifecycle::ToolCallLifecycle};
use crate::protocol::{
ServiceContext, ToolCallCommand, ToolCallOutcome, ToolsCommand, ToolsListCommand, ToolsOutcome,
};
// Crank extensions are not SDK fields and must survive the typed request boundary unchanged.
enum SdkToolsCommand {
List {
params: PaginatedRequestParams,
limit: Option<usize>,
},
Call {
params: CallToolRequestParams,
confirmation_token: Option<String>,
},
}
pub(super) async fn handle_tools_command(
state: &Arc<AppState>,
context: ServiceContext<'_>,
command: ToolsCommand,
lifecycle: Option<ToolCallLifecycle>,
) -> ToolsOutcome {
let request = match command {
ToolsCommand::List(list) => SdkToolsCommand::List {
params: PaginatedRequestParams::default().with_cursor(list.cursor),
limit: list.limit,
},
ToolsCommand::Call(call) => {
let Value::Object(arguments) = call.arguments else {
return ToolsOutcome::Call(ToolCallOutcome::InvalidParams(
"tool arguments must be an object".to_owned(),
));
};
SdkToolsCommand::Call {
params: CallToolRequestParams::new(call.tool_name).with_arguments(arguments),
confirmation_token: call.confirmation_token,
}
}
};
#[cfg(test)]
super::dispatch_observer::sdk_tools_dispatch_enter();
let command = match request {
SdkToolsCommand::List { params, limit } => ToolsCommand::List(ToolsListCommand {
cursor: params.cursor,
limit,
}),
SdkToolsCommand::Call {
params,
confirmation_token,
} => ToolsCommand::Call(ToolCallCommand {
tool_name: params.name.into_owned(),
arguments: Value::Object(params.arguments.unwrap_or_default()),
confirmation_token,
}),
};
crate::protocol::handle_tools_command(state, context, command, lifecycle).await
}
pub(super) async fn handle_prompts_command(
state: &Arc<AppState>,
context: ServiceContext<'_>,
revision: &str,
command: crate::protocol::PromptsCommand,
) -> crate::protocol::PromptsOutcome {
use crate::protocol::{PromptGetCommand, PromptsCommand, PromptsListCommand};
let command = match command {
PromptsCommand::List(list) => {
let params = PaginatedRequestParams::default().with_cursor(list.cursor);
PromptsCommand::List(PromptsListCommand {
cursor: params.cursor,
limit: list.limit,
})
}
PromptsCommand::Get(get) => {
let mut params = rmcp::model::GetPromptRequestParams::new(get.name);
params.arguments = get.arguments;
PromptsCommand::Get(PromptGetCommand {
name: params.name,
arguments: params.arguments,
})
}
};
#[cfg(test)]
super::dispatch_observer::sdk_prompts_dispatch_enter();
crate::protocol::handle_prompts_command(state, context, revision, command).await
}
pub(super) async fn handle_resources_command(
state: &Arc<AppState>,
context: ServiceContext<'_>,
revision: &str,
command: crate::protocol::ResourcesCommand,
) -> crate::protocol::ResourcesOutcome {
use crate::protocol::{ResourceReadCommand, ResourcesCommand};
let command = match command {
ResourcesCommand::List(list) => ResourcesCommand::List(sdk_resource_page(list)),
ResourcesCommand::Read(read) => {
let params = ReadResourceRequestParams::new(read.uri);
ResourcesCommand::Read(ResourceReadCommand { uri: params.uri })
}
};
#[cfg(test)]
super::dispatch_observer::sdk_resources_dispatch_enter();
crate::protocol::handle_resources_command(state, context, revision, command).await
}
pub(super) async fn list_resource_templates(
state: &Arc<AppState>,
workspace_slug: &str,
agent_slug: &str,
revision: &str,
command: crate::protocol::ResourcesListCommand,
) -> Result<(Vec<Value>, Option<String>), crate::protocol::ResourceTemplatesListError> {
let command = sdk_resource_page(command);
#[cfg(test)]
super::dispatch_observer::sdk_resources_dispatch_enter();
crate::protocol::list_resource_templates(state, workspace_slug, agent_slug, revision, command)
.await
}
pub(super) async fn handle_completion_command(
state: &Arc<AppState>,
context: ServiceContext<'_>,
revision: &str,
command: crate::protocol::CompletionCommand,
) -> crate::protocol::CompletionOutcome {
use crate::protocol::{
CompletionArgument, CompletionCommand, CompletionOutcome, CompletionReference,
};
use rmcp::model::{ArgumentInfo, CompleteRequestParams, Reference};
let reference = match command.reference {
CompletionReference::Prompt { name } => Reference::for_prompt(name),
CompletionReference::Resource { uri } => Reference::for_resource(uri),
};
let params = CompleteRequestParams::new(
reference,
ArgumentInfo::new(command.argument.name, command.argument.value),
);
// SDK context only supports strings; retain arbitrary JSON unchanged for domain validation.
let context_arguments = command.context;
#[cfg(test)]
super::dispatch_observer::sdk_completion_dispatch_enter();
let reference = match params.r#ref {
Reference::Prompt(reference) => CompletionReference::Prompt {
name: reference.name,
},
Reference::Resource(reference) => CompletionReference::Resource { uri: reference.uri },
_ => return CompletionOutcome::InvalidParams("invalid completion/complete parameters"),
};
let command = CompletionCommand {
reference,
argument: CompletionArgument {
name: params.argument.name,
value: params.argument.value,
},
context: context_arguments,
};
crate::protocol::handle_completion_command(state, context, revision, command).await
}
fn sdk_resource_page(
list: crate::protocol::ResourcesListCommand,
) -> crate::protocol::ResourcesListCommand {
let params = PaginatedRequestParams::default().with_cursor(list.cursor);
// Crank's page limit is not an SDK field; retain its existing validation and clamping.
crate::protocol::ResourcesListCommand {
cursor: params.cursor,
limit: list.limit,
}
}
@@ -0,0 +1,197 @@
//! Isolated single-Agent loopback experiment. Not installed in the production router.
//! Each instance owns one route and one SDK manager; no durable store is replaced.
use super::{
AgentRoutePath, AppState,
lifecycle::InFlightScope,
metrics::ActiveStreamGuard,
rmcp_tools::{RmcpRequestIdentity, RmcpTools},
stages::{enforce_traced_rate_limit, require_traced_machine_access},
};
use crate::{
protocol::{self, ContinuationSupport, RequestEra},
transport,
};
use axum::{
Router,
body::{Body, to_bytes},
extract::Request,
http::{Method, StatusCode},
response::{IntoResponse, Response},
};
use crank_core::PlatformApiKeyScope;
use futures_util::StreamExt;
use rmcp::transport::streamable_http_server::{
StreamableHttpServerConfig, StreamableHttpService, session::local::LocalSessionManager,
};
use serde_json::Value;
use std::sync::Arc;
const BODY_LIMIT: usize = 2 * 1024 * 1024;
pub(super) fn router(
state: Arc<AppState>,
path: AgentRoutePath,
active: Arc<std::sync::atomic::AtomicUsize>,
) -> Router {
let handler_state = state.clone();
let mut config = StreamableHttpServerConfig::default().with_json_response(true);
config.max_request_body_bytes = BODY_LIMIT;
let shutdown = Arc::new(config.cancellation_token.clone().drop_guard());
let mut manager = LocalSessionManager::default();
manager.session_config.channel_capacity = 16;
let service = StreamableHttpService::new(
move || {
Ok(RmcpTools {
state: handler_state.clone(),
active: active.clone(),
})
},
Arc::new(manager),
config,
);
let route = format!("/v1/{}/{}", path.workspace_slug, path.agent_slug);
Router::new()
.route(
&route,
axum::routing::any(move |request: Request| {
let state = state.clone();
let path = path.clone();
let service = service.clone();
let shutdown = shutdown.clone();
async move {
let _shutdown = shutdown;
let (mut parts, body) = request.into_parts();
if let Err(status) =
transport::validate_origin(&state.allowed_origins, &parts.headers)
{
return status.into_response();
}
let bytes = match to_bytes(body, BODY_LIMIT).await {
Ok(bytes) => bytes,
Err(_) => return StatusCode::PAYLOAD_TOO_LARGE.into_response(),
};
let message: Value = if parts.method == Method::POST {
match serde_json::from_slice(&bytes) {
Ok(message) => message,
Err(_) => return StatusCode::BAD_REQUEST.into_response(),
}
} else {
Value::Null
};
let method = message["method"].as_str().unwrap_or("");
let required = if method == "tools/call" {
PlatformApiKeyScope::Write
} else {
PlatformApiKeyScope::Read
};
let credential = match require_traced_machine_access(
&state,
&path,
&parts.headers,
required,
)
.await
{
Ok(credential) => credential,
Err(error) => return error.into_response(),
};
let session = match transport::session_id_from_headers(&parts.headers) {
Ok(session) => session,
Err(_) => {
return protocol::ambiguous_session_carrier_response(
&parts.headers,
&message,
);
}
};
let era = if parts.method == Method::POST {
if let Err(response) = protocol::validate_jsonrpc_envelope(&message) {
return *response;
}
if let Err(status) = transport::negotiate_post_response_mode(&parts.headers)
{
return status.into_response();
}
match protocol::resolve_request_identity(&parts.headers, &message) {
Ok(identity) => identity.era,
Err(response) => return *response,
}
} else {
if let Err(response) = protocol::session_lifecycle_admission(&parts.headers)
{
return *response;
}
RequestEra::Legacy
};
if matches!(era, RequestEra::Modern) && session.is_some() {
return protocol::modern_carrier_conflict_response(&message);
}
if let Err(error) =
enforce_traced_rate_limit(&state, &path, &parts.headers).await
{
return crate::rate_limit::rate_limited_status_response(error);
}
// SDK cancellation is keyed only by request id. Never forward an unchecked cancel.
if method == "notifications/cancelled" {
if let Some(session_id) = session {
state.in_flight.cancel(
&InFlightScope {
session_id,
owner: credential
.platform_api_key_id
.as_ref()
.map(|id| id.as_str())
.unwrap_or("")
.to_owned(),
},
&message["params"]["requestId"],
);
}
return StatusCode::ACCEPTED.into_response();
}
let Some(context) = parts
.extensions
.get::<crate::request_context::RequestContext>()
else {
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
};
let correlation = context.correlation.clone();
parts.extensions.insert(RmcpRequestIdentity {
workspace_slug: path.workspace_slug,
agent_slug: path.agent_slug,
credential,
correlation,
continuation: ContinuationSupport::from_supported(false),
locale: crate::tool_error::execution_locale(&message),
session_id: session,
progress_meta: message.pointer("/params/_meta").cloned(),
sse_acceptable: transport::accepts_event_stream(&parts.headers),
});
// Legacy SDK calls outlive disconnected bodies: both lifetimes share one slot.
let permit = match ActiveStreamGuard::try_acquire(&state.session_slots) {
Ok(permit) => Arc::new(permit),
Err(_) => return StatusCode::TOO_MANY_REQUESTS.into_response(),
};
if method == "tools/call" {
parts.extensions.insert(permit.clone());
}
let response = service
.handle(Request::from_parts(parts, Body::from(bytes)))
.await;
let (parts, body) = response.into_parts();
let stream = futures_util::stream::unfold(
(Box::pin(Body::new(body).into_data_stream()), permit),
|(mut stream, permit)| async move {
stream.next().await.map(|chunk| (chunk, (stream, permit)))
},
);
Response::from_parts(parts, Body::from_stream(stream))
}
}),
)
.layer(axum::middleware::from_fn(
crate::request_context::apply_request_context,
))
}
@@ -0,0 +1,31 @@
//! Lossless projection of legacy cancellation targets through the pinned SDK.
use rmcp::model::CancelledNotificationParam;
use serde_json::{Map, Value};
pub(super) fn decode_cancelled_request_id(params: &Value) -> Option<Value> {
let mut projection = Map::new();
let original_id = params.get("requestId");
if let Some(id) = original_id {
// SDK IDs use i64; the temporary string never reaches the in-flight lookup.
let projected = match id {
Value::Number(number) if number.as_i64().is_none() => Value::String(number.to_string()),
_ => id.clone(),
};
projection.insert("requestId".to_owned(), projected);
}
// Only requestId names a target; reason, _meta and extensions retain their ignore semantics.
let decoded =
serde_json::from_value::<CancelledNotificationParam>(Value::Object(projection)).ok()?;
#[cfg(test)]
super::dispatch_observer::sdk_cancelled_params_decoded();
let id = decoded.request_id?;
Some(match original_id {
Some(Value::Number(number)) => Value::Number(number.clone()),
_ => id.into_json_value(),
})
}
#[cfg(test)]
#[path = "rmcp_progress_cancel_tests.rs"]
mod tests;
@@ -0,0 +1,52 @@
use super::decode_cancelled_request_id;
use serde_json::{Value, json};
#[test]
fn rmcp_progress_cancel_decoder_preserves_exact_ids_and_ignores_other_fields() {
for wire_id in [
"\"7\"",
"7",
"-7",
"1.5",
"1.0",
"-0.0",
"18446744073709551615",
"\"\"",
"\"line\\nbreak\"",
] {
let id: Value = serde_json::from_str(wire_id).unwrap();
let params = json!({
"requestId": id, "reason": 42, "_meta": [], "unknown": {"nested": [true]}
});
let decoded = decode_cancelled_request_id(&params).unwrap();
assert_eq!(decoded.to_string(), wire_id);
}
let long_id = Value::String("x".repeat(1024));
assert_eq!(
decode_cancelled_request_id(&json!({"requestId": long_id})),
Some(long_id)
);
assert_ne!(
decode_cancelled_request_id(&json!({"requestId": "7"})),
decode_cancelled_request_id(&json!({"requestId": 7}))
);
}
#[test]
fn rmcp_progress_cancel_decoder_has_no_target_for_invalid_or_missing_ids() {
for params in [
json!(null),
json!(true),
json!(7),
json!("requestId"),
json!([]),
json!({}),
json!({"requestId": null}),
json!({"requestId": true}),
json!({"requestId": []}),
json!({"requestId": {}}),
json!({"reason": "ignored", "_meta": []}),
] {
assert_eq!(decode_cancelled_request_id(&params), None, "{params}");
}
}
@@ -0,0 +1,43 @@
//! Behavioural ATDD for rmcp 3.7.0 LocalSessionManager reconnect/restart semantics.
use rmcp::transport::streamable_http_server::session::{
SessionManager, local::LocalSessionManager,
};
#[tokio::test]
async fn rmcp_local_session_manager_preserves_access_to_created_session() {
let manager = LocalSessionManager::default();
let (session_id, _transport) = manager
.create_session()
.await
.expect("local manager must create a session");
assert!(
manager
.has_session(&session_id)
.await
.expect("live manager must answer has_session"),
"a live manager must keep access to a session it created"
);
}
#[tokio::test]
async fn rmcp_fresh_session_manager_without_external_store_rejects_known_session_id() {
let first_manager = LocalSessionManager::default();
let (session_id, _transport) = first_manager
.create_session()
.await
.expect("first local manager must create a session");
let second_manager = LocalSessionManager::default();
assert!(
!second_manager
.has_session(&session_id)
.await
.expect("fresh manager must answer has_session"),
"a fresh in-memory manager must not claim ownership of another manager's session"
);
}
@@ -0,0 +1,242 @@
//! Isolated rmcp 3.7.0 Tools adapter over Crank's existing typed Tools service.
//!
//! This module deliberately does not provide a second command engine. It owns only
//! the verified request identity (`ServiceContext` data) and delegates both
//! `tools/list` and `tools/call` to `handle_tools_command` via rmcp's
//! `ServerHandler` trait. HTTP admission must supply identity for each request.
use std::sync::Arc;
use crank_core::{CorrelationContext, ExecutionLocale};
use crate::app::{AppState, lifecycle::ToolCallLifecycle};
use crate::auth::VerifiedMachineCredential;
use crate::protocol::{
ContinuationSupport, ServiceContext, ToolCallCommand, ToolsCommand, ToolsListCommand,
ToolsOutcome, handle_tools_command,
};
/// Long-lived handler: never stores credentials obtained on initialization.
#[derive(Clone)]
pub(super) struct RmcpTools {
pub(super) state: Arc<AppState>,
pub(super) active: Arc<std::sync::atomic::AtomicUsize>,
}
struct ActiveCall(Arc<std::sync::atomic::AtomicUsize>);
impl Drop for ActiveCall {
fn drop(&mut self) {
self.0.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
}
}
/// Server-created identity attached to the individual admitted HTTP request.
#[derive(Clone)]
pub(super) struct RmcpRequestIdentity {
pub(super) workspace_slug: String,
pub(super) agent_slug: String,
pub(super) credential: VerifiedMachineCredential,
pub(super) correlation: CorrelationContext,
pub(super) continuation: ContinuationSupport,
pub(super) locale: ExecutionLocale,
pub(super) session_id: Option<String>,
pub(super) progress_meta: Option<serde_json::Value>,
pub(super) sse_acceptable: bool,
}
impl RmcpRequestIdentity {
/// Build the neutral `ServiceContext` borrowed from this adapter's owned data.
pub(super) fn service_context(&self) -> ServiceContext<'_> {
ServiceContext {
workspace_slug: &self.workspace_slug,
agent_slug: &self.agent_slug,
credential: &self.credential,
correlation: &self.correlation,
continuation: self.continuation,
locale: self.locale,
}
}
}
impl RmcpTools {
/// Delegate one neutral Tools command to the existing shared service.
pub(super) async fn handle(
&self,
identity: &RmcpRequestIdentity,
command: ToolsCommand,
lifecycle: Option<ToolCallLifecycle>,
) -> ToolsOutcome {
handle_tools_command(&self.state, identity.service_context(), command, lifecycle).await
}
pub(super) async fn list_tools(
&self,
identity: &RmcpRequestIdentity,
command: ToolsListCommand,
lifecycle: Option<ToolCallLifecycle>,
) -> ToolsOutcome {
self.handle(identity, ToolsCommand::List(command), lifecycle)
.await
}
pub(super) async fn call_tool(
&self,
identity: &RmcpRequestIdentity,
command: ToolCallCommand,
lifecycle: Option<ToolCallLifecycle>,
) -> ToolsOutcome {
self.handle(identity, ToolsCommand::Call(command), lifecycle)
.await
}
}
use rmcp::ErrorData as McpError;
use rmcp::model::{CallToolRequestParams, ListToolsResult, PaginatedRequestParams};
use rmcp::service::RequestContext;
use rmcp::{RoleServer, ServerHandler};
fn request_identity(context: &RequestContext<RoleServer>) -> Result<RmcpRequestIdentity, McpError> {
context
.extensions
.get::<axum::http::request::Parts>()
.and_then(|parts| parts.extensions.get::<RmcpRequestIdentity>())
.cloned()
.ok_or_else(|| McpError::internal_error("missing authenticated request context", None))
}
impl ServerHandler for RmcpTools {
fn supported_protocol_versions(
&self,
) -> std::borrow::Cow<'static, [rmcp::model::ProtocolVersion]> {
std::borrow::Cow::Borrowed(&[
rmcp::model::ProtocolVersion::V_2025_11_25,
rmcp::model::ProtocolVersion::V_2026_07_28,
])
}
fn get_info(&self) -> rmcp::model::ServerConfig {
let mut info = rmcp::model::ServerConfig::default();
info.capabilities = rmcp::model::ServerCapabilities::builder()
.enable_tools()
.build();
info
}
fn list_tools(
&self,
request: Option<PaginatedRequestParams>,
context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<ListToolsResult, McpError>> + Send + '_ {
let request = request.unwrap_or_default();
async move {
let identity = request_identity(&context)?;
let cursor = request.cursor;
let command = crate::protocol::ToolsListCommand {
cursor,
limit: None,
};
match self.list_tools(&identity, command, None).await {
crate::protocol::ToolsOutcome::List { tools, next_cursor } => {
super::response::sdk_tools_list_result(&tools, next_cursor)
.map_err(|_| McpError::internal_error("invalid tool catalog entry", None))
}
crate::protocol::ToolsOutcome::InvalidParams(message) => {
Err(McpError::invalid_params(message, None))
}
_ => Err(McpError::internal_error("crate catalog unavailable", None)),
}
}
}
async fn call_tool(
&self,
request: CallToolRequestParams,
context: RequestContext<RoleServer>,
) -> Result<rmcp::model::CallToolResponse, McpError> {
let identity = request_identity(&context)?;
let arguments = request
.arguments
.map(serde_json::Value::Object)
.unwrap_or_else(|| serde_json::json!({}));
let command = crate::protocol::ToolCallCommand {
tool_name: request.name.to_string(),
arguments,
confirmation_token: None,
};
let token = identity
.sse_acceptable
.then(|| super::lifecycle::token_from_meta(identity.progress_meta.as_ref()))
.flatten();
// Observe future cleanup directly in the isolated loopback oracle.
self.active
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let _active = ActiveCall(self.active.clone());
let (mut lifecycle, mut events) = ToolCallLifecycle::new(token.clone());
let entry = identity.session_id.as_ref().and_then(|session_id| {
self.state.in_flight.register(
&super::lifecycle::InFlightScope {
session_id: session_id.clone(),
owner: identity
.credential
.platform_api_key_id
.as_ref()
.map(|id| id.as_str())
.unwrap_or("")
.to_owned(),
},
&serde_json::to_value(&context.id).expect("request id serializes"),
)
});
if let Some(entry) = &entry {
lifecycle = lifecycle.with_cancel(entry.cancel_signal());
}
let call = self.call_tool(&identity, command, Some(lifecycle.clone()));
tokio::pin!(call);
let outcome = loop {
tokio::select! {
biased;
event = async { match events.as_mut() { Some(events) => events.recv().await, None => std::future::pending().await } } => {
if let Some(event) = event {
if let Some(token) = &token {
let params = serde_json::from_value(super::lifecycle::progress_frame(token, &event)["params"].clone()).map_err(|_| McpError::internal_error("invalid progress event", None))?;
let _ = context.peer.notify_progress(params).await;
}
} else { events = None; }
}
outcome = &mut call => break outcome,
_ = lifecycle.cancelled() => { return Err(McpError::internal_error("request cancelled", None)); },
_ = context.ct.cancelled() => { return Err(McpError::internal_error("request cancelled", None)); },
}
};
match outcome {
crate::protocol::ToolsOutcome::Call(outcome) => match outcome {
crate::protocol::ToolCallOutcome::Completed(value)
| crate::protocol::ToolCallOutcome::ApprovalPending(value) => {
Ok(super::response::sdk_success_tool_result(value).into())
}
crate::protocol::ToolCallOutcome::ApprovalUnavailable(error) => {
Ok(super::response::sdk_error_tool_result(
crate::tool_error::control_tool_error_contract(
error,
identity.correlation.request_id().as_str(),
identity.correlation.trace_id().as_str(),
identity.locale,
),
)
.into())
}
crate::protocol::ToolCallOutcome::InvalidParams(message) => {
Err(McpError::invalid_params(message, None))
}
crate::protocol::ToolCallOutcome::Error(contract) => {
Ok(super::response::sdk_error_tool_result(contract).into())
}
crate::protocol::ToolCallOutcome::InternalFailure => {
Err(McpError::internal_error("tool execution failed", None))
}
},
crate::protocol::ToolsOutcome::InvalidParams(message) => {
Err(McpError::invalid_params(message, None))
}
_ => Err(McpError::internal_error("crate catalog unavailable", None)),
}
}
}
@@ -0,0 +1,381 @@
//! Application-level durable Task handler and attempt authority (Story 4.3/4.4).
//!
//! The handler owns everything that touches product authority: the exact published pin, the typed
//! Platform API key, the approval policy, the encrypted executable input and the resolved upstream
//! auth. The runtime owns the single bounded retry loop and the durable attempt checkpoint/resolution;
//! this module only revalidates and prepares one attempt at a time.
use std::time::{Duration, Instant};
use async_trait::async_trait;
use crank_core::{
AgentId, ExecutionOrigin, MachineAccessMode, OperationSecurityLevel, PlatformApiKeyScope,
TaskError, TaskLease, TaskOwnerKind, TraceContext, VerifiedMachineCredential, WorkspaceId,
current_unix_ms,
};
use crank_registry::PostgresRegistry;
use crank_registry::PublishedAgentTool;
use crank_runtime::{
ExecutionAuthorization, PrepareAttempt, RuntimeExecutor, RuntimeOperation, SecretCrypto,
TaskAttemptAuthority, TaskAttemptContext, TaskAttemptOutcome, TaskAttemptPlan,
TaskAttemptRefusal, TaskHandler, TaskService, TaskWorkerError,
};
use sha2::{Digest, Sha256};
use super::TaskApplication;
use super::auth_resolution::resolve_operation_auth_with;
use super::tool_resolution::runtime_operation;
use crate::access::{allows_scope, credential_allows_security_level};
use crate::catalog::PublishedToolCatalog;
/// Server-owned evidence that one published operation deduplicates repeated sends of the same stable
/// idempotency key.
///
/// The descriptor is constructed by the application for an integration whose upstream contract was
/// actually qualified (key delivery, dedup scope, canonical input binding and a dedup window that
/// covers the pinned retry budget). It is never deserialized from Task input, so a caller key or
/// header alone can never make a write retryable. Production wiring passes an empty set until such a
/// contract exists.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VerifiedIdempotentWrite {
pub operation_id: String,
pub operation_version: u32,
/// Header through which the stable key is delivered to this exact upstream.
pub key_header: String,
/// Dedup window of the upstream contract, which must cover the pinned retry budget.
pub validity_ms: i64,
}
pub struct TaskWorkerHandler {
registry: PostgresRegistry,
catalog: PublishedToolCatalog,
runtime: RuntimeExecutor,
secret_crypto: SecretCrypto,
service: TaskService,
verified_writes: Vec<VerifiedIdempotentWrite>,
}
impl TaskWorkerHandler {
/// Production construction from the same application entry, registry and runtime the MCP
/// process serves requests with. The worker needs no HTTP state, only these authoritative
/// dependencies.
pub fn from_application(
tasks: &TaskApplication,
registry: PostgresRegistry,
runtime: RuntimeExecutor,
secret_crypto: SecretCrypto,
) -> Self {
Self {
registry,
catalog: tasks.catalog(),
runtime,
secret_crypto,
service: tasks.task_service(),
verified_writes: Vec::new(),
}
}
/// Registers server-owned verified-idempotency evidence of qualified integrations.
pub fn with_verified_writes(mut self, verified_writes: Vec<VerifiedIdempotentWrite>) -> Self {
self.verified_writes = verified_writes;
self
}
/// Authoritative pre-dispatch authorization of one leased Task (Story 4.3).
///
/// Resolves the exact pinned Published Version in the current publication, repeats the approval
/// policy and revalidates the typed persisted Platform API key identity against the registry:
/// the key must still be active, unexpired and bound to this workspace/agent, and its exact
/// scopes/security level must still authorize the operation. Any denial is a typed error, so the
/// worker finishes the attempt without dispatch (fail closed before any network use).
///
/// An administrator Task has no authoritative Task revalidation API in the registry, so it stays
/// fail-closed here instead of inventing authority.
async fn authorize(&self, lease: &TaskLease) -> Result<PublishedAgentTool, TaskError> {
if lease.record.owner_kind != TaskOwnerKind::Agent {
return Err(TaskError::invalid(
"admin task authority revalidation is not implemented",
));
}
let Some(key_id) = lease.record.platform_api_key_id.as_ref() else {
return Err(TaskError::invalid("task authority reference is missing"));
};
let workspace_id = WorkspaceId::new(lease.record.workspace_id.as_str());
let agent_id = AgentId::new(lease.record.owner_id.as_str());
let workspace = self
.registry
.get_workspace(&workspace_id)
.await
.map_err(|_| TaskError::unavailable("published catalog unavailable"))?
.ok_or_else(|| TaskError::invalid("task workspace no longer exists"))?;
let agents = self
.registry
.list_agents(&workspace_id)
.await
.map_err(|_| TaskError::unavailable("published catalog unavailable"))?;
let agent = agents
.into_iter()
.find(|agent| agent.id.as_str() == lease.record.owner_id.as_str())
.ok_or_else(|| TaskError::invalid("task agent no longer exists"))?;
let catalog = self
.catalog
.get_catalog(&workspace.workspace.slug, &agent.slug)
.await
.map_err(|_| TaskError::unavailable("published catalog unavailable"))?;
let tool = catalog
.tools
.into_iter()
.find(|tool| {
tool.operation.id.as_str() == lease.record.operation_id.as_str()
&& tool.operation.version == lease.record.pins.operation_version
})
.ok_or_else(|| TaskError::invalid("task published operation pin no longer matches"))?;
if tool.workspace_id.as_str() != lease.record.workspace_id.as_str() {
return Err(TaskError::invalid("task workspace binding mismatch"));
}
if tool.agent_id.as_str() != lease.record.owner_id.as_str() {
return Err(TaskError::invalid("task agent binding mismatch"));
}
if tool
.operation
.execution_config
.approval_policy
.as_ref()
.is_some_and(|policy| policy.required)
{
return Err(TaskError::invalid(
"missing approval proof for task operation",
));
}
// Authoritative revalidation of the exact immutable key identity. A revoked, expired,
// foreign-workspace or foreign-agent key is absent here and never reaches the network.
let key = self
.registry
.revalidate_platform_api_key(&workspace_id, &agent_id, key_id)
.await
.map_err(|_| TaskError::unavailable("task authority store unavailable"))?
.ok_or_else(|| TaskError::invalid("task authority is no longer active"))?;
let credential = VerifiedMachineCredential {
machine_access_mode: MachineAccessMode::StaticAgentKey,
max_security_level: OperationSecurityLevel::Standard,
scopes: key.api_key.scopes.clone(),
platform_api_key_id: Some(key.api_key.id.clone()),
};
if !allows_scope(&credential.scopes, PlatformApiKeyScope::Write) {
return Err(TaskError::invalid("task authority scope is insufficient"));
}
if !credential_allows_security_level(&credential, tool.operation.security_level) {
return Err(TaskError::invalid(
"task authority security level is insufficient",
));
}
Ok(tool)
}
/// Safe-retry classification of one attempt and the stable upstream key digest.
///
/// A read is always safe to repeat. A write is only safe to repeat when this process holds
/// server-owned evidence for the exact published operation and that evidence's dedup window still
/// covers the accepted retry budget; the presence of a client key or an idempotency header alone
/// never qualifies.
fn retry_classification(
&self,
operation: &RuntimeOperation,
input: &serde_json::Value,
retry_window_ms: i64,
) -> (bool, Option<String>) {
if matches!(
&operation.target,
crank_core::Target::Rest(target) if target.method == crank_core::HttpMethod::Get
) {
return (true, None);
}
let Some(policy) = operation.execution_config.idempotency.as_ref() else {
return (false, None);
};
let Some(field) = policy
.input_field
.as_deref()
.filter(|field| !field.trim().is_empty())
else {
return (false, None);
};
let Some(header) = policy
.header_name
.as_deref()
.filter(|value| !value.is_empty())
else {
return (false, None);
};
let key = match input.get(field) {
Some(serde_json::Value::String(value)) if !value.trim().is_empty() => value.clone(),
Some(serde_json::Value::Number(value)) => value.to_string(),
_ => return (false, None),
};
let evidence = self.verified_writes.iter().find(|evidence| {
evidence.operation_id == operation.operation_id.as_str()
&& evidence.operation_version == operation.operation_version
&& evidence.key_header.eq_ignore_ascii_case(header)
&& evidence.validity_ms >= retry_window_ms
});
let Some(evidence) = evidence else {
return (false, None);
};
// Only a digest of the stable key is persisted; the key itself stays in the encrypted input.
let mut digest = Sha256::new();
digest.update(evidence.key_header.as_bytes());
digest.update([0u8]);
digest.update(key.as_bytes());
(true, Some(format!("{:x}", digest.finalize())))
}
/// Story 4.5 producer: the pinned operation still lacks a required input item, so the Task must be
/// suspended before any dispatch. The schema snapshot and its presentation projection are frozen
/// here; the TTL never exceeds the accepted absolute execution deadline.
fn input_request(
&self,
tool: &PublishedAgentTool,
lease: &TaskLease,
missing_path: &str,
expires_at_unix_ms: i64,
) -> Result<crank_core::TaskInputRequest, TaskAttemptRefusal> {
let schema_snapshot = serde_json::to_value(&tool.operation.input_schema).map_err(|_| {
TaskAttemptRefusal::Failed {
code: "task_input_schema_unavailable".to_owned(),
}
})?;
let request = crank_core::TaskInputRequest {
wait_id: format!("wait-{}", uuid::Uuid::now_v7()),
expected_version: lease.record.version.saturating_add(1),
schema_revision: tool.operation.version,
schema_format_version: crank_core::TASK_INPUT_SCHEMA_FORMAT_VERSION,
schema_snapshot,
projected_schema: crate::manifest::schema_to_json_schema(&tool.operation.input_schema),
prompt_code: "required_input_missing".to_owned(),
missing_fields: vec![missing_path.to_owned()],
expires_at_unix_ms,
fingerprint_format_version: crank_core::TASK_INPUT_SCHEMA_FORMAT_VERSION,
};
request.validate().map_err(|_| TaskAttemptRefusal::Failed {
code: "task_input_request_invalid".to_owned(),
})?;
Ok(request)
}
/// Maps a preparation failure onto the attempt refusal vocabulary: a transient authority-store
/// outage leaves the Task leased for recovery, everything else is a proven pre-dispatch refusal.
fn refusal(error: TaskError) -> TaskAttemptRefusal {
match error {
TaskError::Unavailable { .. } => TaskAttemptRefusal::Stopped,
_ => TaskAttemptRefusal::Failed {
code: "task_authority_rejected".to_owned(),
},
}
}
}
#[async_trait]
impl TaskHandler for TaskWorkerHandler {
async fn revalidate(&self, lease: &TaskLease) -> Result<(), TaskWorkerError> {
self.authorize(lease)
.await
.map_err(TaskWorkerError::Store)?;
Ok(())
}
async fn execute(&self, context: TaskAttemptContext) -> TaskAttemptOutcome {
self.runtime.execute_task_outcome(&context, self).await
}
}
#[async_trait]
impl TaskAttemptAuthority for TaskWorkerHandler {
async fn prepare_attempt(
&self,
lease: &TaskLease,
) -> Result<PrepareAttempt, TaskAttemptRefusal> {
let tool = self.authorize(lease).await.map_err(Self::refusal)?;
let input = self
.service
.executable_input(lease)
.await
.map_err(Self::refusal)?;
let operation = runtime_operation(&tool);
let now_ms = current_unix_ms();
let deadline_ms = lease.record.pins.execution_deadline_unix_ms;
if deadline_ms <= now_ms {
// The accepted absolute deadline already elapsed: no further attempt may be dispatched,
// and any previous uncertainty was already recorded as such.
return Err(TaskAttemptRefusal::Failed {
code: "task_deadline_elapsed".to_owned(),
});
}
// Only a missing required item of the pinned schema becomes a typed input request. Any other
// schema violation keeps the existing pre-dispatch attempt validation, and a generation that
// already accepted a continuation can never open a second wait for the same Task.
if let Some(missing_path) =
crank_runtime::missing_required_input(&tool.operation.input_schema, &input)
{
if lease.record.effective_input_fingerprint != lease.record.input_fingerprint {
return Err(TaskAttemptRefusal::Failed {
code: "task_input_still_incomplete".to_owned(),
});
}
return Ok(PrepareAttempt::InputRequired(self.input_request(
&tool,
lease,
&missing_path,
deadline_ms,
)?));
}
let deadline = Instant::now() + Duration::from_millis((deadline_ms - now_ms) as u64);
let workspace_id = WorkspaceId::new(lease.record.workspace_id.as_str());
let agent_id = AgentId::new(lease.record.owner_id.as_str());
let resolved_auth = resolve_operation_auth_with(
&self.registry,
&self.secret_crypto,
&workspace_id,
&operation.execution_config,
)
.await
.map_err(|_| TaskAttemptRefusal::Failed {
code: "task_auth_resolution_failed".to_owned(),
})?;
// Continue the parent trace persisted with the accepted Task; every attempt adds its own
// child `adapter_attempt` span carrying the committed durable attempt ID.
let trace_context =
TraceContext::parse(&lease.record.support.traceparent).map_err(|_| {
TaskAttemptRefusal::Failed {
code: "task_trace_parent_invalid".to_owned(),
}
})?;
let request_context = crank_runtime::RuntimeRequestContext::new(
lease.record.support.request_id.clone(),
trace_context,
)
.with_metering_context(
workspace_id.clone(),
Some(agent_id.clone()),
crank_core::InvocationSource::AgentToolCall,
);
let (retry_allowed, upstream_key_digest) =
self.retry_classification(&operation, &input, deadline_ms - now_ms);
Ok(PrepareAttempt::Ready(Box::new(TaskAttemptPlan {
workspace_id,
origin: ExecutionOrigin::AgentSnapshot,
agent_id: Some(agent_id),
operation,
input,
authorization: ExecutionAuthorization::Authorized,
resolved_auth,
request_context,
deadline,
retry_allowed,
upstream_key_digest,
})))
}
}
+414
View File
@@ -0,0 +1,414 @@
//! Application entry of durable background Tasks (Story 4.2, FR-22/FR-23).
//!
//! The entry takes an already authenticated principal (a verified machine credential, or an
//! administrator identity verified by the calling admin surface), resolves the exact published Tool,
//! repeats the caller's authorization, pins the Published Version/policy/deadline and delegates the
//! durable acceptance to the runtime Task service. The repository owns the transaction, so the
//! returned Task ID exists only after Task + dispatch intent + queue slot committed.
//!
//! Deliberately not wired: the public MCP `tools/call`/Tasks wire (Story 4.7) and the worker/leases
//! (Story 4.3). The existing synchronous/unsupported `tools/call` path is unchanged, and this entry
//! never spawns an ephemeral execution future.
use std::sync::Arc;
use std::time::{Duration, Instant};
use crank_core::{
CorrelationContext, PlatformApiKeyId, TaskAcceptance, TaskAccess, TaskError, TaskId,
TaskOwnerKind, TaskRecord, ToolExecutionMode,
};
use crank_registry::{PostgresRegistry, PublishedAgentTool};
use crank_runtime::{TaskAcceptDraft, TaskOutcome, TaskService};
use serde_json::Value;
use crate::access::credential_allows_security_level;
use crate::catalog::PublishedToolCatalog;
/// Authenticated principal of one Task operation. The authorization scope identity is derived here
/// from the verified credential/admin identity, never from a caller-supplied request string.
#[derive(Clone, Debug)]
pub enum TaskPrincipal {
Agent {
workspace_slug: String,
agent_slug: String,
credential: crank_core::VerifiedMachineCredential,
},
/// Administrator identity that the admin surface already authenticated. It is never a wildcard:
/// owner, workspace and authorization scope all participate in the stored predicate.
Admin {
workspace_slug: String,
agent_slug: String,
user_id: String,
},
}
impl TaskPrincipal {
fn workspace_slug(&self) -> &str {
match self {
Self::Agent { workspace_slug, .. } | Self::Admin { workspace_slug, .. } => {
workspace_slug
}
}
}
fn agent_slug(&self) -> &str {
match self {
Self::Agent { agent_slug, .. } | Self::Admin { agent_slug, .. } => agent_slug,
}
}
fn owner(&self, tool: &PublishedAgentTool) -> (TaskOwnerKind, String) {
match self {
Self::Agent { .. } => (TaskOwnerKind::Agent, tool.agent_id.as_str().to_owned()),
Self::Admin { user_id, .. } => (TaskOwnerKind::Admin, user_id.clone()),
}
}
/// Typed immutable Platform API key identity of this principal. A verified agent credential
/// without a key ID cannot be revalidated by a worker, so it is refused instead of accepted;
/// an administrator has no machine key and is stored as `None` (the worker fails closed).
fn platform_api_key_id(&self) -> Result<Option<PlatformApiKeyId>, TaskApplicationError> {
match self {
Self::Agent { credential, .. } => credential
.platform_api_key_id
.clone()
.map(Some)
.ok_or(TaskApplicationError::Denied),
Self::Admin { .. } => Ok(None),
}
}
/// Bounded server-derived authorization scope identity of this principal.
fn authorization_scope(&self) -> String {
let identity = match self {
Self::Agent { credential, .. } => {
let mut scopes = credential
.scopes
.iter()
.map(|scope| format!("{scope:?}"))
.collect::<Vec<_>>();
scopes.sort();
format!(
"key:{}|mode:{:?}|level:{:?}|scopes:{}",
credential
.platform_api_key_id
.as_ref()
.map(|id| id.as_str().to_owned())
.unwrap_or_else(|| "-".to_owned()),
credential.machine_access_mode,
credential.max_security_level,
scopes.join(",")
)
}
Self::Admin { user_id, .. } => format!("admin:{user_id}"),
};
identity.chars().take(160).collect()
}
}
/// Typed refusal of the application entry. `NotFound`/`Denied` never reveal whether a foreign Task
/// exists, and no variant carries Task payload or fingerprint.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum TaskApplicationError {
#[error("task request was not found")]
NotFound,
#[error("task request is not authorized")]
Denied,
#[error("the published Tool policy does not accept durable background work")]
NotBackground,
#[error("the requested published Tool version is not available")]
VersionUnavailable,
#[error("this Tool requires a verified approval before acceptance")]
ApprovalRequired,
#[error(transparent)]
Task(#[from] TaskError),
}
#[derive(Clone)]
pub struct TaskApplication {
catalog: PublishedToolCatalog,
service: TaskService,
}
impl TaskApplication {
/// Production construction of the application entry over the process registry, the published
/// catalog and the durable Task service.
pub fn new(
registry: PostgresRegistry,
refresh_interval: Duration,
coordination_store: Arc<dyn crank_core::CoordinationStateStore>,
service: TaskService,
) -> Self {
Self {
catalog: PublishedToolCatalog::new(registry, refresh_interval, coordination_store),
service,
}
}
/// Runtime task service carrying encrypted executable input.
pub fn task_service(&self) -> TaskService {
self.service.clone()
}
/// Published catalog of the same application entry, for the durable Task worker's pre-dispatch
/// revalidation.
pub(super) fn catalog(&self) -> PublishedToolCatalog {
self.catalog.clone()
}
/// Readiness of the durable Task store this entry depends on.
pub async fn health(&self) -> Result<(), TaskError> {
self.service.health().await
}
/// Accepts one background request for an already authenticated principal.
///
/// `expected_version` is the caller's explicit Published Version pin. Without it the current
/// publication is pinned for new work and a retry of the same scoped key returns the stored Task
/// even after a newer publication. An explicit pin may only retry the Task accepted with that
/// exact version: a different pin is a typed conflict, never a rebind.
pub async fn accept_background(
&self,
principal: &TaskPrincipal,
tool_name: &str,
expected_version: Option<u32>,
scoped_key: &str,
input: &Value,
correlation: &CorrelationContext,
) -> Result<TaskAcceptance, TaskApplicationError> {
let tool = self.resolve_authorized_tool(principal, tool_name).await?;
let current_version = tool.operation.version;
let Some(policy) = tool.operation.execution_config.execution_policy else {
return Err(TaskApplicationError::NotBackground);
};
if policy.mode == ToolExecutionMode::Synchronous {
return Err(TaskApplicationError::NotBackground);
}
if tool
.operation
.execution_config
.approval_policy
.as_ref()
.is_some_and(|approval| approval.required)
{
// Without a verified, non-consuming approval reference this entry fails closed instead
// of accepting work a worker could never safely execute.
return Err(TaskApplicationError::ApprovalRequired);
}
let platform_api_key_id = principal.platform_api_key_id()?;
let draft = |operation_version: u32| {
let (owner_kind, owner_id) = principal.owner(&tool);
TaskAcceptDraft {
workspace_id: tool.workspace_id.as_str().to_owned(),
owner_kind,
owner_id,
authorization_scope: principal.authorization_scope(),
platform_api_key_id: platform_api_key_id.clone(),
operation_id: tool.operation.id.as_str().to_owned(),
operation_version,
policy_mode: policy.mode,
scoped_key: scoped_key.to_owned(),
input: input.clone(),
correlation: correlation.clone(),
deadline: Instant::now() + Duration::from_millis(policy.deadline_ms.max(1)),
retention_ms: TaskService::default_retention_ms(),
approval_reference: None,
retry: task_retry_policy(&tool),
}
};
let Some(requested) = expected_version else {
return Ok(self.service.accept(draft(current_version)).await?);
};
let draft = draft(requested);
match self.service.lookup(&draft).await? {
Some(record) => {
if record.input_fingerprint != TaskService::input_fingerprint(input)? {
return Err(TaskApplicationError::Task(TaskError::FingerprintConflict));
}
if record.pins.operation_version != requested {
return Err(TaskApplicationError::VersionUnavailable);
}
Ok(TaskAcceptance {
task_id: record.task_id,
state: record.state,
operation_version: record.pins.operation_version,
support: record.support,
created_at_unix_ms: record.created_at_unix_ms,
retain_until_unix_ms: record.pins.retain_until_unix_ms,
replayed: true,
})
}
None if requested == current_version => Ok(self.service.accept(draft).await?),
// A new Task can only be accepted against the currently published version.
None => Err(TaskApplicationError::VersionUnavailable),
}
}
/// Owner-scoped status read of one Task of the addressed published Tool.
pub async fn status(
&self,
principal: &TaskPrincipal,
tool_name: &str,
task_id: TaskId,
) -> Result<TaskRecord, TaskApplicationError> {
let access = self.task_access(principal, tool_name).await?;
match self.service.status(&access, task_id).await {
Ok(record) => Ok(record),
Err(TaskError::NotFound) => Err(TaskApplicationError::NotFound),
Err(error) => Err(TaskApplicationError::Task(error)),
}
}
/// Owner-scoped result projection of one Task of the addressed published Tool.
pub async fn result(
&self,
principal: &TaskPrincipal,
tool_name: &str,
task_id: TaskId,
) -> Result<TaskOutcome, TaskApplicationError> {
let access = self.task_access(principal, tool_name).await?;
match self.service.result(&access, task_id).await {
Ok(outcome) => Ok(outcome),
Err(TaskError::NotFound) => Err(TaskApplicationError::NotFound),
Err(error) => Err(TaskApplicationError::Task(error)),
}
}
/// Owner-scoped cancellation of one Task of the addressed published Tool (Story 4.4).
///
/// Cancellation is decided by the durable store under a row lock: before the dispatch checkpoint
/// it is an honest cancellation with zero outbound calls, after it the Task becomes the
/// non-terminal uncertain outcome instead of a plain cancel or a retry.
pub async fn cancel(
&self,
principal: &TaskPrincipal,
tool_name: &str,
task_id: TaskId,
expected_version: Option<u32>,
) -> Result<crank_core::TaskCancelOutcome, TaskApplicationError> {
let access = self.task_access(principal, tool_name).await?;
match self
.service
.cancel(&access, task_id, expected_version, "user_cancelled")
.await
{
Ok(outcome) => Ok(outcome),
Err(TaskError::NotFound) => Err(TaskApplicationError::NotFound),
Err(error) => Err(TaskApplicationError::Task(error)),
}
}
/// Applies one additional validated input to a waiting Task (Story 4.5).
///
/// This is the server-internal application entry: the public Task MCP API stays Story 4.7. The
/// principal is already authenticated here, the owner scope is derived from the verified
/// credential and the durable transaction revalidates the same key before any receipt lookup.
/// An identical authorized replay returns the committed outcome; a conflicting or stale answer is
/// typed, and an elapsed TTL never revives the Task.
#[allow(clippy::too_many_arguments)]
pub async fn update_input(
&self,
principal: &TaskPrincipal,
tool_name: &str,
task_id: TaskId,
wait_id: &str,
expected_version: u32,
update_key: &str,
input: &Value,
) -> Result<crank_core::TaskInputUpdateOutcome, TaskApplicationError> {
let access = self.task_access(principal, tool_name).await?;
match self
.service
.update_input(
&access,
task_id,
wait_id,
expected_version,
update_key,
input,
)
.await
{
Ok(outcome) => Ok(outcome),
Err(TaskError::NotFound) => Err(TaskApplicationError::NotFound),
Err(error) => Err(TaskApplicationError::Task(error)),
}
}
/// Owner-scoped attempt history of one Task of the addressed published Tool.
pub async fn attempts(
&self,
principal: &TaskPrincipal,
tool_name: &str,
task_id: TaskId,
) -> Result<Vec<crank_core::TaskAttempt>, TaskApplicationError> {
let access = self.task_access(principal, tool_name).await?;
match self.service.attempts(&access, task_id).await {
Ok(attempts) => Ok(attempts),
Err(TaskError::NotFound) => Err(TaskApplicationError::NotFound),
Err(error) => Err(TaskApplicationError::Task(error)),
}
}
async fn task_access(
&self,
principal: &TaskPrincipal,
tool_name: &str,
) -> Result<TaskAccess, TaskApplicationError> {
let tool = self.resolve_authorized_tool(principal, tool_name).await?;
let (owner_kind, owner_id) = principal.owner(&tool);
Ok(TaskAccess {
workspace_id: tool.workspace_id.as_str().to_owned(),
owner_kind,
owner_id,
authorization_scope: principal.authorization_scope(),
})
}
/// Resolves the addressed Tool of the principal's published catalog and repeats its authorization
/// check. A foreign workspace, an unknown Tool and an insufficient security level are typed
/// refusals without metadata.
async fn resolve_authorized_tool(
&self,
principal: &TaskPrincipal,
tool_name: &str,
) -> Result<PublishedAgentTool, TaskApplicationError> {
let catalog = self
.catalog
.get_catalog(principal.workspace_slug(), principal.agent_slug())
.await
.map_err(|_| TaskApplicationError::NotFound)?;
let tool = catalog
.tools
.into_iter()
.find(|tool| tool.tool_name == tool_name)
.ok_or(TaskApplicationError::NotFound)?;
if let TaskPrincipal::Admin { workspace_slug, .. } = principal
&& tool.workspace_slug != *workspace_slug
{
return Err(TaskApplicationError::Denied);
}
if let TaskPrincipal::Agent { credential, .. } = principal
&& !credential_allows_security_level(credential, tool.operation.security_level)
{
return Err(TaskApplicationError::Denied);
}
Ok(tool)
}
}
/// Bounded retry snapshot pinned from the exact published operation at acceptance. A later
/// publication, a restart or a new binary never changes the numbers an accepted Task was admitted
/// with; a missing or oversized policy is clamped to the closed story bound.
fn task_retry_policy(tool: &PublishedAgentTool) -> crank_core::TaskRetryPolicy {
let max_attempts = tool
.operation
.execution_config
.retry_policy
.as_ref()
.map_or(1, |policy| {
policy.max_attempts.clamp(1, crank_core::TASK_MAX_ATTEMPTS)
});
crank_core::TaskRetryPolicy::new(max_attempts, 1)
}
@@ -122,6 +122,17 @@ pub(super) struct CountedSessionStore {
counters: Arc<SessionCounters>,
}
impl CountedSessionStore {
pub(super) fn snapshot(&self) -> SessionOps {
self.counters.snapshot()
}
/// Inspect retained state without incrementing the HTTP request's session-read oracle.
pub(super) async fn retained(&self, id: &str) -> Option<SessionState> {
self.inner.get(id).await.unwrap()
}
}
#[async_trait::async_trait]
impl TransportSessionStore for CountedSessionStore {
async fn create(
@@ -415,7 +426,7 @@ pub(super) fn agent_id() -> AgentId {
AgentId::new(format!("agent_{AGENT_SLUG}"))
}
pub(super) async fn publish_agent(
pub(crate) async fn publish_agent(
registry: &PostgresRegistry,
operation: &Operation<Schema, MappingSet>,
) {
@@ -470,7 +481,7 @@ pub(super) async fn publish_agent(
.expect("the agent must be published");
}
pub(super) async fn create_api_key(registry: &PostgresRegistry) -> String {
pub(crate) async fn create_api_key(registry: &PostgresRegistry) -> String {
let secret = format!("crk_dispatch_observer_{}", uuid::Uuid::now_v7().simple());
let api_key = PlatformApiKey {
id: PlatformApiKeyId::new("pk_dispatch_observer"),
@@ -500,7 +511,7 @@ pub(super) fn hash_access_secret(secret: &str) -> String {
URL_SAFE_NO_PAD.encode(Sha256::digest(secret.as_bytes()))
}
pub(super) fn test_operation(base_url: &str) -> Operation<Schema, MappingSet> {
pub(crate) fn test_operation(base_url: &str) -> Operation<Schema, MappingSet> {
Operation {
id: OperationId::new(format!("op_{TOOL_NAME}")),
name: TOOL_NAME.to_owned(),
@@ -550,6 +561,7 @@ pub(super) fn test_operation(base_url: &str) -> Operation<Schema, MappingSet> {
idempotency: None,
safety: None,
approval_policy: None,
execution_policy: None,
auth_profile_ref: None,
headers: BTreeMap::new(),
},
+128 -17
View File
@@ -1,3 +1,6 @@
use rmcp::model::{
ErrorCode, ErrorData, JsonRpcError, JsonRpcResponse, JsonRpcVersion2_0, RequestId,
};
use serde_json::{Value, json};
/// The two approved MCP revisions (`docs/`-approved window of 2026-10-11).
@@ -18,6 +21,9 @@ pub const CURRENT_PROTOCOL_VERSION: &str = LEGACY_PROTOCOL_VERSION;
pub const META_PROTOCOL_VERSION_KEY: &str = "io.modelcontextprotocol/protocolVersion";
pub const META_CLIENT_CAPABILITIES_KEY: &str = "io.modelcontextprotocol/clientCapabilities";
pub const META_CLIENT_INFO_KEY: &str = "io.modelcontextprotocol/clientInfo";
/// `_meta` key of the per-request log level (Story 3.18). SEP-2577 replaces the former
/// `logging/setLevel` request of the legacy era with this per-request opt-in.
pub const META_LOG_LEVEL_KEY: &str = "io.modelcontextprotocol/logLevel";
/// Result `_meta` key carrying the self-reported server identity (pinned `DiscoverResult`).
pub const META_SERVER_INFO_KEY: &str = "io.modelcontextprotocol/serverInfo";
@@ -57,28 +63,50 @@ pub fn is_response(message: &Value) -> bool {
&& (message.get("result").is_some() || message.get("error").is_some())
}
fn projected_sdk_id(id: &Value) -> RequestId {
match id {
Value::String(value) => RequestId::String(value.as_str().into()),
Value::Number(value) => RequestId::Number(value.as_i64().unwrap_or(0)),
_ => RequestId::Number(0),
}
}
pub fn jsonrpc_result(id: Value, result: Value) -> Value {
json!({
"jsonrpc": "2.0",
"id": id,
"result": result
})
let sdk = JsonRpcResponse::<Value> {
jsonrpc: JsonRpcVersion2_0,
id: projected_sdk_id(&id),
result,
};
let mut wire = serde_json::to_value(sdk).expect("SDK response contains only JSON values");
#[cfg(test)]
crate::app::dispatch_observer::sdk_jsonrpc_response_serialized();
// SDK RequestId cannot preserve the full Value domain or floating-point form.
wire["id"] = id;
wire
}
pub fn jsonrpc_error(id: Value, code: i64, message: impl Into<String>) -> Value {
let mut error = json!({
"code": code,
"message": message.into()
});
let (request_id, trace_id) = crank_observability::current_request_correlation();
if request_id.is_some() && trace_id.is_some() {
error["data"] = correlated_error_data(json!({}));
}
json!({
"jsonrpc": "2.0",
"id": id,
"error": error
})
let data = if request_id.is_some() && trace_id.is_some() {
Some(correlated_error_data(json!({})))
} else {
None
};
let sdk = JsonRpcError::new(
Some(projected_sdk_id(&id)),
ErrorData::new(
ErrorCode(i32::try_from(code).unwrap_or(0)),
message.into(),
data,
),
);
let mut wire = serde_json::to_value(sdk).expect("SDK error contains only JSON values");
#[cfg(test)]
crate::app::dispatch_observer::sdk_jsonrpc_error_serialized();
// Restore the helper's wider id/code contract before exposing the SDK envelope.
wire["id"] = id;
wire["error"]["code"] = Value::from(code);
wire
}
pub fn correlated_error_data(mut data: Value) -> Value {
@@ -101,3 +129,86 @@ pub fn negotiated_protocol_version(requested: &str) -> Option<&'static str> {
.copied()
.find(|version| *version == requested)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn jsonrpc_preserves_helper_values_and_numeric_forms() {
let values = [
json!(""),
json!("Юникод"),
json!(i64::MIN),
json!(i64::MAX),
json!(9007199254740993_u64),
json!(u64::MAX),
json!(1.5),
json!(1.0),
json!(-0.0),
Value::Null,
json!(true),
json!([null, u64::MAX]),
json!({"extension": {"nested": null}}),
];
for id in &values {
for result in &values {
let actual = jsonrpc_result(id.clone(), result.clone());
let expected = json!({"jsonrpc": "2.0", "id": id, "result": result});
assert_eq!(actual, expected);
assert_eq!(actual.to_string(), expected.to_string());
}
for code in [
i64::MIN,
i64::from(i32::MIN) - 1,
i64::from(i32::MIN),
-32602,
0,
i64::from(i32::MAX),
i64::from(i32::MAX) + 1,
i64::MAX,
] {
for message in ["", "Ошибка Ю"] {
let actual = jsonrpc_error(id.clone(), code, message);
let expected = json!({"jsonrpc": "2.0", "id": id,
"error": {"code": code, "message": message}});
assert_eq!(actual, expected);
assert_eq!(actual.to_string(), expected.to_string());
}
}
}
}
#[tokio::test]
async fn jsonrpc_error_requires_complete_valid_correlation() {
use crank_observability::{current_request_correlation, with_request_correlation};
let request_id = "request-123";
let trace_id = "1234567890abcdef1234567890abcdef";
for (request, trace) in [("", ""), (request_id, ""), ("", trace_id)] {
with_request_correlation(request.into(), trace.into(), async {
assert_eq!(current_request_correlation(), (None, None));
assert_eq!(
jsonrpc_error(Value::Null, -32602, "error"),
json!({"jsonrpc": "2.0", "id": null,
"error": {"code": -32602, "message": "error"}})
);
})
.await;
}
with_request_correlation(request_id.into(), trace_id.into(), async {
assert_eq!(
jsonrpc_error(Value::Null, i64::MAX, "Ошибка"),
json!({"jsonrpc": "2.0", "id": null, "error": {
"code": i64::MAX, "message": "Ошибка",
"data": {"request_id": request_id, "trace_id": trace_id}}})
);
assert_eq!(
correlated_error_data(json!({"extension": [null, u64::MAX]})),
json!({"extension": [null, u64::MAX],
"request_id": request_id, "trace_id": trace_id})
);
})
.await;
assert_eq!(current_request_correlation(), (None, None));
}
}
+3 -1
View File
@@ -16,6 +16,8 @@ mod tool_search;
mod transport;
pub use app::{
build_app, build_app_with_artifacts, build_app_with_background_workers,
TaskApplication, TaskApplicationError, TaskPrincipal, TaskWorkerHandler,
VerifiedIdempotentWrite, build_app, build_app_with_artifacts,
build_app_with_artifacts_and_tasks, build_app_with_background_workers,
build_app_with_background_workers_and_limits,
};
@@ -33,6 +33,7 @@ use crate::{
access::credential_allows_security_level,
app::{
AppState, ApprovalPolicyContext, ApprovalPolicyDecision, InvocationRecord,
lifecycle::{Phase, ToolCallLifecycle},
maybe_handle_approval_policy, persist_invocation_for_key, resolve_generated_tool,
resolve_operation_auth, runtime_operation, take_confirmation_token,
},
@@ -140,10 +141,15 @@ pub(crate) enum ToolCallOutcome {
/// Handle one shared Tools command. No wire envelope, `ResponseMode`, `axum::Response`, session or
/// protocol revision crosses this boundary.
///
/// `lifecycle` is the request-scoped progress/cancellation handle of a `tools/call` (Story 3.4);
/// a command that cannot have one (`tools/list`) passes `None`, and the service then simply never
/// produces a progress event.
pub(crate) async fn handle_tools_command(
state: &Arc<AppState>,
context: ServiceContext<'_>,
command: ToolsCommand,
lifecycle: Option<ToolCallLifecycle>,
) -> ToolsOutcome {
// One marker per entered owning Tools service/handler (Story 3.1 section 7.2). It is a
// `#[cfg(test)]`-only, request-scoped counter and has no production effect.
@@ -166,9 +172,9 @@ pub(crate) async fn handle_tools_command(
Err(message) => ToolsOutcome::InvalidParams(message.to_owned()),
}
}
ToolsCommand::Call(call) => {
ToolsOutcome::Call(execute_tool_command(state, &context, call, &catalog).await)
}
ToolsCommand::Call(call) => ToolsOutcome::Call(
execute_tool_command(state, &context, call, &catalog, lifecycle.as_ref()).await,
),
}
}
@@ -178,6 +184,7 @@ async fn execute_tool_command(
context: &ServiceContext<'_>,
command: ToolCallCommand,
catalog: &PublishedAgentCatalog,
lifecycle: Option<&ToolCallLifecycle>,
) -> ToolCallOutcome {
match catalog.tool_selection_policy.mode {
ToolAccessMode::Direct => {
@@ -188,6 +195,7 @@ async fn execute_tool_command(
&command.tool_name,
command.arguments,
command.confirmation_token,
lifecycle,
)
.await
}
@@ -222,6 +230,7 @@ async fn execute_tool_command(
&proxy.name,
arguments,
confirmation_token,
lifecycle,
)
.await
}
@@ -240,6 +249,7 @@ async fn execute_resolved_tool(
tool_name: &str,
arguments: Value,
confirmation_token: Option<String>,
lifecycle: Option<&ToolCallLifecycle>,
) -> ToolCallOutcome {
let correlation = context.correlation;
let transport_request_id = correlation.request_id().as_str();
@@ -250,6 +260,10 @@ async fn execute_resolved_tool(
Some(resolved) => {
StageOutcome::Success.record(&resolve_span);
drop(resolve_span);
// First real execution step: the addressed tool of the published catalog is resolved.
if let Some(lifecycle) = lifecycle {
lifecycle.advance(Phase::Resolved);
}
resolved.tool
}
None => {
@@ -349,8 +363,14 @@ async fn execute_resolved_tool(
let resolved_auth =
resolve_operation_auth(state, &tool.workspace_id, &operation.execution_config).await;
// A cancellation can only stop the call before it reaches its terminal outcome: nothing is
// persisted for a stopped call, so Invocation History can never claim a false `completed`.
let result = match resolved_auth {
Ok(resolved_auth) => {
// Second real step: the operation's own authorization is resolved, so the runtime may run.
if let Some(lifecycle) = lifecycle {
lifecycle.advance(Phase::Authorized);
}
match RuntimeExecutionRequest::try_new(
&tool.workspace_id,
ExecutionOrigin::AgentSnapshot,
@@ -365,7 +385,15 @@ async fn execute_resolved_tool(
operation.execution_config.timeout_ms.max(1),
),
) {
Ok(request) => state.runtime.execute_outcome(request).await,
Ok(request) => {
// Third real step: the request is about to enter the runtime, so from here on an
// outbound side effect may happen and a cancellation can no longer promise that
// nothing was sent.
if let Some(lifecycle) = lifecycle {
lifecycle.advance(Phase::Dispatching);
}
state.runtime.execute_outcome(request).await
}
Err(_) => Err(ExecutionFailure::new(
ExecutionErrorCode::RuntimeInternal,
correlation.clone(),
@@ -0,0 +1,151 @@
//! Generic SDK envelopes for routing after the authoritative Crank admission guard.
use rmcp::model::{JsonRpcNotification, JsonRpcRequest};
use serde_json::{Value, json};
#[derive(Debug)]
pub(crate) enum DispatchEnvelope {
Request(JsonRpcRequest<Value>),
Notification(JsonRpcNotification<Value>),
}
impl DispatchEnvelope {
pub(crate) fn method(&self) -> Option<&str> {
let payload = match self {
Self::Request(envelope) => &envelope.request,
Self::Notification(envelope) => &envelope.notification,
};
payload.get("method").and_then(Value::as_str)
}
pub(crate) fn is_request(&self) -> bool {
matches!(self, Self::Request(_))
}
pub(crate) fn is_notification(&self) -> bool {
matches!(self, Self::Notification(_))
}
}
/// Call only after `validate_jsonrpc_envelope`; responses keep their existing non-SDK path.
/// Params and extensions stay in the raw message, not in this routing-only projection.
pub(crate) fn decode_dispatch_envelope(
message: &Value,
) -> Result<Option<DispatchEnvelope>, serde_json::Error> {
let Some(method) = message.get("method") else {
return Ok(None);
};
let mut projection = json!({"jsonrpc": message.get("jsonrpc"), "method": method});
let envelope = if let Some(id) = message.get("id") {
// SDK ids are string/i64; the surrogate must never leave this routing-only envelope.
projection["id"] = if id.is_number() && id.as_i64().is_none() {
json!(0)
} else {
id.clone()
};
DispatchEnvelope::Request(serde_json::from_value(projection)?)
} else {
DispatchEnvelope::Notification(serde_json::from_value(projection)?)
};
#[cfg(test)]
crate::app::dispatch_observer::sdk_dispatch_envelope_decoded();
Ok(Some(envelope))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn arbitrary_method_ignores_params_and_mixed_extra_fields() {
for params in [
None,
Some(json!({})),
Some(json!({
"_meta": {"clientInfo": false}, "extension": [null, 3]
})),
] {
let mut message = json!({
"jsonrpc": "2.0", "id": "request", "method": "private/arbitrary",
"result": false, "error": [1], "extension": {"anything": true}
});
if let Some(params) = params {
message["params"] = params;
}
let original = message.clone();
let envelope = decode_dispatch_envelope(&message)
.expect("admitted request decodes")
.expect("request envelope");
assert_eq!(envelope.method(), Some("private/arbitrary"));
assert!(envelope.is_request());
assert!(!envelope.is_notification());
let DispatchEnvelope::Request(request) = envelope else {
panic!("request must retain its SDK variant");
};
assert_eq!(request.request, json!({"method": "private/arbitrary"}));
assert_eq!(message, original);
}
}
#[test]
fn request_ids_preserve_raw_value_and_only_surrogate_sdk_incompatible_numbers() {
for (id, sdk_id) in [
(json!("string-id"), json!("string-id")),
(json!(i64::MIN), json!(i64::MIN)),
(json!(i64::MAX), json!(i64::MAX)),
(json!(u64::MAX), json!(0)),
(json!(1.25), json!(0)),
(json!(1.0), json!(0)),
] {
let message = json!({"jsonrpc": "2.0", "id": id, "method": "arbitrary"});
let original = message.clone();
let DispatchEnvelope::Request(request) = decode_dispatch_envelope(&message)
.expect("admitted id decodes")
.expect("request envelope")
else {
panic!("id-bearing method must be a request");
};
assert_eq!(serde_json::to_value(request.id).expect("SDK id"), sdk_id);
assert_eq!(message, original);
assert_eq!(message.to_string(), original.to_string());
}
}
#[test]
fn notification_kind_is_independent_of_method_and_ignored_fields() {
let message = json!({
"jsonrpc": "2.0", "method": "private/arbitrary", "params": {},
"result": {}, "error": {}, "extension": 42
});
let original = message.clone();
let envelope = decode_dispatch_envelope(&message)
.expect("notification decodes")
.expect("notification envelope");
assert_eq!(envelope.method(), Some("private/arbitrary"));
assert!(!envelope.is_request());
assert!(envelope.is_notification());
let DispatchEnvelope::Notification(notification) = envelope else {
panic!("notification must retain its SDK variant");
};
assert_eq!(
notification.notification,
json!({"method": "private/arbitrary"})
);
assert_eq!(message, original);
}
#[test]
fn incoming_success_and_error_responses_are_not_migrated() {
for message in [
json!({"jsonrpc": "2.0", "id": 1, "result": {}}),
json!({"jsonrpc": "2.0", "id": "id", "error": {"code": -1, "message": "error"}}),
json!({"jsonrpc": "2.0", "error": {"code": -1, "message": "error"}}),
] {
assert!(
decode_dispatch_envelope(&message)
.expect("response bypass")
.is_none()
);
}
}
}
@@ -0,0 +1,129 @@
//! Typed negotiation results; admission and session lifecycle remain in the HTTP adapter.
use rmcp::model::{
CacheScope, DiscoverResult, Implementation, InitializeRequestParams, InitializeResult,
PingRequest, ProtocolVersion, ServerCapabilities, ServerResult,
};
use serde_json::Value;
use super::advertised_server_capabilities;
use crate::jsonrpc::SUPPORTED_PROTOCOL_VERSIONS;
pub(crate) fn initialize_params(
params: &Value,
) -> Result<InitializeRequestParams, serde_json::Error> {
// Only protocolVersion was validated here; ignored client fields must not acquire SDK constraints.
serde_json::from_value(serde_json::json!({
"protocolVersion": params.get("protocolVersion"),
"capabilities": {},
"clientInfo": {"name": "", "version": ""}
}))
}
fn protocol_version(revision: &str) -> ProtocolVersion {
serde_json::from_value(Value::String(revision.to_owned()))
.expect("served protocol revisions are strings")
}
fn capabilities(advertised: &Value) -> ServerCapabilities {
serde_json::from_value(advertised.clone())
.expect("registry capability fragments match SDK capability shapes")
}
fn restore_registry_extensions(result: &mut Value, advertised: Value) {
let capabilities = result["capabilities"]
.as_object_mut()
.expect("SDK server capabilities serialize as an object");
// SDK ServerCapabilities has no flattened map for extra registry capability names.
for (name, settings) in advertised
.as_object()
.expect("registry capabilities are an object")
{
capabilities
.entry(name.clone())
.or_insert_with(|| settings.clone());
}
}
pub(crate) fn initialized_notification(message: &Value) -> rmcp::model::ClientNotification {
// Admission already checked the envelope; ignored params must not acquire SDK constraints.
let notification: rmcp::model::InitializedNotification =
serde_json::from_value(serde_json::json!({"method": message["method"]}))
.expect("registry dispatched a validated initialized notification");
#[cfg(test)]
crate::app::dispatch_observer::sdk_initialized_notification_decoded(&notification);
notification.into()
}
pub(crate) fn ping_result(message: &Value, revision: &str) -> Value {
// Ping params were ignored after admission; do not add SDK constraints to extensions.
let request: PingRequest = serde_json::from_value(serde_json::json!({
"method": message["method"]
}))
.expect("registry dispatched a validated ping request");
#[cfg(test)]
crate::app::dispatch_observer::sdk_ping_request_consumed(&request);
let result = match rmcp::model::ClientRequest::from(request) {
rmcp::model::ClientRequest::PingRequest(_) => ServerResult::empty(()),
_ => unreachable!("typed ping can only dispatch ping"),
};
let mut result = serde_json::to_value(result).expect("SDK ping result is serializable");
#[cfg(test)]
crate::app::dispatch_observer::sdk_ping_result_serialized();
// Crank keeps the admitted/session revision in addition to the SDK's empty ping result.
result["protocolVersion"] = Value::String(revision.to_owned());
result
}
pub(crate) fn initialize_result(revision: &str, name: &str, version: &str) -> Value {
let advertised = advertised_server_capabilities(revision);
let result = InitializeResult::new(capabilities(&advertised))
.with_protocol_version(protocol_version(revision))
.with_server_info(Implementation::new(name, version));
let mut result = serde_json::to_value(result)
.expect("SDK initialize result with registry capabilities is serializable");
#[cfg(test)]
crate::app::dispatch_observer::sdk_initialize_result_serialized();
restore_registry_extensions(&mut result, advertised);
result
}
pub(crate) fn discover_result(revision: &str, name: &str, version: &str) -> Value {
let advertised = advertised_server_capabilities(revision);
let result = DiscoverResult::new(
SUPPORTED_PROTOCOL_VERSIONS
.iter()
.map(|revision| protocol_version(revision))
.collect(),
capabilities(&advertised),
)
.with_server_info(Implementation::new(name, version))
.with_ttl_ms(0)
.with_cache_scope(CacheScope::Private);
let mut result = serde_json::to_value(result)
.expect("SDK discovery result with registry capabilities is serializable");
#[cfg(test)]
crate::app::dispatch_observer::sdk_discover_result_serialized();
restore_registry_extensions(&mut result, advertised);
result
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn lifecycle_sdk_retains_extra_registry_capabilities_without_replacing_sdk_fields() {
let advertised =
json!({"tools":{"listChanged":false}, "inert_test_capability":{"enabled":true}});
let mut result = serde_json::to_value(InitializeResult::new(capabilities(&advertised)))
.expect("typed result serializes");
assert_eq!(
result["capabilities"],
json!({"tools":{"listChanged":false}})
);
restore_registry_extensions(&mut result, advertised.clone());
assert_eq!(result["capabilities"], advertised);
}
}
@@ -0,0 +1,426 @@
//! Story 3.18: safe MCP server logging (FR-19).
//!
//! Scope of this module is the server side of the pinned `logging` capability of the two approved
//! revisions, and nothing else:
//!
//! * `2025-11-25` (legacy session era) declares `logging/setLevel` as a client request with one
//! `level` of the pinned `LoggingLevel` union, and `logging?: object` in `ServerCapabilities`.
//! The request sets the severity a client wants to receive for its own session.
//! * `2026-07-28` (modern stateless era) has no `logging/setLevel` method at all: SEP-2577 replaces
//! it with the per-request `_meta["io.modelcontextprotocol/logLevel"]`, and the schema says that
//! when that key is absent "the server MUST NOT send any notifications/message notifications for
//! this request". The `logging` capability and `notifications/message` themselves stay in the
//! pinned schema as deprecated for at least twelve months.
//!
//! The answer of this story is therefore the narrow one the contract offers: an opt-in level that
//! belongs to the current negotiated client context (one legacy session, or one modern request —
//! never a process-global level shared by every agent), and `notifications/message` events that are
//! built **only** from the closed safe vocabulary below.
//!
//! Redaction is structural, not a filter: no request payload, credential, internal error cause,
//! Prompt/Resource/Tool name, URI, argument value or any other user-supplied value is ever accepted
//! by this module, so none of them can reach the wire. A `notifications/message` body is exactly
//! `{"level": <severity of the event>, "logger": "crank-mcp-server", "data": {"event": <closed name>,
//! "outcome": <closed label>}}` — every member of it is a `&'static str` from this file.
//!
//! Bounds (issue requirement: bounded frequency/queues/bytes, logging never blocks execution and
//! never replaces the audit trail):
//! * at most one event per response is recorded, and at most [`MAX_PENDING_NOTIFICATIONS`] are
//! drained into one response — a fixed, per-response frequency bound;
//! * every event has a fixed size and no variable input, so the byte bound follows from the count
//! bound without a separate accounting path;
//! * recording is a bounded in-memory push that drops the event when the buffer is full, the active
//! level is unset, or the task has no request scope. No I/O, no lock acquisition that can wait, and
//! no error path: logging can never fail or slow down the request it describes.
//! * the legacy opt-in table is a bounded in-memory map read/written with `try_lock` only.
use std::{
cell::RefCell,
collections::HashMap,
future::Future,
sync::{Mutex, MutexGuard},
};
// Both pinned revisions still require logging despite the SDK's SEP-2577 deprecation.
#[allow(deprecated)]
use rmcp::model::{LoggingLevel, LoggingMessageNotificationParam, SetLevelRequestParams};
use serde::Deserialize;
use serde_json::{Value, json};
use tokio::task_local;
/// The pinned `LoggingLevel` union: RFC 5424 severities, ordered from the most verbose to the most
/// severe. The ordering is the filtering contract — a client is sent the events at or above its
/// level.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum LogLevel {
Debug,
Info,
Notice,
Warning,
Error,
Critical,
Alert,
Emergency,
}
impl LogLevel {
/// The exact wire literal of this level (contract assertions only; runtime uses SDK serde).
#[cfg(test)]
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Debug => "debug",
Self::Info => "info",
Self::Notice => "notice",
Self::Warning => "warning",
Self::Error => "error",
Self::Critical => "critical",
Self::Alert => "alert",
Self::Emergency => "emergency",
}
}
/// Exact parse of the eight pinned literals.
///
/// Nothing is coerced: no case folding, no aliases (`warn`), no numeric severities. A value that
/// is not one of the pinned strings is malformed, which is what the pinned `LoggingLevel` type
/// says and what `InvalidParamsError` is for.
#[allow(deprecated)]
pub(crate) fn parse(value: &str) -> Option<Self> {
if value.len() > MAX_LEVEL_BYTES {
return None;
}
let level: LoggingLevel = serde_json::from_value(Value::String(value.to_owned())).ok()?;
#[cfg(test)]
crate::app::dispatch_observer::sdk_logging_level_parsed();
Some(level.into())
}
}
// Keep domain severity ordering independent of SDK enum discriminants (which have no Ord).
#[allow(deprecated)]
impl From<LoggingLevel> for LogLevel {
fn from(level: LoggingLevel) -> Self {
match level {
LoggingLevel::Debug => Self::Debug,
LoggingLevel::Info => Self::Info,
LoggingLevel::Notice => Self::Notice,
LoggingLevel::Warning => Self::Warning,
LoggingLevel::Error => Self::Error,
LoggingLevel::Critical => Self::Critical,
LoggingLevel::Alert => Self::Alert,
LoggingLevel::Emergency => Self::Emergency,
}
}
}
#[allow(deprecated)]
impl From<LogLevel> for LoggingLevel {
fn from(level: LogLevel) -> Self {
match level {
LogLevel::Debug => Self::Debug,
LogLevel::Info => Self::Info,
LogLevel::Notice => Self::Notice,
LogLevel::Warning => Self::Warning,
LogLevel::Error => Self::Error,
LogLevel::Critical => Self::Critical,
LogLevel::Alert => Self::Alert,
LogLevel::Emergency => Self::Emergency,
}
}
}
/// Bound of `params.level` before it is compared: the longest pinned literal is nine bytes, so a
/// value beyond this cannot be a level and must not be scanned further.
pub(crate) const MAX_LEVEL_BYTES: usize = 16;
/// The one safe detail of a malformed `logging/setLevel`: it never echoes the offending value, so an
/// oversized or hostile value cannot be reflected back or widened into an error payload.
pub(crate) const INVALID_LEVEL: &str = "invalid logging level";
/// Notifications drained into one response (frequency bound).
pub(crate) const MAX_PENDING_NOTIFICATIONS: usize = 4;
/// Bound of the in-memory legacy opt-in table (memory bound; see [`LogLevelStore`]).
pub(crate) const MAX_TRACKED_SESSIONS: usize = 1_024;
/// The fixed logger name carried by every notification. It is a server constant, never a
/// client-supplied or catalog-supplied value.
const LOGGER_NAME: &str = "crank-mcp-server";
/// Closed safe vocabulary: the server served the request.
pub(crate) const EVENT_SERVED: &str = "mcp.request.served";
/// Closed safe vocabulary: the request was rejected as invalid before any service work.
pub(crate) const EVENT_REJECTED: &str = "mcp.request.rejected";
/// Closed safe vocabulary: the request failed on the server side.
pub(crate) const EVENT_FAILED: &str = "mcp.request.failed";
/// Closed safe vocabulary: this client set its logging level.
pub(crate) const EVENT_LEVEL_APPLIED: &str = "mcp.logging.level_applied";
/// The `params` of one `logging/setLevel` request as the legacy adapter parses it from the wire form.
///
/// Only the single pinned member exists here: an unknown member is ignored by serde, and a `level`
/// that is not a string is a deserialization error, which the adapter answers with the same safe
/// [`INVALID_LEVEL`] detail instead of a serde diagnostic.
#[derive(Debug, Deserialize)]
pub(crate) struct SetLevelCommand {
pub(crate) level: String,
}
impl SetLevelCommand {
/// The bounded input contract of AC2: the level is length-bounded and compared exactly, so a
/// malformed or oversized value is refused with bounded work and one safe detail.
#[allow(deprecated)]
pub(crate) fn validate(&self) -> Result<SetLevelRequestParams, &'static str> {
if self.level.is_empty() || self.level.len() > MAX_LEVEL_BYTES {
return Err(INVALID_LEVEL);
}
// Only level was part of the old command: unknown members (including _meta) stay ignored.
let request =
serde_json::from_value(json!({"level": self.level})).map_err(|_| INVALID_LEVEL)?;
#[cfg(test)]
{
crate::app::dispatch_observer::sdk_logging_level_parsed();
crate::app::dispatch_observer::sdk_logging_dispatch_enter();
}
Ok(request)
}
}
/// A neutral outcome of the shared `logging/setLevel` service.
pub(crate) enum LoggingOutcome {
/// The level of this client context was accepted.
Applied(LogLevel),
/// Malformed or oversized level: one safe detail, no echoed input.
InvalidParams(&'static str),
}
/// One recorded event of the closed vocabulary.
#[derive(Debug, Clone, Copy)]
struct SafeEvent {
level: LogLevel,
event: &'static str,
outcome: &'static str,
}
/// The request-scoped logging state: the level this client opted in to, whether the client offered
/// the SSE response mode that is the only carrier of a server notification, and the bounded events
/// this response will carry.
#[derive(Default)]
struct RequestLog {
active: Option<LogLevel>,
sse_acceptable: bool,
pending: Vec<SafeEvent>,
}
task_local! {
/// Per-request logging scope. Installed by the HTTP entry point of the MCP endpoint, so two
/// concurrently served requests never share a level or a buffer. Code outside such a scope
/// (unit tests, background workers) sees a no-op.
static REQUEST_LOG: RefCell<RequestLog>;
}
/// Run one request inside its own logging scope.
pub(crate) async fn scoped<F: Future>(future: F) -> F::Output {
REQUEST_LOG
.scope(RefCell::new(RequestLog::default()), future)
.await
}
/// Activate (or clear) the level of the current client context. Outside a scope this is a no-op.
pub(crate) fn set_active_level(level: Option<LogLevel>) {
let _ = REQUEST_LOG.try_with(|log| {
if let Ok(mut log) = log.try_borrow_mut() {
log.active = level;
}
});
}
/// The request's logging context, carried into a streamed response body (Story 3.4).
///
/// A streamed `tools/call` response is assembled after the request handler returned, so its terminal
/// event would be projected outside the task-local scope of the request. This snapshot carries the
/// only two facts the projection needs — the level the client opted in to and whether the request
/// offered the SSE carrier — and never a request value.
#[derive(Clone, Copy, Default)]
pub(crate) struct CarriedScope {
active: Option<LogLevel>,
sse_acceptable: bool,
}
/// Snapshot the scope of the request that is being served now. Outside a scope the snapshot is
/// "not opted in", which delivers nothing.
pub(crate) fn carry_scope() -> CarriedScope {
REQUEST_LOG
.try_with(|log| {
let log = log.borrow();
CarriedScope {
active: log.active,
sse_acceptable: log.sse_acceptable,
}
})
.unwrap_or_default()
}
/// Run one step of the streamed response inside a scope carrying the request's logging context.
pub(crate) async fn in_carried_scope<F: Future>(scope: CarriedScope, future: F) -> F::Output {
REQUEST_LOG
.scope(
RefCell::new(RequestLog {
active: scope.active,
sse_acceptable: scope.sse_acceptable,
pending: Vec::new(),
}),
future,
)
.await
}
/// Remember that this request offered `text/event-stream`, the only carrier of a notification.
pub(crate) fn set_sse_acceptable() {
let _ = REQUEST_LOG.try_with(|log| {
if let Ok(mut log) = log.try_borrow_mut() {
log.sse_acceptable = true;
}
});
}
/// Record one event of the closed vocabulary for the level this client opted in to.
///
/// Everything below the active level, every event without an active level (the client did not opt
/// in), and every event beyond the fixed buffer bound is dropped silently: the caller never branches
/// on logging.
pub(crate) fn note(level: LogLevel, event: &'static str, outcome: &'static str) {
let _ = REQUEST_LOG.try_with(|log| {
let Ok(mut log) = log.try_borrow_mut() else {
return;
};
let Some(active) = log.active else {
return;
};
if level < active || log.pending.len() >= MAX_PENDING_NOTIFICATIONS {
return;
}
log.pending.push(SafeEvent {
level,
event,
outcome,
});
});
}
/// The pending events as wire `notifications/message` bodies, and whether this request can carry
/// them at all. Draining is the last step of the response projection, so a notification can never be
/// delivered twice or survive into another request.
pub(crate) fn take_notifications() -> Vec<Value> {
REQUEST_LOG
.try_with(|log| {
let Ok(mut log) = log.try_borrow_mut() else {
return Vec::new();
};
if !log.sse_acceptable {
// A JSON body has no place for a server notification: the event is not delivered
// instead of being smuggled into the result object.
log.pending.clear();
return Vec::new();
}
std::mem::take(&mut log.pending)
.into_iter()
.filter_map(|event| {
#[allow(deprecated)]
let params = LoggingMessageNotificationParam::new(
event.level.into(),
json!({"event": event.event, "outcome": event.outcome}),
)
.with_logger(LOGGER_NAME);
// Logging remains best-effort: a serialization failure drops only this event.
let params = serde_json::to_value(params).ok()?;
#[cfg(test)]
crate::app::dispatch_observer::sdk_logging_notification_serialized();
Some(json!({
"jsonrpc": "2.0",
"method": "notifications/message",
"params": params,
}))
})
.collect()
})
.unwrap_or_default()
}
/// The per-session opt-in of the legacy era.
///
/// The login belongs to the session the client negotiated, so a level set by one agent's client can
/// never apply to another. It is deliberately **in-memory only**: persisting it would need a schema
/// migration of the transport session store, and the deprecated capability does not justify that.
///
/// shortcut: the table is per process — a level set on one replica is not seen by another replica
/// serving the same session, and a restart clears it; upgrade to the session store if a durable or
/// cross-replica level is ever required.
#[derive(Default)]
pub(crate) struct LogLevelStore {
levels: Mutex<HashMap<String, LogLevel>>,
}
impl LogLevelStore {
pub(crate) fn new() -> Self {
Self::default()
}
/// Store the level of one session. Never waits and never fails: on lock contention the update is
/// skipped rather than blocking the request.
pub(crate) fn set(&self, session_id: &str, level: LogLevel) {
let Some(mut levels) = self.lock() else {
return;
};
if levels.len() >= MAX_TRACKED_SESSIONS && !levels.contains_key(session_id) {
// Bounded table: the oldest opt-ins are dropped instead of growing without a limit. A
// dropped client simply stops receiving notifications until it sets its level again.
levels.clear();
}
levels.insert(session_id.to_owned(), level);
}
/// The level of one session, if this process still remembers it.
pub(crate) fn get(&self, session_id: &str) -> Option<LogLevel> {
self.lock()?.get(session_id).copied()
}
fn lock(&self) -> Option<MutexGuard<'_, HashMap<String, LogLevel>>> {
self.levels.try_lock().ok()
}
}
/// Serve one `logging/setLevel` request of the legacy era (AC1/AC2).
///
/// The command has already been parsed from the wire form, so this function only validates and
/// records; it never reads the registry, the catalog or any stored value.
pub(crate) fn handle_logging_command(
store: &LogLevelStore,
session_id: &str,
command: SetLevelCommand,
) -> LoggingOutcome {
match command.validate() {
Ok(request) => {
#[allow(deprecated)]
let level = request.level.into();
store.set(session_id, level);
LoggingOutcome::Applied(level)
}
Err(detail) => LoggingOutcome::InvalidParams(detail),
}
}
/// The level a modern request asked for in `_meta`.
///
/// The pinned schema says the server must not send `notifications/message` for a request that does
/// not carry the key, so an absent, non-string or unknown value is simply "not opted in": it is not
/// an error of the business request and never widens the accepted vocabulary.
pub(crate) fn modern_request_level(message: &Value) -> Option<LogLevel> {
let meta = crate::jsonrpc::params(message).get("_meta")?.clone();
let value = meta.get(crate::jsonrpc::META_LOG_LEVEL_KEY)?.as_str()?;
LogLevel::parse(value)
}
#[cfg(test)]
#[path = "logging_tests.rs"]
mod tests;
@@ -0,0 +1,320 @@
//! Unit contract of Story 3.18: the pinned level vocabulary, the bounded input contract of
//! `logging/setLevel`, the request-scoped bounded event buffer and the closed safe notification
//! shape.
use serde_json::json;
use super::*;
/// Parse one `logging/setLevel` params object and validate its level, or `None` when the shape itself
/// is malformed (which the adapter answers with the same safe detail).
fn validate(level: &str) -> Option<Result<LogLevel, &'static str>> {
let command: SetLevelCommand = serde_json::from_value(json!({ "level": level })).ok()?;
#[allow(deprecated)]
Some(command.validate().map(|request| request.level.into()))
}
#[test]
fn every_pinned_level_is_accepted_exactly_once_and_in_severity_order() {
// The pinned `LoggingLevel` union of both revisions, most verbose first.
let pinned = [
("debug", LogLevel::Debug),
("info", LogLevel::Info),
("notice", LogLevel::Notice),
("warning", LogLevel::Warning),
("error", LogLevel::Error),
("critical", LogLevel::Critical),
("alert", LogLevel::Alert),
("emergency", LogLevel::Emergency),
];
for (index, (literal, expected)) in pinned.iter().enumerate() {
assert_eq!(
LogLevel::parse(literal),
Some(*expected),
"{literal} is a pinned level"
);
assert_eq!(
expected.as_str(),
*literal,
"the wire literal of a level is exactly the pinned string"
);
assert_eq!(
*expected as usize, index,
"the severity ordering of {literal} must follow the RFC 5424 order"
);
assert_eq!(
validate(literal),
Some(Ok(*expected)),
"{literal} must be accepted through the wire shape too"
);
}
assert!(
pinned.windows(2).all(|pair| pair[0].1 < pair[1].1),
"severity must be strictly increasing so the filter is `event >= level`"
);
}
#[test]
fn nothing_outside_the_pinned_union_is_a_level() {
for value in [
"",
"warn",
"WARN",
"Info",
"information",
" debug",
"debug ",
"0",
"7",
"emergency!",
"debug\u{0}",
"информация",
] {
assert_eq!(
LogLevel::parse(value),
None,
"{value:?} is not one of the pinned literals and must never be coerced into one"
);
assert_eq!(
validate(value),
Some(Err(INVALID_LEVEL)),
"a value outside the union is a bounded invalid-params detail, got {value:?}"
);
}
}
#[test]
fn malformed_level_shapes_share_one_safe_detail() {
// A `level` the schema types as a string, sent as something else: a deserialization error that the
// adapter answers with the same safe detail. It never echoes what the client sent.
for value in [json!(7), json!(1.5), json!(0)] {
assert!(
serde_json::from_value::<SetLevelCommand>(json!({ "level": value })).is_err(),
"a numeric level is not a pinned LoggingLevel"
);
}
for value in [json!(null), json!({}), json!(["info"]), json!(true)] {
assert!(
serde_json::from_value::<SetLevelCommand>(json!({ "level": value })).is_err(),
"a non-string level is not a pinned LoggingLevel"
);
}
assert!(
serde_json::from_value::<SetLevelCommand>(json!({})).is_err(),
"the pinned request requires `level`"
);
// Control: the same safe detail is what an unknown string produces, so both malformed shapes are
// indistinguishable on the wire and neither discloses what was sent.
assert_eq!(validate("verbose"), Some(Err(INVALID_LEVEL)));
assert!(
INVALID_LEVEL.len() < 64,
"the detail stays a fixed short text"
);
}
#[test]
fn an_oversized_level_is_refused_before_any_comparison() {
let oversized = "i".repeat(MAX_LEVEL_BYTES + 1);
assert_eq!(validate(&oversized), Some(Err(INVALID_LEVEL)));
// Positive control: the bound is not so tight that a pinned literal is refused.
assert_eq!(validate("emergency"), Some(Ok(LogLevel::Emergency)));
}
#[test]
fn the_session_opt_in_is_per_session_and_bounded() {
let store = LogLevelStore::new();
store.set("session-a", LogLevel::Error);
store.set("session-b", LogLevel::Debug);
assert_eq!(store.get("session-a"), Some(LogLevel::Error));
assert_eq!(store.get("session-b"), Some(LogLevel::Debug));
assert_eq!(
store.get("session-c"),
None,
"a session that never opted in must not inherit another session's level"
);
// The table stays bounded: filling it past the cap drops the previous opt-ins instead of growing.
for index in 0..MAX_TRACKED_SESSIONS {
store.set(&format!("filler-{index}"), LogLevel::Info);
}
store.set("session-after-cap", LogLevel::Alert);
assert_eq!(store.get("session-after-cap"), Some(LogLevel::Alert));
}
#[tokio::test]
async fn an_event_is_delivered_only_at_or_above_the_opted_in_level() {
// One closed-vocabulary event per severity, recorded inside a request scope that offered SSE.
for (active, expected) in [
(LogLevel::Debug, 4usize),
(LogLevel::Info, 4),
(LogLevel::Notice, 3),
(LogLevel::Warning, 2),
(LogLevel::Error, 1),
(LogLevel::Critical, 0),
(LogLevel::Alert, 0),
(LogLevel::Emergency, 0),
] {
let delivered = scoped(async move {
set_active_level(Some(active));
set_sse_acceptable();
note(LogLevel::Info, EVENT_SERVED, "ok");
note(LogLevel::Notice, EVENT_LEVEL_APPLIED, "applied");
note(LogLevel::Warning, EVENT_REJECTED, "invalid_params");
note(LogLevel::Error, EVENT_FAILED, "internal");
take_notifications()
})
.await;
assert_eq!(
delivered.len(),
expected,
"level {} must deliver exactly the events at or above it",
active.as_str()
);
}
}
#[tokio::test]
async fn without_an_opt_in_nothing_is_even_recorded() {
// Outside a scope (no request, e.g. a background worker): a no-op.
note(LogLevel::Info, EVENT_SERVED, "ok");
assert!(take_notifications().is_empty());
// Inside a scope without an opted-in level.
let no_level = scoped(async {
set_sse_acceptable();
note(LogLevel::Error, EVENT_FAILED, "internal");
take_notifications()
})
.await;
assert!(
no_level.is_empty(),
"the pinned modern schema forbids notifications for a client that did not opt in"
);
}
#[tokio::test]
async fn a_level_is_present_but_the_client_offered_no_stream() {
let delivered = scoped(async {
set_active_level(Some(LogLevel::Debug));
note(LogLevel::Error, EVENT_FAILED, "internal");
take_notifications()
})
.await;
assert!(
delivered.is_empty(),
"a JSON body has no carrier for notifications/message; the event is dropped, not smuggled in"
);
}
#[tokio::test]
async fn the_buffer_is_bounded_and_drains_once() {
let (first, second) = scoped(async {
set_active_level(Some(LogLevel::Debug));
set_sse_acceptable();
for _ in 0..(MAX_PENDING_NOTIFICATIONS * 4) {
note(LogLevel::Info, EVENT_SERVED, "ok");
}
let first = take_notifications();
let second = take_notifications();
(first, second)
})
.await;
assert_eq!(
first.len(),
MAX_PENDING_NOTIFICATIONS,
"the per-response frequency bound"
);
assert!(
second.is_empty(),
"a delivered event must not survive into a later response"
);
}
#[tokio::test]
async fn the_notification_body_is_exactly_the_closed_safe_shape() {
let delivered = scoped(async {
set_active_level(Some(LogLevel::Debug));
set_sse_acceptable();
note(LogLevel::Warning, EVENT_REJECTED, "invalid_params");
take_notifications()
})
.await;
assert_eq!(
delivered,
vec![json!({
"jsonrpc": "2.0",
"method": "notifications/message",
"params": {
"level": "warning",
"logger": "crank-mcp-server",
"data": {"event": "mcp.request.rejected", "outcome": "invalid_params"}
}
})],
"`params` carries only the severity, the fixed logger name and the closed data object"
);
// A notification has no `id`, so it can never be mistaken for a response.
assert!(delivered[0].get("id").is_none());
}
#[test]
fn the_modern_level_is_read_from_the_pinned_meta_key_only() {
let modern = |meta: Value| json!({"jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {"_meta": meta}});
assert_eq!(
modern_request_level(&modern(
json!({ "io.modelcontextprotocol/logLevel": "error" })
)),
Some(LogLevel::Error)
);
// Not opted in: an absent key, a non-string value and an unknown literal are all "no level".
assert_eq!(modern_request_level(&modern(json!({}))), None);
assert_eq!(
modern_request_level(&modern(json!({ "io.modelcontextprotocol/logLevel": 7 }))),
None
);
assert_eq!(
modern_request_level(&modern(
json!({ "io.modelcontextprotocol/logLevel": "verbose" })
)),
None
);
// A level under another key is not the pinned carrier.
assert_eq!(
modern_request_level(&modern(json!({ "logLevel": "error" }))),
None
);
assert_eq!(
modern_request_level(&json!({"jsonrpc": "2.0", "id": 1, "method": "tools/list"})),
None
);
}
#[test]
fn the_command_records_the_level_of_its_own_session_only() {
let store = LogLevelStore::new();
store.set("other-session", LogLevel::Debug);
// Unknown members were ignored by the legacy command; SDK metadata must not narrow that wire.
let command: SetLevelCommand = serde_json::from_value(json!({
"level": "critical", "_meta": 7, "unknown": {"private": "ignored"}
}))
.expect("a pinned level parses regardless of unknown members");
let outcome = handle_logging_command(&store, "this-session", command);
assert!(matches!(
outcome,
LoggingOutcome::Applied(LogLevel::Critical)
));
assert_eq!(store.get("this-session"), Some(LogLevel::Critical));
assert_eq!(
store.get("other-session"),
Some(LogLevel::Debug),
"setting one session's level must never change another client's level"
);
// A malformed level changes nothing.
let malformed: SetLevelCommand =
serde_json::from_value(json!({ "level": "verbose" })).expect("the shape parses");
assert!(matches!(
handle_logging_command(&store, "this-session", malformed),
LoggingOutcome::InvalidParams(INVALID_LEVEL)
));
assert_eq!(store.get("this-session"), Some(LogLevel::Critical));
}
+23 -18
View File
@@ -26,15 +26,33 @@ use crate::transport::{json_response, protocol_version_from_headers, singleton_h
mod command;
mod completion;
mod dispatch_sdk;
mod lifecycle_sdk;
mod logging;
mod prompts;
mod registry;
mod resources;
mod signals;
pub(crate) use dispatch_sdk::decode_dispatch_envelope;
pub(crate) use command::{
ContinuationSupport, ServiceContext, ToolCallCommand, ToolCallOutcome, ToolsCommand,
ToolsListCommand, ToolsOutcome, handle_tools_command,
};
pub(crate) use completion::{CompletionCommand, CompletionOutcome, handle_completion_command};
pub(crate) use completion::{
CompletionArgument, CompletionCommand, CompletionOutcome, CompletionReference,
handle_completion_command,
};
pub(crate) use lifecycle_sdk::{
discover_result, initialize_params, initialize_result, initialized_notification, ping_result,
};
pub(crate) use logging::{
CarriedScope, EVENT_FAILED, EVENT_LEVEL_APPLIED, EVENT_REJECTED, EVENT_SERVED, INVALID_LEVEL,
LogLevel, LogLevelStore, LoggingOutcome, SetLevelCommand, carry_scope, handle_logging_command,
in_carried_scope, modern_request_level, note, scoped as logging_scope, set_active_level,
set_sse_acceptable, take_notifications,
};
pub(crate) use prompts::{
PromptGetCommand, PromptsCommand, PromptsListCommand, PromptsOutcome, handle_prompts_command,
};
@@ -42,6 +60,10 @@ pub(crate) use resources::{
ResourceReadCommand, ResourceTemplatesListError, ResourcesCommand, ResourcesListCommand,
ResourcesOutcome, handle_resources_command, list_resource_templates,
};
pub(crate) use signals::{
FilterOutcome, INVALID_LISTEN_FILTER, ListenParams, Signals, StreamRegistry, Watch, Wire,
acknowledged_frame, frames_stream, spawn as spawn_signal_watch, subscription_id,
};
/// Resolved era of an incoming POST request.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -829,20 +851,3 @@ pub(crate) fn resolve_request_identity(
}
}
}
/// The pinned `DiscoverResult` for `server/discover` of the revision the caller serves.
pub(crate) fn discover_result(revision: &str, server_name: &str, server_version: &str) -> Value {
json!({
"resultType": "complete",
"supportedVersions": SUPPORTED_PROTOCOL_VERSIONS,
"capabilities": advertised_server_capabilities(revision),
"_meta": {
crate::jsonrpc::META_SERVER_INFO_KEY: {
"name": server_name,
"version": server_version,
}
},
"ttlMs": 0,
"cacheScope": "private"
})
}
@@ -22,6 +22,20 @@ pub(crate) enum CapabilityId {
Prompts,
/// Bounded argument completion for bound Published Prompts and Resource templates (Story 3.17).
Completions,
/// `subscriptions/listen` of the modern revision: a *client-sent* request that opens the
/// notification stream (Story 3.19). It is a dispatch-only registration without an advertisement
/// fragment because the pinned modern schema has no server `subscriptions` capability object;
/// there is no such method in the legacy revision at all.
Subscriptions,
/// `logging` server capability: safe structured `notifications/message` events of the negotiated
/// client context (Story 3.18). Advertised in both revisions; the pinned modern revision carries
/// the level per request in `_meta` and marks the capability deprecated (SEP-2577).
Logging,
/// `logging/setLevel` as the *legacy* client request that sets a session level. It is a
/// dispatch-only registration without an advertisement fragment because the pinned modern
/// revision has no such method: there the per-request `_meta` key replaces it, so the same wire
/// name is a wrong-era request and must never be served.
LoggingSetLevel,
Ping,
Discovery,
LegacyLifecycle,
@@ -81,9 +95,20 @@ pub(crate) enum MethodHandler {
/// `completion/complete`: bounded suggestions from the typed schema of one exact bound Prompt
/// argument or one exact bound Resource template parameter (Story 3.17).
Completion,
/// `subscriptions/listen`: open the long-lived modern notification stream and deliver the
/// Resource/Prompt list-change signals the client opted in to (Story 3.19).
SubscriptionsListen,
/// `logging/setLevel`: validate the pinned level and record it for this legacy session, so the
/// adapter can deliver safe structured `notifications/message` events of that context
/// (Story 3.18). The handler never widens the accepted vocabulary and never echoes the input.
LoggingSetLevel,
Ping,
Discover,
Lifecycle,
/// `notifications/cancelled`: the legacy client cancels a `tools/call` it already issued
/// (Story 3.4). The handler only routes the id to the owner's in-flight call; it never widens
/// what may be cancelled and never authorizes by the progress token.
Cancelled,
}
#[derive(Clone, Copy, Debug)]
@@ -348,7 +373,9 @@ pub(crate) const CAPABILITY_DESCRIPTORS: &[CapabilityIdDescriptor] = &[
extension: None,
deprecated: None,
sub_features: FeatureSet(&["listChanged"]),
advertised: Some(("tools", &[("listChanged", false)])),
// Story 3.5: the adapter emits `notifications/tools/list_changed` from the committed
// published Tool bindings of the Agent snapshot, so the declaration is now true.
advertised: Some(("tools", &[("listChanged", true)])),
revisions: MODERN_AND_LEGACY,
},
CapabilityIdDescriptor {
@@ -358,7 +385,9 @@ pub(crate) const CAPABILITY_DESCRIPTORS: &[CapabilityIdDescriptor] = &[
extension: None,
deprecated: None,
sub_features: FeatureSet(&["subscribe", "listChanged"]),
advertised: Some(("resources", &[("subscribe", false), ("listChanged", false)])),
// Story 3.19: the server really emits `notifications/resources/list_changed`; URI-content
// subscriptions (`subscribe`) remain unimplemented and are still advertised as false.
advertised: Some(("resources", &[("subscribe", false), ("listChanged", true)])),
revisions: MODERN_AND_LEGACY,
},
CapabilityIdDescriptor {
@@ -368,7 +397,8 @@ pub(crate) const CAPABILITY_DESCRIPTORS: &[CapabilityIdDescriptor] = &[
extension: None,
deprecated: None,
sub_features: FeatureSet(&["listChanged"]),
advertised: Some(("prompts", &[("listChanged", false)])),
// Story 3.19: the server really emits `notifications/prompts/list_changed`.
advertised: Some(("prompts", &[("listChanged", true)])),
revisions: MODERN_AND_LEGACY,
},
CapabilityIdDescriptor {
@@ -382,6 +412,46 @@ pub(crate) const CAPABILITY_DESCRIPTORS: &[CapabilityIdDescriptor] = &[
advertised: Some(("completions", &[])),
revisions: MODERN_AND_LEGACY,
},
CapabilityIdDescriptor {
// Story 3.18: the standard `logging` declaration of both approved revisions. The pinned
// modern revision marks it deprecated (SEP-2577) but keeps it in the schema for at least
// twelve months, and `notifications/message` remains the only log channel there; the legacy
// revision additionally serves `logging/setLevel`.
id: CapabilityId::Logging,
roster: Roster::Server,
kind: Kind::Capability,
extension: None,
deprecated: None,
// The pinned schema declares `logging` as an open object with no named sub-features.
sub_features: FeatureSet(&[]),
advertised: Some(("logging", &[])),
revisions: MODERN_AND_LEGACY,
},
CapabilityIdDescriptor {
// Dispatch-only registration of the legacy `logging/setLevel` request: it is a client
// request, not an advertised server capability, and it does not exist in the modern schema.
id: CapabilityId::LoggingSetLevel,
roster: Roster::Server,
kind: Kind::Method,
extension: None,
deprecated: None,
sub_features: FeatureSet(&[]),
advertised: None,
revisions: LEGACY_ONLY,
},
CapabilityIdDescriptor {
// Story 3.19: dispatch-only registration of the modern `subscriptions/listen` request. The
// client sends it; the pinned modern schema declares no server `subscriptions` capability
// object, so it carries no advertisement fragment. The legacy revision has no such method.
id: CapabilityId::Subscriptions,
roster: Roster::Client,
kind: Kind::Method,
extension: None,
deprecated: None,
sub_features: FeatureSet(&[]),
advertised: None,
revisions: MODERN_ONLY,
},
CapabilityIdDescriptor {
id: CapabilityId::Ping,
roster: Roster::Server,
@@ -486,6 +556,27 @@ pub(crate) const METHOD_DESCRIPTORS: &[MethodDescriptor] = &[
required_headers: HEADERS_METHOD_ONLY,
handler: MethodHandler::Completion,
},
MethodDescriptor {
// Story 3.18: the legacy client request that sets the log level of its session. It declares
// the level, not a server-side name, so only the method header applies; the pinned modern
// revision has no such method at all (per-request `_meta` replaces it).
name: "logging/setLevel",
capability: CapabilityId::LoggingSetLevel,
kind: Kind::Method,
required_headers: HEADERS_METHOD_ONLY,
handler: MethodHandler::LoggingSetLevel,
},
MethodDescriptor {
// Story 3.19: the modern long-lived notification stream. It declares the notification filter
// in its params, not a server-side name, so only the method header applies. The legacy
// revision opens its notification stream with `GET` instead, so this name is a wrong-era
// request there and must never be served.
name: "subscriptions/listen",
capability: CapabilityId::Subscriptions,
kind: Kind::Method,
required_headers: HEADERS_METHOD_ONLY,
handler: MethodHandler::SubscriptionsListen,
},
MethodDescriptor {
name: "ping",
capability: CapabilityId::Ping,
@@ -514,6 +605,17 @@ pub(crate) const METHOD_DESCRIPTORS: &[MethodDescriptor] = &[
required_headers: HEADERS_NONE,
handler: MethodHandler::Lifecycle,
},
MethodDescriptor {
// Story 3.4: the legacy revision's client-side cancellation of a request it issued. The
// pinned modern revision has no client-to-server notification over Streamable HTTP (closing
// the response stream is its cancellation signal), so this name exists in the legacy era
// only. It is a dispatch-only notification registration and advertises nothing.
name: "notifications/cancelled",
capability: CapabilityId::LegacyLifecycle,
kind: Kind::Notification,
required_headers: HEADERS_NONE,
handler: MethodHandler::Cancelled,
},
];
#[cfg(test)]
@@ -36,9 +36,10 @@ fn static_registration_is_unique_and_advertises_only_the_registered_capability()
registry.advertised_capabilities(revision),
json!({
"completions": {},
"prompts": {"listChanged": false},
"resources": {"listChanged": false, "subscribe": false},
"tools": {"listChanged": false}
"logging": {},
"prompts": {"listChanged": true},
"resources": {"listChanged": true, "subscribe": false},
"tools": {"listChanged": true}
}),
"the registered advertisement must be the same reviewed object in {revision}"
);
@@ -46,9 +47,10 @@ fn static_registration_is_unique_and_advertises_only_the_registered_capability()
advertised_server_capabilities(revision),
json!({
"completions": {},
"prompts": {"listChanged": false},
"resources": {"listChanged": false, "subscribe": false},
"tools": {"listChanged": false}
"logging": {},
"prompts": {"listChanged": true},
"resources": {"listChanged": true, "subscribe": false},
"tools": {"listChanged": true}
}),
"the production advertisement entry point must serve {revision}"
);
@@ -259,9 +261,10 @@ fn an_inert_test_descriptor_changes_both_projections_and_is_never_registered_in_
without.advertised_capabilities(MODERN_PROTOCOL_VERSION),
json!({
"completions": {},
"prompts": {"listChanged": false},
"resources": {"listChanged": false, "subscribe": false},
"tools": {"listChanged": false}
"logging": {},
"prompts": {"listChanged": true},
"resources": {"listChanged": true, "subscribe": false},
"tools": {"listChanged": true}
})
);
assert_eq!(
@@ -269,9 +272,10 @@ fn an_inert_test_descriptor_changes_both_projections_and_is_never_registered_in_
json!({
"inert_test_capability": {"enabled": true},
"completions": {},
"prompts": {"listChanged": false},
"resources": {"listChanged": false, "subscribe": false},
"tools": {"listChanged": false}
"logging": {},
"prompts": {"listChanged": true},
"resources": {"listChanged": true, "subscribe": false},
"tools": {"listChanged": true}
})
);
assert_eq!(
@@ -279,9 +283,10 @@ fn an_inert_test_descriptor_changes_both_projections_and_is_never_registered_in_
json!({
"inert_test_capability": {"enabled": true},
"completions": {},
"prompts": {"listChanged": false},
"resources": {"listChanged": false, "subscribe": false},
"tools": {"listChanged": false}
"logging": {},
"prompts": {"listChanged": true},
"resources": {"listChanged": true, "subscribe": false},
"tools": {"listChanged": true}
})
);
@@ -462,9 +467,10 @@ fn a_single_era_descriptor_never_leaks_into_the_other_era_advertisement() {
other_era_advertisement,
json!({
"completions": {},
"prompts": {"listChanged": false},
"resources": {"listChanged": false, "subscribe": false},
"tools": {"listChanged": false}
"logging": {},
"prompts": {"listChanged": true},
"resources": {"listChanged": true, "subscribe": false},
"tools": {"listChanged": true}
}),
"the other era advertisement stays the reviewed object"
);
@@ -654,6 +660,56 @@ fn story317_completion_is_advertised_and_dispatchable_in_both_revisions() {
);
}
#[test]
fn story318_logging_is_advertised_on_both_revisions_and_set_level_is_legacy_only() {
let registry = production_registry();
for revision in [MODERN_PROTOCOL_VERSION, LEGACY_PROTOCOL_VERSION] {
assert!(
registry.advertise_for(CapabilityId::Logging, revision),
"the implemented logging surface must be advertised in {revision}"
);
assert_eq!(
registry.advertised_capabilities(revision)["logging"],
json!({}),
"the pinned advertisement is an empty `logging` object in {revision}"
);
}
// `logging/setLevel` is a legacy request only: the pinned modern revision replaces it with the
// per-request `_meta` log level, so the modern era must never serve it.
assert_eq!(
registry.dispatch_decision("logging/setLevel", LEGACY_PROTOCOL_VERSION),
DispatchDecision::Allowed {
handler: MethodHandler::LoggingSetLevel,
kind: Kind::Method,
}
);
assert_eq!(
registry.dispatch_decision("logging/setLevel", MODERN_PROTOCOL_VERSION),
DispatchDecision::WrongEra {
handler: MethodHandler::LoggingSetLevel,
kind: Kind::Method,
},
"a method the modern schema does not declare must never reach a handler there"
);
let descriptor = METHOD_DESCRIPTORS
.iter()
.find(|descriptor| descriptor.name == "logging/setLevel")
.expect("logging/setLevel is a registered method");
assert_eq!(
descriptor.required_headers.0,
&["Mcp-Method"],
"the request declares a level, not a server-side name"
);
assert!(
CAPABILITY_DESCRIPTORS
.iter()
.filter(|descriptor| descriptor.id == CapabilityId::LoggingSetLevel)
.all(|descriptor| descriptor.advertised.is_none()),
"the client request must never be advertised as a server capability"
);
}
#[test]
fn adding_and_removing_a_method_descriptor_changes_the_dispatch_decision() {
// The inert capability owns one inert method. With the method descriptor registered the
@@ -702,9 +758,10 @@ fn adding_and_removing_a_method_descriptor_changes_the_dispatch_decision() {
json!({
"inert_test_capability": {"enabled": true},
"completions": {},
"prompts": {"listChanged": false},
"resources": {"listChanged": false, "subscribe": false},
"tools": {"listChanged": false}
"logging": {},
"prompts": {"listChanged": true},
"resources": {"listChanged": true, "subscribe": false},
"tools": {"listChanged": true}
})
);
}
@@ -0,0 +1,518 @@
//! Story 3.19: Resource and Prompt list-change signals (FR-19); Story 3.5 adds the Tools signal of
//! the same seam (FR-14).
//!
//! The positive result of the story is that a client which asked for the list-change signal of its
//! revision learns that the *committed* Resource, Prompt or Tool catalog changed, and that the
//! ordinary `resources/list`, `resources/read`, `resources/templates/list`, `prompts/list`,
//! `prompts/get` and `tools/list` stay authoritative for a client that misses the notification.
//!
//! Shape of the chosen seam (ARC decision, kanban t_c12863fb): a **stream-local watcher**. One open
//! notification stream owns one bounded periodic read of the published Agent catalog the stream is
//! scoped to; a change of the visible Resource/Prompt/Tool projection of that snapshot (the Resource
//! and Prompt bindings plus the exact Tool definitions `tools/list` serves) produces the revision's
//! list-change notification. There is no post-commit hook, no cross-process bus and no durable
//! queue: the committed catalog is the source of truth, so a signal can never be produced before its
//! commit, and a failed read can never roll the commit back — it only skips that tick and stays
//! observable in tracing.
//!
//! Bounds (issue requirement: bounded queues/frequency/bytes):
//! * one in-flight read per stream, one fixed poll interval, a read timeout and skipped ticks;
//! * at most one frame per change type per tick, so a burst coalesces by the latest observed
//! revision instead of queueing every intermediate commit;
//! * the queue is a fixed-capacity channel; a slow reader drops the newest frame instead of blocking
//! the producer or growing memory, and recovers through the authoritative list methods;
//! * every frame has a fixed shape — a closed method name plus the request id of the subscription —
//! so the byte bound follows from the id bound ([`MAX_SUBSCRIPTION_ID_BYTES`]).
//!
//! Scope boundary: this module serves list-change signals only. URI-content subscriptions
//! (`resources/subscribe` / `notifications/resources/updated`) stay unimplemented and are never
//! acknowledged or emitted here.
use std::{
collections::{HashMap, VecDeque},
future::Future,
pin::Pin,
sync::{
Arc, Mutex, MutexGuard,
atomic::{AtomicU64, Ordering},
},
time::{Duration, Instant},
};
use axum::response::sse::Event;
use crank_registry::PublishedAgentCatalog;
use futures_util::stream;
use rmcp::model::{SubscriptionFilter, SubscriptionsListenRequestParams};
use serde::{Deserialize, Deserializer, de::Error};
use serde_json::{Value, json};
#[path = "signals_sdk.rs"]
mod sdk;
use tokio::sync::mpsc;
use tracing::warn;
use crate::app::AppState;
/// The pinned key that correlates every message of one subscription stream with its
/// `subscriptions/listen` request id.
pub(crate) const SUBSCRIPTION_ID_KEY: &str = "io.modelcontextprotocol/subscriptionId";
/// `notifications/resources/list_changed` (both approved revisions).
pub(crate) const RESOURCE_LIST_CHANGED: &str = "notifications/resources/list_changed";
/// `notifications/prompts/list_changed` (both approved revisions).
pub(crate) const PROMPT_LIST_CHANGED: &str = "notifications/prompts/list_changed";
/// `notifications/tools/list_changed` (both approved revisions, Story 3.5).
pub(crate) const TOOL_LIST_CHANGED: &str = "notifications/tools/list_changed";
/// `notifications/subscriptions/acknowledged` (modern revision only).
#[cfg(test)]
pub(crate) const SUBSCRIPTIONS_ACKNOWLEDGED: &str = "notifications/subscriptions/acknowledged";
/// Engineering default of the watcher tick. It is not a measured latency SLO: the story only needs
/// "after commit", and the client always keeps the authoritative list methods for recovery.
pub(crate) const POLL_INTERVAL: Duration = Duration::from_secs(1);
/// Upper bound of one watcher read. A read that exceeds it is a skipped tick, exactly like a failed
/// read: no signal, no rollback, one safe tracing event.
pub(crate) const READ_TIMEOUT: Duration = Duration::from_secs(5);
/// Frames a slow reader may fall behind before the newest frame is dropped (`coalescing`).
pub(crate) const SIGNAL_QUEUE_CAPACITY: usize = 4;
/// Bound of the encoded `subscriptions/listen` id carried by every frame of one stream.
pub(crate) const MAX_SUBSCRIPTION_ID_BYTES: usize = 64;
/// Bound of the legacy session stream table (memory bound; see [`StreamRegistry`]).
pub(crate) const MAX_TRACKED_STREAMS: usize = 1_024;
/// Bound of the URIs a modern filter may name. The server does not implement URI subscriptions, so
/// this many names is already far more than a well-behaved client asks for.
pub(crate) const MAX_FILTER_URIS: usize = 32;
/// Bound of one URI in the modern filter: longer values are rejected without being echoed back.
pub(crate) const MAX_FILTER_URI_BYTES: usize = 2_048;
/// The one safe detail of a malformed or oversized `subscriptions/listen` filter.
pub(crate) const INVALID_LISTEN_FILTER: &str = "invalid subscriptions/listen notification filter";
/// The three list-change signal types this server implements.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Signals {
pub(crate) resources: bool,
pub(crate) prompts: bool,
pub(crate) tools: bool,
}
impl Signals {
/// All types: the legacy revision defines no client opt-in, so its stream carries every type.
pub(crate) const ALL: Self = Self {
resources: true,
prompts: true,
tools: true,
};
#[cfg_attr(not(test), allow(dead_code))] // read by the unit oracle of the projection
pub(crate) fn any(self) -> bool {
self.resources || self.prompts || self.tools
}
fn intersect(self, other: Self) -> Self {
Self {
resources: self.resources && other.resources,
prompts: self.prompts && other.prompts,
tools: self.tools && other.tools,
}
}
}
/// Compatibility bounds around the real SDK filter, not a second protocol field model.
#[derive(Debug, Default)]
pub(crate) struct ListenFilter(SubscriptionFilter);
// SDK optional fields accept null; the existing public filter contract rejects explicit null.
fn validate_filter(value: &Value) -> Result<(), &'static str> {
let object = value.as_object().ok_or(INVALID_LISTEN_FILTER)?;
for key in [
"toolsListChanged",
"promptsListChanged",
"resourcesListChanged",
"resourceSubscriptions",
] {
if object.get(key).is_some_and(Value::is_null) {
return Err(INVALID_LISTEN_FILTER);
}
}
Ok(())
}
impl<'de> Deserialize<'de> for ListenFilter {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = Value::deserialize(deserializer)?;
validate_filter(&value).map_err(D::Error::custom)?;
serde_json::from_value(value)
.map(Self)
.map_err(D::Error::custom)
}
}
/// Decode only the reviewed notifications projection: HTTP admission already owns `_meta`.
#[derive(Debug)]
pub(crate) struct ListenParams {
pub(crate) notifications: ListenFilter,
}
impl<'de> Deserialize<'de> for ListenParams {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = Value::deserialize(deserializer)?;
let notifications = value
.as_object()
.and_then(|object| object.get("notifications"))
.ok_or_else(|| D::Error::custom(INVALID_LISTEN_FILTER))?;
validate_filter(notifications).map_err(D::Error::custom)?;
let params: SubscriptionsListenRequestParams =
serde_json::from_value(json!({"notifications": notifications}))
.map_err(D::Error::custom)?;
#[cfg(test)]
crate::app::dispatch_observer::sdk_subscriptions_listen_decoded();
Ok(Self {
notifications: ListenFilter(params.notifications),
})
}
}
/// The bounded input contract of a `subscriptions/listen` filter.
pub(crate) enum FilterOutcome {
/// The list-change subset this server honours for the stream.
Accepted(Signals),
/// One safe detail: no URI, name or value the client sent is echoed back.
Invalid(&'static str),
}
impl ListenFilter {
/// Validate the bounded inputs and keep only the honoured types.
///
/// `resourceSubscriptions` is not implemented in the current scope, which is what the
/// acknowledgment reports: it is simply not honoured, and the pinned schema forbids the server
/// from sending a type it did not agree to.
pub(crate) fn accepted(&self) -> FilterOutcome {
let uris = self.0.resource_subscriptions.as_deref().unwrap_or_default();
if uris.len() > MAX_FILTER_URIS || uris.iter().any(|uri| uri.len() > MAX_FILTER_URI_BYTES) {
return FilterOutcome::Invalid(INVALID_LISTEN_FILTER);
}
FilterOutcome::Accepted(Signals {
resources: self.0.resources_list_changed == Some(true),
prompts: self.0.prompts_list_changed == Some(true),
tools: self.0.tools_list_changed == Some(true),
})
}
}
/// The exact bound Resource/Prompt/Tool projection of one published Agent catalog.
///
/// The projection is derived from the same committed catalog read that already feeds
/// `resources/list`, `resources/templates/list`, `prompts/list`, `prompts/get` and `tools/list`: the
/// exact Resource and Prompt bindings of the published Agent snapshot, in deterministic binding
/// order, and the exact Tool definitions that `tools/list` would serve. The Tool part is taken from
/// [`crate::manifest::catalog_tool_definitions`] rather than from the bindings, because that is the
/// projection the client sees: it also depends on the Tool selection policy (mode, groups, search
/// settings), and in `Search` mode it does not depend on the bindings at all. `catalog_revision`
/// alone is *not* the comparison key — a commit that only changes Tools must not produce a Resource
/// or Prompt signal, and vice versa — so the three projections are compared on their own.
///
/// shortcut: a Resource revocation invalidates a bound publication without a new Agent snapshot, so
/// it does not change this projection and produces no list-change signal; the revocation gate at
/// read time (Story 3.8) still fails closed, and a faithful revocation signal needs a bounded
/// registry read that the current scope does not have.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct CatalogFingerprint {
resources: Vec<(String, u32)>,
prompts: Vec<(String, u32)>,
/// Canonical JSON of every definition `tools/list` serves, sorted so a re-ordered read is not a
/// false change.
tools: Vec<String>,
}
impl CatalogFingerprint {
pub(crate) fn of(catalog: &PublishedAgentCatalog) -> Self {
fn sorted<T: Ord>(values: impl Iterator<Item = T>) -> Vec<T> {
let mut sorted = values.collect::<Vec<_>>();
sorted.sort_unstable();
sorted
}
Self {
resources: sorted(catalog.resources.iter().map(|binding| {
(
binding.resource_id.as_str().to_owned(),
binding.resource_version,
)
})),
prompts: sorted(catalog.prompts.iter().map(|binding| {
(
binding.prompt_id.as_str().to_owned(),
binding.prompt_version,
)
})),
tools: sorted(
crate::manifest::catalog_tool_definitions(catalog)
.into_iter()
.map(|definition| definition.to_string()),
),
}
}
/// The list-change types whose visible projection differs between `previous` and `self`.
pub(crate) fn changes_since(&self, previous: &Self) -> Signals {
Signals {
resources: self.resources != previous.resources,
prompts: self.prompts != previous.prompts,
tools: self.tools != previous.tools,
}
}
}
/// The wire contract of one stream.
#[derive(Clone, Debug)]
pub(crate) enum Wire {
/// `2025-11-25`: a bare server notification without parameters.
Legacy,
/// `2026-07-28`: every message carries the subscription id of its `subscriptions/listen` request.
Modern(Value),
}
impl Wire {
fn signal_frames(&self, signals: Signals) -> Vec<String> {
let mut frames = Vec::with_capacity(3);
for (changed, method) in [
(signals.resources, RESOURCE_LIST_CHANGED),
(signals.prompts, PROMPT_LIST_CHANGED),
(signals.tools, TOOL_LIST_CHANGED),
] {
if !changed {
continue;
}
let subscription_id = match self {
Wire::Legacy => None,
Wire::Modern(subscription_id) => Some(subscription_id),
};
frames.push(sdk::signal_frame(method, subscription_id));
}
frames
}
}
/// The first modern frame: the accepted subset, before any list-change notification of the stream.
pub(crate) fn acknowledged_frame(subscription_id: &Value, accepted: Signals) -> String {
sdk::acknowledged_frame(subscription_id, accepted)
}
/// The subscription id of a `subscriptions/listen` request, or `None` when its encoding is not a
/// bounded identity carrier.
pub(crate) fn subscription_id(request_id: &Value) -> Option<Value> {
let encoded = request_id.to_string();
(encoded.len() <= MAX_SUBSCRIPTION_ID_BYTES).then(|| request_id.clone())
}
/// Bounded table of the legacy session that currently owns the notification stream.
///
/// The pinned legacy transport forbids broadcasting one message across several SSE streams of the
/// same session, and the community scope keeps exactly one producer per session: the newest admitted
/// `GET` takes the signals over and the older stream ends. A retired stream (session `DELETE`) or a
/// superseded generation never writes into a reconnect.
#[derive(Default)]
pub(crate) struct StreamRegistry {
next_generation: AtomicU64,
owners: Mutex<HashMap<String, u64>>,
}
impl StreamRegistry {
pub(crate) fn new() -> Self {
Self::default()
}
/// Admit the notification stream of one session and return its generation.
pub(crate) fn admit(&self, session_id: &str) -> Option<u64> {
let generation = self.next_generation.fetch_add(1, Ordering::Relaxed) + 1;
let mut owners = self.lock()?;
if owners.len() >= MAX_TRACKED_STREAMS && !owners.contains_key(session_id) {
// Bounded table: forget the whole owner set instead of growing without a limit. Each
// running producer still holds its own generation, so the worst case is a stream that
// can no longer be told apart from a newer one — never unbounded memory.
owners.clear();
}
owners.insert(session_id.to_owned(), generation);
Some(generation)
}
/// Whether `generation` still owns the stream of `session_id`.
pub(crate) fn is_current(&self, session_id: &str, generation: u64) -> bool {
self.lock()
.and_then(|owners| owners.get(session_id).copied())
.is_some_and(|current| current == generation)
}
/// Stop the producer of one session (`DELETE`, or a session that no longer exists).
pub(crate) fn retire(&self, session_id: &str) {
if let Some(mut owners) = self.lock() {
owners.remove(session_id);
}
}
fn lock(&self) -> Option<MutexGuard<'_, HashMap<String, u64>>> {
self.owners.try_lock().ok()
}
}
/// Everything one watcher needs.
pub(crate) struct Watch {
pub(crate) workspace_slug: String,
pub(crate) agent_slug: String,
/// The signal types this stream may deliver.
pub(crate) signals: Signals,
pub(crate) wire: Wire,
/// The legacy session lease: the producer stops when the stream is superseded or retired.
pub(crate) legacy_stream: Option<(Arc<StreamRegistry>, String, u64)>,
/// Lifetime bound inherited from the legacy transport session TTL.
pub(crate) deadline: Option<Instant>,
}
/// One committed-catalog read of a watcher tick.
///
/// The reader is a closure so the failure path is driveable in a unit oracle without a live
/// registry: production passes [`read`] over the shared [`AppState`], and the oracle passes a
/// scripted sequence.
pub(crate) type ReadFuture =
Pin<Box<dyn Future<Output = Result<CatalogFingerprint, ReadError>> + Send>>;
/// A source of committed-catalog reads. One call per tick.
pub(crate) type Reader = Box<dyn FnMut() -> ReadFuture + Send>;
/// Start the stream-local watcher and return the bounded frame queue of the response stream.
///
/// The first read is the baseline taken **before** the acknowledgment is written; it is never reset
/// afterwards, so a publication between the baseline and the acknowledgment is still delivered.
pub(crate) fn spawn(state: Arc<AppState>, watch: Watch) -> mpsc::Receiver<String> {
let scope = (watch.workspace_slug.clone(), watch.agent_slug.clone());
let reader: Reader = Box::new(move || {
let state = Arc::clone(&state);
let scope = scope.clone();
Box::pin(async move { read(&state, &scope).await })
});
let (sender, receiver) = mpsc::channel(SIGNAL_QUEUE_CAPACITY);
#[cfg(not(test))]
tokio::spawn(run(watch, sender, reader));
#[cfg(test)]
{
let observer = crate::app::dispatch_observer::installed_observer();
tokio::spawn(async move {
match observer {
Some(observer) => {
crate::app::dispatch_observer::observe(observer, run(watch, sender, reader))
.await
}
None => run(watch, sender, reader).await,
}
});
}
receiver
}
async fn run(watch: Watch, sender: mpsc::Sender<String>, mut read: Reader) {
let mut baseline = match read().await {
Ok(fingerprint) => Some(fingerprint),
Err(ReadError::Gone) => return,
Err(ReadError::Retry) => {
observe_failure();
None
}
};
loop {
if !still_owned(&watch) || sender.is_closed() {
return;
}
if watch
.deadline
.is_some_and(|deadline| Instant::now() >= deadline)
{
return;
}
tokio::time::sleep(POLL_INTERVAL).await;
if !still_owned(&watch) {
return;
}
match read().await {
Ok(fingerprint) => {
if let Some(previous) = baseline.as_ref() {
let changes = fingerprint.changes_since(previous).intersect(watch.signals);
for frame in watch.wire.signal_frames(changes) {
// A slow reader coalesces: the newest frame is dropped instead of blocking
// the producer or growing a queue, and the client recovers through the
// authoritative list methods.
let _ = sender.try_send(frame);
}
}
baseline = Some(fingerprint);
}
// The Agent is no longer published: fail closed by ending the stream instead of serving a
// catalog nobody may read any more.
Err(ReadError::Gone) => return,
// The committed catalog is untouched: the failure is observable and this tick is skipped.
Err(_) => observe_failure(),
}
}
}
pub(crate) enum ReadError {
/// The published Agent behind this stream is gone: the producer ends.
Gone,
/// The catalog could not be read this tick: skip it, keep the committed state.
Retry,
}
fn still_owned(watch: &Watch) -> bool {
match &watch.legacy_stream {
Some((registry, session_id, generation)) => registry.is_current(session_id, *generation),
None => true,
}
}
async fn read(state: &AppState, scope: &(String, String)) -> Result<CatalogFingerprint, ReadError> {
let catalog = tokio::time::timeout(READ_TIMEOUT, state.catalog.get_catalog(&scope.0, &scope.1))
.await
.map_err(|_| ReadError::Retry)?
.map_err(|error| match error {
crank_registry::RegistryError::PublishedAgentNotFound { .. } => ReadError::Gone,
_ => ReadError::Retry,
})?;
Ok(CatalogFingerprint::of(&catalog))
}
/// One safe, closed-vocabulary observation of a skipped tick: no URI, Agent name, credential or raw
/// error text is read here.
fn observe_failure() {
warn!(
name: "mcp.signals.read_failed",
error_category = "catalog",
"catalog change signal read failed"
);
}
/// The bounded SSE body of one signal stream: the leading frames (acknowledgment), then the queue,
/// ending when the producer ends. The caller's stream permit lives in `guard` and is released when
/// the response stream is dropped.
pub(crate) fn frames_stream<G: Send + 'static>(
first: Vec<String>,
receiver: mpsc::Receiver<String>,
guard: G,
) -> impl stream::Stream<Item = Result<Event, std::convert::Infallible>> {
let pending: VecDeque<String> = first.into();
stream::unfold(
(pending, receiver, guard),
|(mut pending, mut receiver, guard)| async move {
loop {
if let Some(frame) = pending.pop_front() {
return Some((Ok(Event::default().data(frame)), (pending, receiver, guard)));
}
let frame = receiver.recv().await?;
pending.push_back(frame);
}
},
)
}
#[cfg(test)]
#[path = "signals_tests.rs"]
mod tests;
@@ -0,0 +1,178 @@
//! SDK serialization for the stream-local list-change notifications.
use rmcp::model::{
JsonRpcNotification, JsonRpcVersion2_0, NotificationMetaObject, PromptListChangedNotification,
ResourceListChangedNotification, SubscriptionFilter, SubscriptionsAcknowledgedNotification,
SubscriptionsAcknowledgedNotificationParams, ToolListChangedNotification,
};
use serde_json::{Map, Value};
use super::{PROMPT_LIST_CHANGED, RESOURCE_LIST_CHANGED, Signals, TOOL_LIST_CHANGED};
fn subscription_meta(subscription_id: &Value) -> NotificationMetaObject {
let mut meta = Map::new();
// Keep the JSON identity: SDK RequestId narrows numbers to i64.
meta.insert(
super::SUBSCRIPTION_ID_KEY.to_owned(),
subscription_id.clone(),
);
NotificationMetaObject::from(meta)
}
pub(super) fn signal_frame(method: &str, subscription_id: Option<&Value>) -> String {
let meta = subscription_id.map(subscription_meta);
let frame = match method {
RESOURCE_LIST_CHANGED => {
let mut notification = ResourceListChangedNotification::default();
if let Some(meta) = meta {
notification.extensions.insert(meta);
}
serde_json::to_string(&JsonRpcNotification {
jsonrpc: JsonRpcVersion2_0,
notification,
})
}
PROMPT_LIST_CHANGED => {
let mut notification = PromptListChangedNotification::default();
if let Some(meta) = meta {
notification.extensions.insert(meta);
}
serde_json::to_string(&JsonRpcNotification {
jsonrpc: JsonRpcVersion2_0,
notification,
})
}
TOOL_LIST_CHANGED => {
let mut notification = ToolListChangedNotification::default();
if let Some(meta) = meta {
notification.extensions.insert(meta);
}
serde_json::to_string(&JsonRpcNotification {
jsonrpc: JsonRpcVersion2_0,
notification,
})
}
_ => unreachable!("list-change methods are a closed set"),
}
.expect("SDK list-change notification with JSON metadata is serializable");
#[cfg(test)]
crate::app::dispatch_observer::sdk_signal_notification_serialized();
frame
}
pub(super) fn acknowledged_frame(subscription_id: &Value, accepted: Signals) -> String {
let mut notifications = SubscriptionFilter::new();
notifications.resources_list_changed = accepted.resources.then_some(true);
notifications.prompts_list_changed = accepted.prompts.then_some(true);
notifications.tools_list_changed = accepted.tools.then_some(true);
let params = SubscriptionsAcknowledgedNotificationParams::new(notifications)
.with_meta(subscription_meta(subscription_id));
let notification = SubscriptionsAcknowledgedNotification::new(params);
let frame = serde_json::to_string(&JsonRpcNotification {
jsonrpc: JsonRpcVersion2_0,
notification,
})
.expect("SDK subscription acknowledgment with JSON metadata is serializable");
#[cfg(test)]
crate::app::dispatch_observer::sdk_subscriptions_acknowledged_serialized();
frame
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
const METHODS: [&str; 3] = [
RESOURCE_LIST_CHANGED,
PROMPT_LIST_CHANGED,
TOOL_LIST_CHANGED,
];
#[test]
fn legacy_signals_omit_params() {
for method in METHODS {
let frame: Value = serde_json::from_str(&signal_frame(method, None)).unwrap();
assert_eq!(frame, json!({"jsonrpc": "2.0", "method": method}));
}
}
#[test]
fn modern_signals_and_acknowledgments_preserve_json_identity() {
for encoded in [
r#""subscription-\"quoted""#,
"-7",
"18446744073709551615",
"1.0",
"0.12345678901234567890123456789",
"1.234567890123456789e+25",
] {
let id: Value = serde_json::from_str(encoded).unwrap();
let mut frames = METHODS
.map(|method| (signal_frame(method, Some(&id)), method))
.to_vec();
frames.push((
acknowledged_frame(&id, Signals::default()),
super::super::SUBSCRIPTIONS_ACKNOWLEDGED,
));
for (encoded_frame, method) in frames {
let frame: Value = serde_json::from_str(&encoded_frame).unwrap();
assert_eq!(frame["jsonrpc"], "2.0");
assert_eq!(frame["method"], method);
assert_eq!(
frame["params"]["_meta"][super::super::SUBSCRIPTION_ID_KEY],
id
);
assert_eq!(
frame["params"]["_meta"][super::super::SUBSCRIPTION_ID_KEY].to_string(),
id.to_string(),
);
assert_eq!(frame["params"]["_meta"].as_object().unwrap().len(), 1);
assert!(frame.get("id").is_none());
if method != super::super::SUBSCRIPTIONS_ACKNOWLEDGED {
assert_eq!(frame["params"].as_object().unwrap().len(), 1);
}
}
}
}
#[test]
fn acknowledgment_reports_only_accepted_true_flags_and_never_uris() {
for resources in [false, true] {
for prompts in [false, true] {
for tools in [false, true] {
let accepted = Signals {
resources,
prompts,
tools,
};
let frame: Value =
serde_json::from_str(&acknowledged_frame(&json!("listen"), accepted))
.unwrap();
let mut expected = Map::new();
for (enabled, name) in [
(resources, "resourcesListChanged"),
(prompts, "promptsListChanged"),
(tools, "toolsListChanged"),
] {
if enabled {
expected.insert(name.to_owned(), Value::Bool(true));
}
}
assert_eq!(
frame,
json!({
"jsonrpc": "2.0",
"method": super::super::SUBSCRIPTIONS_ACKNOWLEDGED,
"params": {
"_meta": {super::super::SUBSCRIPTION_ID_KEY: "listen"},
"notifications": expected,
},
}),
);
}
}
}
}
}
@@ -0,0 +1,741 @@
//! Unit oracles of the Story 3.19 signal seam: the catalog projection, the bounded filter, the exact
//! wire frames of both approved revisions and the bounded legacy stream ownership.
use std::{
collections::BTreeMap,
io,
sync::{Arc, Mutex},
time::{Duration, Instant},
};
use crank_core::ids::{PromptId, ResourceId};
use crank_core::{
ExecutionConfig, HttpMethod, Operation, OperationId, OperationSecurityLevel, OperationStatus,
Protocol, RestTarget, Target, ToolAccessMode, ToolDescription, ToolGroup, ToolSearchSettings,
ToolSelectionPolicy, WorkspaceId,
};
use crank_mapping::MappingSet;
use crank_registry::PublishedAgentCatalog;
use crank_registry::model::{AgentPromptBinding, AgentResourceBinding, PublishedAgentTool};
use crank_schema::{Schema, SchemaKind};
use serde_json::{Value, json};
use time::OffsetDateTime;
use tokio::sync::mpsc;
use super::{
CatalogFingerprint, FilterOutcome, INVALID_LISTEN_FILTER, ListenFilter, MAX_FILTER_URIS,
MAX_SUBSCRIPTION_ID_BYTES, PROMPT_LIST_CHANGED, RESOURCE_LIST_CHANGED, ReadError, Reader,
SIGNAL_QUEUE_CAPACITY, SUBSCRIPTION_ID_KEY, SUBSCRIPTIONS_ACKNOWLEDGED, Signals,
StreamRegistry, TOOL_LIST_CHANGED, Watch, Wire, acknowledged_frame, run, subscription_id,
};
fn catalog(
resources: &[(&str, u32)],
prompts: &[(&str, u32)],
catalog_revision: &str,
) -> PublishedAgentCatalog {
catalog_with(
ToolSelectionPolicy::default(),
Vec::new(),
resources,
prompts,
catalog_revision,
)
}
fn catalog_with(
tool_selection_policy: ToolSelectionPolicy,
tools: Vec<PublishedAgentTool>,
resources: &[(&str, u32)],
prompts: &[(&str, u32)],
catalog_revision: &str,
) -> PublishedAgentCatalog {
PublishedAgentCatalog {
agent_version: 1,
catalog_revision: catalog_revision.to_owned(),
tool_selection_policy,
tools,
resources: resources
.iter()
.map(|(id, version)| AgentResourceBinding {
resource_id: ResourceId::new(*id),
resource_version: *version,
})
.collect(),
prompts: prompts
.iter()
.map(|(id, version)| AgentPromptBinding {
prompt_id: PromptId::new(*id),
prompt_version: *version,
})
.collect(),
}
}
fn search_policy(tool_names: &[&str]) -> ToolSelectionPolicy {
ToolSelectionPolicy {
mode: ToolAccessMode::Search,
groups: vec![ToolGroup {
id: "crm".to_owned(),
name: "CRM".to_owned(),
description: "CRM operations".to_owned(),
tool_names: tool_names.iter().map(|name| (*name).to_owned()).collect(),
}],
search: ToolSearchSettings::default(),
}
}
fn empty_object_schema() -> Schema {
Schema {
kind: SchemaKind::Object,
format: None,
description: None,
required: false,
nullable: false,
default_value: None,
fields: BTreeMap::new(),
items: None,
enum_values: Vec::new(),
variants: Vec::new(),
}
}
/// One published Tool binding whose served definition is exactly `title`/`description`.
fn published_tool(name: &str, title: &str, description: &str, version: u32) -> PublishedAgentTool {
PublishedAgentTool {
workspace_id: WorkspaceId::new("ws_test"),
workspace_slug: "ws-test".to_owned(),
agent_id: "agent_test".into(),
agent_slug: "agent".to_owned(),
operation: Operation {
id: OperationId::new(format!("op_{name}")),
name: name.to_owned(),
display_name: title.to_owned(),
category: "test".to_owned(),
protocol: Protocol::Rest,
security_level: OperationSecurityLevel::Standard,
status: OperationStatus::Published,
version,
target: Target::Rest(RestTarget {
base_url: "https://example.test".to_owned(),
method: HttpMethod::Get,
path_template: "/leads".to_owned(),
static_headers: BTreeMap::new(),
request_contract: None,
response_contract: Vec::new(),
multipart_field_mime_types: BTreeMap::new(),
}),
input_schema: empty_object_schema(),
output_schema: empty_object_schema(),
input_mapping: MappingSet { rules: Vec::new() },
output_mapping: MappingSet { rules: Vec::new() },
execution_config: ExecutionConfig {
timeout_ms: 10_000,
retry_policy: None,
response_cache: None,
idempotency: None,
safety: None,
approval_policy: None,
execution_policy: None,
auth_profile_ref: None,
headers: BTreeMap::new(),
},
tool_description: ToolDescription {
title: title.to_owned(),
description: description.to_owned(),
tags: Vec::new(),
examples: Vec::new(),
},
samples: None,
generated_draft: None,
config_export: None,
wizard_state: None,
created_at: OffsetDateTime::UNIX_EPOCH,
updated_at: OffsetDateTime::UNIX_EPOCH,
published_at: None,
},
tool_name: name.to_owned(),
tool_title: title.to_owned(),
tool_description: description.to_owned(),
}
}
#[test]
fn a_committed_resource_binding_is_a_resource_change_only() {
let before = CatalogFingerprint::of(&catalog(&[("res_a", 1)], &[("prm_a", 1)], "rev-1"));
let after = CatalogFingerprint::of(&catalog(
&[("res_a", 1), ("res_b", 1)],
&[("prm_a", 1)],
"rev-2",
));
assert_eq!(
after.changes_since(&before),
Signals {
resources: true,
prompts: false,
tools: false
}
);
assert!(!before.changes_since(&after).prompts);
assert_eq!(
before.changes_since(&after),
Signals {
resources: true,
prompts: false,
tools: false
}
);
}
#[test]
fn a_committed_prompt_binding_is_a_prompt_change_only() {
let before = CatalogFingerprint::of(&catalog(&[], &[("prm_a", 1)], "rev-1"));
let after = CatalogFingerprint::of(&catalog(&[], &[("prm_a", 1), ("prm_b", 2)], "rev-2"));
assert_eq!(
after.changes_since(&before),
Signals {
resources: false,
prompts: true,
tools: false
}
);
}
#[test]
fn a_pinned_version_bump_is_a_change_even_when_the_binding_names_are_stable() {
let before = CatalogFingerprint::of(&catalog(&[("res_a", 1)], &[("prm_a", 1)], "rev-1"));
let after = CatalogFingerprint::of(&catalog(&[("res_a", 2)], &[("prm_a", 1)], "rev-1"));
assert_eq!(
after.changes_since(&before),
Signals {
resources: true,
prompts: false,
tools: false
}
);
}
#[test]
fn a_committed_tool_definition_change_is_a_tool_change_only() {
let lead = || published_tool("crm_create_lead", "Create Lead", "Creates a lead", 1);
let before = CatalogFingerprint::of(&catalog_with(
ToolSelectionPolicy::default(),
vec![lead()],
&[("res_a", 1)],
&[("prm_a", 1)],
"rev-1",
));
let added = CatalogFingerprint::of(&catalog_with(
ToolSelectionPolicy::default(),
vec![
lead(),
published_tool(
"crm_create_second",
"Create Second",
"Creates a second lead",
1,
),
],
&[("res_a", 1)],
&[("prm_a", 1)],
"rev-2",
));
let retitled = CatalogFingerprint::of(&catalog_with(
ToolSelectionPolicy::default(),
vec![published_tool(
"crm_create_lead",
"Create a lead",
"Creates a lead",
1,
)],
&[("res_a", 1)],
&[("prm_a", 1)],
"rev-2",
));
let tools_only = Signals {
resources: false,
prompts: false,
tools: true,
};
assert_eq!(added.changes_since(&before), tools_only);
assert_eq!(
retitled.changes_since(&before),
tools_only,
"a retitled binding changes the visible Tool projection without a new Operation version"
);
// The reverse direction is a Tool change as well: a removed binding is a real change.
assert!(before.changes_since(&added).tools);
}
#[test]
fn a_republished_operation_version_with_an_identical_definition_is_silent() {
// `tools/list` serves the definition, not the Operation version: a version bump that leaves the
// served definition unchanged is not a client-visible Tool catalog change.
let before = CatalogFingerprint::of(&catalog_with(
ToolSelectionPolicy::default(),
vec![published_tool(
"crm_create_lead",
"Create Lead",
"Creates a lead",
1,
)],
&[("res_a", 1)],
&[("prm_a", 1)],
"rev-1",
));
let after = CatalogFingerprint::of(&catalog_with(
ToolSelectionPolicy::default(),
vec![published_tool(
"crm_create_lead",
"Create Lead",
"Creates a lead",
2,
)],
&[("res_a", 1)],
&[("prm_a", 1)],
"rev-2",
));
assert!(!after.changes_since(&before).any());
}
#[test]
fn a_policy_only_commit_that_changes_tools_list_is_a_tool_change_only() {
// The Tool selection policy is part of the served `tools/list` projection: switching the same
// bindings from direct to search mode replaces the served tools with `search_tools`/`call_tool`.
let tool = published_tool("crm_create_lead", "Create Lead", "Creates a lead", 1);
let direct = CatalogFingerprint::of(&catalog_with(
ToolSelectionPolicy::default(),
vec![tool.clone()],
&[("res_a", 1)],
&[("prm_a", 1)],
"rev-1",
));
let search = CatalogFingerprint::of(&catalog_with(
search_policy(&["crm_create_lead"]),
vec![tool],
&[("res_a", 1)],
&[("prm_a", 1)],
"rev-2",
));
let tools_only = Signals {
resources: false,
prompts: false,
tools: true,
};
assert_eq!(search.changes_since(&direct), tools_only);
assert_eq!(direct.changes_since(&search), tools_only);
}
#[test]
fn a_policy_field_invisible_to_tools_list_is_silent() {
// `search.max_results` is only served inside the search-mode definitions: in direct mode it
// changes nothing the client receives.
let tool = || published_tool("crm_create_lead", "Create Lead", "Creates a lead", 1);
let before = CatalogFingerprint::of(&catalog_with(
ToolSelectionPolicy::default(),
vec![tool()],
&[],
&[],
"rev-1",
));
let after = CatalogFingerprint::of(&catalog_with(
ToolSelectionPolicy {
search: ToolSearchSettings { max_results: 5 },
..ToolSelectionPolicy::default()
},
vec![tool()],
&[],
&[],
"rev-2",
));
assert!(!after.changes_since(&before).any());
}
#[test]
fn a_search_mode_binding_change_is_silent_when_tools_list_is_unchanged() {
// In search mode the served list is `search_tools`/`call_tool`; the bindings behind it are not
// part of `tools/list`, so a binding-only commit must not claim a visible Tool change.
let policy = search_policy(&["crm_create_lead"]);
let before = CatalogFingerprint::of(&catalog_with(
policy.clone(),
vec![published_tool(
"crm_create_lead",
"Create Lead",
"Creates a lead",
1,
)],
&[],
&[],
"rev-1",
));
let after = CatalogFingerprint::of(&catalog_with(
policy,
vec![
published_tool("crm_create_lead", "Create Lead", "Creates a lead", 1),
published_tool(
"crm_create_second",
"Create Second",
"Creates a second lead",
1,
),
],
&[],
&[],
"rev-2",
));
assert!(!after.changes_since(&before).any());
}
#[test]
fn a_resource_or_prompt_change_is_not_a_tool_change() {
let tool = || published_tool("crm_create_lead", "Create Lead", "Creates a lead", 1);
let before = CatalogFingerprint::of(&catalog_with(
ToolSelectionPolicy::default(),
vec![tool()],
&[("res_a", 1)],
&[("prm_a", 1)],
"rev-1",
));
let changed = CatalogFingerprint::of(&catalog_with(
ToolSelectionPolicy::default(),
vec![tool()],
&[("res_a", 2)],
&[("prm_a", 1), ("prm_b", 1)],
"rev-1",
));
assert_eq!(
changed.changes_since(&before),
Signals {
resources: true,
prompts: true,
tools: false
}
);
}
#[test]
fn a_catalog_revision_bump_without_a_visible_change_is_silent() {
// `catalog_revision` moved, but no visible Resource/Prompt/Tool projection did: no signal.
let before = CatalogFingerprint::of(&catalog(&[("res_a", 1)], &[("prm_a", 1)], "rev-1"));
let after = CatalogFingerprint::of(&catalog(&[("res_a", 1)], &[("prm_a", 1)], "rev-2"));
assert!(!after.changes_since(&before).any());
}
#[test]
fn the_projection_is_order_independent_and_has_no_duplicate_growth() {
let forward = CatalogFingerprint::of(&catalog(
&[("res_a", 1), ("res_b", 2)],
&[("prm_a", 1), ("prm_b", 2)],
"rev-1",
));
let reversed = CatalogFingerprint::of(&catalog(
&[("res_b", 2), ("res_a", 1)],
&[("prm_b", 2), ("prm_a", 1)],
"rev-1",
));
assert!(!reversed.changes_since(&forward).any());
}
#[test]
fn the_legacy_frames_carry_a_closed_method_name_and_no_parameters() {
let frames = Wire::Legacy.signal_frames(Signals::ALL);
assert_eq!(
frames,
vec![
json!({"jsonrpc": "2.0", "method": RESOURCE_LIST_CHANGED}).to_string(),
json!({"jsonrpc": "2.0", "method": PROMPT_LIST_CHANGED}).to_string(),
json!({"jsonrpc": "2.0", "method": TOOL_LIST_CHANGED}).to_string(),
]
);
// No URI, name or payload of a changed object is part of a list-change notification.
assert!(frames.iter().all(|frame| !frame.contains("uri")));
}
#[test]
fn the_modern_frames_carry_the_subscription_id_of_their_listen_request() {
let id = json!(41);
let frames = Wire::Modern(id.clone()).signal_frames(Signals::ALL);
assert_eq!(
frames,
vec![
json!({
"jsonrpc": "2.0",
"method": RESOURCE_LIST_CHANGED,
"params": { "_meta": { SUBSCRIPTION_ID_KEY: 41 } }
})
.to_string(),
json!({
"jsonrpc": "2.0",
"method": PROMPT_LIST_CHANGED,
"params": { "_meta": { SUBSCRIPTION_ID_KEY: 41 } }
})
.to_string(),
json!({
"jsonrpc": "2.0",
"method": TOOL_LIST_CHANGED,
"params": { "_meta": { SUBSCRIPTION_ID_KEY: 41 } }
})
.to_string(),
]
);
assert_eq!(
serde_json::from_str::<Value>(&frames[0]).unwrap()["params"]["_meta"][SUBSCRIPTION_ID_KEY],
id
);
}
#[test]
fn only_the_requested_and_honoured_types_are_encoded() {
let frames = Wire::Modern(json!(1)).signal_frames(Signals {
resources: false,
prompts: true,
tools: false,
});
assert_eq!(frames.len(), 1);
assert!(frames[0].contains(PROMPT_LIST_CHANGED));
}
#[test]
fn the_acknowledgment_reports_exactly_the_honoured_subset() {
let frame: Value = serde_json::from_str(&acknowledged_frame(
&json!(7),
Signals {
resources: true,
prompts: false,
tools: true,
},
))
.unwrap();
assert_eq!(frame["method"], SUBSCRIPTIONS_ACKNOWLEDGED);
assert_eq!(frame["params"]["_meta"][SUBSCRIPTION_ID_KEY], json!(7));
assert_eq!(
frame["params"]["notifications"],
json!({ "resourcesListChanged": true, "toolsListChanged": true })
);
}
#[test]
fn an_empty_honoured_subset_is_an_empty_filter_object() {
let frame: Value =
serde_json::from_str(&acknowledged_frame(&json!("sub-1"), Signals::default())).unwrap();
assert_eq!(frame["params"]["notifications"], json!({}));
}
#[test]
fn the_filter_keeps_only_the_implemented_list_change_types() {
let filter: ListenFilter = serde_json::from_value(json!({
"toolsListChanged": true,
"promptsListChanged": true,
"resourcesListChanged": false,
"resourceSubscriptions": ["file:///project/config.json"]
}))
.unwrap();
match filter.accepted() {
FilterOutcome::Accepted(signals) => assert_eq!(
signals,
Signals {
resources: false,
prompts: true,
tools: true
},
"the honoured set is the requested list-change subset: {signals:?}"
),
FilterOutcome::Invalid(detail) => panic!("a bounded filter must be accepted: {detail}"),
}
}
#[test]
fn an_absent_filter_field_means_not_subscribed() {
let filter: ListenFilter = serde_json::from_value(json!({})).unwrap();
match filter.accepted() {
FilterOutcome::Accepted(signals) => assert!(!signals.any()),
FilterOutcome::Invalid(detail) => panic!("an empty filter must be accepted: {detail}"),
}
}
#[test]
fn an_oversized_uri_filter_is_refused_without_echoing_it() {
let filter: ListenFilter = serde_json::from_value(json!({
"resourceSubscriptions": (0..=MAX_FILTER_URIS)
.map(|index| format!("file:///resource-{index}"))
.collect::<Vec<_>>()
}))
.unwrap();
match filter.accepted() {
FilterOutcome::Invalid(detail) => assert_eq!(detail, INVALID_LISTEN_FILTER),
FilterOutcome::Accepted(_) => panic!("an oversized filter must be refused"),
}
let oversized: ListenFilter = serde_json::from_value(json!({
"resourceSubscriptions": ["u".repeat(super::MAX_FILTER_URI_BYTES + 1)]
}))
.unwrap();
assert!(matches!(
oversized.accepted(),
FilterOutcome::Invalid(INVALID_LISTEN_FILTER)
));
}
#[test]
fn a_non_object_filter_is_a_deserialization_error() {
assert!(serde_json::from_value::<ListenFilter>(json!(["toolsListChanged"])).is_err());
assert!(serde_json::from_value::<ListenFilter>(json!({"promptsListChanged": "yes"})).is_err());
}
#[test]
fn the_subscription_id_is_bound_by_its_encoded_size() {
assert_eq!(subscription_id(&json!(1)), Some(json!(1)));
assert_eq!(subscription_id(&json!("sub-1")), Some(json!("sub-1")));
assert_eq!(
subscription_id(&json!("x".repeat(MAX_SUBSCRIPTION_ID_BYTES))),
None,
"an id longer than the frame bound must not be used as a subscription id"
);
}
#[test]
fn a_newer_legacy_stream_supersedes_the_previous_one() {
let streams = StreamRegistry::new();
let first = streams.admit("session-1").unwrap();
let second = streams.admit("session-1").unwrap();
assert!(!streams.is_current("session-1", first));
assert!(streams.is_current("session-1", second));
// Another session is unaffected.
let other = streams.admit("session-2").unwrap();
assert!(streams.is_current("session-2", other));
streams.retire("session-1");
assert!(!streams.is_current("session-1", second));
assert!(streams.is_current("session-2", other));
}
#[derive(Clone, Default)]
struct SharedLogWriter {
buffer: Arc<Mutex<Vec<u8>>>,
}
impl SharedLogWriter {
fn output(&self) -> String {
String::from_utf8(self.buffer.lock().unwrap().clone()).unwrap()
}
}
impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for SharedLogWriter {
type Writer = SharedLogGuard;
fn make_writer(&'a self) -> Self::Writer {
SharedLogGuard {
buffer: Arc::clone(&self.buffer),
}
}
}
struct SharedLogGuard {
buffer: Arc<Mutex<Vec<u8>>>,
}
impl io::Write for SharedLogGuard {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
self.buffer.lock().unwrap().extend_from_slice(bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
/// AC-2: a failed committed-catalog read is observable and loses nothing — the tick is skipped (no
/// fabricated signal, no write path that could roll the catalog back) while the baseline survives, so
/// the next successful read still reports the change of the committed catalog.
#[tokio::test]
async fn a_failed_catalog_read_is_observable_and_loses_no_committed_change() {
let writer = SharedLogWriter::default();
let subscriber = crank_observability::build_subscriber(
crank_observability::ObservabilityConfig::new(
crank_observability::ServiceIdentity::try_new("mcp-server", "test", "test").unwrap(),
"info",
crank_observability::RedactionLimits::default(),
),
writer.clone(),
)
.unwrap();
let dispatch = tracing::Dispatch::new(subscriber);
let _guard = tracing::dispatcher::set_default(&dispatch);
let baseline = CatalogFingerprint::of(&catalog(&[], &[("prm_a", 1)], "rev-1"));
let committed = CatalogFingerprint::of(&catalog(&[], &[("prm_a", 1), ("prm_b", 1)], "rev-2"));
let script = Arc::new(Mutex::new(
vec![Ok(baseline), Err(ReadError::Retry), Ok(committed)].into_iter(),
));
let reader: Reader = Box::new(move || {
let next = script
.lock()
.unwrap()
.next()
.unwrap_or(Err(ReadError::Retry));
Box::pin(async move { next })
});
let (sender, mut receiver) = mpsc::channel(SIGNAL_QUEUE_CAPACITY);
tokio::spawn(run(
Watch {
workspace_slug: "workspace-canary".to_owned(),
agent_slug: "agent-canary".to_owned(),
signals: Signals::ALL,
wire: Wire::Legacy,
legacy_stream: None,
deadline: Some(Instant::now() + Duration::from_millis(2_100)),
},
sender,
reader,
));
let mut frames = Vec::new();
while let Some(frame) = receiver.recv().await {
frames.push(frame);
}
// The failed tick produced no frame; the committed change of the next successful read did.
assert_eq!(
frames,
vec![json!({"jsonrpc": "2.0", "method": PROMPT_LIST_CHANGED}).to_string()]
);
let output = writer.output();
let failures: Vec<Value> = output
.lines()
.filter_map(|line| serde_json::from_str::<Value>(line).ok())
.filter(|event| event["event"] == "mcp.signals.read_failed")
.collect();
assert!(
!failures.is_empty(),
"a skipped tick must be observable: {output}"
);
for event in &failures {
assert_eq!(event["fields"]["error_category"], "catalog");
}
// Closed vocabulary: no workspace, Agent, URI or raw database text is read into the event.
assert!(!output.contains("workspace-canary"));
assert!(!output.contains("agent-canary"));
}
@@ -0,0 +1,345 @@
//! #107 Completion: real build_app HTTP wire controls and a separate SDK-path oracle.
use super::{CounterSet, observe, support::*};
use axum::{body::Body, response::Response};
use futures_util::{StreamExt, stream};
use serde_json::{Value, json};
use std::{sync::Arc, time::Duration};
#[path = "rmcp_completion_test_support.rs"]
mod fixtures;
use fixtures::{BOUNDED_URI, Endpoint, LARGE_URI, URI};
fn observe_body(response: Response, counters: Arc<CounterSet>) -> Response {
let (parts, body) = response.into_parts();
let body = stream::unfold(
(body.into_data_stream(), counters),
|(mut body, c)| async move {
observe(c.clone(), body.next())
.await
.map(|chunk| (chunk, (body, c)))
},
);
Response::from_parts(parts, Body::from_stream(body))
}
fn counts(c: &CounterSet) -> [usize; 2] {
[c.sdk_completion_dispatch(), c.sdk_completion_result()]
}
fn params(reference: Value, name: &str, value: &str) -> Value {
json!({"ref":reference,"argument":{"name":name,"value":value}})
}
fn prompt(name: &str) -> Value {
json!({"type":"ref/prompt","name":name})
}
fn resource(uri: &str) -> Value {
json!({"type":"ref/resource","uri":uri})
}
fn request(revision: &str, mut params: Value) -> Value {
if revision == MODERN_REVISION {
params["_meta"] = modern_meta();
}
json!({"jsonrpc":"2.0","id":9007199254740993_u64,"method":"completion/complete","params":params})
}
async fn wire(response: reqwest::Response, sse: bool) -> Value {
assert_eq!(response.status(), 200);
assert_eq!(response.headers()["x-request-id"], "sdk-completion-wire");
assert_eq!(
response.headers()["x-trace-id"],
"11111111111111111111111111111111"
);
let content_type = response.headers()["content-type"]
.to_str()
.unwrap()
.to_owned();
let text = tokio::time::timeout(Duration::from_secs(5), response.text())
.await
.unwrap()
.unwrap();
let body: Value = if sse {
assert!(
content_type.starts_with("text/event-stream"),
"{content_type}: {text}"
);
let frames: Vec<Value> = text
.lines()
.filter_map(|line| {
line.strip_prefix("data:")
.map(|s| serde_json::from_str(s.trim()).unwrap())
})
.collect();
let terminal: Vec<_> = frames
.iter()
.filter(|f| f.get("id") == Some(&json!(9007199254740993_u64)))
.collect();
assert_eq!(terminal.len(), 1, "{frames:?}");
assert_eq!(frames.last(), Some(terminal[0]), "terminal must be last");
terminal[0].clone()
} else {
assert!(
content_type.starts_with("application/json"),
"{content_type}: {text}"
);
serde_json::from_str(&text).unwrap()
};
assert_eq!(body["jsonrpc"], "2.0");
assert_eq!(body["id"], json!(9007199254740993_u64));
body
}
async fn success(
h: &Endpoint,
revision: &str,
session: Option<&str>,
p: Value,
sse: bool,
) -> (Value, [usize; 2]) {
let before = counts(&h.counters);
let body = wire(
h.post(revision, session, &request(revision, p), sse).await,
sse,
)
.await;
assert!(body.get("error").is_none(), "{body}");
let result = &body["result"];
if revision == MODERN_REVISION {
assert_eq!(result["resultType"], "complete", "{body}");
} else {
assert!(result.get("resultType").is_none(), "{body}");
}
for key in ["ttlMs", "cacheScope", "_meta"] {
assert!(result.get(key).is_none(), "{body}");
}
let after = counts(&h.counters);
(
result["completion"].clone(),
std::array::from_fn(|i| after[i] - before[i]),
)
}
async fn control(revision: &str) -> Vec<[usize; 2]> {
let h = Endpoint::started().await;
let session = h.session(revision).await;
let session = session.as_deref();
let modes: &[bool] = if revision == MODERN_REVISION {
&[false, true]
} else {
&[false]
};
let mut deltas = Vec::new();
for &sse in modes {
for (p, expected) in [
(
params(prompt("greeting"), "tone", ""),
json!({"values":["warm","dry"],"total":2}),
),
(
params(prompt("archived-prompt"), "tone", "W"),
json!({"values":["warm"],"total":1}),
),
(
params(prompt("greeting"), "name", "A"),
json!({"values":[],"total":0}),
),
(
params(resource(URI), "region", ""),
json!({"values":["eu","us","apac"],"total":3}),
),
(
params(resource(URI), "region", "A"),
json!({"values":["apac"],"total":1}),
),
(
params(resource(URI), "region", "zzz"),
json!({"values":[],"total":0}),
),
] {
let (completion, delta) = success(&h, revision, session, p, sse).await;
assert_eq!(completion, expected);
deltas.push(delta);
}
for (uri, total) in [(BOUNDED_URI, Some(150)), (LARGE_URI, None)] {
let (completion, delta) = success(
&h,
revision,
session,
params(resource(uri), "region", "item"),
sse,
)
.await;
assert_eq!(
completion["values"],
json!((0..100).map(|i| format!("item{i:04}")).collect::<Vec<_>>())
);
assert_eq!(completion["hasMore"], true);
if let Some(total) = total {
assert_eq!(completion["total"], total);
} else {
assert!(
completion.get("total").is_none(),
"partial scan: {completion}"
);
}
deltas.push(delta);
}
let mut with_context = params(prompt("greeting"), "tone", "w");
with_context["context"] = json!({"arguments":{"name":"Ada","tone":"dry"}});
let (completion, delta) = success(&h, revision, session, with_context, sse).await;
assert_eq!(completion, json!({"values":["warm"],"total":1}));
deltas.push(delta);
let mut unavailable = None;
for p in [
params(prompt("draft-prompt"), "tone", ""),
params(prompt("unbound-prompt"), "tone", ""),
params(prompt("other-agent-prompt"), "tone", ""),
params(prompt("never-existed"), "tone", ""),
params(prompt("changed-draft"), "tone", ""),
params(prompt("greeting"), "unknown-argument", ""),
params(resource("crank://foreign/{region}"), "region", ""),
params(resource(URI), "unknown-parameter", ""),
] {
let body = wire(
h.post(revision, session, &request(revision, p), sse).await,
sse,
)
.await;
assert_eq!(body["error"]["code"], -32602, "{body}");
assert_eq!(
body["error"]["message"],
"the completion reference is not available"
);
assert!(body.get("result").is_none());
if let Some(ref previous) = unavailable {
assert_eq!(&body, previous);
}
unavailable = Some(body);
}
let mut invalid = vec![
json!({"argument":{"name":"tone","value":""}}),
params(json!({"type":"ref/tool","name":"greeting"}), "tone", ""),
params(prompt("greeting"), "tone", &"s".repeat(1025)),
params(prompt("greeting"), &"n".repeat(129), ""),
];
// Preserve domain validation on arbitrary JSON, never drop or stringify context values for SDK.
for value in [
json!(7),
json!(true),
json!(null),
json!(["eu"]),
json!({"region":"eu"}),
json!("s".repeat(1025)),
] {
let mut p = params(prompt("greeting"), "tone", "");
p["context"] = json!({"arguments":{"secret-context":value}});
invalid.push(p);
}
let mut p = params(prompt("greeting"), "tone", "");
p["context"] = json!({"arguments":(0..33).map(|i| (format!("arg{i}"), json!("eu"))).collect::<serde_json::Map<_,_>>()});
invalid.push(p);
for p in invalid {
let body = wire(
h.post(revision, session, &request(revision, p), sse).await,
sse,
)
.await;
assert_eq!(body["error"]["code"], -32602, "{body}");
assert_eq!(
body["error"]["message"],
"invalid completion/complete parameters"
);
assert!(body.get("result").is_none());
for leaked in ["secret-context", &"s".repeat(1025), &"n".repeat(129)] {
assert!(!body.to_string().contains(leaked), "{body}");
}
}
}
h.registry
.revoke_platform_api_key(
&workspace_id(),
&crank_core::PlatformApiKeyId::new("pk_dispatch_observer"),
&time::OffsetDateTime::now_utc(),
)
.await
.unwrap();
let before = h.counters.snapshot();
let sdk_before = counts(&h.counters);
let response = h
.post(
revision,
session,
&request(revision, params(prompt("greeting"), "tone", "")),
false,
)
.await;
assert_eq!(response.status(), 401);
let _ = response.bytes().await.unwrap();
assert_eq!(
counts(&h.counters),
sdk_before,
"revoked key must not dispatch to SDK"
);
let after = h.counters.snapshot();
assert_eq!(after.era_modern, before.era_modern);
assert_eq!(after.era_legacy, before.era_legacy);
assert_eq!(after.tools_service, before.tools_service);
eprintln!(
"existing GREEN Completion HTTP {revision}: Prompt+Resource refs, exact bound v1/archive, prefix/empty/bounded/truncated, JSON/context/errors/correlation/auth; SDK actual={deltas:?}, expected=[1,1] per success"
);
deltas
}
#[tokio::test]
async fn rmcp_completion_wire_legacy() {
control(LEGACY_REVISION).await;
}
#[tokio::test]
async fn rmcp_completion_wire_modern() {
control(MODERN_REVISION).await;
}
#[tokio::test]
async fn rmcp_completion_sdk_legacy() {
let deltas = control(LEGACY_REVISION).await;
assert!(
deltas.iter().all(|d| *d == [1, 1]),
"SDK typed dispatch/result missing after successful HTTP: {deltas:?}"
);
}
#[tokio::test]
async fn rmcp_completion_sdk_modern() {
let deltas = control(MODERN_REVISION).await;
assert!(
deltas.iter().all(|d| *d == [1, 1]),
"SDK typed dispatch/result missing after successful HTTP: {deltas:?}"
);
}
#[tokio::test]
async fn rmcp_completion_observer_scope_control() {
use super::{sdk_completion_dispatch_enter, sdk_completion_result_serialized};
let a = Arc::new(CounterSet::default());
let b = Arc::new(CounterSet::default());
sdk_completion_dispatch_enter();
sdk_completion_result_serialized();
assert_eq!(counts(&a), [0; 2]);
let snapshot = a.snapshot();
tokio::join!(
observe(a.clone(), async {
sdk_completion_dispatch_enter();
tokio::task::yield_now().await;
sdk_completion_result_serialized();
}),
observe(b.clone(), async {
sdk_completion_result_serialized();
})
);
assert_eq!(counts(&a), [1, 1]);
assert_eq!(counts(&b), [0, 1]);
assert_eq!(a.snapshot(), snapshot);
let body = Body::from_stream(stream::once(async {
sdk_completion_result_serialized();
Ok::<_, std::convert::Infallible>("data: terminal\n\n")
}));
let response = observe_body(Response::new(body), b.clone());
assert_eq!(counts(&b), [0, 1], "body must be deferred");
assert_eq!(
&axum::body::to_bytes(response.into_body(), 100)
.await
.unwrap()[..],
b"data: terminal\n\n"
);
assert_eq!(counts(&b), [0, 2]);
assert_eq!(counts(&a), [1, 1]);
}
@@ -0,0 +1,445 @@
//! Completion HTTP fixture using the existing observed HTTP app and disposable PostgreSQL.
use super::*;
use crank_core::prompt::{PromptDefinition, PromptSensitivity};
use crank_core::{
Agent, AgentStatus, AgentVersion, HttpMethod, RestTarget, Target, ToolSelectionPolicy,
ids::PromptId,
};
use crank_registry::PostgresRegistry;
use crank_registry::model::{
AgentPromptBinding, ArchivePromptRequest, CreatePromptDraftRequest, PublishPromptRequest,
SaveAgentCatalogConfigRequest, UpdatePromptDraftRequest,
};
use crank_registry::{CreateAgentRequest, PublishAgentRequest};
use crank_runtime::{
InMemoryCoordinationStateStore, RequestRateLimitConfig, RequestRateLimiter, SecretCrypto,
};
pub(super) fn definition(name: &str, content: &str) -> PromptDefinition {
PromptDefinition::parse(&json!({
"name":name, "description":format!("Published {name}"),
"arguments":[
{"name":"name","schema":{"type":"string"},"required":true},
{"name":"count","schema":{"type":"integer"},"required":false,"default":2},
{"name":"enabled","schema":{"type":"boolean"},"required":false,"default":true},
{"name":"tone","schema":{"type":"string","enum":["warm","dry"]},"required":false,"default":"warm"}
],
"messages":[{"role":"system","content":"Be brief."},{"role":"user","content":content}]
})).unwrap()
}
pub(super) struct Endpoint {
pub registry: crank_registry::PostgresRegistry,
pub counters: Arc<CounterSet>,
pub key: String,
pub url: String,
server: tokio::task::JoinHandle<()>,
}
impl Drop for Endpoint {
fn drop(&mut self) {
self.server.abort();
}
}
impl Endpoint {
pub async fn started() -> Self {
crank_test_support::postgres_schema_url_for_task("completion_owner", "t_3fa1ad63").await;
let registry = test_registry().await;
publish_region_operation(&registry, "completion_lookup").await;
for (id, uri, size) in [
("res_completion_small", URI, 3),
("res_completion_large", LARGE_URI, 1025),
("res_completion_bounded", BOUNDED_URI, 150),
] {
publish_template(&registry, &ResourceId::new(id), uri, size).await;
}
let mut archived_at = test_time();
for (id, name, publish) in [
("prm_a", "greeting", true),
("prm_b", "archived-prompt", true),
("prm_c", "unbound-prompt", true),
("prm_d", "draft-prompt", false),
("prm_e", "other-agent-prompt", true),
] {
let row = registry
.create_prompt_draft(CreatePromptDraftRequest {
workspace_id: workspace_id(),
prompt_id: PromptId::new(id),
sensitivity: PromptSensitivity::Internal,
definition: definition(
name,
"Hello {{name}} count={{count}} enabled={{enabled}} tone={{tone}}",
),
created_at: time::OffsetDateTime::now_utc(),
})
.await
.unwrap();
if publish {
let row = registry
.publish_prompt_draft(PublishPromptRequest {
workspace_id: workspace_id(),
prompt_id: PromptId::new(id),
expected_updated_at: row.updated_at,
published_at: time::OffsetDateTime::now_utc(),
})
.await
.unwrap();
if id == "prm_b" {
archived_at = row.updated_at;
}
}
}
for (id, slug) in [
(agent_id(), AGENT_SLUG),
(
crank_core::AgentId::new("agent_other_prompts"),
"other-prompts",
),
] {
registry
.create_agent(CreateAgentRequest {
agent: &Agent {
id: id.clone(),
workspace_id: workspace_id(),
slug: slug.into(),
display_name: "SDK Prompts".into(),
description: String::new(),
status: AgentStatus::Draft,
current_draft_version: 1,
latest_published_version: None,
created_at: test_time(),
updated_at: test_time(),
published_at: None,
},
version: &AgentVersion {
agent_id: id,
version: 1,
status: AgentStatus::Draft,
instructions: json!({}),
tool_selection_policy: ToolSelectionPolicy::default(),
created_at: test_time(),
},
bindings: &[],
})
.await
.unwrap();
}
let other_agent = crank_core::AgentId::new("agent_other_prompts");
registry
.save_agent_catalog_config_editable_with_prompts(
SaveAgentCatalogConfigRequest {
workspace_id: &workspace_id(),
agent_id: &other_agent,
agent_version: 1,
bindings: &[],
tool_selection_policy: &ToolSelectionPolicy::default(),
resource_bindings: &[],
expected_state: None,
},
&[AgentPromptBinding {
prompt_id: PromptId::new("prm_e"),
prompt_version: 1,
}],
)
.await
.unwrap();
registry
.publish_agent(PublishAgentRequest {
workspace_id: &workspace_id(),
agent_id: &other_agent,
version: 1,
published_at: &test_time(),
published_by: Some("alice"),
expected_state: None,
})
.await
.unwrap();
registry
.save_agent_catalog_config_editable_with_prompts(
SaveAgentCatalogConfigRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
agent_version: 1,
bindings: &[],
tool_selection_policy: &ToolSelectionPolicy::default(),
resource_bindings: &[
AgentResourceBinding {
resource_id: ResourceId::new("res_completion_small"),
resource_version: 1,
},
AgentResourceBinding {
resource_id: ResourceId::new("res_completion_large"),
resource_version: 1,
},
AgentResourceBinding {
resource_id: ResourceId::new("res_completion_bounded"),
resource_version: 1,
},
],
expected_state: None,
},
&[
AgentPromptBinding {
prompt_id: PromptId::new("prm_a"),
prompt_version: 1,
},
AgentPromptBinding {
prompt_id: PromptId::new("prm_b"),
prompt_version: 1,
},
],
)
.await
.unwrap();
registry
.publish_agent(PublishAgentRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
version: 1,
published_at: &test_time(),
published_by: Some("alice"),
expected_state: None,
})
.await
.unwrap();
registry
.archive_prompt_draft(ArchivePromptRequest {
workspace_id: workspace_id(),
prompt_id: PromptId::new("prm_b"),
expected_updated_at: archived_at,
archived_at: time::OffsetDateTime::now_utc(),
})
.await
.unwrap();
// A mutable v2 draft must not change the bound v1 rendering or discovery metadata.
let draft = registry
.get_prompt_draft(&workspace_id(), &PromptId::new("prm_a"))
.await
.unwrap();
registry
.update_prompt_draft(UpdatePromptDraftRequest {
workspace_id: workspace_id(),
prompt_id: PromptId::new("prm_a"),
sensitivity: PromptSensitivity::Internal,
definition: definition("changed-draft", "Changed {{name}}"),
expected_updated_at: draft.updated_at,
updated_at: time::OffsetDateTime::now_utc(),
})
.await
.unwrap();
let key = create_api_key(&registry).await;
let counters = Arc::new(CounterSet::default());
let app = crate::build_app(
registry.clone(),
Duration::ZERO,
None,
SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap(),
crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build(),
RequestRateLimiter::new(RequestRateLimitConfig::new(10_000, 10_000).unwrap()),
Arc::new(InMemoryCoordinationStateStore::default()),
Arc::new(crate::session::InMemorySessionStore::default()),
Arc::new(crate::auth::CommunityMachineCredentialVerifier),
);
let app = observe_requests(app, counters.clone(), None).layer(axum::middleware::from_fn({
let counters = counters.clone();
move |request: axum::extract::Request, next: axum::middleware::Next| {
let counters = counters.clone();
async move { observe_body(next.run(request).await, counters) }
}
}));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!(
"http://{}/v1/{WORKSPACE_SLUG}/{AGENT_SLUG}",
listener.local_addr().unwrap()
);
let server = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
Self {
registry,
counters,
key,
url,
server,
}
}
pub async fn post(
&self,
revision: &str,
session: Option<&str>,
body: &Value,
sse: bool,
) -> reqwest::Response {
let mut request = reqwest::Client::new()
.post(&self.url)
.bearer_auth(&self.key)
.header(
"Accept",
if sse {
"text/event-stream, application/json"
} else {
"application/json"
},
)
.header("x-request-id", "sdk-completion-wire")
.header(
"traceparent",
"00-11111111111111111111111111111111-2222222222222222-01",
)
.header("MCP-Protocol-Version", revision);
if let Some(session) = session {
request = request.header("MCP-Session-Id", session);
}
if revision == MODERN_REVISION {
request = request.header("Mcp-Method", body["method"].as_str().unwrap());
}
request.json(body).send().await.unwrap()
}
pub async fn session(&self, revision: &str) -> Option<String> {
if revision == MODERN_REVISION {
return None;
}
let response = self
.post(revision, None, &legacy_initialize(1), false)
.await;
assert_eq!(response.status(), 200);
let session = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
assert_eq!(
json_body(response).await["result"]["protocolVersion"],
revision
);
assert_eq!(
self.post(revision, Some(&session), &initialized_notification(), false)
.await
.status(),
202
);
Some(session)
}
}
use crank_core::{ids::ResourceId, resource::TemplateSource};
use crank_mapping::{MappingRule, MappingSet};
use crank_registry::model::{
CreateResourceTemplateDraftRequest, PublishResourceTemplateVersionRequest,
};
use crank_registry::{AgentResourceBinding, PublishRequest};
use sha2::{Digest, Sha256};
pub(super) const URI: &str = "crank://completion-orders/{region}";
pub(super) const LARGE_URI: &str = "crank://completion-large/{region}";
pub(super) const BOUNDED_URI: &str = "crank://completion-bounded/{region}";
async fn publish_region_operation(registry: &PostgresRegistry, name: &str) {
let mut operation = test_operation("http://127.0.0.1:9");
operation.id = crank_core::OperationId::new(format!("op_{name}"));
operation.name = name.to_owned();
operation.display_name = "Region lookup".to_owned();
operation.target = Target::Rest(RestTarget {
base_url: "http://127.0.0.1:9".to_owned(),
method: HttpMethod::Get,
path_template: "/regions/{region}".to_owned(),
static_headers: Default::default(),
request_contract: None,
response_contract: Vec::new(),
multipart_field_mime_types: Default::default(),
});
operation.input_schema = object_schema("region");
operation
.input_schema
.fields
.get_mut("region")
.unwrap()
.enum_values = vec!["eu".to_owned(), "us".to_owned(), "apac".to_owned()];
operation.input_mapping = MappingSet {
rules: vec![MappingRule {
source: "$.mcp.region".to_owned(),
target: "$.request.path.region".to_owned(),
required: true,
default_value: None,
transform: None,
condition: None,
notes: None,
}],
};
registry
.create_operation(&workspace_id(), &operation, Some("alice"))
.await
.expect("the Read Operation is created through the registry API");
registry
.publish_operation(PublishRequest {
workspace_id: &workspace_id(),
operation_id: &operation.id,
version: 1,
published_at: &time::OffsetDateTime::now_utc(),
published_by: Some("alice"),
})
.await
.expect("the Read Operation exact version publishes");
}
/// One published Resource template with an enumerated `region` parameter.
async fn publish_template(
registry: &PostgresRegistry,
resource_id: &ResourceId,
uri_template: &str,
size: usize,
) {
let operation_name = "completion_lookup";
let options: Vec<String> = if size == 3 {
vec!["eu".into(), "us".into(), "apac".into()]
} else {
(0..size).map(|i| format!("item{i:04}")).collect()
};
let definition = json!({
"operation_id": format!("op_{operation_name}"),
"operation_version": 1,
"uri_template": uri_template,
"parameters": [{
"name": "region",
"input_path": "input.region",
"schema": {
"type": "string",
"required": true,
"enum_values": options
}
}],
"cache": {"ttl_ms": 60_000, "vary": ["region"], "max_bytes": 65_536},
"failure": {"outcome": "error"}
});
let source = TemplateSource::parse(&definition)
.expect("the template fixture satisfies the Story 3.10 source contract");
let definition_json =
serde_json::to_string(&source).expect("the normalized definition serializes canonically");
let digest = format!("{:x}", Sha256::digest(definition_json.as_bytes()));
let draft = registry
.create_resource_template_draft(CreateResourceTemplateDraftRequest {
workspace_id: workspace_id(),
resource_id: resource_id.clone(),
uri: format!("crank://templates/{}", resource_id.as_str()),
name: format!("{}_template", resource_id.as_str()),
description: "Bound completion fixture template".to_owned(),
kind: "text".to_owned(),
mime_type: "application/json".to_owned(),
sensitivity: "internal".to_owned(),
definition_json: definition_json.clone(),
definition_digest: digest.clone(),
created_at: time::OffsetDateTime::now_utc(),
})
.await
.expect("the template Draft is created through the registry API");
registry
.publish_resource_template_version(PublishResourceTemplateVersionRequest {
workspace_id: workspace_id(),
resource_id: resource_id.clone(),
expected_updated_at: draft.snapshot.resource.updated_at,
expected_version: draft.snapshot.version.version,
expected_digest: digest,
published_at: time::OffsetDateTime::now_utc(),
})
.await
.expect("the template exact version publishes");
}
@@ -0,0 +1,330 @@
//! #107 generic dispatch: existing wire GREEN precedes independent SDK RED assertions.
use super::rmcp_progress_cancel_runtime_tests::fixtures::Endpoint;
use super::{CounterSet, Snapshot, observe, support::*};
use serde_json::{Value, json};
use std::sync::{Arc, atomic::Ordering};
#[path = "rmcp_dispatch_test_support.rs"]
mod fixtures;
use fixtures::*;
async fn success_control() -> Vec<[usize; 2]> {
let h = endpoint().await;
let mut deltas = vec![];
for revision in [LEGACY_REVISION, MODERN_REVISION] {
let session = prepared(&h, revision).await;
for accept in ["application/json", "text/event-stream, application/json"] {
for id in [
json!("dispatch/Ю"),
json!(18446744073709551615u64),
json!(3.25),
] {
for method in ["tools/list", "ping"] {
let mut message = payload(revision, method, id.clone());
// Legacy params remain absent; modern identity metadata remains mandatory.
message["extension"] = json!({"future":[null,true]});
message["result"] = json!({"ignored":true});
message["error"] = json!({"ignored":"not an error envelope"});
let before = sdk(&h.counters);
let ops = h.sessions.snapshot();
let entries = h.counters.snapshot();
let response = request(&h, revision, session.as_deref(), accept, message)
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
assert_eq!(response.headers()["MCP-Protocol-Version"], revision);
assert!(response.headers().get("MCP-Session-Id").is_none());
let wire = body(response, accept.starts_with("text/event-stream")).await;
assert_eq!(wire["jsonrpc"], "2.0");
assert_eq!(wire["id"], id, "lossless response correlation");
assert!(wire.get("error").is_none(), "{wire}");
if method == "tools/list" {
assert_eq!(wire["result"]["tools"][0]["name"], TOOL_NAME);
assert_eq!(wire["result"]["tools"].as_array().unwrap().len(), 1);
} else {
let result = if revision == MODERN_REVISION {
json!({"protocolVersion":revision,"resultType":"complete"})
} else {
json!({"protocolVersion":revision})
};
assert_eq!(wire, json!({"jsonrpc":"2.0","id":id,"result":result}));
}
if revision == MODERN_REVISION {
assert_eq!(wire["result"]["resultType"], "complete");
assert_eq!(h.sessions.snapshot(), ops, "no modern session access");
}
assert_deltas(
method,
entries,
h.counters.snapshot(),
Snapshot::new(
1,
usize::from(revision == MODERN_REVISION),
usize::from(revision == LEGACY_REVISION),
usize::from(method == "tools/list"),
),
);
assert_eq!(h.started.load(Ordering::SeqCst), 0);
let after = sdk(&h.counters);
deltas.push([after[0] - before[0], after[1] - before[1]]);
}
}
}
}
eprintln!(
"existing GREEN 24 HTTP requests after legacy handshake / modern discovery; SDK deltas={deltas:?}"
);
deltas
}
#[tokio::test]
async fn wire_positive_control_json_sse_ids_tolerance_and_routes() {
success_control().await;
}
#[tokio::test]
async fn sdk_envelope_decoded_on_successful_http() {
let deltas = success_control().await;
assert_eq!(
deltas.iter().map(|d| d[0]).collect::<Vec<_>>(),
vec![1; 24],
"successful HTTP must decode a real SDK request envelope"
);
}
#[tokio::test]
async fn sdk_envelope_consumed_for_registry_routing_on_successful_http() {
let deltas = success_control().await;
assert_eq!(
deltas.iter().map(|d| d[1]).collect::<Vec<_>>(),
vec![1; 24],
"decoded SDK method/kind must drive registry routing, not decode-and-discard"
);
}
async fn notification_control() -> Vec<[usize; 2]> {
let h = endpoint().await;
let response = request(
&h,
LEGACY_REVISION,
None,
"application/json",
legacy_initialize(1),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
let session = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
let _ = body(response, false).await;
for method in ["notifications/initialized", "unknown/notification"] {
let mut wrong_kind = json!({"jsonrpc":"2.0","id":"wrong-kind","method":method});
let ops = h.sessions.snapshot();
let response = request(
&h,
LEGACY_REVISION,
Some(&session),
"application/json",
wrong_kind.clone(),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
assert_eq!(body(response, false).await["error"]["code"], -32601);
assert_eq!(h.sessions.snapshot().mark_initialized, ops.mark_initialized);
assert!(!h.sessions.retained(&session).await.unwrap().initialized);
wrong_kind.as_object_mut().unwrap().remove("id");
if method.starts_with("unknown") {
ack(request(
&h,
LEGACY_REVISION,
Some(&session),
"application/json",
wrong_kind,
)
.send()
.await
.unwrap())
.await;
assert!(!h.sessions.retained(&session).await.unwrap().initialized);
}
}
let before = sdk(&h.counters);
ack(request(
&h,
LEGACY_REVISION,
Some(&session),
"application/json",
initialized_notification(),
)
.send()
.await
.unwrap())
.await;
assert!(h.sessions.retained(&session).await.unwrap().initialized);
let after = sdk(&h.counters);
let mut deltas = vec![[after[0] - before[0], after[1] - before[1]]];
let ops = h.sessions.snapshot();
let entries = h.counters.snapshot();
for method in [
"notifications/initialized",
"unknown/notification",
"tools/call",
] {
let mut message = payload(MODERN_REVISION, method, json!(1));
message.as_object_mut().unwrap().remove("id");
let mut req = request(&h, MODERN_REVISION, None, "application/json", message)
.build()
.unwrap();
req.headers_mut().remove("Mcp-Method");
req.headers_mut().remove("Mcp-Name");
let before = sdk(&h.counters);
ack(reqwest::Client::new().execute(req).await.unwrap()).await;
let after = sdk(&h.counters);
deltas.push([after[0] - before[0], after[1] - before[1]]);
}
assert_eq!(h.sessions.snapshot(), ops);
assert_eq!(h.counters.snapshot().tools_service, entries.tools_service);
assert_eq!(h.started.load(Ordering::SeqCst), 0);
eprintln!("existing GREEN notification kind/state/early ACK; SDK deltas={deltas:?}");
deltas
}
#[tokio::test]
async fn notification_order_and_legacy_registered_kind_wire_control() {
notification_control().await;
}
#[tokio::test]
async fn sdk_notification_kind_consumed_on_successful_http() {
assert_eq!(
notification_control().await,
vec![[1, 1]; 4],
"SDK notification variant must drive legacy handshake / modern early ACK"
);
}
#[tokio::test]
async fn rejection_controls_never_enter_service_or_outbound() {
let h = endpoint().await;
for revision in [LEGACY_REVISION, MODERN_REVISION] {
let session = prepared(&h, revision).await;
let base = payload(revision, "tools/list", json!("reject"));
let mut malformed = base.clone();
malformed["jsonrpc"] = json!("1.0");
let mut bad_params = base.clone();
bad_params["params"] = json!([]);
let mut bad_meta = base.clone();
bad_meta["params"] = json!({"_meta":false});
let unknown = payload(revision, "unknown/request", json!("reject"));
let wrong_era = payload(
revision,
if revision == MODERN_REVISION {
"initialize"
} else {
"server/discover"
},
json!("reject"),
);
for (message, status, code) in [
(malformed, 400, -32600),
(bad_params, 400, -32600),
(bad_meta, 400, -32020),
(
unknown,
if revision == MODERN_REVISION {
404
} else {
200
},
-32601,
),
(
wrong_era,
if revision == MODERN_REVISION {
404
} else {
200
},
-32601,
),
] {
let entries = h.counters.snapshot();
let ops = h.sessions.snapshot();
let response = request(
&h,
revision,
session.as_deref(),
"application/json",
message.clone(),
)
.send()
.await
.unwrap();
assert_eq!(response.status().as_u16(), status, "{revision} {message}");
let wire = body(response, false).await;
assert_eq!(wire["error"]["code"], code, "{revision} {message}: {wire}");
assert!(wire.get("result").is_none());
assert_eq!(h.counters.snapshot().tools_service, entries.tools_service);
assert_eq!(h.started.load(Ordering::SeqCst), 0);
if revision == MODERN_REVISION {
assert_eq!(h.sessions.snapshot(), ops);
}
}
}
for (header, value) in [("Mcp-Method", "ping"), ("Mcp-Name", "wrong-tool")] {
let ops = h.sessions.snapshot();
let entries = h.counters.snapshot();
let message = json!({"jsonrpc":"2.0","id":"headers","method":"tools/call","params":{"name":TOOL_NAME,"arguments":{"email":"dispatch@example.com"},"_meta":modern_meta()}});
let response = request(&h, MODERN_REVISION, None, "application/json", message)
.header(header, value)
.send()
.await
.unwrap();
assert_eq!(response.status(), 400);
assert!(body(response, false).await.get("error").is_some());
assert_eq!(h.sessions.snapshot(), ops);
assert_eq!(h.counters.snapshot().tools_service, entries.tools_service);
assert_eq!(h.started.load(Ordering::SeqCst), 0);
}
let ops = h.sessions.snapshot();
let response = request(
&h,
"2099-01-01",
None,
"application/json",
payload(LEGACY_REVISION, "tools/list", json!(1)),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 400);
assert!(body(response, false).await.get("error").is_some());
assert_eq!(h.sessions.snapshot(), ops);
assert_eq!(h.started.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn observer_independent_positive_control() {
let a = Arc::new(CounterSet::default());
let b = Arc::new(CounterSet::default());
super::sdk_dispatch_envelope_decoded();
super::sdk_dispatch_envelope_consumed();
assert_eq!(sdk(&a), [0, 0]);
tokio::join!(
observe(a.clone(), async {
super::sdk_dispatch_envelope_decoded();
}),
observe(b.clone(), async {
super::sdk_dispatch_envelope_consumed();
})
);
assert_eq!(sdk(&a), [1, 0]);
assert_eq!(sdk(&b), [0, 1]);
assert_eq!(a.snapshot(), Snapshot::new(0, 0, 0, 0));
assert_eq!(b.snapshot(), Snapshot::new(0, 0, 0, 0));
}
@@ -0,0 +1,103 @@
//! Narrow HTTP helpers; all app/auth/catalog/upstream fixtures remain shared.
use super::*;
pub(super) async fn endpoint() -> Endpoint {
crank_test_support::postgres_schema_url_for_task("dispatch_owner", "t_e862066c").await;
Endpoint::started().await
}
pub(super) fn sdk(c: &CounterSet) -> [usize; 2] {
[c.sdk_dispatch_decoded(), c.sdk_dispatch_consumed()]
}
pub(super) fn payload(revision: &str, method: &str, id: Value) -> Value {
let mut message = json!({"jsonrpc":"2.0","id":id,"method":method});
if revision == MODERN_REVISION {
message["params"] = json!({"_meta":modern_meta()});
}
message
}
pub(super) fn request(
h: &Endpoint,
revision: &str,
session: Option<&str>,
accept: &str,
message: Value,
) -> reqwest::RequestBuilder {
h.request(revision, session, &h.key, accept, message)
.header("x-request-id", "dispatch-atdd")
.header(
"traceparent",
"00-11111111111111111111111111111111-2222222222222222-01",
)
}
pub(super) async fn body(response: reqwest::Response, sse: bool) -> Value {
assert_eq!(response.headers()["x-request-id"], "dispatch-atdd");
assert_eq!(
response.headers()["x-trace-id"],
"11111111111111111111111111111111"
);
assert!(
response.headers()["content-type"]
.to_str()
.unwrap()
.starts_with(if sse {
"text/event-stream"
} else {
"application/json"
})
);
let raw = tokio::time::timeout(std::time::Duration::from_secs(6), response.text())
.await
.unwrap()
.unwrap();
if sse {
let frames: Vec<Value> = raw
.lines()
.filter_map(|l| l.strip_prefix("data:"))
.map(|l| serde_json::from_str(l.trim()).unwrap())
.collect();
assert_eq!(frames.len(), 1, "one terminal SSE frame: {raw}");
frames.into_iter().next().unwrap()
} else {
serde_json::from_str(&raw).unwrap()
}
}
pub(super) async fn ack(response: reqwest::Response) {
assert_eq!(response.status(), 202);
assert!(response.headers().get("content-type").is_none());
assert!(response.headers().get("MCP-Session-Id").is_none());
assert!(response.bytes().await.unwrap().is_empty());
}
pub(super) async fn prepared(h: &Endpoint, revision: &str) -> Option<String> {
if revision == LEGACY_REVISION {
return h.session(revision).await;
}
let ops = h.sessions.snapshot();
let response = request(
h,
revision,
None,
"application/json",
payload(revision, "server/discover", json!(1)),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
assert_eq!(response.headers()["MCP-Protocol-Version"], revision);
assert!(response.headers().get("MCP-Session-Id").is_none());
let wire = body(response, false).await;
assert_eq!(
wire["result"]["supportedVersions"],
json!([MODERN_REVISION, LEGACY_REVISION])
);
assert_eq!(wire["result"]["resultType"], "complete");
assert!(wire.get("error").is_none(), "{wire}");
assert_eq!(h.sessions.snapshot(), ops);
None
}
@@ -0,0 +1,331 @@
//! #107 envelope migration: independent real HTTP wire and SDK participation controls.
use super::{CounterSet, observe, support::*};
use serde_json::{Value, json};
use std::{sync::Arc, time::Duration};
// Reuse the existing real build_app, published catalog, auth and loopback execution fixture.
use super::rmcp_progress_cancel_runtime_tests::fixtures::Endpoint;
fn counts(c: &CounterSet) -> [usize; 2] {
[c.sdk_jsonrpc_response(), c.sdk_jsonrpc_error()]
}
async fn wire(
response: reqwest::Response,
revision: &str,
id: &Value,
sse: bool,
session: Option<&str>,
) -> Value {
assert_eq!(
response.status(),
200,
"envelope id={id}, SSE={sse}, revision={revision}"
);
assert_eq!(response.headers()["MCP-Protocol-Version"], revision);
assert_eq!(
response
.headers()
.get("MCP-Session-Id")
.map(|v| v.to_str().unwrap()),
if sse { session } else { None }
);
let request_id = response.headers()["x-request-id"]
.to_str()
.unwrap()
.to_owned();
let trace_id = response.headers()["x-trace-id"]
.to_str()
.unwrap()
.to_owned();
let content_type = response.headers()["content-type"].to_str().unwrap();
assert!(content_type.starts_with(if sse {
"text/event-stream"
} else {
"application/json"
}));
let raw = tokio::time::timeout(Duration::from_secs(6), response.text())
.await
.unwrap()
.unwrap();
let body: Value = if sse {
let frames: Vec<Value> = raw
.lines()
.filter_map(|line| line.strip_prefix("data:"))
.map(|data| serde_json::from_str(data.trim()).unwrap())
.collect();
let terminals: Vec<_> = frames.iter().filter(|f| f.get("id").is_some()).collect();
assert_eq!(terminals.len(), 1, "one terminal envelope: {raw}");
assert_eq!(frames.last(), Some(terminals[0]), "terminal must be last");
for frame in &frames[..frames.len() - 1] {
assert_eq!(frame["method"], "notifications/progress");
assert_eq!(frame["params"]["progressToken"], "envelope-stream");
}
terminals[0].clone()
} else {
serde_json::from_str(&raw).unwrap()
};
assert_eq!(body["jsonrpc"], "2.0");
assert_eq!(body["id"], *id);
assert_eq!(
body["id"].to_string(),
id.to_string(),
"preserve numeric form"
);
assert_eq!(
body.as_object().unwrap().len(),
3,
"no envelope extensions: {body}"
);
assert!(body.get("method").is_none());
if let Some(error) = body.get("error") {
assert!(
body.get("result").is_none(),
"protocol error, not tool isError"
);
assert_eq!(error["code"], -32602);
if sse {
// Existing stream polling has no request correlation scope; preserve omission.
assert!(
error.get("data").is_none(),
"existing SSE omission: {error}"
);
assert_eq!(error.as_object().unwrap().len(), 2);
} else {
assert_eq!(
error["data"],
json!({"request_id":request_id,"trace_id":trace_id})
);
assert_eq!(error.as_object().unwrap().len(), 3);
}
} else {
assert!(body.get("result").is_some());
}
body
}
async fn control(revision: &str) -> Vec<[usize; 2]> {
// Own the process-wide disposable DB before the reused fixture's older ownership hint.
crank_test_support::postgres_schema_url_for_task("envelopes_owner", "t_b27f5c9d").await;
let h = Endpoint::started().await;
let session = h.session(revision).await;
let mut deltas = vec![];
for id in [
json!("opaque/Ю"),
json!(""),
json!(i64::MIN),
json!(i64::MAX),
json!(9007199254740993_u64),
json!(u64::MAX),
json!(1.5),
json!(1.0),
json!(-0.0),
] {
for invalid in [false, true] {
let mut params = if invalid {
json!({"cursor":"not-a-cursor"})
} else {
json!({"cursor":"1"})
};
if revision == MODERN_REVISION {
params["_meta"] = modern_meta();
}
let before = counts(&h.counters);
let response = h
.request(
revision,
session.as_deref(),
&h.key,
"application/json",
json!({"jsonrpc":"2.0","id":id,"method":"tools/list","params":params}),
)
.send()
.await
.unwrap();
let body = wire(response, revision, &id, false, session.as_deref()).await;
if invalid {
assert_eq!(body["error"]["message"], "invalid tools/list cursor");
} else {
let mut expected = json!({"tools":[]});
if revision == MODERN_REVISION {
expected["resultType"] = json!("complete");
expected["cacheScope"] = json!("private");
expected["ttlMs"] = json!(0);
}
assert_eq!(
body["result"], expected,
"empty result and omission of nextCursor"
);
}
let after = counts(&h.counters);
deltas.push([after[0] - before[0], after[1] - before[1]]);
}
}
// Both revisions support streamed calls with the combined Accept negotiation.
// Missing tool resolves inside the shared Tools command, after the stream opens.
for sse in [false, true] {
for invalid in [false, true] {
let id = if invalid {
json!(u64::MAX)
} else {
json!(-0.0)
};
let mut params =
json!({"name":TOOL_NAME,"arguments":{"email":"envelopes@example.test"}});
if revision == MODERN_REVISION {
params["_meta"] = modern_meta();
}
if invalid {
params["name"] = json!("missing_tool");
} else {
h.gate.add_permits(1);
}
if sse {
params["_meta"]["progressToken"] = json!("envelope-stream");
}
let before = counts(&h.counters);
let mut request = h
.request(
revision,
session.as_deref(),
&h.key,
if sse {
"text/event-stream, application/json"
} else {
"application/json"
},
json!({"jsonrpc":"2.0","id":id,"method":"tools/call","params":params}),
)
.build()
.unwrap();
if revision == MODERN_REVISION {
request.headers_mut().insert(
"Mcp-Name",
if invalid { "missing_tool" } else { TOOL_NAME }
.parse()
.unwrap(),
);
}
let response = reqwest::Client::new().execute(request).await.unwrap();
let body = wire(response, revision, &id, sse, session.as_deref()).await;
if invalid {
assert_eq!(body["error"]["message"], "tool missing_tool was not found");
} else {
assert_eq!(
body["result"].as_object().unwrap().len(),
if revision == MODERN_REVISION { 4 } else { 3 }
);
assert_eq!(body["result"]["isError"], false);
assert_eq!(
body["result"]["structuredContent"],
json!({"id":"lead_progress"})
);
assert_eq!(
body["result"]["content"],
json!([{"type":"text","text":serde_json::to_string_pretty(&json!({"id":"lead_progress"})).unwrap()}])
);
if revision == MODERN_REVISION {
assert_eq!(body["result"]["resultType"], "complete");
} else {
assert!(body["result"].get("resultType").is_none());
}
}
let after = counts(&h.counters);
deltas.push([after[0] - before[0], after[1] - before[1]]);
}
}
eprintln!(
"existing GREEN envelopes wire {revision}: exact id forms, JSON success/protocol error, terminal SSE success/protocol error; SDK deltas={deltas:?}"
);
deltas
}
#[tokio::test]
async fn rmcp_envelopes_wire_legacy() {
control(LEGACY_REVISION).await;
}
#[tokio::test]
async fn rmcp_envelopes_wire_modern() {
control(MODERN_REVISION).await;
}
#[tokio::test]
async fn rmcp_envelopes_sdk_legacy() {
let actual = control(LEGACY_REVISION).await;
let expected: Vec<_> = (0..actual.len())
.map(|i| if i % 2 == 0 { [1, 0] } else { [0, 1] })
.collect();
assert_eq!(
actual, expected,
"real SDK envelope serialization after complete HTTP body"
);
}
#[tokio::test]
async fn rmcp_envelopes_sdk_modern() {
let actual = control(MODERN_REVISION).await;
let expected: Vec<_> = (0..actual.len())
.map(|i| if i % 2 == 0 { [1, 0] } else { [0, 1] })
.collect();
assert_eq!(
actual, expected,
"real SDK envelope serialization after complete HTTP body"
);
}
#[tokio::test]
async fn rmcp_envelopes_observer_positive_control() {
let c = Arc::new(CounterSet::default());
observe(c.clone(), async {
super::sdk_jsonrpc_response_serialized();
super::sdk_jsonrpc_error_serialized();
})
.await;
assert_eq!(counts(&c), [1, 1]);
super::sdk_jsonrpc_response_serialized();
super::sdk_jsonrpc_error_serialized();
assert_eq!(counts(&c), [1, 1], "out-of-scope markers are no-ops");
}
#[test]
fn rmcp_envelopes_helper_lossless_control() {
use crate::jsonrpc::{jsonrpc_error, jsonrpc_result};
let values = [
json!(null),
json!(""),
json!("Ю"),
json!(i64::MIN),
json!(i64::MAX),
json!(9007199254740993_u64),
json!(u64::MAX),
json!(1.5),
json!(1.0),
json!(-0.0),
json!([null, {"nested":u64::MAX}]),
json!({"x":null}),
];
for id in &values {
for result in &values {
let actual = jsonrpc_result(id.clone(), result.clone());
let expected = json!({"jsonrpc":"2.0","id":id,"result":result});
assert_eq!(actual, expected);
assert_eq!(actual.to_string(), expected.to_string());
}
for code in [
i64::MIN,
i32::MIN as i64 - 1,
i32::MIN as i64,
-32602,
i32::MAX as i64,
i32::MAX as i64 + 1,
i64::MAX,
] {
for message in ["", "Ошибка Ю"] {
let actual = jsonrpc_error(id.clone(), code, message);
let expected =
json!({"jsonrpc":"2.0","id":id,"error":{"code":code,"message":message}});
assert_eq!(actual, expected, "data absent outside correlation scope");
assert_eq!(actual.to_string(), expected.to_string());
}
}
}
}
@@ -0,0 +1,409 @@
use super::*;
#[tokio::test]
async fn rmcp_http_catalog_follows_new_publication_without_reinitializing() {
let h = Harness::new().await;
let session = h.initialize().await;
let list = json!({"jsonrpc":"2.0","id":60,"method":"tools/list"});
let listed = frames(
h.request("2025-11-25", Some(&session), &list)
.send()
.await
.unwrap(),
)
.await;
assert_eq!(
listed.last().unwrap()["result"]["tools"][0]["name"],
"create_lead"
);
let workspace = crank_core::WorkspaceId::new("ws_default");
let agent = crank_core::AgentId::new("agent_dispatch-observer");
let now = time::OffsetDateTime::now_utc();
let version = crank_core::AgentVersion {
agent_id: agent.clone(),
version: 2,
status: crank_core::AgentStatus::Draft,
instructions: json!({}),
tool_selection_policy: crank_core::ToolSelectionPolicy::default(),
created_at: now,
};
let bindings = [crank_core::AgentOperationBinding {
operation_id: crank_core::OperationId::new("op_create_lead"),
operation_version: 1,
tool_name: "new_publication_tool".into(),
tool_title: "New Tool".into(),
tool_description_override: None,
enabled: true,
}];
h.state
.registry
.create_agent_draft_version(crank_registry::CreateAgentDraftVersionRequest {
workspace_id: &workspace,
agent_id: &agent,
version: &version,
bindings: &bindings,
updated_at: &now,
})
.await
.unwrap();
h.state
.registry
.publish_agent(crank_registry::PublishAgentRequest {
workspace_id: &workspace,
agent_id: &agent,
version: 2,
published_at: &now,
published_by: Some("alice"),
expected_state: None,
})
.await
.unwrap();
let listed = frames(
h.request("2025-11-25", Some(&session), &list)
.send()
.await
.unwrap(),
)
.await;
assert_eq!(
listed.last().unwrap()["result"]["tools"][0]["name"],
"new_publication_tool"
);
let mut message = call(61, "new-publication@example.test", false, false);
message["params"]["name"] = json!("new_publication_tool");
let result = frames(
h.request("2025-11-25", Some(&session), &message)
.send()
.await
.unwrap(),
)
.await;
assert_eq!(
result.last().unwrap()["result"]["structuredContent"]["id"],
"lead_for_new-publication@example.test"
);
assert_eq!(h.calls.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn rmcp_http_identical_request_ids_in_two_sessions_cancel_independently() {
let h = Harness::new().await;
let a = h.initialize().await;
let b = h.initialize().await;
let message = call(62, "slow-same-id@example.test", false, true);
let first = h
.request("2025-11-25", Some(&a), &message)
.send()
.await
.unwrap();
let second = h
.request("2025-11-25", Some(&b), &message)
.send()
.await
.unwrap();
wait_count(&h.calls, 2).await;
let cancel =
json!({"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":62}});
assert_eq!(
h.request("2025-11-25", Some(&a), &cancel)
.send()
.await
.unwrap()
.status(),
202
);
wait_count(&h.active, 1).await;
assert_eq!(
h.request("2025-11-25", Some(&b), &cancel)
.send()
.await
.unwrap()
.status(),
202
);
wait_count(&h.active, 0).await;
for response in [first, second] {
assert_eq!(
frames(response).await.last().unwrap()["error"]["message"],
"request cancelled"
);
}
assert_eq!(h.calls.load(Ordering::SeqCst), 2);
}
// Independent QA: dropped legacy response bodies must not bypass the stream ceiling.
#[tokio::test]
async fn rmcp_http_qa_legacy_disconnect_does_not_bypass_admission() {
let h = Harness::new().await;
let session = h.initialize().await;
let mut admitted = Vec::new();
for id in 100..117 {
let response = h
.request(
"2025-11-25",
Some(&session),
&call(id, "slow-qa-limit@example.test", false, true),
)
.send()
.await
.unwrap();
if response.status() == 429 {
break;
}
assert_eq!(response.status(), 200);
admitted.push(id);
wait_count(&h.calls, admitted.len()).await;
drop(response);
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
let active_before_cleanup = h.active.load(Ordering::SeqCst);
let free_slots = h.state.session_slots.available_permits();
let calls_before_cleanup = h.calls.load(Ordering::SeqCst);
for id in &admitted {
let cancel =
json!({"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":id}});
assert_eq!(
h.request("2025-11-25", Some(&session), &cancel)
.send()
.await
.unwrap()
.status(),
202
);
}
wait_count(&h.active, 0).await;
assert!(
admitted.len() <= 16 && calls_before_cleanup <= 16 && active_before_cleanup <= 16,
"legacy disconnect bypassed admission: admitted={}, calls={calls_before_cleanup}, active={active_before_cleanup}, free_slots={free_slots}",
admitted.len()
);
tokio::time::timeout(std::time::Duration::from_secs(2), async {
while h.state.session_slots.available_permits() != 16 {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
})
.await
.expect("owned cancellation cleanup must release all admission permits");
assert_eq!(h.state.session_slots.available_permits(), 16);
// After cleanup, normal admission and REST execution must work again.
let calls_before = h.calls.load(Ordering::SeqCst);
let message = call(199, "recovery-after-cleanup@example.test", false, false);
let response = h
.request("2025-11-25", Some(&session), &message)
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
let result = frames(response).await;
assert_eq!(
result.last().unwrap()["result"]["structuredContent"]["id"],
"lead_for_recovery-after-cleanup@example.test"
);
assert_eq!(h.calls.load(Ordering::SeqCst), calls_before + 1);
}
#[tokio::test]
async fn rmcp_http_correlation_headers_preserve_each_concurrent_request() {
let h = Harness::new().await;
let first = call(50, "correlation-first@example.test", true, false);
let second = call(51, "correlation-second@example.test", true, false);
let (a, b) = tokio::join!(
h.request("2026-07-28", None, &first)
.header("x-request-id", "rmcp-oracle-first")
.send(),
h.request("2026-07-28", None, &second)
.header("x-request-id", "rmcp-oracle-second")
.send()
);
let a = a.unwrap();
let b = b.unwrap();
assert_eq!(a.headers()["x-request-id"], "rmcp-oracle-first");
assert_eq!(b.headers()["x-request-id"], "rmcp-oracle-second");
assert_eq!(
frames(a).await[0]["result"]["structuredContent"]["id"],
"lead_for_correlation-first@example.test"
);
assert_eq!(
frames(b).await[0]["result"]["structuredContent"]["id"],
"lead_for_correlation-second@example.test"
);
}
#[tokio::test]
async fn rmcp_http_stream_limit_rejects_without_dispatch_then_recovers() {
let h = Harness::new().await;
let permits = h
.state
.session_slots
.clone()
.acquire_many_owned(16)
.await
.unwrap();
let message = call(40, "limit@example.test", true, false);
assert_eq!(
h.request("2026-07-28", None, &message)
.send()
.await
.unwrap()
.status(),
429
);
assert_eq!(h.calls.load(Ordering::SeqCst), 0);
drop(permits);
let response = h
.request("2026-07-28", None, &message)
.send()
.await
.unwrap();
assert_eq!(
frames(response).await[0]["result"]["structuredContent"]["id"],
"lead_for_limit@example.test"
);
assert_eq!(h.calls.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn rmcp_http_rate_limit_is_existing_admission_not_sdk_default() {
let h = Harness::with_rate(1).await;
let list = json!({"jsonrpc":"2.0","id":41,"method":"tools/list","params":{"_meta":meta()}});
assert_eq!(
h.request("2026-07-28", None, &list)
.send()
.await
.unwrap()
.status(),
200
);
assert_eq!(
h.request(
"2026-07-28",
None,
&call(42, "rate@example.test", true, false)
)
.send()
.await
.unwrap()
.status(),
429
);
assert_eq!(h.calls.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn rmcp_http_current_durable_store_survives_recreation_but_is_not_sdk_store() {
use crate::session::{PostgresTransportSessionStore, TransportSessionStore};
let h = Harness::new().await;
let store = PostgresTransportSessionStore::from_pool(h.state.registry.pool().clone())
.await
.unwrap();
let now = time::OffsetDateTime::now_utc();
let id = store
.create(
"2025-11-25",
"default",
"dispatch-observer",
false,
now,
Some(now + time::Duration::hours(24)),
)
.await
.unwrap();
assert!(store.mark_initialized(&id, now).await.unwrap());
drop(store);
let restarted = PostgresTransportSessionStore::from_pool(h.state.registry.pool().clone())
.await
.unwrap();
let persisted = restarted.get(&id).await.unwrap().unwrap();
assert!(persisted.initialized);
assert_eq!(persisted.workspace_slug, "default");
assert_eq!(persisted.protocol_version, "2025-11-25");
assert_eq!(
h.request(
"2025-11-25",
Some(&id),
&call(43, "durable@example.test", false, false)
)
.send()
.await
.unwrap()
.status(),
404
);
assert!(
restarted.get(&id).await.unwrap().is_some(),
"SDK experiment never mutates existing storage"
);
assert_eq!(h.calls.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn rmcp_http_json_modern_and_legacy_sse_only_gap_are_observed() {
let h = Harness::new().await;
let session = h.initialize().await;
let response = h
.request(
"2026-07-28",
None,
&call(44, "json@example.test", true, false),
)
.send()
.await
.unwrap();
assert!(
response.headers()["content-type"]
.to_str()
.unwrap()
.starts_with("application/json")
);
assert_eq!(
frames(response).await[0]["result"]["structuredContent"]["id"],
"lead_for_json@example.test"
);
// Build a JSON-only request (not two contradictory Accept carriers).
let response = reqwest::Client::new()
.post(format!("{}/v1/default/dispatch-observer", h.server.1))
.bearer_auth(&h.key)
.header("Accept", "application/json")
.header("MCP-Protocol-Version", "2025-11-25")
.header("MCP-Session-Id", &session)
.json(&call(45, "legacy-json@example.test", false, false))
.send()
.await
.unwrap();
assert_eq!(
response.status(),
406,
"bundled SDK legacy mode requires SSE Accept; not transport parity"
);
assert_eq!(h.calls.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn rmcp_http_concurrent_keys_keep_outputs_and_cancel_ownership_separate() {
let h = Harness::new().await;
let session = h.initialize().await;
sqlx::query("insert into platform_api_keys (id, workspace_id, agent_id, key_kind, name, prefix, secret_hash, scopes_json, status, created_at) select 'pk_second', workspace_id, agent_id, key_kind, 'second', prefix, $1, scopes_json, status, created_at from platform_api_keys where id = 'pk_dispatch_observer'")
.bind(crate::access::hash_access_secret("test-second-valid-key")).execute(h.state.registry.pool()).await.unwrap();
let first = call(46, "first@example.test", false, false);
let second = call(47, "second@example.test", false, false);
let (a, b) = tokio::join!(
h.request("2025-11-25", Some(&session), &first).send(),
h.request_as(
"2025-11-25",
Some(&session),
&second,
"test-second-valid-key"
)
.send()
);
assert_eq!(
frames(a.unwrap()).await.last().unwrap()["result"]["structuredContent"]["id"],
"lead_for_first@example.test"
);
assert_eq!(
frames(b.unwrap()).await.last().unwrap()["result"]["structuredContent"]["id"],
"lead_for_second@example.test"
);
assert_eq!(h.calls.load(Ordering::SeqCst), 2);
}
@@ -0,0 +1,563 @@
//! Wire oracle for the isolated SDK adapter, with real registry and REST execution.
#[path = "rmcp_http_limits.rs"]
mod limits;
use super::{AgentRoutePath, dispatch_observer::support, rmcp_http, rmcp_tools_tests::test_state};
use axum::{Json, Router, extract::State, routing::post};
use serde_json::{Value, json};
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
struct Server(tokio::task::JoinHandle<()>, String);
impl Drop for Server {
fn drop(&mut self) {
self.0.abort();
}
}
async fn serve(router: Router) -> Server {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!("http://{}", listener.local_addr().unwrap());
Server(
tokio::spawn(async move {
axum::serve(listener, router).await.unwrap();
}),
url,
)
}
async fn upstream(State(count): State<Arc<AtomicUsize>>, Json(body): Json<Value>) -> Json<Value> {
count.fetch_add(1, Ordering::SeqCst);
if body["email"].as_str().unwrap().starts_with("slow") {
tokio::time::sleep(std::time::Duration::from_secs(8)).await;
}
Json(json!({"id": format!("lead_for_{}", body["email"].as_str().unwrap())}))
}
struct Harness {
server: Server,
_upstream: Server,
key: String,
calls: Arc<AtomicUsize>,
state: Arc<super::AppState>,
active: Arc<AtomicUsize>,
}
impl Harness {
async fn new() -> Self {
Self::with_rate(10_000).await
}
async fn with_rate(burst: u32) -> Self {
let mut state = test_state().await;
Arc::get_mut(&mut state).unwrap().api_rate_limiter = crank_runtime::RequestRateLimiter::new(
crank_runtime::RequestRateLimitConfig::new(burst, burst).unwrap(),
);
let calls = Arc::new(AtomicUsize::new(0));
let upstream = serve(
Router::new()
.route("/crm/leads", post(upstream))
.with_state(calls.clone()),
)
.await;
let mut operation = support::test_operation(&upstream.1);
operation.execution_config.timeout_ms = 10_000;
state
.registry
.create_operation(
&crank_core::WorkspaceId::new("ws_default"),
&operation,
Some("alice"),
)
.await
.unwrap();
state
.registry
.publish_operation(crank_registry::PublishRequest {
workspace_id: &crank_core::WorkspaceId::new("ws_default"),
operation_id: &operation.id,
version: 1,
published_at: &operation.created_at,
published_by: Some("alice"),
})
.await
.unwrap();
support::publish_agent(&state.registry, &operation).await;
let key = support::create_api_key(&state.registry).await;
let active = Arc::new(AtomicUsize::new(0));
let server = serve(rmcp_http::router(
state.clone(),
AgentRoutePath {
workspace_slug: "default".into(),
agent_slug: "dispatch-observer".into(),
},
active.clone(),
))
.await;
Self {
server,
_upstream: upstream,
key,
calls,
state,
active,
}
}
fn request(
&self,
revision: &str,
session: Option<&str>,
message: &Value,
) -> reqwest::RequestBuilder {
self.request_as(revision, session, message, &self.key)
}
fn request_as(
&self,
revision: &str,
session: Option<&str>,
message: &Value,
key: &str,
) -> reqwest::RequestBuilder {
let mut request = reqwest::Client::new()
.post(format!("{}/v1/default/dispatch-observer", self.server.1))
.bearer_auth(key)
.header("Accept", "application/json, text/event-stream")
.header("MCP-Protocol-Version", revision);
if let Some(session) = session {
request = request.header("MCP-Session-Id", session);
}
if revision == "2026-07-28" {
request = request.header("Mcp-Method", message["method"].as_str().unwrap());
if let Some(name) = message.pointer("/params/name").and_then(Value::as_str) {
request = request.header("Mcp-Name", name);
}
}
request
.json(message)
.timeout(std::time::Duration::from_secs(10))
}
async fn initialize(&self) -> String {
let message = json!({"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"oracle","version":"1"}}});
let response = self
.request("2025-11-25", None, &message)
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
let session = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
let body = frames(response).await;
assert_eq!(
body.last().unwrap()["result"]["protocolVersion"],
"2025-11-25"
);
let response = self
.request(
"2025-11-25",
Some(&session),
&json!({"jsonrpc":"2.0","method":"notifications/initialized"}),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 202);
session
}
}
fn meta() -> Value {
json!({"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientCapabilities":{},"io.modelcontextprotocol/clientInfo":{"name":"oracle","version":"1"}})
}
async fn frames(response: reqwest::Response) -> Vec<Value> {
let status = response.status();
let text = response.text().await.unwrap();
if let Ok(json) = serde_json::from_str(&text) {
return vec![json];
}
let values: Vec<Value> = text
.lines()
.filter_map(|line| line.strip_prefix("data:"))
.filter_map(|data| serde_json::from_str(data.trim()).ok())
.collect();
assert!(!values.is_empty(), "{status}: {text}");
values
}
#[tokio::test]
async fn rmcp_http_positive_tools_both_revisions() {
let h = Harness::new().await;
let session = h.initialize().await;
for (revision, session) in [("2025-11-25", Some(session.as_str())), ("2026-07-28", None)] {
if session.is_none() {
let discover = json!({"jsonrpc":"2.0","id":2,"method":"server/discover","params":{"_meta":meta()}});
let response = h.request(revision, None, &discover).send().await.unwrap();
assert_eq!(response.status(), 200);
assert!(!frames(response).await[0]["result"].is_null());
}
let list = json!({"jsonrpc":"2.0","id":3,"method":"tools/list","params":{"_meta": if session.is_none() { meta() } else { json!({}) }}});
let response = h.request(revision, session, &list).send().await.unwrap();
assert_eq!(response.status(), 200);
assert!(response.headers().contains_key("x-request-id"));
let listed = frames(response).await;
assert_eq!(
listed.last().unwrap()["result"]["tools"][0]["name"],
"create_lead"
);
let call = json!({"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"create_lead","arguments":{"email":"rmcp@example.test"},"_meta":if session.is_none() { meta() } else { json!({}) }}});
let response = h.request(revision, session, &call).send().await.unwrap();
assert_eq!(response.status(), 200);
if session.is_none() {
assert!(!response.headers().contains_key("MCP-Session-Id"));
}
let result = frames(response).await;
assert_eq!(
result.last().unwrap()["result"]["structuredContent"]["id"],
"lead_for_rmcp@example.test",
"{result:?}"
);
}
assert_eq!(h.calls.load(Ordering::SeqCst), 2);
}
fn call(id: i64, email: &str, modern: bool, progress: bool) -> Value {
let mut metadata = if modern { meta() } else { json!({}) };
if progress {
metadata["progressToken"] = json!("wire-progress");
}
json!({"jsonrpc":"2.0","id":id,"method":"tools/call","params":{"name":"create_lead","arguments":{"email":email},"_meta":metadata}})
}
#[tokio::test]
async fn rmcp_http_credentials_revalidated_after_initialize_and_no_outbound() {
let h = Harness::new().await;
let session = h.initialize().await;
let message = call(10, "denied@example.test", false, false);
assert_eq!(
h.request_as("2025-11-25", Some(&session), &message, "invalid-test-key")
.send()
.await
.unwrap()
.status(),
401
);
// A scope change and expiry are authoritative registry changes, not session credentials.
sqlx::query("update platform_api_keys set scopes_json = '[\"read\"]'::jsonb where id = 'pk_dispatch_observer'").execute(h.state.registry.pool()).await.unwrap();
assert_eq!(
h.request("2025-11-25", Some(&session), &message)
.send()
.await
.unwrap()
.status(),
403
);
sqlx::query("update platform_api_keys set scopes_json = '[\"read\",\"write\"]'::jsonb, expires_at = now() - interval '1 second' where id = 'pk_dispatch_observer'").execute(h.state.registry.pool()).await.unwrap();
assert_eq!(
h.request("2025-11-25", Some(&session), &message)
.send()
.await
.unwrap()
.status(),
401
);
sqlx::query("update platform_api_keys set expires_at = null where id = 'pk_dispatch_observer'")
.execute(h.state.registry.pool())
.await
.unwrap();
h.state
.registry
.revoke_platform_api_key(
&crank_core::WorkspaceId::new("ws_default"),
&crank_core::PlatformApiKeyId::new("pk_dispatch_observer"),
&time::OffsetDateTime::now_utc(),
)
.await
.unwrap();
for method in [reqwest::Method::GET, reqwest::Method::DELETE] {
assert_eq!(
reqwest::Client::new()
.request(
method,
format!("{}/v1/default/dispatch-observer", h.server.1)
)
.bearer_auth(&h.key)
.header("MCP-Session-Id", &session)
.header("MCP-Protocol-Version", "2025-11-25")
.header("Accept", "text/event-stream")
.send()
.await
.unwrap()
.status(),
401
);
}
assert_eq!(
h.request("2025-11-25", Some(&session), &message)
.send()
.await
.unwrap()
.status(),
401
);
assert_eq!(h.calls.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn rmcp_http_admission_rejects_foreign_route_revision_headers_and_body_overflow() {
let h = Harness::new().await;
let message = call(11, "denied@example.test", true, false);
let response = h
.request("2026-07-28", Some("foreign"), &message)
.send()
.await
.unwrap();
assert_eq!(response.status(), 400);
let response = h
.request("2026-07-28", None, &message)
.header("Mcp-Name", "foreign")
.send()
.await
.unwrap();
assert_eq!(response.status(), 400);
let response = h
.request("2026-07-28", None, &message)
.header("Origin", "https://evil.example")
.send()
.await
.unwrap();
assert_eq!(response.status(), 403);
assert_eq!(
h.request("2030-01-01", None, &message)
.send()
.await
.unwrap()
.status(),
400
);
let response = reqwest::Client::new()
.post(format!("{}/v1/default/foreign-agent", h.server.1))
.bearer_auth(&h.key)
.json(&message)
.send()
.await
.unwrap();
assert_eq!(response.status(), 404);
let response = h
.request("2026-07-28", None, &message)
.body(vec![b' '; 2 * 1024 * 1024 + 1])
.send()
.await
.unwrap();
assert_eq!(response.status(), 413);
assert_eq!(h.calls.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn rmcp_http_progress_is_real_and_tool_error_is_not_protocol_error() {
let h = Harness::new().await;
let session = h.initialize().await;
for (revision, session) in [("2025-11-25", Some(session.as_str())), ("2026-07-28", None)] {
let response = h
.request(
revision,
session,
&call(20, "progress@example.test", session.is_none(), true),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
assert!(
response.headers()["content-type"]
.to_str()
.unwrap()
.starts_with("text/event-stream")
);
let messages = frames(response).await;
let progress: Vec<_> = messages
.iter()
.filter(|m| m["method"] == "notifications/progress")
.collect();
assert_eq!(progress.len(), 3, "{messages:?}");
for (index, phase) in [
"tool_resolved",
"authorization_resolved",
"execution_dispatched",
]
.iter()
.enumerate()
{
assert_eq!(progress[index]["params"]["message"], *phase);
assert_eq!(
progress[index]["params"]["progress"].as_f64(),
Some((index + 1) as f64)
);
assert_eq!(progress[index]["params"]["progressToken"], "wire-progress");
}
let mut invalid = call(21, "unused", session.is_none(), false);
invalid["params"]["arguments"] = json!({"email":123});
let response = h.request(revision, session, &invalid).send().await.unwrap();
let messages = frames(response).await;
let result = messages.last().unwrap();
assert_eq!(result["result"]["isError"], true, "{messages:?}");
assert!(result.get("error").is_none());
}
assert_eq!(h.calls.load(Ordering::SeqCst), 2);
}
#[tokio::test]
async fn rmcp_http_reconnect_live_manager_and_restart_loses_sdk_session() {
let h = Harness::new().await;
let session = h.initialize().await;
for _ in 0..2 {
let response = reqwest::Client::new()
.get(format!("{}/v1/default/dispatch-observer", h.server.1))
.bearer_auth(&h.key)
.header("MCP-Session-Id", &session)
.header("MCP-Protocol-Version", "2025-11-25")
.header("Accept", "text/event-stream")
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
drop(response);
}
let response = h
.request(
"2025-11-25",
Some(&session),
&call(22, "reconnect@example.test", false, false),
)
.send()
.await
.unwrap();
assert_eq!(
frames(response).await.last().unwrap()["result"]["structuredContent"]["id"],
"lead_for_reconnect@example.test"
);
let fresh = serve(rmcp_http::router(
h.state.clone(),
AgentRoutePath {
workspace_slug: "default".into(),
agent_slug: "dispatch-observer".into(),
},
h.active.clone(),
))
.await;
let response = reqwest::Client::new()
.post(format!("{}/v1/default/dispatch-observer", fresh.1))
.bearer_auth(&h.key)
.header("MCP-Session-Id", &session)
.header("MCP-Protocol-Version", "2025-11-25")
.header("Accept", "application/json, text/event-stream")
.json(&call(23, "restart@example.test", false, false))
.send()
.await
.unwrap();
assert_eq!(
response.status(),
404,
"new SDK manager cannot restore from Crank session storage"
);
assert_eq!(h.calls.load(Ordering::SeqCst), 1);
}
async fn wait_count(counter: &AtomicUsize, expected: usize) {
tokio::time::timeout(std::time::Duration::from_secs(3), async {
while counter.load(Ordering::SeqCst) != expected {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
})
.await
.expect("bounded wait for the real execution/cleanup");
}
#[tokio::test]
async fn rmcp_http_legacy_cancel_is_owned_by_session_and_current_key() {
let h = Harness::new().await;
let session = h.initialize().await;
let foreign_session = h.initialize().await;
// A second valid credential can use this Agent/session but does not own the first call.
sqlx::query("insert into platform_api_keys (id, workspace_id, agent_id, key_kind, name, prefix, secret_hash, scopes_json, status, created_at) select 'pk_second', workspace_id, agent_id, key_kind, 'second', prefix, $1, scopes_json, status, created_at from platform_api_keys where id = 'pk_dispatch_observer'")
.bind(crate::access::hash_access_secret("test-second-valid-key")).execute(h.state.registry.pool()).await.unwrap();
let response = h
.request(
"2025-11-25",
Some(&session),
&call(30, "slow-cancel@example.test", false, true),
)
.send()
.await
.unwrap();
wait_count(&h.calls, 1).await;
let cancel =
json!({"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":30}});
assert_eq!(
h.request_as(
"2025-11-25",
Some(&session),
&cancel,
"test-second-valid-key"
)
.send()
.await
.unwrap()
.status(),
202
);
assert_eq!(
h.request("2025-11-25", Some(&foreign_session), &cancel)
.send()
.await
.unwrap()
.status(),
202
);
tokio::time::sleep(std::time::Duration::from_millis(75)).await;
assert_eq!(
h.active.load(Ordering::SeqCst),
1,
"foreign cancel must not stop call"
);
assert_eq!(
h.request("2025-11-25", Some(&session), &cancel)
.send()
.await
.unwrap()
.status(),
202
);
wait_count(&h.active, 0).await;
// Gap: SDK emits an error frame where the existing transport suppresses all terminal frames.
let messages = frames(response).await;
assert_eq!(
messages.last().unwrap()["error"]["message"],
"request cancelled"
);
assert!(!messages.iter().any(|m| m.get("result").is_some()));
}
#[tokio::test]
async fn rmcp_http_modern_disconnect_drops_execution_and_releases_stream_slot() {
let h = Harness::new().await;
let response = h
.request(
"2026-07-28",
None,
&call(31, "slow-disconnect@example.test", true, true),
)
.send()
.await
.unwrap();
wait_count(&h.calls, 1).await;
assert_eq!(h.active.load(Ordering::SeqCst), 1);
drop(response);
wait_count(&h.active, 0).await;
tokio::time::timeout(std::time::Duration::from_secs(2), async {
while h.state.session_slots.available_permits() != 16 {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
assert_eq!(
h.calls.load(Ordering::SeqCst),
1,
"disconnect never retries a possible external effect"
);
}
@@ -0,0 +1,416 @@
//! #107 initialize input: existing wire tolerance GREEN, independent SDK-consumption RED.
use super::rmcp_progress_cancel_runtime_tests::fixtures::Endpoint;
use super::{CounterSet, Snapshot, observe, support::*};
use serde_json::{Value, json};
use std::{sync::Arc, time::Duration};
const ACCEPTS: [&str; 2] = ["application/json", "text/event-stream, application/json"];
const TRACE: &str = "11111111111111111111111111111111";
async fn endpoint() -> Endpoint {
crank_test_support::postgres_schema_url_for_task("initialize_input_owner", "t_ffc30fcd").await;
Endpoint::started().await
}
async fn post(h: &Endpoint, params: Option<Value>, accept: &str) -> reqwest::Response {
let mut payload = json!({"jsonrpc":"2.0","id":"initialize/Ю","method":"initialize"});
if let Some(params) = params {
payload["params"] = params;
}
h.request(LEGACY_REVISION, None, &h.key, accept, payload)
.header("x-request-id", "initialize-input")
.header(
"traceparent",
"00-11111111111111111111111111111111-2222222222222222-01",
)
.send()
.await
.unwrap()
}
async fn body(response: reqwest::Response, sse: bool) -> Value {
assert_eq!(response.headers()["x-request-id"], "initialize-input");
assert_eq!(response.headers()["x-trace-id"], TRACE);
assert!(
response.headers()["content-type"]
.to_str()
.unwrap()
.starts_with(if sse {
"text/event-stream"
} else {
"application/json"
})
);
let raw = tokio::time::timeout(Duration::from_secs(6), response.text())
.await
.unwrap()
.unwrap();
if sse {
let frames: Vec<Value> = raw
.lines()
.filter_map(|line| line.strip_prefix("data:"))
.map(|data| serde_json::from_str(data.trim()).unwrap())
.collect();
assert_eq!(frames.len(), 1, "exactly one terminal SSE frame: {raw}");
frames.into_iter().next().unwrap()
} else {
serde_json::from_str(&raw).unwrap()
}
}
fn valid_shapes() -> Vec<(&'static str, Value, bool)> {
let mut cases = vec![(
"capabilities omitted",
json!({"protocolVersion":LEGACY_REVISION}),
false,
)];
for (name, caps, elicitation) in [
("null capabilities", Value::Null, false),
("boolean capabilities", json!(true), false),
("number capabilities", json!(7), false),
("string capabilities", json!("caps"), false),
("array capabilities", json!([{"elicitation":{}}]), false),
("empty capabilities", json!({}), false),
("null elicitation", json!({"elicitation":null}), false),
("boolean elicitation", json!({"elicitation":true}), false),
("number elicitation", json!({"elicitation":1}), false),
("string elicitation", json!({"elicitation":"yes"}), false),
("array elicitation", json!({"elicitation":[]}), false),
("empty elicitation object", json!({"elicitation":{}}), true),
(
"extended elicitation",
json!({"elicitation":{"form":{},"future":42},"future":[1]}),
true,
),
] {
cases.push((
name,
json!({"protocolVersion":LEGACY_REVISION,"capabilities":caps}),
elicitation,
));
}
for info in [
Value::Null,
json!(42),
json!([]),
json!({"name":false,"version":null}),
json!({"name":"client","version":"1","future":true}),
] {
cases.push((
"ignored clientInfo and extensions",
json!({"protocolVersion":LEGACY_REVISION,
"clientInfo":info,"unknown":{"nested":[null,1]},"_meta":{"future":true}}),
false,
));
}
cases
}
async fn valid_control() -> Vec<usize> {
let h = endpoint().await;
let mut deltas = vec![];
for accept in ACCEPTS {
for (name, params, elicitation) in valid_shapes() {
let ops = h.sessions.snapshot();
let admission = h.counters.snapshot();
let sdk = h.counters.sdk_initialize_input();
let response = post(&h, Some(params), accept).await;
assert_eq!(response.status(), 200, "{name}");
assert_eq!(response.headers()["MCP-Protocol-Version"], LEGACY_REVISION);
let id = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
assert!(!id.is_empty());
let wire = body(response, accept.starts_with("text/event-stream")).await;
assert_eq!(wire["jsonrpc"], "2.0");
assert_eq!(wire["id"], "initialize/Ю");
assert_eq!(wire["result"]["protocolVersion"], LEGACY_REVISION);
assert!(wire.get("error").is_none());
assert_deltas(
name,
admission,
h.counters.snapshot(),
Snapshot::new(1, 0, 1, 0),
);
assert_session_ops(
name,
ops,
h.sessions.snapshot(),
SessionOps {
get: 0,
create: 1,
mark_initialized: 0,
},
);
let state = h
.sessions
.retained(&id)
.await
.expect("retained session, not a discarded DTO");
assert_eq!(state.id, id);
assert_eq!(state.protocol_version, LEGACY_REVISION);
assert_eq!(state.workspace_slug, WORKSPACE_SLUG);
assert_eq!(state.agent_slug, AGENT_SLUG);
assert_eq!(state.supports_elicitation, elicitation, "{name}");
assert!(!state.initialized);
assert_eq!(state.updated_at, state.created_at);
assert_eq!(
(state.expires_at.unwrap() - state.created_at).whole_milliseconds(),
86_400_000
);
deltas.push(h.counters.sdk_initialize_input() - sdk);
let response = h
.request(
LEGACY_REVISION,
Some(&id),
&h.key,
"application/json",
initialized_notification(),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 202);
assert!(response.bytes().await.unwrap().is_empty());
assert!(h.sessions.retained(&id).await.unwrap().initialized);
let response = h
.request(
LEGACY_REVISION,
Some(&id),
&h.key,
"application/json",
json!({"jsonrpc":"2.0","id":2,"method":"tools/list"}),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
let result = json_body(response).await;
assert!(
result.get("error").is_none(),
"initialized opens a protected request: {result}"
);
assert_eq!(result["result"]["tools"][0]["name"], TOOL_NAME);
eprintln!("existing GREEN {name}; accept={accept}; elicitation={elicitation}");
}
}
eprintln!("SDK input consumption deltas={deltas:?}");
deltas
}
#[tokio::test]
async fn rmcp_initialize_input_wire_tolerance_and_retained_sessions() {
valid_control().await;
}
#[tokio::test]
async fn rmcp_initialize_input_sdk_consumed_by_negotiation() {
let deltas = valid_control().await;
assert!(!deltas.is_empty());
assert_eq!(
deltas,
vec![1; valid_shapes().len() * ACCEPTS.len()],
"real HTTP initialize must consume SDK InitializeRequestParams.protocol_version in negotiation"
);
}
// Current HTTP envelope admission rejects sequence params before serde's derived struct decoder.
// Pin that observed boundary instead of imposing a new object-only check in the SDK decoder.
#[tokio::test]
async fn rmcp_initialize_input_wire_invalid_shapes_and_admission() {
let h = endpoint().await;
for accept in ACCEPTS {
for params in [
None,
Some(json!({})),
Some(json!({"protocolVersion":null})),
Some(json!({"protocolVersion":true})),
Some(json!({"protocolVersion":42})),
Some(json!({"protocolVersion":[]})),
Some(json!({"protocolVersion":{}})),
] {
rejection(
&h,
params,
accept,
200,
-32602,
"invalid initialize parameters",
false,
)
.await;
}
for params in [
json!(null),
json!(true),
json!(42),
json!("bad"),
json!([]),
json!([LEGACY_REVISION]),
json!([LEGACY_REVISION, {}]),
json!([LEGACY_REVISION, {}, {}]),
] {
rejection(
&h,
Some(params),
accept,
400,
-32600,
"invalid JSON-RPC request",
true,
)
.await;
}
for version in ["2025-03-26", "2025-06-18", "2099-01-01", ""] {
rejection(
&h,
Some(json!({"protocolVersion":version})),
accept,
400,
-32602,
&format!("unsupported protocol version {version}"),
false,
)
.await;
}
for meta in [Value::Null, json!(true), json!(42), json!("bad"), json!([])] {
rejection(
&h,
Some(json!({"protocolVersion":LEGACY_REVISION,"_meta":meta})),
accept,
400,
-32020,
"protocol identity mismatch",
true,
)
.await;
}
}
}
#[allow(clippy::too_many_arguments)]
async fn rejection(
h: &Endpoint,
params: Option<Value>,
accept: &str,
status: u16,
code: i64,
message: &str,
admission_only: bool,
) {
let ops = h.sessions.snapshot();
let admission = h.counters.snapshot();
let sdk = h.counters.sdk_initialize_input();
let response = post(h, params.clone(), accept).await;
assert_eq!(response.status().as_u16(), status, "params={params:?}");
assert!(response.headers().get("MCP-Session-Id").is_none());
// Only HTTP 200 dispatch errors use SSE; HTTP 400 stays JSON regardless of Accept.
let sse = status == 200 && !admission_only && accept.starts_with("text/event-stream");
let wire = body(response, sse).await;
assert_eq!(wire["jsonrpc"], "2.0");
assert_eq!(
wire["id"],
if code == -32600 {
Value::Null
} else {
json!("initialize/Ю")
}
);
assert_eq!(wire["error"]["code"], code, "params={params:?}; {wire}");
assert_eq!(wire["error"]["message"], message);
assert!(wire.get("result").is_none());
// Initialize errors are constructed in the request scope, including terminal SSE errors.
assert_eq!(wire["error"]["data"]["request_id"], "initialize-input");
assert_eq!(wire["error"]["data"]["trace_id"], TRACE);
if code == -32020 {
assert_eq!(
wire["error"]["data"]["details"],
"request _meta must be an object"
);
}
assert_deltas(
"reject",
admission,
h.counters.snapshot(),
Snapshot::new(1, 0, usize::from(!admission_only), 0),
);
assert_session_ops(
"reject",
ops,
h.sessions.snapshot(),
SessionOps {
get: 0,
create: 0,
mark_initialized: 0,
},
);
if !message.starts_with("unsupported protocol version") {
assert_eq!(
h.counters.sdk_initialize_input(),
sdk,
"malformed input never reaches SDK consumption"
);
}
eprintln!(
"existing GREEN reject params={params:?}; accept={accept}; status={status}; code={code}"
);
}
#[tokio::test]
async fn rmcp_initialize_input_sdk_unsupported_revision_uses_same_allowlist() {
let h = endpoint().await;
let mut deltas = vec![];
for accept in ACCEPTS {
for version in ["2025-03-26", "2025-06-18", "2099-01-01", ""] {
let sdk = h.counters.sdk_initialize_input();
rejection(
&h,
Some(json!({"protocolVersion":version})),
accept,
400,
-32602,
&format!("unsupported protocol version {version}"),
false,
)
.await;
deltas.push(h.counters.sdk_initialize_input() - sdk);
}
}
assert_eq!(
deltas,
vec![1; 8],
"unsupported strings must reach the same SDK-backed negotiation, without minting sessions"
);
}
#[tokio::test]
async fn rmcp_initialize_input_observer_positive_control() {
let c = Arc::new(CounterSet::default());
let params: rmcp::model::InitializeRequestParams = serde_json::from_value(json!({
"protocolVersion":LEGACY_REVISION,"capabilities":{},"clientInfo":{"name":"control","version":"1"}
})).unwrap();
observe(c.clone(), async {
super::admission_enter();
super::sdk_initialize_input_consumed(&params);
assert_eq!(c.sdk_initialize_input(), 1);
assert_eq!(
c.sdk_initialize_result(),
0,
"input and output are independent"
);
})
.await;
assert_eq!(c.snapshot(), Snapshot::new(1, 0, 0, 0));
super::sdk_initialize_input_consumed(&params);
assert_eq!(c.sdk_initialize_input(), 1, "out-of-scope call is a no-op");
observe(Arc::new(CounterSet::default()), async {
super::sdk_initialize_input_consumed(&params);
})
.await;
assert_eq!(
c.sdk_initialize_input(),
1,
"another request scope is isolated"
);
}
@@ -0,0 +1,247 @@
//! Initialized HTTP transition against the existing PostgreSQL store, not an SDK session manager.
use super::*;
use crate::session::{PostgresTransportSessionStore, TransportSessionStore};
use crate::session::{SessionState, SessionStoreError};
use std::sync::atomic::{AtomicBool, Ordering};
use time::OffsetDateTime;
struct FaultStore {
inner: PostgresTransportSessionStore,
fail_get: AtomicBool,
fail_mark: AtomicBool,
}
#[async_trait::async_trait]
impl TransportSessionStore for FaultStore {
async fn create(
&self,
revision: &str,
workspace: &str,
agent: &str,
elicitation: bool,
now: OffsetDateTime,
expires: Option<OffsetDateTime>,
) -> Result<String, SessionStoreError> {
self.inner
.create(revision, workspace, agent, elicitation, now, expires)
.await
}
async fn get(&self, id: &str) -> Result<Option<SessionState>, SessionStoreError> {
if self.fail_get.load(Ordering::SeqCst) {
return Err(SessionStoreError {
details: "test read failure".into(),
});
}
self.inner.get(id).await
}
async fn mark_initialized(
&self,
id: &str,
now: OffsetDateTime,
) -> Result<bool, SessionStoreError> {
if self.fail_mark.load(Ordering::SeqCst) {
return Err(SessionStoreError {
details: "test mark failure".into(),
});
}
self.inner.mark_initialized(id, now).await
}
async fn delete(&self, id: &str) -> Result<bool, SessionStoreError> {
self.inner.delete(id).await
}
async fn cleanup_expired(&self, now: OffsetDateTime) -> Result<u64, SessionStoreError> {
self.inner.cleanup_expired(now).await
}
async fn active_count(&self, now: OffsetDateTime) -> Result<u64, SessionStoreError> {
self.inner.active_count(now).await
}
}
#[tokio::test]
async fn initialized_durable_transition_reconnect_and_store_failures() {
crank_test_support::postgres_schema_url_for_task("initialized_durable", "t_9c033310").await;
let registry = test_registry().await;
let pool = registry.pool().clone();
let store = Arc::new(FaultStore {
inner: PostgresTransportSessionStore::from_pool(pool.clone())
.await
.unwrap(),
fail_get: AtomicBool::new(false),
fail_mark: AtomicBool::new(false),
});
let h = Endpoint::started_with_store(Some(store.clone())).await;
let response = post(&h, None, "application/json", legacy_initialize(80)).await;
assert_eq!(response.status(), 200);
let id = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
let before = store.get(&id).await.unwrap().unwrap();
assert!(!before.initialized);
let response = post(
&h,
Some(&id),
"application/json",
initialized_notification(),
)
.await;
assert_eq!(response.status(), 202);
assert!(response.bytes().await.unwrap().is_empty());
assert_eq!(sdk(&h.counters), [1, 1]);
let restarted = PostgresTransportSessionStore::from_pool(pool.clone())
.await
.unwrap();
let after = restarted.get(&id).await.unwrap().unwrap();
assert!(after.initialized);
assert_eq!(after.id, before.id);
assert_eq!(after.workspace_slug, before.workspace_slug);
assert_eq!(after.agent_slug, before.agent_slug);
assert_eq!(after.protocol_version, before.protocol_version);
assert_eq!(after.created_at, before.created_at);
assert_eq!(after.expires_at, before.expires_at);
assert_eq!(after.supports_elicitation, before.supports_elicitation);
let response = post(
&h,
Some(&id),
"application/json",
json!({"jsonrpc":"2.0","id":81,"method":"ping"}),
)
.await;
assert_eq!(response.status(), 200);
assert!(body(response, false).await.get("error").is_none());
let pending = store
.create(
LEGACY_REVISION,
WORKSPACE_SLUG,
AGENT_SLUG,
false,
OffsetDateTime::now_utc(),
None,
)
.await
.unwrap();
store.fail_mark.store(true, Ordering::SeqCst);
let counts = sdk(&h.counters);
let response = post(
&h,
Some(&pending),
"application/json",
initialized_notification(),
)
.await;
assert_eq!(response.status(), 500);
assert!(response.bytes().await.unwrap().is_empty());
assert_eq!(sdk(&h.counters), [counts[0] + 1, counts[1] + 1]);
assert!(!restarted.get(&pending).await.unwrap().unwrap().initialized);
store.fail_mark.store(false, Ordering::SeqCst);
// Fail reads without touching registry/auth tables; admission must stop before the SDK.
store.fail_get.store(true, Ordering::SeqCst);
let counts = sdk(&h.counters);
let response = post(
&h,
Some(&pending),
"application/json",
initialized_notification(),
)
.await;
assert_eq!(response.status(), 500);
assert_eq!(sdk(&h.counters), counts);
store.fail_get.store(false, Ordering::SeqCst);
assert!(!restarted.get(&pending).await.unwrap().unwrap().initialized);
}
#[tokio::test]
async fn initialized_foreign_and_revision_guards_leave_state_untouched() {
let h = endpoint().await;
for (revision, workspace, agent) in [
(LEGACY_REVISION, "foreign-workspace", AGENT_SLUG),
(LEGACY_REVISION, WORKSPACE_SLUG, "foreign-agent"),
("2024-11-05", WORKSPACE_SLUG, AGENT_SLUG),
(MODERN_REVISION, WORKSPACE_SLUG, AGENT_SLUG),
] {
let id = h
.sessions
.create(
revision,
workspace,
agent,
false,
OffsetDateTime::now_utc(),
None,
)
.await
.unwrap();
let state = h.sessions.retained(&id).await.unwrap();
let ops = h.sessions.snapshot();
let counts = sdk(&h.counters);
let response = post(
&h,
Some(&id),
"application/json",
initialized_notification(),
)
.await;
assert_eq!(
response.status(),
if workspace != WORKSPACE_SLUG || agent != AGENT_SLUG {
404
} else {
400
}
);
assert_eq!(h.counters.sdk_initialized_consumed(), counts[1]);
if workspace == WORKSPACE_SLUG && agent == AGENT_SLUG {
assert_eq!(h.counters.sdk_initialized_decoded(), counts[0]);
}
assert_eq!(h.sessions.snapshot().mark_initialized, ops.mark_initialized);
assert_eq!(h.sessions.retained(&id).await.unwrap(), state);
}
let id = initialize(&h, "application/json").await;
let counts = sdk(&h.counters);
let ops = h.sessions.snapshot();
let response = h
.request(
LEGACY_REVISION,
Some(&id),
&h.key,
"application/json",
initialized_notification(),
)
.header("MCP-Session-Id", "conflicting-session")
.send()
.await
.unwrap();
assert_eq!(response.status(), 400);
assert_eq!(sdk(&h.counters), counts);
assert_eq!(h.sessions.snapshot().mark_initialized, ops.mark_initialized);
assert!(!h.sessions.retained(&id).await.unwrap().initialized);
h.registry
.revoke_platform_api_key_for_agent(
&workspace_id(),
&agent_id(),
&crank_core::PlatformApiKeyId::new("pk_progress_other"),
&OffsetDateTime::now_utc(),
)
.await
.unwrap();
let counts = sdk(&h.counters);
let ops = h.sessions.snapshot();
let response = h
.request(
LEGACY_REVISION,
Some(&id),
&h.other_key,
"application/json",
initialized_notification(),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 401);
assert_eq!(sdk(&h.counters), counts);
assert_eq!(h.sessions.snapshot().create, ops.create);
assert_eq!(h.sessions.snapshot().mark_initialized, ops.mark_initialized);
assert!(!h.sessions.retained(&id).await.unwrap().initialized);
}
@@ -0,0 +1,305 @@
//! #107 initialized: real HTTP wire control, independent SDK decode and consumption oracles.
use super::rmcp_progress_cancel_runtime_tests::fixtures::Endpoint;
use super::{CounterSet, Snapshot, observe, support::*};
use serde_json::{Value, json};
use std::{sync::Arc, time::Duration};
#[path = "rmcp_initialized_durable_tests.rs"]
mod durable;
async fn endpoint() -> Endpoint {
crank_test_support::postgres_schema_url_for_task("initialized_owner", "t_9c033310").await;
Endpoint::started().await
}
fn sdk(c: &CounterSet) -> [usize; 2] {
[c.sdk_initialized_decoded(), c.sdk_initialized_consumed()]
}
async fn post(
h: &Endpoint,
session: Option<&str>,
accept: &str,
payload: Value,
) -> reqwest::Response {
h.request(LEGACY_REVISION, session, &h.key, accept, payload)
.header("x-request-id", "initialized-atdd")
.header(
"traceparent",
"00-11111111111111111111111111111111-2222222222222222-01",
)
.send()
.await
.unwrap()
}
fn correlation(response: &reqwest::Response) {
assert_eq!(response.headers()["x-request-id"], "initialized-atdd");
assert_eq!(
response.headers()["x-trace-id"],
"11111111111111111111111111111111"
);
}
async fn body(response: reqwest::Response, sse: bool) -> Value {
correlation(&response);
assert!(
response.headers()["content-type"]
.to_str()
.unwrap()
.starts_with(if sse {
"text/event-stream"
} else {
"application/json"
})
);
let raw = tokio::time::timeout(Duration::from_secs(6), response.text())
.await
.unwrap()
.unwrap();
if sse {
let frames: Vec<Value> = raw
.lines()
.filter_map(|l| l.strip_prefix("data:"))
.map(|l| serde_json::from_str(l.trim()).unwrap())
.collect();
assert_eq!(frames.len(), 1, "one terminal SSE frame: {raw}");
frames.into_iter().next().unwrap()
} else {
serde_json::from_str(&raw).unwrap()
}
}
async fn initialize(h: &Endpoint, accept: &str) -> String {
let response = post(h, None, accept, legacy_initialize(71)).await;
assert_eq!(response.status(), 200);
assert_eq!(response.headers()["MCP-Protocol-Version"], LEGACY_REVISION);
let id = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
let wire = body(response, accept.starts_with("text/event-stream")).await;
assert_eq!(wire["id"], 71);
assert_eq!(wire["result"]["protocolVersion"], LEGACY_REVISION);
assert!(wire.get("error").is_none());
let state = h.sessions.retained(&id).await.expect("retained session");
assert!(!state.initialized);
assert_eq!(state.workspace_slug, WORKSPACE_SLUG);
assert_eq!(state.agent_slug, AGENT_SLUG);
id
}
async fn control() -> Vec<[usize; 2]> {
let h = endpoint().await;
let mut deltas = vec![];
for accept in ["application/json", "text/event-stream, application/json"] {
for params in [
None,
Some(json!({})),
Some(json!({"ignored":[null,true],
"protocolVersion":false,"capabilities":42,"_meta":{"future":[]}})),
] {
let id = initialize(&h, accept).await;
let untouched = initialize(&h, "application/json").await;
let before_state = h.sessions.retained(&id).await.unwrap();
let mut notification = json!({"jsonrpc":"2.0","method":"notifications/initialized",
"extension":{"future":[null,42]}});
if let Some(params) = params {
notification["params"] = params;
}
let before = sdk(&h.counters);
let ops = h.sessions.snapshot();
let admission = h.counters.snapshot();
let response = post(&h, Some(&id), accept, notification.clone()).await;
assert_eq!(response.status(), 202);
correlation(&response);
assert!(response.headers().get("MCP-Session-Id").is_none());
assert!(response.headers().get("content-type").is_none());
assert!(response.bytes().await.unwrap().is_empty());
assert_session_ops(
"initialized",
ops,
h.sessions.snapshot(),
SessionOps {
get: 2,
create: 0,
mark_initialized: 1,
},
);
assert_deltas(
"initialized",
admission,
h.counters.snapshot(),
Snapshot::new(1, 0, 1, 0),
);
let after = sdk(&h.counters);
deltas.push([after[0] - before[0], after[1] - before[1]]);
let state = h.sessions.retained(&id).await.unwrap();
assert!(state.initialized);
assert_eq!(state.id, before_state.id);
assert_eq!(state.workspace_slug, before_state.workspace_slug);
assert_eq!(state.agent_slug, before_state.agent_slug);
assert_eq!(state.protocol_version, before_state.protocol_version);
assert_eq!(state.created_at, before_state.created_at);
assert_eq!(state.expires_at, before_state.expires_at);
assert_eq!(
state.supports_elicitation,
before_state.supports_elicitation
);
assert!(state.updated_at >= before_state.updated_at);
assert!(!h.sessions.retained(&untouched).await.unwrap().initialized);
let response = post(
&h,
Some(&id),
accept,
json!({"jsonrpc":"2.0","id":"tools/Ю","method":"tools/list"}),
)
.await;
assert_eq!(response.status(), 200);
assert_eq!(response.headers()["MCP-Protocol-Version"], LEGACY_REVISION);
let wire = body(response, accept.starts_with("text/event-stream")).await;
assert_eq!(wire["id"], "tools/Ю");
assert!(wire.get("error").is_none());
assert_eq!(wire["result"]["tools"][0]["name"], TOOL_NAME);
eprintln!(
"existing GREEN initialize -> initialized -> tools/list accept={accept} notification={notification}; SDK delta={:?}",
deltas.last().unwrap()
);
}
}
deltas
}
#[tokio::test]
async fn initialized_wire_handshake_tolerance_and_scoped_state() {
control().await;
}
#[tokio::test]
async fn initialized_sdk_decoded_on_real_http_handshake() {
let deltas = control().await;
assert_eq!(
deltas.iter().map(|d| d[0]).collect::<Vec<_>>(),
vec![1; 6],
"real HTTP initialized must decode the SDK InitializedNotification"
);
}
#[tokio::test]
async fn initialized_sdk_variant_consumed_by_session_transition() {
let deltas = control().await;
assert_eq!(
deltas.iter().map(|d| d[1]).collect::<Vec<_>>(),
vec![1; 6],
"matched ClientNotification variant must drive the real scoped session mutation, not just decode"
);
}
#[tokio::test]
async fn initialized_admission_and_wrong_kind_never_mutate_or_consume() {
let h = endpoint().await;
let id = initialize(&h, "application/json").await;
let mut wrong_initialize = legacy_initialize(72);
wrong_initialize.as_object_mut().unwrap().remove("id");
let mut wrong_initialized = initialized_notification();
wrong_initialized["id"] = json!(73);
let mut cases = vec![
(wrong_initialize, 202, None),
(wrong_initialized, 200, Some(-32601)),
];
for params in [json!(null), json!(true), json!(42), json!("bad"), json!([])] {
cases.push((
json!({"jsonrpc":"2.0","method":"notifications/initialized","params":params}),
400,
Some(-32600),
));
}
for meta in [
json!(null),
json!(false),
json!(42),
json!("bad"),
json!([]),
] {
cases.push((
json!({"jsonrpc":"2.0","method":"notifications/initialized","params":{"_meta":meta}}),
400,
Some(-32020),
));
}
cases.push((
json!({"jsonrpc":"1.0","method":"notifications/initialized"}),
400,
Some(-32600),
));
for (payload, status, code) in cases {
let ops = h.sessions.snapshot();
let before = sdk(&h.counters);
let response = post(&h, Some(&id), "application/json", payload.clone()).await;
assert_eq!(response.status().as_u16(), status, "{payload}");
if let Some(code) = code {
let wire = body(response, false).await;
assert_eq!(wire["error"]["code"], code, "{payload}: {wire}");
assert!(wire.get("result").is_none());
} else {
assert!(response.bytes().await.unwrap().is_empty());
}
let after = h.sessions.snapshot();
assert_eq!(after.create, ops.create);
assert_eq!(after.mark_initialized, ops.mark_initialized);
assert!(!h.sessions.retained(&id).await.unwrap().initialized);
assert_eq!(
sdk(&h.counters),
before,
"rejected before SDK decode/consume: {payload}"
);
}
}
#[tokio::test]
async fn initialized_missing_unknown_sessions_and_modern_never_consume() {
let h = endpoint().await;
for (session, status) in [(None, 400), (Some("unknown-session"), 404)] {
let ops = h.sessions.snapshot();
let response = post(&h, session, "application/json", initialized_notification()).await;
assert_eq!(response.status().as_u16(), status);
assert!(response.bytes().await.unwrap().is_empty());
assert_eq!(h.sessions.snapshot().mark_initialized, ops.mark_initialized);
assert_eq!(h.counters.sdk_initialized_consumed(), 0);
}
let ops = h.sessions.snapshot();
let response = h.request(MODERN_REVISION, None, &h.key, "application/json",
json!({"jsonrpc":"2.0","method":"notifications/initialized","params":{"_meta":modern_meta()}}))
.send().await.unwrap();
assert_eq!(response.status(), 202);
assert!(response.bytes().await.unwrap().is_empty());
assert_eq!(
h.sessions.snapshot(),
ops,
"modern signal never accesses storage"
);
assert_eq!(h.counters.sdk_initialized_consumed(), 0);
}
#[tokio::test]
async fn initialized_observer_independent_positive_control() {
let a = Arc::new(CounterSet::default());
let b = Arc::new(CounterSet::default());
let notification: rmcp::model::InitializedNotification =
serde_json::from_value(json!({"method":"notifications/initialized"})).unwrap();
super::sdk_initialized_notification_decoded(&notification);
super::sdk_initialized_notification_consumed(&notification);
assert_eq!(sdk(&a), [0, 0]);
tokio::join!(
observe(a.clone(), async {
super::sdk_initialized_notification_decoded(&notification);
}),
observe(b.clone(), async {
super::sdk_initialized_notification_consumed(&notification);
})
);
assert_eq!(sdk(&a), [1, 0]);
assert_eq!(sdk(&b), [0, 1], "decode alone never proves consumption");
assert_eq!(a.snapshot(), Snapshot::new(0, 0, 0, 0));
assert_eq!(b.snapshot(), Snapshot::new(0, 0, 0, 0));
}
@@ -0,0 +1,270 @@
//! #107 negotiation results: existing HTTP wire GREEN, independent SDK execution oracle RED.
use super::{CounterSet, observe, support::*};
use serde_json::{Value, json};
use std::{sync::Arc, time::Duration};
use super::rmcp_progress_cancel_runtime_tests::fixtures::Endpoint;
fn sdk_counts(c: &CounterSet) -> [usize; 2] {
[c.sdk_initialize_result(), c.sdk_discover_result()]
}
fn capabilities() -> Value {
// Pin the reviewed advertisement, not a call to the builder under test or SDK defaults.
json!({"completions":{},"logging":{},"prompts":{"listChanged":true},
"resources":{"listChanged":true,"subscribe":false},"tools":{"listChanged":true}})
}
fn expected_result(revision: &str) -> Value {
let server = json!({"name":"crank-mcp-server","version":env!("CARGO_PKG_VERSION")});
if revision == LEGACY_REVISION {
json!({"protocolVersion":LEGACY_REVISION,"capabilities":capabilities(),"serverInfo":server})
} else {
json!({"resultType":"complete","supportedVersions":["2026-07-28","2025-11-25"],
"capabilities":capabilities(),"_meta":{"io.modelcontextprotocol/serverInfo":server},
"ttlMs":0,"cacheScope":"private"})
}
}
async fn wire(response: reqwest::Response, revision: &str, id: &Value, sse: bool) -> Value {
assert_eq!(response.status(), 200);
assert_eq!(response.headers()["MCP-Protocol-Version"], revision);
assert_eq!(response.headers()["x-request-id"], "negotiation-request");
assert_eq!(
response.headers()["x-trace-id"],
"11111111111111111111111111111111"
);
let content_type = response.headers()["content-type"].to_str().unwrap();
assert!(content_type.starts_with(if sse {
"text/event-stream"
} else {
"application/json"
}));
let raw = tokio::time::timeout(Duration::from_secs(6), response.text())
.await
.unwrap()
.unwrap();
let body: Value = if sse {
let frames: Vec<Value> = raw
.lines()
.filter_map(|line| line.strip_prefix("data:"))
.map(|data| serde_json::from_str(data.trim()).unwrap())
.collect();
assert_eq!(
frames.len(),
1,
"one terminal, no unsolicited notifications: {raw}"
);
frames.into_iter().next().unwrap()
} else {
serde_json::from_str(&raw).unwrap()
};
assert_eq!(
body,
json!({"jsonrpc":"2.0","id":id,"result":expected_result(revision)}),
"exact result/envelope fields and omissions (including title/icons/instructions/defaults)"
);
body
}
async fn control(revision: &str) -> Vec<[usize; 2]> {
crank_test_support::postgres_schema_url_for_task("negotiation_owner", "t_99aa603c").await;
let h = Endpoint::started().await;
let mut deltas = vec![];
// Accept order selects JSON or a single terminal SSE frame on the real transport.
for accept in ["application/json", "text/event-stream, application/json"] {
let id = json!("negotiation/Ю");
let payload = if revision == LEGACY_REVISION {
let mut initialize = legacy_initialize(1);
initialize["id"] = id.clone();
initialize
} else {
json!({"jsonrpc":"2.0","id":id,"method":"server/discover","params":{"_meta":modern_meta()}})
};
let before = sdk_counts(&h.counters);
let admission_before = h.counters.snapshot();
let response = h
.request(revision, None, &h.key, accept, payload)
.header("x-request-id", "negotiation-request")
.header(
"traceparent",
"00-11111111111111111111111111111111-2222222222222222-01",
)
.send()
.await
.unwrap();
let session = response
.headers()
.get("MCP-Session-Id")
.map(|v| v.to_str().unwrap().to_owned());
if revision == LEGACY_REVISION {
assert!(
!session
.as_deref()
.expect("initialize creates a session")
.is_empty()
);
} else {
assert_eq!(
session, None,
"stateless discovery never creates a session carrier"
);
}
wire(
response,
revision,
&id,
accept.starts_with("text/event-stream"),
)
.await;
assert_deltas(
"negotiation admission positive control",
admission_before,
h.counters.snapshot(),
if revision == LEGACY_REVISION {
super::Snapshot::new(1, 0, 1, 0)
} else {
super::Snapshot::new(1, 1, 0, 0)
},
);
let after = sdk_counts(&h.counters);
deltas.push([after[0] - before[0], after[1] - before[1]]);
if let Some(session) = session {
let response = h
.request(
revision,
Some(&session),
&h.key,
accept,
initialized_notification(),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 202);
assert!(response.bytes().await.unwrap().is_empty());
// Prove the minted session can authorize the next protected request.
let response = h
.request(
revision,
Some(&session),
&h.key,
"application/json",
json!({"jsonrpc":"2.0","id":2,"method":"ping"}),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
assert_eq!(
json_body(response).await,
json!({"jsonrpc":"2.0","id":2,"result":{"protocolVersion":LEGACY_REVISION}})
);
}
}
eprintln!("existing GREEN lifecycle negotiation wire {revision}; SDK deltas={deltas:?}");
deltas
}
#[tokio::test]
async fn rmcp_lifecycle_negotiation_wire_legacy() {
control(LEGACY_REVISION).await;
}
#[tokio::test]
async fn rmcp_lifecycle_negotiation_wire_modern() {
control(MODERN_REVISION).await;
}
#[tokio::test]
async fn rmcp_lifecycle_negotiation_sdk_legacy() {
assert_eq!(
control(LEGACY_REVISION).await,
vec![[1, 0]; 2],
"real HTTP initialize must serialize SDK InitializeResult (not admission/envelope only)"
);
}
#[tokio::test]
async fn rmcp_lifecycle_negotiation_sdk_modern() {
assert_eq!(
control(MODERN_REVISION).await,
vec![[0, 1]; 2],
"real HTTP discovery must serialize SDK DiscoverResult (not admission/envelope only)"
);
}
#[tokio::test]
async fn rmcp_lifecycle_negotiation_observer_positive_control() {
let c = Arc::new(CounterSet::default());
observe(c.clone(), async {
super::admission_enter();
super::sdk_initialize_result_serialized();
assert_eq!(
sdk_counts(&c),
[1, 0],
"initialize and discovery are separate markers"
);
super::sdk_discover_result_serialized();
})
.await;
assert_eq!(sdk_counts(&c), [1, 1]);
assert_eq!(c.snapshot(), super::Snapshot::new(1, 0, 0, 0));
super::sdk_initialize_result_serialized();
super::sdk_discover_result_serialized();
assert_eq!(sdk_counts(&c), [1, 1], "out-of-scope markers are no-ops");
}
#[tokio::test]
async fn rmcp_lifecycle_negotiation_wire_session_operations() {
crank_test_support::postgres_schema_url_for_task("negotiation_sessions_owner", "t_99aa603c")
.await;
let h = Harness::started().await;
// Existing counted-session fixture supplies independent get/create/initialized oracles.
run_valid_legacy_initialize(&h, 10, "legacy create exactly once").await;
let session = h.initialize_legacy_session().await;
run_valid_legacy_call(
&h,
&session,
11,
"negotiation@example.test",
"initialized session works",
)
.await;
for accept in ["application/json", "text/event-stream, application/json"] {
let before = h.session_ops();
let admission_before = h.snapshot();
let response = h
.post(
&[
("MCP-Protocol-Version", MODERN_REVISION),
("Mcp-Method", "server/discover"),
("Accept", accept),
("x-request-id", "negotiation-request"),
(
"traceparent",
"00-11111111111111111111111111111111-2222222222222222-01",
),
],
&json!({"jsonrpc":"2.0","id":"discovery","method":"server/discover",
"params":{"_meta":modern_meta()}}),
)
.await;
assert!(response.headers().get("MCP-Session-Id").is_none());
wire(response, MODERN_REVISION, &json!("discovery"), false).await;
assert_deltas(
"modern discovery",
admission_before,
h.snapshot(),
super::Snapshot::new(1, 1, 0, 0),
);
assert_session_ops(
"stateless discovery",
before,
h.session_ops(),
SessionOps {
get: 0,
create: 0,
mark_initialized: 0,
},
);
}
}
@@ -0,0 +1,258 @@
//! #107 logging: existing HTTP wire control before the independent SDK marker assertions.
use super::{CounterSet, observe, support::*};
use axum::{body::Body, response::Response};
use futures_util::{StreamExt, stream};
use serde_json::{Value, json};
use std::{sync::Arc, time::Duration};
#[path = "rmcp_logging_test_support.rs"]
mod fixtures;
use fixtures::Endpoint;
const LEVEL: &str = "io.modelcontextprotocol/logLevel";
fn counts(c: &CounterSet) -> [usize; 3] {
[
c.sdk_logging_dispatch(),
c.sdk_logging_level(),
c.sdk_logging_notification(),
]
}
fn observe_body(response: Response, counters: Arc<CounterSet>) -> Response {
let (parts, body) = response.into_parts();
let body = stream::unfold(
(body.into_data_stream(), counters),
|(mut body, c)| async move {
observe(c.clone(), body.next())
.await
.map(|chunk| (chunk, (body, c)))
},
);
Response::from_parts(parts, Body::from_stream(body))
}
fn notification(level: &str, event: &str, outcome: &str) -> Value {
json!({"jsonrpc":"2.0","method":"notifications/message","params":{
"level":level,"logger":"crank-mcp-server","data":{"event":event,"outcome":outcome}}})
}
async fn reply(response: reqwest::Response) -> (Value, Vec<Value>, String) {
let status = response.status();
assert!(
status == 200 || status == 404,
"unexpected HTTP status {status}"
);
let sse = response.headers()["content-type"]
.to_str()
.unwrap()
.starts_with("text/event-stream");
let raw = tokio::time::timeout(Duration::from_secs(5), response.text())
.await
.unwrap()
.unwrap();
let mut frames: Vec<Value> = if sse {
raw.lines()
.filter_map(|line| {
line.strip_prefix("data:")
.map(|s| serde_json::from_str(s.trim()).unwrap())
})
.collect()
} else {
vec![serde_json::from_str(&raw).unwrap()]
};
let terminal = frames.pop().expect("one terminal response");
assert_eq!(terminal["jsonrpc"], "2.0");
assert_eq!(terminal["id"], 51);
assert_eq!(
status.as_u16(),
if terminal["error"]["code"] == -32601 {
404
} else {
200
}
);
if !frames.is_empty() {
assert!(sse, "notifications require SSE");
}
assert!(frames.iter().all(|f| f.get("id").is_none()));
(terminal, frames, raw)
}
fn opted(level: Value) -> Value {
let mut meta = modern_meta();
meta[LEVEL] = level;
json!({"_meta":meta})
}
async fn control(revision: &str) -> [usize; 3] {
let h = Endpoint::started().await;
let session = h.session(revision).await;
let session = session.as_deref();
// Negative opt-in controls precede any accepted level; a silent response is not enough alone.
let (body, events, _) = reply(h.post(revision, session, "tools/list", json!({})).await).await;
assert!(body["result"]["tools"].is_array());
assert!(events.is_empty());
assert_eq!(counts(&h.counters), [0; 3]);
if revision == MODERN_REVISION {
for value in [
json!("verbose"),
json!(7),
json!(null),
json!("CANARY".repeat(1000)),
] {
let (body, events, _) =
reply(h.post(revision, session, "tools/list", opted(value)).await).await;
assert!(body["result"]["tools"].is_array());
assert!(events.is_empty());
assert_eq!(counts(&h.counters), [0; 3]);
}
let (body, events, _) = reply(
h.post(
revision,
session,
"logging/setLevel",
json!({"level":"notice"}),
)
.await,
)
.await;
assert_eq!(body["error"]["code"], -32601);
assert!(events.is_empty());
assert_eq!(counts(&h.counters), [0; 3]);
} else {
for params in [
json!({}),
json!({"level":7}),
json!({"level":"CANARY".repeat(1000)}),
] {
let (body, events, raw) =
reply(h.post(revision, session, "logging/setLevel", params).await).await;
assert_eq!(body["error"]["code"], -32602);
assert_eq!(body["error"]["message"], "invalid logging level");
assert!(body.get("result").is_none());
assert!(!raw.contains("CANARY"));
assert!(raw.len() < 4096);
assert!(events.is_empty());
assert_eq!(h.counters.sdk_logging_level(), 0);
assert_eq!(h.counters.sdk_logging_notification(), 0);
}
}
let before = counts(&h.counters);
let (method, params, expected) = if revision == LEGACY_REVISION {
(
"logging/setLevel",
json!({"level":"notice"}),
notification("notice", "mcp.logging.level_applied", "applied"),
)
} else {
(
"tools/list",
opted(json!("info")),
notification("info", "mcp.request.served", "ok"),
)
};
let (body, events, raw) = reply(h.post(revision, session, method, params).await).await;
assert!(body.get("error").is_none(), "{body}");
if revision == LEGACY_REVISION {
assert_eq!(body["result"], json!({}));
} else {
assert!(body["result"]["tools"].is_array());
}
assert_eq!(events, vec![expected]);
assert!(raw.len() < 4096);
let after = counts(&h.counters);
let delta = std::array::from_fn(|i| after[i] - before[i]);
// Legacy severity persists, modern opt-in does not leak into the next request.
let (body, events, _) = reply(h.post(revision, session, "tools/list", json!({})).await).await;
assert!(body["result"]["tools"].is_array());
assert!(events.is_empty());
h.registry
.revoke_platform_api_key(
&workspace_id(),
&crank_core::PlatformApiKeyId::new("pk_dispatch_observer"),
&time::OffsetDateTime::now_utc(),
)
.await
.unwrap();
let before = counts(&h.counters);
let era = h.counters.snapshot();
let response = h
.post(
revision,
session,
method,
if revision == LEGACY_REVISION {
json!({"level":"notice"})
} else {
opted(json!("info"))
},
)
.await;
assert_eq!(response.status(), 401);
let raw = response.text().await.unwrap();
assert!(!raw.contains("notifications/message"));
assert_eq!(counts(&h.counters), before);
let after = h.counters.snapshot();
assert_eq!(after.era_modern, era.era_modern);
assert_eq!(after.era_legacy, era.era_legacy);
eprintln!(
"existing GREEN logging HTTP {revision}: safe SSE, result, opt-in/invalid/era/auth controls passed; SDK positive delta={delta:?}"
);
delta
}
#[tokio::test]
async fn rmcp_logging_wire_legacy() {
control(LEGACY_REVISION).await;
}
#[tokio::test]
async fn rmcp_logging_wire_modern() {
control(MODERN_REVISION).await;
}
#[tokio::test]
async fn rmcp_logging_sdk_legacy() {
assert_eq!(
control(LEGACY_REVISION).await,
[1, 1, 1],
"missing SDK legacy request/level/notification after successful HTTP wire assertions"
);
}
#[tokio::test]
async fn rmcp_logging_sdk_modern() {
assert_eq!(
control(MODERN_REVISION).await,
[0, 1, 1],
"missing SDK modern level/notification after successful HTTP wire assertions"
);
}
#[tokio::test]
async fn rmcp_logging_observer_scope_control() {
use super::{
sdk_logging_dispatch_enter, sdk_logging_level_parsed, sdk_logging_notification_serialized,
};
let a = Arc::new(CounterSet::default());
let b = Arc::new(CounterSet::default());
sdk_logging_dispatch_enter();
sdk_logging_level_parsed();
sdk_logging_notification_serialized();
assert_eq!(counts(&a), [0; 3]);
tokio::join!(
observe(a.clone(), async {
sdk_logging_dispatch_enter();
tokio::task::yield_now().await;
sdk_logging_level_parsed();
}),
observe(b.clone(), async {
sdk_logging_notification_serialized();
})
);
assert_eq!(counts(&a), [1, 1, 0]);
assert_eq!(counts(&b), [0, 0, 1]);
let body = Body::from_stream(stream::once(async {
sdk_logging_notification_serialized();
Ok::<_, std::convert::Infallible>("data: terminal\n\n")
}));
let response = observe_body(Response::new(body), a.clone());
assert_eq!(counts(&a), [1, 1, 0]);
assert_eq!(
&axum::body::to_bytes(response.into_body(), 100)
.await
.unwrap()[..],
b"data: terminal\n\n"
);
assert_eq!(counts(&a), [1, 1, 1]);
assert_eq!(counts(&b), [0, 0, 1]);
}
@@ -0,0 +1,143 @@
//! Minimal logging HTTP fixture; shared registry/auth/lifecycle fixtures remain authoritative.
use super::*;
use crank_runtime::{
InMemoryCoordinationStateStore, RequestRateLimitConfig, RequestRateLimiter, SecretCrypto,
};
pub(super) struct Endpoint {
pub registry: crank_registry::PostgresRegistry,
pub counters: Arc<CounterSet>,
key: String,
url: String,
server: tokio::task::JoinHandle<()>,
}
impl Drop for Endpoint {
fn drop(&mut self) {
self.server.abort();
}
}
impl Endpoint {
pub async fn started() -> Self {
crank_test_support::postgres_schema_url_for_task("logging_owner", "t_c28c3800").await;
let registry = test_registry().await;
let operation = test_operation("http://127.0.0.1:9");
registry
.create_operation(&workspace_id(), &operation, Some("alice"))
.await
.unwrap();
registry
.publish_operation(crank_registry::PublishRequest {
workspace_id: &workspace_id(),
operation_id: &operation.id,
version: 1,
published_at: &test_time(),
published_by: Some("alice"),
})
.await
.unwrap();
publish_agent(&registry, &operation).await;
let key = create_api_key(&registry).await;
let counters = Arc::new(CounterSet::default());
let app = crate::build_app(
registry.clone(),
Duration::ZERO,
Some("https://crank.example.com".into()),
SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap(),
crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build(),
RequestRateLimiter::new(RequestRateLimitConfig::new(10_000, 10_000).unwrap()),
Arc::new(InMemoryCoordinationStateStore::default()),
Arc::new(crate::session::InMemorySessionStore::default()),
Arc::new(crate::auth::CommunityMachineCredentialVerifier),
);
let scope = counters.clone();
let app = app.layer(axum::middleware::from_fn(
move |request: axum::extract::Request, next: axum::middleware::Next| {
let counters = scope.clone();
async move {
let response = observe(counters.clone(), next.run(request)).await;
observe_body(response, counters)
}
},
));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!(
"http://{}/v1/{WORKSPACE_SLUG}/{AGENT_SLUG}",
listener.local_addr().unwrap()
);
let server = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
Self {
registry,
counters,
key,
url,
server,
}
}
pub async fn post(
&self,
revision: &str,
session: Option<&str>,
method: &str,
mut params: Value,
) -> reqwest::Response {
let mut request = reqwest::Client::new()
.post(&self.url)
.bearer_auth(&self.key)
.header("Accept", "text/event-stream, application/json")
.header("MCP-Protocol-Version", revision);
if let Some(session) = session {
request = request.header("MCP-Session-Id", session);
}
if revision == MODERN_REVISION {
request = request.header("Mcp-Method", method);
if params.get("_meta").is_none() {
params["_meta"] = modern_meta();
}
}
request
.json(&json!({"jsonrpc":"2.0", "id":51, "method":method, "params":params}))
.send()
.await
.unwrap()
}
pub async fn session(&self, revision: &str) -> Option<String> {
if revision == MODERN_REVISION {
return None;
}
let response = self
.post(
revision,
None,
"initialize",
json!({"protocolVersion":revision,"capabilities":{}}),
)
.await;
assert_eq!(response.status(), 200);
let session = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
assert_eq!(
reply(response).await.0["result"]["protocolVersion"],
revision
);
let response = reqwest::Client::new()
.post(&self.url)
.bearer_auth(&self.key)
.header("Accept", "application/json, text/event-stream")
.header("MCP-Protocol-Version", revision)
.header("MCP-Session-Id", &session)
.json(&initialized_notification())
.send()
.await
.unwrap();
assert_eq!(response.status(), 202);
response.bytes().await.unwrap();
Some(session)
}
}
@@ -0,0 +1,211 @@
//! #107 ping: existing wire GREEN is independent from the SDK execution oracle.
use super::rmcp_progress_cancel_runtime_tests::fixtures::Endpoint;
use super::{CounterSet, Snapshot, observe, support::*};
use serde_json::{Value, json};
use std::{sync::Arc, time::Duration};
async fn endpoint() -> Endpoint {
crank_test_support::postgres_schema_url_for_task("ping_owner", "t_83746dc7").await;
Endpoint::started().await
}
fn ping(revision: &str, extended: bool) -> Value {
let mut payload = json!({"jsonrpc":"2.0","id":"ping/Ю","method":"ping"});
if revision == MODERN_REVISION || extended {
payload["params"] = json!({"_meta":if revision == MODERN_REVISION {
modern_meta()
} else { json!({}) }});
}
if extended {
payload["params"]["ignored"] = json!([null, {"future":true}]);
payload["params"]["_meta"]["future"] = json!(42);
payload["extension"] = json!({"nested":[false]});
}
payload
}
async fn wire_body(response: reqwest::Response, sse: bool) -> Value {
assert_eq!(response.headers()["x-request-id"], "ping-atdd");
assert_eq!(
response.headers()["x-trace-id"],
"11111111111111111111111111111111"
);
assert!(
response.headers()["content-type"]
.to_str()
.unwrap()
.starts_with(if sse {
"text/event-stream"
} else {
"application/json"
})
);
let raw = tokio::time::timeout(Duration::from_secs(6), response.text())
.await
.unwrap()
.unwrap();
if sse {
let frames: Vec<Value> = raw
.lines()
.filter_map(|line| line.strip_prefix("data:"))
.map(|line| serde_json::from_str(line.trim()).unwrap())
.collect();
assert_eq!(frames.len(), 1, "one terminal frame: {raw}");
frames.into_iter().next().unwrap()
} else {
serde_json::from_str(&raw).unwrap()
}
}
fn correlated(request: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
request.header("x-request-id", "ping-atdd").header(
"traceparent",
"00-11111111111111111111111111111111-2222222222222222-01",
)
}
async fn success_control(revision: &str) -> Vec<(usize, usize)> {
let h = endpoint().await;
// The existing fixture performs real initialize -> initialized for legacy only.
let session = h.session(revision).await;
let mut sdk = vec![];
for (accept, extended) in [
("application/json", false),
("text/event-stream, application/json", true),
] {
let counters = h.counters.snapshot();
let ops = h.sessions.snapshot();
let before = (h.counters.sdk_ping_request(), h.counters.sdk_ping_result());
let response = correlated(h.request(
revision,
session.as_deref(),
&h.key,
accept,
ping(revision, extended),
))
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
assert_eq!(response.headers()["MCP-Protocol-Version"], revision);
assert!(response.headers().get("MCP-Session-Id").is_none());
let wire = wire_body(response, accept.starts_with("text/event-stream")).await;
let result = if revision == MODERN_REVISION {
json!({"protocolVersion":revision,"resultType":"complete"})
} else {
json!({"protocolVersion":revision})
};
assert_eq!(wire, json!({"jsonrpc":"2.0","id":"ping/Ю","result":result}));
assert_deltas(
"ping",
counters,
h.counters.snapshot(),
Snapshot::new(
1,
usize::from(revision == MODERN_REVISION),
usize::from(revision == LEGACY_REVISION),
0,
),
);
assert_session_ops(
"ping",
ops,
h.sessions.snapshot(),
SessionOps {
get: 2 * usize::from(revision == LEGACY_REVISION),
create: 0,
mark_initialized: 0,
},
);
sdk.push((
h.counters.sdk_ping_request() - before.0,
h.counters.sdk_ping_result() - before.1,
));
eprintln!(
"existing GREEN ping revision={revision} accept={accept} extended={extended}; SDK delta={:?}",
sdk.last().unwrap()
);
}
sdk
}
#[tokio::test]
async fn ping_wire_json_sse_tolerance_and_session_contract() {
success_control(LEGACY_REVISION).await;
success_control(MODERN_REVISION).await;
}
#[tokio::test]
async fn ping_sdk_legacy_request_and_result() {
assert_eq!(
success_control(LEGACY_REVISION).await,
vec![(1, 1); 2],
"successful legacy HTTP ping must consume SDK request and serialize SDK result"
);
}
#[tokio::test]
async fn ping_sdk_modern_request_and_result() {
assert_eq!(
success_control(MODERN_REVISION).await,
vec![(1, 1); 2],
"successful stateless HTTP ping must consume SDK request and serialize SDK result"
);
}
#[tokio::test]
async fn ping_guards_never_enter_sdk() {
let h = endpoint().await;
for revision in [LEGACY_REVISION, MODERN_REVISION] {
let mut payload = ping(revision, true);
if revision == MODERN_REVISION {
payload["params"]["_meta"] = Value::Null;
}
let response = correlated(h.request(revision, None, &h.key, "application/json", payload))
.send()
.await
.unwrap();
assert_eq!(response.status(), 400);
if revision == LEGACY_REVISION {
assert!(response.headers().get("content-type").is_none());
assert!(response.bytes().await.unwrap().is_empty());
} else {
let wire = wire_body(response, false).await;
assert_eq!(wire["id"], "ping/Ю");
assert_eq!(wire["error"]["code"], -32020);
assert!(wire.get("result").is_none());
}
assert_eq!(
(h.counters.sdk_ping_request(), h.counters.sdk_ping_result()),
(0, 0)
);
eprintln!("existing GREEN pre-seam guard revision={revision}; SDK=(0,0)");
}
}
#[tokio::test]
async fn ping_observer_positive_control() {
let c = Arc::new(CounterSet::default());
let request: rmcp::model::PingRequest =
serde_json::from_value(json!({"method":"ping"})).unwrap();
observe(c.clone(), async {
super::sdk_ping_request_consumed(&request);
assert_eq!((c.sdk_ping_request(), c.sdk_ping_result()), (1, 0));
super::sdk_ping_result_serialized();
assert_eq!((c.sdk_ping_request(), c.sdk_ping_result()), (1, 1));
})
.await;
super::sdk_ping_request_consumed(&request);
super::sdk_ping_result_serialized();
observe(Arc::new(CounterSet::default()), async {
super::sdk_ping_request_consumed(&request);
super::sdk_ping_result_serialized();
})
.await;
assert_eq!(
(c.sdk_ping_request(), c.sdk_ping_result()),
(1, 1),
"scope isolation and out-of-scope no-op"
);
assert_eq!(c.snapshot(), Snapshot::new(0, 0, 0, 0));
}
@@ -0,0 +1,296 @@
//! Narrow #107 HTTP fixture; registry/catalog/auth data reuse the dispatch observer support.
use super::*;
use crank_runtime::{
InMemoryCoordinationStateStore, RequestRateLimitConfig, RequestRateLimiter, SecretCrypto,
};
use std::sync::atomic::{AtomicUsize, Ordering};
use tokio::sync::Semaphore;
pub(in crate::app::dispatch_observer) struct Endpoint {
pub registry: crank_registry::PostgresRegistry,
pub counters: Arc<CounterSet>,
pub sessions: Arc<CountedSessionStore>,
pub key: String,
pub other_key: String,
pub gate: Arc<Semaphore>,
pub started: Arc<AtomicUsize>,
url: String,
servers: Vec<tokio::task::JoinHandle<()>>,
}
impl Drop for Endpoint {
fn drop(&mut self) {
for server in &self.servers {
server.abort();
}
}
}
impl Endpoint {
pub async fn started() -> Self {
Self::started_with_store(None).await
}
pub async fn started_with_store(store: Option<crate::session::SharedSessionStore>) -> Self {
crank_test_support::postgres_schema_url_for_task("progress_owner", "t_d65dd129").await;
let registry = test_registry().await;
let gate = Arc::new(Semaphore::new(0));
let started = Arc::new(AtomicUsize::new(0));
let (g, s) = (gate.clone(), started.clone());
let upstream = axum::Router::new().route(
"/crm/leads",
axum::routing::post(move || {
let (g, s) = (g.clone(), s.clone());
async move {
s.fetch_add(1, Ordering::SeqCst);
g.acquire().await.unwrap().forget();
axum::Json(json!({"id":"lead_progress"}))
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
let upstream_server = tokio::spawn(async move {
axum::serve(listener, upstream).await.unwrap();
});
let mut operation = test_operation(&base);
operation.execution_config.timeout_ms = 30_000;
registry
.create_operation(&workspace_id(), &operation, Some("alice"))
.await
.unwrap();
registry
.publish_operation(crank_registry::PublishRequest {
workspace_id: &workspace_id(),
operation_id: &operation.id,
version: 1,
published_at: &test_time(),
published_by: Some("alice"),
})
.await
.unwrap();
publish_agent(&registry, &operation).await;
let key = create_api_key(&registry).await;
let other_key = format!("crk_test_{}", uuid::Uuid::now_v7().simple());
let other = crank_core::PlatformApiKey {
id: crank_core::PlatformApiKeyId::new("pk_progress_other"),
workspace_id: workspace_id(),
agent_id: Some(agent_id()),
key_kind: crank_core::PlatformApiKeyKind::McpClient,
name: "progress-other".into(),
prefix: other_key.chars().take(16).collect(),
scopes: vec![
crank_core::PlatformApiKeyScope::Read,
crank_core::PlatformApiKeyScope::Write,
],
status: crank_core::PlatformApiKeyStatus::Active,
created_at: test_time(),
last_used_at: None,
expires_at: None,
allowed_origins: vec![],
};
registry
.create_platform_api_key(crank_registry::CreatePlatformApiKeyRequest {
api_key: &other,
secret_hash: &hash_access_secret(&other_key),
})
.await
.unwrap();
let counters = Arc::new(CounterSet::default());
let sessions = Arc::new(CountedSessionStore::default());
let app = crate::build_app(
registry.clone(),
Duration::ZERO,
Some("https://crank.example.com".into()),
SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap(),
crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build(),
RequestRateLimiter::new(RequestRateLimitConfig::new(10_000, 10_000).unwrap()),
Arc::new(InMemoryCoordinationStateStore::default()),
store.unwrap_or_else(|| sessions.clone()),
Arc::new(crate::auth::CommunityMachineCredentialVerifier),
);
let scope = counters.clone();
let app = app.layer(axum::middleware::from_fn(
move |request: axum::extract::Request, next: axum::middleware::Next| {
let c = scope.clone();
async move {
let response = observe(c.clone(), next.run(request)).await;
observe_body(response, c)
}
},
));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!(
"http://{}/v1/{WORKSPACE_SLUG}/{AGENT_SLUG}",
listener.local_addr().unwrap()
);
let server = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
Self {
registry,
counters,
sessions,
key,
other_key,
gate,
started,
url,
servers: vec![server, upstream_server],
}
}
pub fn request(
&self,
revision: &str,
session: Option<&str>,
key: &str,
accept: &str,
payload: Value,
) -> reqwest::RequestBuilder {
let mut request = reqwest::Client::new()
.post(&self.url)
.bearer_auth(key)
.header("Accept", accept)
.header("MCP-Protocol-Version", revision);
if let Some(session) = session {
request = request.header("MCP-Session-Id", session);
}
if revision == MODERN_REVISION {
request = request
.header("Mcp-Method", payload["method"].as_str().unwrap())
.header("Mcp-Name", TOOL_NAME);
}
request.json(&payload)
}
pub fn call(
&self,
revision: &str,
session: Option<&str>,
id: Value,
token: Option<Value>,
accept: &str,
) -> reqwest::RequestBuilder {
let mut meta = if revision == MODERN_REVISION {
modern_meta()
} else {
json!({})
};
if let Some(token) = token {
meta["progressToken"] = token;
}
self.request(
revision,
session,
&self.key,
accept,
json!({"jsonrpc":"2.0","id":id,"method":"tools/call",
"params":{"name":TOOL_NAME,"arguments":{"email":"progress@example.com"},"_meta":meta}}),
)
}
pub async fn session(&self, revision: &str) -> Option<String> {
if revision == MODERN_REVISION {
return None;
}
let response = self
.request(
revision,
None,
&self.key,
"application/json",
legacy_initialize(1),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
let session = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
assert_eq!(
json_body(response).await["result"]["protocolVersion"],
revision
);
let response = self
.request(
revision,
Some(&session),
&self.key,
"application/json",
initialized_notification(),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 202);
assert!(response.bytes().await.unwrap().is_empty());
Some(session)
}
pub async fn cancel(&self, key: &str, session: &str, params: Value) {
let shape = match params.get("requestId") {
None => "missing",
Some(Value::Null) => "null",
Some(Value::Bool(_)) => "bool",
Some(Value::Array(_)) => "array",
Some(Value::Object(_)) => "object",
Some(Value::Number(_)) => "number",
Some(Value::String(_)) => "string",
};
let response = self
.request(
LEGACY_REVISION,
Some(session),
key,
"application/json",
json!({"jsonrpc":"2.0","method":"notifications/cancelled","params":params}),
)
.send()
.await
.unwrap();
let status = response.status();
let body = response.bytes().await.unwrap();
if status != 202 {
let error: Value = serde_json::from_slice(&body).unwrap_or(Value::Null);
panic!(
"cancel target shape={shape}, HTTP {status}, code={}, message={}, details={}",
error["error"]["code"],
error["error"]["message"],
error["error"]["data"]["details"]
);
}
assert!(body.is_empty(), "ack must disclose nothing");
}
pub async fn wait_started(&self, count: usize) {
tokio::time::timeout(Duration::from_secs(5), async {
while self.started.load(Ordering::SeqCst) < count {
tokio::time::sleep(Duration::from_millis(5)).await;
}
})
.await
.expect("actual outbound dispatch before cancellation");
}
pub async fn history(&self, request: &str) -> Vec<crank_core::InvocationLog> {
self.registry
.list_invocation_logs(crank_registry::ListInvocationLogsQuery {
workspace_id: &workspace_id(),
level: None,
status: None,
outcome_group: None,
search_text: None,
source: Some(crank_core::InvocationSource::AgentToolCall),
operation_id: Some(&crank_core::OperationId::new(format!("op_{TOOL_NAME}"))),
agent_id: None,
created_after: None,
created_before: None,
cursor_created_at: None,
cursor_id: None,
limit: 50,
})
.await
.unwrap()
.into_iter()
.map(|row| row.log)
.filter(|log| log.request_id.as_deref() == Some(request))
.collect()
}
}
@@ -0,0 +1,457 @@
//! #107: real HTTP wire controls first; SDK participation is an independent migration oracle.
use super::{CounterSet, observe, support::*};
use axum::{body::Body, response::Response};
use futures_util::{StreamExt, stream};
use serde_json::{Value, json};
use std::{sync::Arc, time::Duration};
#[path = "rmcp_progress_cancel_http_support.rs"]
pub(super) mod fixtures;
use fixtures::Endpoint;
fn observe_body(response: Response, counters: Arc<CounterSet>) -> Response {
let (parts, body) = response.into_parts();
let body = stream::unfold(
(body.into_data_stream(), counters),
|(mut body, c)| async move {
observe(c.clone(), body.next())
.await
.map(|chunk| (chunk, (body, c)))
},
);
Response::from_parts(parts, Body::from_stream(body))
}
async fn frames(response: reqwest::Response) -> Vec<Value> {
assert_eq!(response.status(), 200);
let sse = response.headers()["content-type"]
.to_str()
.unwrap()
.starts_with("text/event-stream");
let raw = tokio::time::timeout(Duration::from_secs(5), response.text())
.await
.unwrap()
.unwrap();
if sse {
raw.lines()
.filter_map(|line| {
line.strip_prefix("data:")
.map(|data| serde_json::from_str(data.trim()).unwrap())
})
.collect()
} else if raw.is_empty() {
vec![]
} else {
vec![serde_json::from_str(&raw).unwrap()]
}
}
fn terminal(frames: &[Value], revision: &str, id: Value) -> Value {
let last = frames.last().expect("terminal result");
assert_eq!(last["jsonrpc"], "2.0");
assert_eq!(last["id"], id);
assert!(last.get("method").is_none());
assert!(last.get("error").is_none());
assert_eq!(last["result"]["isError"], false);
assert_eq!(
last["result"]["structuredContent"],
json!({"id":"lead_progress"})
);
if revision == MODERN_REVISION {
assert_eq!(last["result"]["resultType"], "complete");
}
last["result"].clone()
}
async fn progress_control(revision: &str) -> usize {
let h = Endpoint::started().await;
let session = h.session(revision).await;
// Establish the unchanged successful execution independently of any notification/marker.
h.gate.add_permits(1);
let plain = frames(
h.call(
revision,
session.as_deref(),
json!(51),
None,
"application/json",
)
.send()
.await
.unwrap(),
)
.await;
assert_eq!(plain.len(), 1);
let expected = terminal(&plain, revision, json!(51));
assert_eq!(h.counters.sdk_progress_notification(), 0);
let mut sdk_positive = usize::MAX;
for token in [
json!("opaque-token"),
json!(-7),
json!(1.5),
json!(1.0),
json!(-0.0),
json!(u64::MAX),
] {
let before = h.counters.sdk_progress_notification();
h.gate.add_permits(1);
let reply = h
.call(
revision,
session.as_deref(),
json!(51),
Some(token.clone()),
"application/json, text/event-stream",
)
.send()
.await
.unwrap();
assert!(
reply.headers()["content-type"]
.to_str()
.unwrap()
.starts_with("text/event-stream")
);
let reply = frames(reply).await;
assert_eq!(reply.len(), 4, "three bounded real steps and one terminal");
for (index, message) in [
"tool_resolved",
"authorization_resolved",
"execution_dispatched",
]
.iter()
.enumerate()
{
let expected_frame = json!({"jsonrpc":"2.0","method":"notifications/progress","params":{
"progressToken":token,"progress":index+1,"message":message}});
// Stringify too: float-form tokens must not silently become integers/strings.
assert_eq!(reply[index].to_string(), expected_frame.to_string());
assert!(reply[index]["params"]["progress"].as_u64().is_some());
}
assert_eq!(terminal(&reply, revision, json!(51)), expected);
sdk_positive = sdk_positive.min(h.counters.sdk_progress_notification() - before);
}
for token in [
None,
Some(json!(null)),
Some(json!(true)),
Some(json!({})),
Some(json!([])),
Some(json!("")),
Some(json!("bad\ntoken")),
Some(json!("x".repeat(129))),
] {
h.gate.add_permits(1);
let before = h.counters.sdk_progress_notification();
let reply = frames(
h.call(
revision,
session.as_deref(),
json!(51),
token,
"text/event-stream",
)
.send()
.await
.unwrap(),
)
.await;
assert_eq!(reply.len(), 1, "invalid/absent token is silent");
assert_eq!(terminal(&reply, revision, json!(51)), expected);
assert_eq!(h.counters.sdk_progress_notification(), before);
}
h.gate.add_permits(1);
let before = h.counters.sdk_progress_notification();
let reply = frames(
h.call(
revision,
session.as_deref(),
json!(51),
Some(json!(u64::MAX)),
"application/json",
)
.send()
.await
.unwrap(),
)
.await;
assert_eq!(reply.len(), 1, "no SSE means no progress");
assert_eq!(terminal(&reply, revision, json!(51)), expected);
assert_eq!(h.counters.sdk_progress_notification(), before);
eprintln!(
"existing GREEN progress HTTP {revision}: exact token forms, bounded integer steps, unchanged result and silent negatives; minimum per-token SDK delta={sdk_positive}"
);
sdk_positive
}
async fn cancel_control(with_meta: bool) -> usize {
let h = Endpoint::started().await;
let session = h.session(LEGACY_REVISION).await.unwrap();
let foreign = h.session(LEGACY_REVISION).await.unwrap();
let request = h
.call(
LEGACY_REVISION,
Some(&session),
json!(u64::MAX),
Some(json!("cancel-token")),
"text/event-stream",
)
.header("x-request-id", "progress-cancel-owner");
let mut call = tokio::spawn(async move { frames(request.send().await.unwrap()).await });
h.wait_started(1).await;
// All no-ops target this active call while its upstream response is held, not a completed call.
for params in [
json!({"requestId":u64::MAX.to_string()}),
json!({"requestId":999}),
json!({}),
json!({"requestId":null}),
json!({"requestId":true}),
json!({"requestId":[]}),
json!({"requestId":{}}),
] {
h.cancel(&h.key, &session, params).await;
}
eprintln!("cancel diagnostic: malformed/unknown/lookalike controls passed");
h.cancel(&h.other_key, &session, json!({"requestId":u64::MAX}))
.await;
h.cancel(&h.key, &foreign, json!({"requestId":u64::MAX}))
.await;
eprintln!("cancel diagnostic: foreign ownership controls passed");
assert!(
tokio::time::timeout(Duration::from_millis(100), &mut call)
.await
.is_err(),
"no-op targets must leave the owned call running"
);
// Now let exactly that call finish: this proves no-op, rather than just delayed cancellation.
h.gate.add_permits(1);
let reply = call.await.unwrap();
terminal(&reply, LEGACY_REVISION, json!(u64::MAX));
let history = h.history("progress-cancel-owner").await;
assert_eq!(history.len(), 1);
assert_eq!(history[0].status, crank_core::InvocationStatus::Ok);
h.cancel(&h.key, &session, json!({"requestId":u64::MAX}))
.await;
assert_eq!(
h.history("progress-cancel-owner").await.len(),
1,
"late cancel adds no outcome"
);
let request = h
.call(
LEGACY_REVISION,
Some(&session),
json!(u64::MAX),
Some(json!("cancel-token")),
"text/event-stream",
)
.header("x-request-id", "progress-cancel-stop");
let call = tokio::spawn(async move { frames(request.send().await.unwrap()).await });
h.wait_started(2).await;
let before = h.counters.sdk_cancelled_params();
let mut params = json!({"requestId":u64::MAX,"reason":42,"unknown":{"nested":true}});
if with_meta {
params["_meta"] = json!({});
}
h.cancel(&h.key, &session, params).await;
let delta = h.counters.sdk_cancelled_params() - before;
let reply = tokio::time::timeout(Duration::from_secs(1), call)
.await
.expect("cancel releases call while upstream is still held")
.unwrap();
assert_eq!(reply.len(), 3);
assert!(
reply
.iter()
.all(|f| f["method"] == "notifications/progress"),
"no false terminal result"
);
assert!(h.history("progress-cancel-stop").await.is_empty());
// Releasing an abandoned upstream cannot turn the cancelled execution into completed history.
h.gate.add_permits(2);
let reply = frames(
h.call(
LEGACY_REVISION,
Some(&session),
json!(u64::MAX),
None,
"application/json",
)
.send()
.await
.unwrap(),
)
.await;
terminal(&reply, LEGACY_REVISION, json!(u64::MAX));
assert!(h.history("progress-cancel-stop").await.is_empty());
assert_eq!(
h.started.load(std::sync::atomic::Ordering::SeqCst),
3,
"no automatic write retry"
);
eprintln!(
"existing GREEN cancel HTTP legacy: active no-ops, numeric owner stop with ignored fields, released/reused id and no false history; SDK delta={delta}"
);
delta
}
#[tokio::test]
async fn rmcp_progress_cancel_wire_legacy() {
progress_control(LEGACY_REVISION).await;
}
#[tokio::test]
async fn rmcp_progress_cancel_wire_modern() {
progress_control(MODERN_REVISION).await;
}
#[tokio::test]
async fn rmcp_progress_cancel_wire_cancellation() {
cancel_control(false).await;
}
#[tokio::test]
async fn rmcp_progress_cancel_sdk_legacy() {
assert!(
progress_control(LEGACY_REVISION).await > 0,
"missing SDK progress serialization after successful HTTP controls"
);
}
#[tokio::test]
async fn rmcp_progress_cancel_sdk_modern() {
assert!(
progress_control(MODERN_REVISION).await > 0,
"missing SDK progress serialization after successful HTTP controls"
);
}
#[tokio::test]
async fn rmcp_progress_cancel_sdk_cancellation() {
assert!(
cancel_control(false).await > 0,
"missing SDK cancelled params decode after successful HTTP stop controls"
);
}
#[tokio::test]
async fn rmcp_progress_cancel_wire_modern_stream_close() {
let h = Endpoint::started().await;
let response = h
.call(
MODERN_REVISION,
None,
json!(52),
Some(json!("disconnect")),
"text/event-stream",
)
.header("x-request-id", "progress-modern-stop")
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
h.wait_started(1).await;
drop(response);
tokio::time::sleep(Duration::from_millis(250)).await;
// Match the existing lifecycle grace; an un-stopped future would complete after gate release.
h.gate.add_permits(2);
tokio::time::sleep(Duration::from_millis(2500)).await;
assert!(
h.history("progress-modern-stop").await.is_empty(),
"closed stream must not reach completed history"
);
let reply = frames(
h.call(MODERN_REVISION, None, json!(52), None, "application/json")
.send()
.await
.unwrap(),
)
.await;
terminal(&reply, MODERN_REVISION, json!(52));
assert_eq!(
h.started.load(std::sync::atomic::Ordering::SeqCst),
2,
"no retry after disconnect"
);
assert!(h.history("progress-modern-stop").await.is_empty());
}
#[tokio::test]
async fn rmcp_progress_cancel_admitted_meta_cancellation() {
assert!(cancel_control(true).await > 0);
}
#[tokio::test]
async fn rmcp_progress_cancel_malformed_meta_rejected_before_decode() {
let h = Endpoint::started().await;
let session = h.session(LEGACY_REVISION).await.unwrap();
let request = h
.call(
LEGACY_REVISION,
Some(&session),
json!(u64::MAX),
Some(json!("meta-boundary")),
"text/event-stream",
)
.header("x-request-id", "progress-meta-rejected");
let mut call = tokio::spawn(async move { frames(request.send().await.unwrap()).await });
h.wait_started(1).await;
let before = h.counters.sdk_cancelled_params();
let response = h
.request(
LEGACY_REVISION,
Some(&session),
&h.key,
"application/json",
json!({"jsonrpc":"2.0","method":"notifications/cancelled","params":{
"requestId":u64::MAX,"reason":42,"_meta":[],"unknown":{"nested":true}}}),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 400);
let error = json_body(response).await;
assert_eq!(error["error"]["code"], -32020);
assert_eq!(error["error"]["message"], "protocol identity mismatch");
assert_eq!(
error["error"]["data"]["details"],
"request _meta must be an object"
);
assert_eq!(h.counters.sdk_cancelled_params(), before);
assert!(
tokio::time::timeout(Duration::from_millis(100), &mut call)
.await
.is_err()
);
h.gate.add_permits(1);
let reply = call.await.unwrap();
terminal(&reply, LEGACY_REVISION, json!(u64::MAX));
let history = h.history("progress-meta-rejected").await;
assert_eq!(history.len(), 1);
assert_eq!(history[0].status, crank_core::InvocationStatus::Ok);
assert_eq!(h.started.load(std::sync::atomic::Ordering::SeqCst), 1);
assert_eq!(h.counters.sdk_cancelled_params(), before);
}
#[tokio::test]
async fn rmcp_progress_cancel_observer_scope_control() {
use super::{sdk_cancelled_params_decoded, sdk_progress_notification_serialized};
let a = Arc::new(CounterSet::default());
let b = Arc::new(CounterSet::default());
sdk_cancelled_params_decoded();
sdk_progress_notification_serialized();
assert_eq!(a.sdk_cancelled_params(), 0);
tokio::join!(
observe(a.clone(), async {
sdk_cancelled_params_decoded();
}),
observe(b.clone(), async {
sdk_progress_notification_serialized();
})
);
assert_eq!(
[a.sdk_cancelled_params(), a.sdk_progress_notification()],
[1, 0]
);
assert_eq!(
[b.sdk_cancelled_params(), b.sdk_progress_notification()],
[0, 1]
);
let body = Body::from_stream(stream::once(async {
sdk_progress_notification_serialized();
Ok::<_, std::convert::Infallible>("data: terminal\n\n")
}));
let response = observe_body(Response::new(body), a.clone());
assert_eq!(a.sdk_progress_notification(), 0);
axum::body::to_bytes(response.into_body(), 100)
.await
.unwrap();
assert_eq!(a.sdk_progress_notification(), 1);
assert_eq!(b.sdk_progress_notification(), 1);
}
@@ -0,0 +1,436 @@
//! #107 Prompts: existing wire GREEN and an independent SDK typed-path RED oracle.
use super::{CounterSet, observe, support::*};
use axum::{body::Body, response::Response};
use futures_util::{StreamExt, stream};
use serde_json::{Value, json};
use std::{sync::Arc, time::Duration};
#[path = "rmcp_prompts_test_support.rs"]
mod fixtures;
use fixtures::Endpoint;
fn observe_body(response: Response, counters: Arc<CounterSet>) -> Response {
let (parts, body) = response.into_parts();
let body = stream::unfold(
(body.into_data_stream(), counters),
|(mut body, counters)| async move {
observe(counters.clone(), body.next())
.await
.map(|chunk| (chunk, (body, counters)))
},
);
Response::from_parts(parts, Body::from_stream(body))
}
fn counts(c: &CounterSet) -> [usize; 3] {
[
c.sdk_prompts_dispatch(),
c.sdk_prompts_list_result(),
c.sdk_prompts_get_result(),
]
}
fn request(revision: &str, method: &str, mut params: Value) -> Value {
if revision == MODERN_REVISION {
params["_meta"] = modern_meta();
}
json!({"jsonrpc":"2.0","id":9007199254740993_u64,"method":method,"params":params})
}
async fn wire(response: reqwest::Response, id: &Value, sse: bool) -> Value {
assert_eq!(response.status(), 200);
assert_eq!(response.headers()["x-request-id"], "sdk-prompts-wire");
assert_eq!(
response.headers()["x-trace-id"],
"11111111111111111111111111111111"
);
let content_type = response.headers()["content-type"]
.to_str()
.unwrap()
.to_owned();
let text = tokio::time::timeout(Duration::from_secs(5), response.text())
.await
.unwrap()
.unwrap();
let body: Value = if sse {
assert!(
content_type.starts_with("text/event-stream"),
"{content_type}: {text}"
);
let frames: Vec<Value> = text
.lines()
.filter_map(|line| {
line.strip_prefix("data:")
.map(|s| serde_json::from_str(s.trim()).unwrap())
})
.collect();
let terminal: Vec<_> = frames.iter().filter(|f| f.get("id") == Some(id)).collect();
assert_eq!(terminal.len(), 1, "{frames:?}");
assert_eq!(frames.last(), Some(terminal[0]), "terminal must be last");
terminal[0].clone()
} else {
assert!(
content_type.starts_with("application/json"),
"{content_type}: {text}"
);
serde_json::from_str(&text).unwrap()
};
assert_eq!(body["jsonrpc"], "2.0");
assert_eq!(&body["id"], id);
body
}
fn era(result: &Value, revision: &str, method: &str) {
if revision == MODERN_REVISION {
assert_eq!(result["resultType"], "complete", "{result}");
if method == "prompts/list" {
assert_eq!(result["ttlMs"], 0);
assert_eq!(result["cacheScope"], "private");
} else {
assert!(result.get("ttlMs").is_none(), "{result}");
assert!(result.get("cacheScope").is_none(), "{result}");
}
} else {
for key in ["resultType", "ttlMs", "cacheScope"] {
assert!(result.get(key).is_none(), "{result}");
}
}
assert!(result.get("_meta").is_none(), "{result}");
}
async fn success(
h: &Endpoint,
revision: &str,
session: Option<&str>,
method: &str,
params: Value,
sse: bool,
) -> (Value, [usize; 3]) {
let req = request(revision, method, params);
let before = counts(&h.counters);
let body = wire(h.post(revision, session, &req, sse).await, &req["id"], sse).await;
assert!(body.get("error").is_none(), "{body}");
era(&body["result"], revision, method);
let after = counts(&h.counters);
(
body["result"].clone(),
std::array::from_fn(|i| after[i] - before[i]),
)
}
async fn control(revision: &str) -> Vec<([usize; 3], [usize; 3])> {
let h = Endpoint::started().await;
let session = h.session(revision).await;
let session = session.as_deref();
let mut deltas = Vec::new();
let mut first_cursor = None;
// Both revisions' JSON contract, plus modern terminal SSE through the same normal app.
let modes: &[bool] = if revision == MODERN_REVISION {
&[false, true]
} else {
&[false]
};
for &sse in modes {
let mut cursor = None;
for (page, name) in ["greeting", "archived-prompt"].iter().enumerate() {
let mut params = json!({"limit":1});
if let Some(ref cursor) = cursor {
params["cursor"] = json!(cursor);
}
let (result, delta) = success(&h, revision, session, "prompts/list", params, sse).await;
let prompts = result["prompts"].as_array().unwrap();
assert_eq!(prompts.len(), 1, "{result}");
assert_eq!(
prompts[0],
json!({
"name":name, "description":format!("Published {name}"),
"arguments":[
{"name":"name","schema":{"type":"string"},"required":true},
{"name":"count","schema":{"type":"integer"},"required":false,"default":2},
{"name":"enabled","schema":{"type":"boolean"},"required":false,"default":true},
{"name":"tone","schema":{"type":"string","enum":["warm","dry"]},"required":false,"default":"warm"}
]
})
);
cursor = result
.get("nextCursor")
.map(|v| v.as_str().unwrap().to_owned());
assert_eq!(cursor.is_some(), page == 0, "{result}");
if page == 0 {
assert_ne!(cursor.as_deref(), Some("1"));
first_cursor = cursor.clone();
}
deltas.push((delta, [1, 1, 0]));
}
for (name, args, text) in [
(
"greeting",
json!({"name":"Ada"}),
"Hello Ada count=2 enabled=true tone=warm",
),
(
"greeting",
json!({"name":"Ada","count":7,"enabled":false,"tone":"dry"}),
"Hello Ada count=7 enabled=false tone=dry",
),
(
"archived-prompt",
json!({"name":"Ada"}),
"Hello Ada count=2 enabled=true tone=warm",
),
] {
let mut previous = None;
for _ in 0..2 {
let (result, delta) = success(
&h,
revision,
session,
"prompts/get",
json!({"name":name,"arguments":args}),
sse,
)
.await;
assert_eq!(result["description"], format!("Published {name}"));
assert_eq!(
result["messages"],
json!([
{"role":"system","content":{"type":"text","text":"Be brief."}},
{"role":"user","content":{"type":"text","text":text}}
])
);
if let Some(ref previous) = previous {
assert_eq!(&result, previous);
}
previous = Some(result);
deltas.push((delta, [1, 0, 1]));
}
}
for (arguments, reason, argument) in [
(json!({}), "missing_argument", "name"),
(
json!({"name":"Ada","extra":"x"}),
"unknown_argument",
"extra",
),
(json!({"name":5}), "argument_invalid", "name"),
(
json!({"name":"Ada","count":"7"}),
"argument_invalid",
"count",
),
(
json!({"name":"Ada","enabled":"false"}),
"argument_invalid",
"enabled",
),
(
json!({"name":"Ada","tone":"loud"}),
"argument_invalid",
"tone",
),
(
json!({"name":"x".repeat(crank_core::prompt::MAX_TEXT_BYTES + 1)}),
"argument_too_large",
"name",
),
] {
let req = request(
revision,
"prompts/get",
json!({"name":"greeting","arguments":arguments}),
);
let body = wire(h.post(revision, session, &req, sse).await, &req["id"], sse).await;
assert_eq!(body["error"]["code"], -32602, "{body}");
assert_eq!(body["error"]["data"]["reason"], reason, "{body}");
assert_eq!(body["error"]["data"]["argument"], argument);
assert!(body.get("result").is_none(), "{body}");
for leaked in [
"Be brief.",
"Hello ",
"Changed ",
"Published unbound-prompt",
] {
assert!(!body.to_string().contains(leaked), "{body}");
}
}
let mut unavailable = None;
for name in [
"draft-prompt",
"unbound-prompt",
"other-agent-prompt",
"never-existed",
"prm_a",
] {
let req = request(revision, "prompts/get", json!({"name":name}));
let body = wire(h.post(revision, session, &req, sse).await, &req["id"], sse).await;
assert_eq!(body["error"]["code"], -32602, "{body}");
assert_eq!(
body["error"]["message"],
"the requested prompt is not available"
);
assert!(body.get("result").is_none());
if let Some(ref previous) = unavailable {
assert_eq!(&body, previous);
}
unavailable = Some(body);
}
}
for params in [json!({"cursor":"invalid"}), json!({"limit":"invalid"})] {
let req = request(revision, "prompts/list", params);
let body = wire(
h.post(revision, session, &req, false).await,
&req["id"],
false,
)
.await;
assert_eq!(body["error"]["code"], -32602, "{body}");
}
let other = if revision == MODERN_REVISION {
LEGACY_REVISION
} else {
MODERN_REVISION
};
let other_session = h.session(other).await;
let req = request(
other,
"prompts/list",
json!({"cursor":first_cursor.unwrap()}),
);
let body = wire(
h.post(other, other_session.as_deref(), &req, false).await,
&req["id"],
false,
)
.await;
assert_eq!(
body["error"]["code"], -32602,
"cross-revision cursor: {body}"
);
// Archive keeps serving v1 above, but disallows new publications and bindings.
use crank_core::ids::PromptId;
use crank_registry::model::{
AgentPromptBinding, PublishPromptRequest, SaveAgentCatalogConfigRequest,
};
let publication = h
.registry
.publish_prompt_draft(PublishPromptRequest {
workspace_id: workspace_id(),
prompt_id: PromptId::new("prm_b"),
expected_updated_at: h.archived_at,
published_at: time::OffsetDateTime::now_utc(),
})
.await;
assert!(
matches!(
publication,
Err(crank_registry::RegistryError::PromptNotDraft { .. })
),
"{publication:?}"
);
let binding = h
.registry
.save_agent_catalog_config_editable_with_prompts(
SaveAgentCatalogConfigRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
agent_version: 1,
bindings: &[],
tool_selection_policy: &crank_core::ToolSelectionPolicy::default(),
resource_bindings: &[],
expected_state: None,
},
&[AgentPromptBinding {
prompt_id: PromptId::new("prm_b"),
prompt_version: 1,
}],
)
.await;
assert!(
matches!(
binding,
Err(crank_registry::RegistryError::PromptArchived { .. })
),
"{binding:?}"
);
h.registry
.revoke_platform_api_key(
&workspace_id(),
&crank_core::PlatformApiKeyId::new("pk_dispatch_observer"),
&time::OffsetDateTime::now_utc(),
)
.await
.unwrap();
for (method, params) in [
("prompts/list", json!({})),
(
"prompts/get",
json!({"name":"greeting","arguments":{"name":"Ada"}}),
),
] {
assert_eq!(
h.post(revision, session, &request(revision, method, params), false)
.await
.status(),
401
);
}
eprintln!(
"existing GREEN Prompts wire {revision}: populated pagination, typed metadata/values, exact immutable rendering, archive, structured refusals/non-disclosure, correlation/ID/cache, revoked401; SDK actual/expected={deltas:?}"
);
deltas
}
#[tokio::test]
async fn rmcp_prompts_wire_legacy() {
control(LEGACY_REVISION).await;
}
#[tokio::test]
async fn rmcp_prompts_wire_modern() {
control(MODERN_REVISION).await;
}
#[tokio::test]
async fn rmcp_prompts_sdk_legacy() {
let deltas = control(LEGACY_REVISION).await;
assert!(
deltas.iter().all(|(a, e)| a == e),
"SDK typed dispatch/result missing after successful HTTP: {deltas:?}"
);
}
#[tokio::test]
async fn rmcp_prompts_sdk_modern() {
let deltas = control(MODERN_REVISION).await;
assert!(
deltas.iter().all(|(a, e)| a == e),
"SDK typed dispatch/result missing after successful HTTP: {deltas:?}"
);
}
#[tokio::test]
async fn rmcp_prompts_observer_scope_control() {
use super::{
sdk_prompts_dispatch_enter, sdk_prompts_get_result_serialized,
sdk_prompts_list_result_serialized,
};
let a = Arc::new(CounterSet::default());
let b = Arc::new(CounterSet::default());
sdk_prompts_dispatch_enter();
assert_eq!(counts(&a), [0; 3]);
let snapshot = a.snapshot();
observe(a.clone(), async {
sdk_prompts_dispatch_enter();
sdk_prompts_list_result_serialized();
sdk_prompts_get_result_serialized();
})
.await;
observe(b.clone(), async {
sdk_prompts_get_result_serialized();
})
.await;
assert_eq!(counts(&a), [1; 3]);
assert_eq!(counts(&b), [0, 0, 1]);
assert_eq!(a.snapshot(), snapshot);
let body = Body::from_stream(stream::once(async {
sdk_prompts_list_result_serialized();
Ok::<_, std::convert::Infallible>("terminal")
}));
let response = observe_body(Response::new(body), b.clone());
assert_eq!(
&axum::body::to_bytes(response.into_body(), 100)
.await
.unwrap()[..],
b"terminal"
);
assert_eq!(counts(&b), [0, 1, 1]);
assert_eq!(counts(&a), [1; 3]);
}
@@ -0,0 +1,305 @@
//! Published Prompts fixture using the existing observed HTTP app and disposable PostgreSQL.
use super::*;
use crank_core::prompt::{PromptDefinition, PromptSensitivity};
use crank_core::{Agent, AgentStatus, AgentVersion, ToolSelectionPolicy, ids::PromptId};
use crank_registry::model::{
AgentPromptBinding, ArchivePromptRequest, CreatePromptDraftRequest, PublishPromptRequest,
SaveAgentCatalogConfigRequest, UpdatePromptDraftRequest,
};
use crank_registry::{CreateAgentRequest, PublishAgentRequest};
use crank_runtime::{
InMemoryCoordinationStateStore, RequestRateLimitConfig, RequestRateLimiter, SecretCrypto,
};
pub(super) fn definition(name: &str, content: &str) -> PromptDefinition {
PromptDefinition::parse(&json!({
"name":name, "description":format!("Published {name}"),
"arguments":[
{"name":"name","schema":{"type":"string"},"required":true},
{"name":"count","schema":{"type":"integer"},"required":false,"default":2},
{"name":"enabled","schema":{"type":"boolean"},"required":false,"default":true},
{"name":"tone","schema":{"type":"string","enum":["warm","dry"]},"required":false,"default":"warm"}
],
"messages":[{"role":"system","content":"Be brief."},{"role":"user","content":content}]
})).unwrap()
}
pub(super) struct Endpoint {
pub registry: crank_registry::PostgresRegistry,
pub counters: Arc<CounterSet>,
pub key: String,
pub url: String,
pub archived_at: time::OffsetDateTime,
server: tokio::task::JoinHandle<()>,
}
impl Drop for Endpoint {
fn drop(&mut self) {
self.server.abort();
}
}
impl Endpoint {
pub async fn started() -> Self {
crank_test_support::postgres_schema_url_for_task("prompts_owner", "t_160a763a").await;
let registry = test_registry().await;
let mut archived_at = test_time();
for (id, name, publish) in [
("prm_a", "greeting", true),
("prm_b", "archived-prompt", true),
("prm_c", "unbound-prompt", true),
("prm_d", "draft-prompt", false),
("prm_e", "other-agent-prompt", true),
] {
let row = registry
.create_prompt_draft(CreatePromptDraftRequest {
workspace_id: workspace_id(),
prompt_id: PromptId::new(id),
sensitivity: PromptSensitivity::Internal,
definition: definition(
name,
"Hello {{name}} count={{count}} enabled={{enabled}} tone={{tone}}",
),
created_at: time::OffsetDateTime::now_utc(),
})
.await
.unwrap();
if publish {
let row = registry
.publish_prompt_draft(PublishPromptRequest {
workspace_id: workspace_id(),
prompt_id: PromptId::new(id),
expected_updated_at: row.updated_at,
published_at: time::OffsetDateTime::now_utc(),
})
.await
.unwrap();
if id == "prm_b" {
archived_at = row.updated_at;
}
}
}
for (id, slug) in [
(agent_id(), AGENT_SLUG),
(
crank_core::AgentId::new("agent_other_prompts"),
"other-prompts",
),
] {
registry
.create_agent(CreateAgentRequest {
agent: &Agent {
id: id.clone(),
workspace_id: workspace_id(),
slug: slug.into(),
display_name: "SDK Prompts".into(),
description: String::new(),
status: AgentStatus::Draft,
current_draft_version: 1,
latest_published_version: None,
created_at: test_time(),
updated_at: test_time(),
published_at: None,
},
version: &AgentVersion {
agent_id: id,
version: 1,
status: AgentStatus::Draft,
instructions: json!({}),
tool_selection_policy: ToolSelectionPolicy::default(),
created_at: test_time(),
},
bindings: &[],
})
.await
.unwrap();
}
let other_agent = crank_core::AgentId::new("agent_other_prompts");
registry
.save_agent_catalog_config_editable_with_prompts(
SaveAgentCatalogConfigRequest {
workspace_id: &workspace_id(),
agent_id: &other_agent,
agent_version: 1,
bindings: &[],
tool_selection_policy: &ToolSelectionPolicy::default(),
resource_bindings: &[],
expected_state: None,
},
&[AgentPromptBinding {
prompt_id: PromptId::new("prm_e"),
prompt_version: 1,
}],
)
.await
.unwrap();
registry
.publish_agent(PublishAgentRequest {
workspace_id: &workspace_id(),
agent_id: &other_agent,
version: 1,
published_at: &test_time(),
published_by: Some("alice"),
expected_state: None,
})
.await
.unwrap();
registry
.save_agent_catalog_config_editable_with_prompts(
SaveAgentCatalogConfigRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
agent_version: 1,
bindings: &[],
tool_selection_policy: &ToolSelectionPolicy::default(),
resource_bindings: &[],
expected_state: None,
},
&[
AgentPromptBinding {
prompt_id: PromptId::new("prm_a"),
prompt_version: 1,
},
AgentPromptBinding {
prompt_id: PromptId::new("prm_b"),
prompt_version: 1,
},
],
)
.await
.unwrap();
registry
.publish_agent(PublishAgentRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
version: 1,
published_at: &test_time(),
published_by: Some("alice"),
expected_state: None,
})
.await
.unwrap();
let archived_at = registry
.archive_prompt_draft(ArchivePromptRequest {
workspace_id: workspace_id(),
prompt_id: PromptId::new("prm_b"),
expected_updated_at: archived_at,
archived_at: time::OffsetDateTime::now_utc(),
})
.await
.unwrap()
.updated_at;
// A mutable v2 draft must not change the bound v1 rendering or discovery metadata.
let draft = registry
.get_prompt_draft(&workspace_id(), &PromptId::new("prm_a"))
.await
.unwrap();
registry
.update_prompt_draft(UpdatePromptDraftRequest {
workspace_id: workspace_id(),
prompt_id: PromptId::new("prm_a"),
sensitivity: PromptSensitivity::Internal,
definition: definition("changed-draft", "Changed {{name}}"),
expected_updated_at: draft.updated_at,
updated_at: time::OffsetDateTime::now_utc(),
})
.await
.unwrap();
let key = create_api_key(&registry).await;
let counters = Arc::new(CounterSet::default());
let app = crate::build_app(
registry.clone(),
Duration::ZERO,
None,
SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap(),
crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build(),
RequestRateLimiter::new(RequestRateLimitConfig::new(10_000, 10_000).unwrap()),
Arc::new(InMemoryCoordinationStateStore::default()),
Arc::new(crate::session::InMemorySessionStore::default()),
Arc::new(crate::auth::CommunityMachineCredentialVerifier),
);
let app = observe_requests(app, counters.clone(), None).layer(axum::middleware::from_fn({
let counters = counters.clone();
move |request: axum::extract::Request, next: axum::middleware::Next| {
let counters = counters.clone();
async move { observe_body(next.run(request).await, counters) }
}
}));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!(
"http://{}/v1/{WORKSPACE_SLUG}/{AGENT_SLUG}",
listener.local_addr().unwrap()
);
let server = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
Self {
registry,
counters,
key,
url,
archived_at,
server,
}
}
pub async fn post(
&self,
revision: &str,
session: Option<&str>,
body: &Value,
sse: bool,
) -> reqwest::Response {
let mut request = reqwest::Client::new()
.post(&self.url)
.bearer_auth(&self.key)
.header(
"Accept",
if sse {
"text/event-stream, application/json"
} else {
"application/json"
},
)
.header("x-request-id", "sdk-prompts-wire")
.header(
"traceparent",
"00-11111111111111111111111111111111-2222222222222222-01",
)
.header("MCP-Protocol-Version", revision);
if let Some(session) = session {
request = request.header("MCP-Session-Id", session);
}
if revision == MODERN_REVISION {
request = request.header("Mcp-Method", body["method"].as_str().unwrap());
if let Some(name) = body["params"]["name"].as_str() {
request = request.header("Mcp-Name", name);
}
}
request.json(body).send().await.unwrap()
}
pub async fn session(&self, revision: &str) -> Option<String> {
if revision == MODERN_REVISION {
return None;
}
let response = self
.post(revision, None, &legacy_initialize(1), false)
.await;
assert_eq!(response.status(), 200);
let session = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
assert_eq!(
json_body(response).await["result"]["protocolVersion"],
revision
);
assert_eq!(
self.post(revision, Some(&session), &initialized_notification(), false)
.await
.status(),
202
);
Some(session)
}
}
@@ -0,0 +1,287 @@
//! #107 Resources: existing wire GREEN is independent of the missing SDK typed-path oracle.
use super::{CounterSet, observe, support::*};
use axum::{body::Body, response::Response};
use futures_util::{StreamExt, stream};
use serde_json::{Value, json};
use std::{sync::Arc, time::Duration};
#[path = "rmcp_resources_test_support.rs"]
mod fixtures;
use fixtures::{Endpoint, TEXT, URI};
// As in rmcp_result_runtime_tests: deferred body polling keeps this request's observer.
fn observe_body(response: Response, counters: Arc<CounterSet>) -> Response {
let (parts, body) = response.into_parts();
let body = stream::unfold(
(body.into_data_stream(), counters),
|(mut body, counters)| async move {
observe(counters.clone(), body.next())
.await
.map(|chunk| (chunk, (body, counters)))
},
);
Response::from_parts(parts, Body::from_stream(body))
}
fn counts(c: &CounterSet) -> [usize; 4] {
[
c.sdk_resources_dispatch(),
c.sdk_resources_list_result(),
c.sdk_resources_read_result(),
c.sdk_resource_templates_list_result(),
]
}
fn request(revision: &str, method: &str, mut params: Value) -> Value {
if revision == MODERN_REVISION {
params["_meta"] = modern_meta();
}
json!({"jsonrpc":"2.0", "id":9007199254740993_u64, "method":method, "params":params})
}
async fn wire(response: reqwest::Response, id: &Value, sse: bool) -> Value {
assert_eq!(response.status(), 200);
assert_eq!(response.headers()["x-request-id"], "sdk-resources-wire");
assert_eq!(
response.headers()["x-trace-id"],
"11111111111111111111111111111111"
);
let content_type = response.headers()["content-type"]
.to_str()
.unwrap()
.to_owned();
let text = tokio::time::timeout(Duration::from_secs(5), response.text())
.await
.unwrap()
.unwrap();
let body: Value = if sse {
assert!(
content_type.starts_with("text/event-stream"),
"{content_type}: {text}"
);
let frames: Vec<Value> = text
.lines()
.filter_map(|line| {
line.strip_prefix("data:")
.map(|s| serde_json::from_str(s.trim()).unwrap())
})
.collect();
let terminal: Vec<_> = frames.iter().filter(|f| f.get("id") == Some(id)).collect();
assert_eq!(terminal.len(), 1, "{frames:?}");
assert_eq!(frames.last(), Some(terminal[0]), "terminal must be last");
terminal[0].clone()
} else {
assert!(
content_type.starts_with("application/json"),
"{content_type}: {text}"
);
serde_json::from_str(&text).unwrap()
};
assert_eq!(body["jsonrpc"], "2.0");
assert_eq!(&body["id"], id);
body
}
fn era(result: &Value, revision: &str) {
if revision == MODERN_REVISION {
assert_eq!(result["resultType"], "complete", "{result}");
assert_eq!(result["ttlMs"], 0);
assert_eq!(result["cacheScope"], "private");
} else {
for key in ["resultType", "ttlMs", "cacheScope"] {
assert!(result.get(key).is_none(), "{result}");
}
}
assert!(result.get("_meta").is_none(), "{result}");
}
async fn success(
h: &Endpoint,
revision: &str,
session: Option<&str>,
method: &str,
params: Value,
sse_accept: bool,
) -> (Value, [usize; 4]) {
let req = request(revision, method, params);
let before = counts(&h.counters);
let body = wire(
h.post(revision, session, &req, sse_accept).await,
&req["id"],
sse_accept,
)
.await;
assert!(body.get("error").is_none(), "{body}");
era(&body["result"], revision);
let after = counts(&h.counters);
(
body["result"].clone(),
std::array::from_fn(|i| after[i] - before[i]),
)
}
async fn control(revision: &str) -> Vec<([usize; 4], [usize; 4])> {
let h = Endpoint::started().await;
let session = h.session(revision).await;
let session = session.as_deref();
let mut deltas = Vec::new();
// Preference order negotiates JSON or terminal SSE in both Resources revisions.
for sse in [false, true] {
let mut cursor = None;
let mut names = Vec::new();
for page in 0..2 {
let mut params = json!({"limit":1});
if let Some(ref cursor) = cursor {
params["cursor"] = json!(cursor);
}
let (result, delta) =
success(&h, revision, session, "resources/list", params, sse).await;
let resources = result["resources"].as_array().unwrap();
assert_eq!(resources.len(), 1, "{result}");
let item = &resources[0];
assert_eq!(item["mimeType"], "text/plain");
assert!(
item.get("description").is_none(),
"static descriptors omit description: {item}"
);
names.push((item["name"].clone(), item["uri"].clone()));
cursor = result
.get("nextCursor")
.map(|v| v.as_str().unwrap().to_owned());
assert_eq!(cursor.is_some(), page == 0, "{result}");
if let Some(ref cursor) = cursor {
assert_ne!(cursor, "1", "opaque cursor required");
}
deltas.push((delta, [1, 1, 0, 0]));
}
names.sort_by_key(|n| n.0.as_str().unwrap().to_owned());
assert_eq!(
names,
vec![
(json!("sdk_a"), json!(URI)),
(json!("sdk_b"), json!("crank://resources/sdk-b"))
]
);
let (result, delta) = success(
&h,
revision,
session,
"resources/read",
json!({"uri":URI}),
sse,
)
.await;
assert_eq!(
result["contents"],
json!([{"uri":URI,"mimeType":"text/plain","text":TEXT}])
);
deltas.push((delta, [1, 0, 1, 0]));
let (result, delta) = success(
&h,
revision,
session,
"resources/templates/list",
json!({}),
sse,
)
.await;
assert_eq!(result["resourceTemplates"], json!([]));
assert!(result.get("nextCursor").is_none());
deltas.push((delta, [1, 0, 0, 1]));
}
// Malformed parameters/cursors and indistinguishable unavailable URIs remain command errors.
for (method, params) in [
("resources/list", json!({"cursor":"invalid"})),
("resources/list", json!({"limit":"invalid"})),
("resources/templates/list", json!({"cursor":"invalid"})),
("resources/templates/list", json!({"limit":"invalid"})),
("resources/read", json!({"uri":"crank://resources/unbound"})),
] {
let req = request(revision, method, params);
let body = wire(
h.post(revision, session, &req, false).await,
&req["id"],
false,
)
.await;
assert_eq!(body["error"]["code"], -32602, "{body}");
assert!(body.get("result").is_none(), "{body}");
}
// An existing session does not preserve revoked authorization.
h.registry
.revoke_platform_api_key(
&workspace_id(),
&crank_core::PlatformApiKeyId::new("pk_dispatch_observer"),
&time::OffsetDateTime::now_utc(),
)
.await
.unwrap();
for (method, params) in [
("resources/list", json!({})),
("resources/read", json!({"uri":URI})),
("resources/templates/list", json!({})),
] {
let req = request(revision, method, params);
assert_eq!(h.post(revision, session, &req, false).await.status(), 401);
}
eprintln!(
"existing GREEN Resources wire {revision}: pagination, exact content URI/bytes, empty templates, JSON/SSE negotiation, InvalidParams, revoked401; actual/expected SDK deltas={deltas:?}"
);
deltas
}
#[tokio::test]
async fn rmcp_resources_wire_legacy() {
control(LEGACY_REVISION).await;
}
#[tokio::test]
async fn rmcp_resources_wire_modern() {
control(MODERN_REVISION).await;
}
#[tokio::test]
async fn rmcp_resources_sdk_legacy() {
let deltas = control(LEGACY_REVISION).await;
assert!(
deltas.iter().all(|(actual, expected)| actual == expected),
"SDK typed dispatch/result missing after successful HTTP: {deltas:?}"
);
}
#[tokio::test]
async fn rmcp_resources_sdk_modern() {
let deltas = control(MODERN_REVISION).await;
assert!(
deltas.iter().all(|(actual, expected)| actual == expected),
"SDK typed dispatch/result missing after successful HTTP: {deltas:?}"
);
}
#[tokio::test]
async fn rmcp_resources_observer_scope_control() {
use super::{
sdk_resource_templates_list_result_serialized, sdk_resources_dispatch_enter,
sdk_resources_list_result_serialized, sdk_resources_read_result_serialized,
};
let a = Arc::new(CounterSet::default());
let b = Arc::new(CounterSet::default());
sdk_resources_dispatch_enter();
assert_eq!(counts(&a), [0; 4]);
let snapshot = a.snapshot();
observe(a.clone(), async {
sdk_resources_dispatch_enter();
sdk_resources_list_result_serialized();
sdk_resources_read_result_serialized();
sdk_resource_templates_list_result_serialized();
})
.await;
observe(b.clone(), async {
sdk_resources_read_result_serialized();
})
.await;
assert_eq!(counts(&a), [1; 4]);
assert_eq!(counts(&b), [0, 0, 1, 0]);
assert_eq!(a.snapshot(), snapshot, "old Snapshot stays unchanged");
let body = Body::from_stream(stream::once(async {
sdk_resource_templates_list_result_serialized();
Ok::<_, std::convert::Infallible>("terminal")
}));
let response = observe_body(Response::new(body), b.clone());
assert_eq!(
&axum::body::to_bytes(response.into_body(), 100)
.await
.unwrap()[..],
b"terminal"
);
assert_eq!(counts(&b), [0, 0, 1, 1]);
assert_eq!(counts(&a), [1; 4]);
}
@@ -0,0 +1,227 @@
//! Small Resources fixture: existing observer helpers, exact artifact publications, normal app.
use super::*;
use crank_artifacts::ArtifactStore;
use crank_core::{Agent, AgentStatus, AgentVersion, ToolSelectionPolicy, ids::ResourceId};
use crank_registry::{
AgentResourceBinding, CreateAgentRequest, PublishAgentRequest, SaveAgentCatalogConfigRequest,
};
use crank_runtime::{
InMemoryCoordinationStateStore, RequestRateLimitConfig, RequestRateLimiter, SecretCrypto,
};
use sha2::{Digest, Sha256};
pub(super) const URI: &str = "crank://resources/sdk-a";
pub(super) const TEXT: &str = "published v1\n";
pub(super) struct Endpoint {
pub registry: crank_registry::PostgresRegistry,
pub counters: Arc<CounterSet>,
pub key: String,
pub url: String,
server: tokio::task::JoinHandle<()>,
root: std::path::PathBuf,
}
impl Drop for Endpoint {
fn drop(&mut self) {
self.server.abort();
let _ = std::fs::remove_dir_all(&self.root);
}
}
impl Endpoint {
pub async fn started() -> Self {
crank_test_support::postgres_schema_url_for_task("resources_owner", "t_73561633").await;
let registry = test_registry().await;
let root = std::env::temp_dir().join(format!("rmcp-resources-{}", uuid::Uuid::now_v7()));
std::fs::create_dir(&root).unwrap();
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&root, std::fs::Permissions::from_mode(0o700)).unwrap();
let store = ArtifactStore::open(&root).unwrap();
// Same immutable row shapes and real ArtifactStore::put as static_resources/fixtures.rs.
for (id, uri, name, bytes) in [
("res_sdk_a", URI, "sdk_a", TEXT),
(
"res_sdk_b",
"crank://resources/sdk-b",
"sdk_b",
"second published resource\n",
),
] {
let stored = store.put(bytes.as_bytes()).unwrap();
let digest = format!("{:x}", Sha256::digest(bytes.as_bytes()));
let source = format!("src_{id}");
sqlx::query("insert into artifact_blobs (digest, artifact_ref, size_bytes, storage_lifecycle, created_at, updated_at) values ($1,$2,$3,'available',now(),now())")
.bind(&digest).bind(format!("sha256:{digest}")).bind(stored.size_bytes as i64).execute(registry.pool()).await.unwrap();
sqlx::query("insert into artifact_sources (workspace_id,source_id,blob_digest,mime_type,sensitivity,lifecycle,created_at,updated_at) values ('ws_default',$1,$2,'text/plain','internal','active',now(),now())")
.bind(&source).bind(&digest).execute(registry.pool()).await.unwrap();
sqlx::query("insert into resources (workspace_id,resource_id,uri,name,description,status,kind,current_draft_version,created_at,updated_at) values ('ws_default',$1,$2,$3,'','published','text',1,now(),now())")
.bind(id).bind(uri).bind(name).execute(registry.pool()).await.unwrap();
sqlx::query("insert into resource_versions (workspace_id,resource_id,version,kind,mime_type,size_bytes,digest,sensitivity,artifact_source_id,created_at,superseded_at) values ('ws_default',$1,1,'text','text/plain',$2,$3,'internal',$4,now(),null)")
.bind(id).bind(stored.size_bytes as i64).bind(&digest).bind(&source).execute(registry.pool()).await.unwrap();
sqlx::query("insert into resource_publications (workspace_id,resource_id,version,uri,name,description,kind,mime_type,size_bytes,digest,sensitivity,artifact_source_id,published_at) values ('ws_default',$1,1,$2,$3,'','text','text/plain',$4,$5,'internal',$6,now())")
.bind(id).bind(uri).bind(name).bind(stored.size_bytes as i64).bind(&digest).bind(&source).execute(registry.pool()).await.unwrap();
}
registry
.create_agent(CreateAgentRequest {
agent: &Agent {
id: agent_id(),
workspace_id: workspace_id(),
slug: AGENT_SLUG.into(),
display_name: "SDK Resources".into(),
description: String::new(),
status: AgentStatus::Draft,
current_draft_version: 1,
latest_published_version: None,
created_at: test_time(),
updated_at: test_time(),
published_at: None,
},
version: &AgentVersion {
agent_id: agent_id(),
version: 1,
status: AgentStatus::Draft,
instructions: json!({}),
tool_selection_policy: ToolSelectionPolicy::default(),
created_at: test_time(),
},
bindings: &[],
})
.await
.unwrap();
registry
.save_agent_catalog_config(SaveAgentCatalogConfigRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
agent_version: 1,
bindings: &[],
tool_selection_policy: &ToolSelectionPolicy::default(),
resource_bindings: &[
AgentResourceBinding {
resource_id: ResourceId::new("res_sdk_a"),
resource_version: 1,
},
AgentResourceBinding {
resource_id: ResourceId::new("res_sdk_b"),
resource_version: 1,
},
],
expected_state: None,
})
.await
.unwrap();
registry
.publish_agent(PublishAgentRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
version: 1,
published_at: &test_time(),
published_by: Some("alice"),
expected_state: None,
})
.await
.unwrap();
let key = create_api_key(&registry).await;
let counters = Arc::new(CounterSet::default());
// Normal build_app composition with its sanctioned artifact root, not the isolated SDK router.
let app = crate::build_app_with_artifacts(
registry.clone(),
Duration::ZERO,
None,
SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap(),
crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build(),
RequestRateLimiter::new(RequestRateLimitConfig::new(10_000, 10_000).unwrap()),
Arc::new(InMemoryCoordinationStateStore::default()),
Arc::new(crate::session::InMemorySessionStore::default()),
Arc::new(crate::auth::CommunityMachineCredentialVerifier),
16,
store,
);
let app = observe_requests(app, counters.clone(), None).layer(axum::middleware::from_fn({
let counters = counters.clone();
move |request: axum::extract::Request, next: axum::middleware::Next| {
let counters = counters.clone();
async move {
let response = next.run(request).await;
observe_body(response, counters)
}
}
}));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!(
"http://{}/v1/{WORKSPACE_SLUG}/{AGENT_SLUG}",
listener.local_addr().unwrap()
);
let server = tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
Self {
registry,
counters,
key,
url,
server,
root,
}
}
pub async fn post(
&self,
revision: &str,
session: Option<&str>,
body: &Value,
sse: bool,
) -> reqwest::Response {
let mut request = reqwest::Client::new()
.post(&self.url)
.bearer_auth(&self.key)
.header(
"Accept",
if sse {
"text/event-stream, application/json"
} else {
"application/json"
},
)
.header("x-request-id", "sdk-resources-wire")
.header(
"traceparent",
"00-11111111111111111111111111111111-2222222222222222-01",
)
.header("MCP-Protocol-Version", revision);
if let Some(session) = session {
request = request.header("MCP-Session-Id", session);
}
if revision == MODERN_REVISION {
request = request.header("Mcp-Method", body["method"].as_str().unwrap());
if let Some(uri) = body["params"]["uri"].as_str() {
request = request.header("Mcp-Name", uri);
}
}
request.json(body).send().await.unwrap()
}
pub async fn session(&self, revision: &str) -> Option<String> {
if revision == MODERN_REVISION {
return None;
}
let response = self
.post(revision, None, &legacy_initialize(1), false)
.await;
assert_eq!(response.status(), 200);
let session = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
assert_eq!(
json_body(response).await["result"]["protocolVersion"],
revision
);
assert_eq!(
self.post(revision, Some(&session), &initialized_notification(), false)
.await
.status(),
202
);
Some(session)
}
}
@@ -0,0 +1,437 @@
//! #107 result serialization: wire controls run independently of the structural SDK oracle.
use super::*;
use axum::{
Json,
body::Body,
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
};
use futures_util::{StreamExt, stream};
const ERROR_EMAIL: &str = "upstream-error@example.test";
const REQUEST_ID: &str = "sdk-result-wire";
const TRACE_ID: &str = "11111111111111111111111111111111";
pub(super) async fn upstream(state: State<Arc<AtomicUsize>>, payload: Json<Value>) -> Response {
if payload.0["email"] == ERROR_EMAIL {
state.0.fetch_add(1, Ordering::SeqCst);
return (
StatusCode::UNPROCESSABLE_ENTITY,
Json(json!({"error":"fixture rejection"})),
)
.into_response();
}
create_lead(state, payload).await.into_response()
}
// The HTTP body is polled after next.run returns. Carry only the test observer, not a new lifecycle.
pub(super) fn observe_body(response: Response, counters: Arc<CounterSet>) -> Response {
let (parts, body) = response.into_parts();
let body = stream::unfold(
(body.into_data_stream(), counters),
|(mut body, counters)| async move {
observe(counters.clone(), body.next())
.await
.map(|chunk| (chunk, (body, counters)))
},
);
Response::from_parts(parts, Body::from_stream(body))
}
#[tokio::test]
async fn rmcp_result_http_pagination_both_revisions() {
let h = Endpoint::started().await;
let mut operation = test_operation(&h._upstream.1);
operation.id = crank_core::OperationId::new("op_second_lead");
operation.name = "second_lead".to_owned();
h.registry
.create_operation(&workspace_id(), &operation, Some("alice"))
.await
.unwrap();
h.registry
.publish_operation(PublishRequest {
workspace_id: &workspace_id(),
operation_id: &operation.id,
version: 1,
published_at: &test_time(),
published_by: Some("alice"),
})
.await
.unwrap();
let now = time::OffsetDateTime::now_utc();
h.registry
.create_agent_draft_version(CreateAgentDraftVersionRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
version: &AgentVersion {
agent_id: agent_id(),
version: 2,
status: AgentStatus::Draft,
instructions: json!({}),
tool_selection_policy: ToolSelectionPolicy::default(),
created_at: now,
},
bindings: &[
AgentOperationBinding {
operation_id: crank_core::OperationId::new("op_create_lead"),
operation_version: 1,
tool_name: TOOL_NAME.into(),
tool_title: "Create Lead".into(),
tool_description_override: None,
enabled: true,
},
AgentOperationBinding {
operation_id: operation.id,
operation_version: 1,
tool_name: "second_lead".into(),
tool_title: "Second Lead".into(),
tool_description_override: None,
enabled: true,
},
],
updated_at: &now,
})
.await
.unwrap();
h.registry
.publish_agent(PublishAgentRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
version: 2,
published_at: &now,
published_by: Some("alice"),
expected_state: None,
})
.await
.unwrap();
for revision in [LEGACY_REVISION, MODERN_REVISION] {
let session = h.session(revision).await;
let mut names = Vec::new();
for (cursor, expected_next, expected_len) in [
(None, Some("1"), 1),
(Some("1"), None, 1),
(Some("2"), None, 0),
] {
let id = json!(format!("page-{cursor:?}"));
let mut request =
json!({"jsonrpc":"2.0","id":id,"method":"tools/list","params":{"limit":1}});
if let Some(cursor) = cursor {
request["params"]["cursor"] = json!(cursor);
}
if revision == MODERN_REVISION {
request["params"]["_meta"] = modern_meta();
}
let before = h.counters.sdk_tools_list_result();
let body = wire_body(
h.post(revision, session.as_deref(), &request).await,
&id,
false,
)
.await;
assert_era(&body["result"], revision, true);
assert_eq!(
body["result"].get("nextCursor"),
expected_next.map(|s| json!(s)).as_ref()
);
let tools = body["result"]["tools"].as_array().unwrap();
assert_eq!(tools.len(), expected_len);
names.extend(
tools
.iter()
.map(|tool| tool["name"].as_str().unwrap().to_owned()),
);
assert_eq!(h.counters.sdk_tools_list_result() - before, 1);
}
names.sort();
let mut expected = vec![TOOL_NAME.to_owned(), "second_lead".to_owned()];
expected.sort();
assert_eq!(names, expected);
}
assert_eq!(h.calls.load(Ordering::SeqCst), 0);
}
fn assert_era(result: &Value, revision: &str, list: bool) {
if revision == MODERN_REVISION {
assert_eq!(result["resultType"], "complete", "{result}");
if list {
assert_eq!(result["ttlMs"], 0);
assert_eq!(result["cacheScope"], "private");
}
} else {
assert!(result.get("resultType").is_none(), "{result}");
assert!(result.get("ttlMs").is_none(), "{result}");
assert!(result.get("cacheScope").is_none(), "{result}");
}
assert!(result.get("_meta").is_none(), "{result}");
}
async fn wire_body(response: reqwest::Response, id: &Value, sse: bool) -> Value {
assert_eq!(response.status(), 200);
assert_eq!(response.headers()["x-request-id"], REQUEST_ID);
assert_eq!(response.headers()["x-trace-id"], TRACE_ID);
let content_type = response.headers()["content-type"]
.to_str()
.unwrap()
.to_owned();
let text = tokio::time::timeout(Duration::from_secs(5), response.text())
.await
.unwrap()
.unwrap();
let body: Value = if sse {
assert!(
content_type.starts_with("text/event-stream"),
"{content_type}"
);
let frames: Vec<Value> = text
.lines()
.filter_map(|line| {
line.strip_prefix("data:")
.map(|data| serde_json::from_str(data.trim()).unwrap())
})
.collect();
let terminal: Vec<_> = frames
.iter()
.filter(|frame| frame.get("id") == Some(id))
.collect();
assert_eq!(terminal.len(), 1, "{frames:?}");
assert_eq!(frames.last(), Some(terminal[0]), "terminal must be last");
assert!(
frames
.iter()
.any(|frame| frame["method"] == "notifications/progress"
&& frame["params"]["progressToken"] == "result-progress"),
"{frames:?}"
);
terminal[0].clone()
} else {
assert!(
content_type.starts_with("application/json"),
"{content_type}"
);
serde_json::from_str(&text).unwrap()
};
assert_eq!(body["jsonrpc"], "2.0");
assert_eq!(&body["id"], id);
body
}
fn assert_call_result(body: &Value, revision: &str, email: &str) {
assert!(body.get("error").is_none(), "{body}");
let result = &body["result"];
assert_era(result, revision, false);
let (output, text, is_error) = if email == ERROR_EMAIL {
let contract = crate::tool_error::tool_error_contract_from_runtime(
&crank_runtime::RuntimeError::ProtocolAdapter(
crank_core::ProtocolAdapterError::UnexpectedStatus {
status: 422,
dispatch: crank_core::DispatchEvidence::MayHaveDispatched,
},
),
REQUEST_ID,
TRACE_ID,
crank_core::ExecutionLocale::En,
);
(
json!({"error":crate::tool_error::tool_error_value(&contract)}),
crate::tool_error::tool_error_text(&contract),
true,
)
} else {
let output = json!({"id":format!("lead_for_{email}")});
let text = serde_json::to_string_pretty(&output).unwrap();
(output, text, false)
};
assert_eq!(result["structuredContent"], output, "{body}");
assert_eq!(result["content"], json!([{"type":"text", "text":text}]));
assert_eq!(result["isError"], is_error);
assert_eq!(
result.as_object().unwrap().len(),
if revision == MODERN_REVISION { 4 } else { 3 },
"no unexpected optional fields: {result}"
);
}
async fn result_control(revision: &str) -> Vec<usize> {
let h = Endpoint::started().await;
let session = h.session(revision).await;
let session = session.as_deref();
let mut counts = Vec::new();
// Actual published descriptor and schema, then the empty page at the catalog boundary.
for (cursor, expected_tools, id) in [
(
None,
json!([{"name":TOOL_NAME,"title":"Create Lead","description":"Creates a CRM lead","inputSchema":{"type":"object","properties":{"email":{"type":"string"}},"required":["email"]}}]),
json!("list-string-id"),
),
(Some("1"), json!([]), json!(9007199254740993_u64)),
] {
let mut request =
json!({"jsonrpc":"2.0","id":id,"method":"tools/list","params":{"limit":1}});
if revision == MODERN_REVISION {
request["params"]["_meta"] = modern_meta();
}
if let Some(cursor) = cursor {
request["params"]["cursor"] = json!(cursor);
}
let before = h.counters.sdk_tools_list_result();
let before_call = h.counters.sdk_tools_call_result();
let before_dispatch = h.counters.sdk_tools_dispatch();
let body = wire_body(h.post(revision, session, &request).await, &id, false).await;
assert!(body.get("error").is_none(), "{body}");
assert_eq!(body["result"]["tools"], expected_tools);
assert!(body["result"].get("nextCursor").is_none(), "{body}");
assert_era(&body["result"], revision, true);
assert_eq!(h.counters.sdk_tools_call_result(), before_call);
assert_eq!(h.counters.sdk_tools_dispatch() - before_dispatch, 1);
counts.push(h.counters.sdk_tools_list_result() - before);
}
assert_eq!(h.calls.load(Ordering::SeqCst), 0);
for (email, id, sse) in [
(
"result-success@example.test",
json!(9007199254740993_u64),
false,
),
(ERROR_EMAIL, json!("tool-error-string-id"), false),
("result-sse@example.test", json!("sse-string-id"), true),
(ERROR_EMAIL, json!(92), true),
] {
if sse && revision != MODERN_REVISION {
continue;
}
let mut request = if revision == MODERN_REVISION {
modern_tool_call(90, email)
} else {
legacy_tool_call(90, email)
};
request["id"] = id.clone();
if sse {
request["params"]["_meta"]["progressToken"] = json!("result-progress");
}
let before = h.counters.sdk_tools_call_result();
let before_list = h.counters.sdk_tools_list_result();
let before_dispatch = h.counters.sdk_tools_dispatch();
let arrivals = h.calls.load(Ordering::SeqCst);
let response = h
.post_accept(
revision,
session,
&request,
if sse {
"application/json, text/event-stream"
} else {
"application/json"
},
)
.await;
let body = wire_body(response, &id, sse).await;
assert_call_result(&body, revision, email);
assert_eq!(h.calls.load(Ordering::SeqCst) - arrivals, 1);
assert_eq!(h.counters.sdk_tools_list_result(), before_list);
assert_eq!(h.counters.sdk_tools_dispatch() - before_dispatch, 1);
counts.push(h.counters.sdk_tools_call_result() - before);
}
// Unknown tool is a command InvalidParams, not an execution tool-error result.
let mut invalid = if revision == MODERN_REVISION {
modern_tool_call(93, "unused@example.test")
} else {
legacy_tool_call(93, "unused@example.test")
};
invalid["params"]["name"] = json!("missing_tool");
invalid["id"] = json!("invalid-params-id");
let before = h.counters.sdk_tools_call_result();
let arrivals = h.calls.load(Ordering::SeqCst);
let body = wire_body(
h.post(revision, session, &invalid).await,
&invalid["id"],
false,
)
.await;
assert_eq!(body["error"]["code"], -32602, "{body}");
assert!(body.get("result").is_none(), "{body}");
assert_eq!(body["error"]["data"]["request_id"], REQUEST_ID);
assert_eq!(body["error"]["data"]["trace_id"], TRACE_ID);
assert_eq!(h.counters.sdk_tools_call_result(), before);
assert_eq!(h.calls.load(Ordering::SeqCst), arrivals);
eprintln!(
"existing GREEN result wire {revision}: published list/schema + empty page, exact success/error JSON{}; InvalidParams/no result/no marker/no outbound; SDK result deltas={counts:?}",
if revision == MODERN_REVISION {
" + success/error terminal SSE"
} else {
" (legacy JSON-only)"
}
);
counts
}
#[tokio::test]
async fn rmcp_result_wire_modern() {
result_control(MODERN_REVISION).await;
}
#[tokio::test]
async fn rmcp_result_wire_legacy() {
result_control(LEGACY_REVISION).await;
}
#[tokio::test]
async fn rmcp_result_sdk_serialization_modern() {
assert_eq!(
result_control(MODERN_REVISION).await,
vec![1; 6],
"structural oracle: SDK list/final-empty/success/error/SSE-success/SSE-error must each serialize once after successful real HTTP wire controls"
);
}
#[tokio::test]
async fn rmcp_result_sdk_serialization_legacy() {
assert_eq!(
result_control(LEGACY_REVISION).await,
vec![1; 4],
"structural oracle: SDK list/final-empty/success/error must each serialize once after successful real HTTP wire controls"
);
}
#[tokio::test]
async fn rmcp_result_observer_scope_control() {
use crate::app::dispatch_observer::{
sdk_tools_call_result_serialized, sdk_tools_list_result_serialized,
};
let a = Arc::new(CounterSet::default());
let b = Arc::new(CounterSet::default());
sdk_tools_list_result_serialized();
sdk_tools_call_result_serialized();
assert_eq!(
(a.sdk_tools_list_result(), a.sdk_tools_call_result()),
(0, 0)
);
observe(a.clone(), async {
sdk_tools_list_result_serialized();
sdk_tools_call_result_serialized();
})
.await;
// A different request cannot alter a's result counters.
observe(b.clone(), async {
sdk_tools_list_result_serialized();
})
.await;
assert_eq!(
(a.sdk_tools_list_result(), a.sdk_tools_call_result()),
(1, 1)
);
assert_eq!(
(b.sdk_tools_list_result(), b.sdk_tools_call_result()),
(1, 0)
);
// Prove the stream-body scope actually records deferred work after the header scope ended.
let stream_counters = b.clone();
let body = Body::from_stream(stream::once(async move {
sdk_tools_call_result_serialized();
Ok::<_, std::convert::Infallible>("terminal")
}));
let response = observe_body(Response::new(body), stream_counters);
let bytes = axum::body::to_bytes(response.into_body(), 100)
.await
.unwrap();
assert_eq!(&bytes[..], b"terminal");
assert_eq!(b.sdk_tools_call_result(), 1);
assert_eq!(a.sdk_tools_call_result(), 1);
}
@@ -0,0 +1,385 @@
//! #107: real build_app endpoint controls plus a separate SDK structural seam oracle.
//! No SDK transport router is used. The structural oracle alone does not prove wire parity.
use std::{
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
time::Duration,
};
use axum::{Router, routing::post};
use crank_core::{
AgentOperationBinding, AgentStatus, AgentVersion, PlatformApiKeyId, ToolSelectionPolicy,
};
use crank_registry::{
CreateAgentDraftVersionRequest, PostgresRegistry, PublishAgentRequest, PublishRequest,
};
use crank_runtime::{
InMemoryCoordinationStateStore, RequestRateLimitConfig, RequestRateLimiter, SecretCrypto,
};
use serde_json::{Value, json};
use super::{CounterSet, observe, sdk_tools_dispatch_enter, support::*};
#[path = "rmcp_result_runtime_tests.rs"]
mod result_serialization;
struct Server(tokio::task::JoinHandle<()>, String);
impl Drop for Server {
fn drop(&mut self) {
self.0.abort();
}
}
async fn serve(app: Router) -> Server {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
Server(
tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
}),
format!("http://{address}"),
)
}
struct Endpoint {
server: Server,
_upstream: Server,
registry: PostgresRegistry,
key: String,
counters: Arc<CounterSet>,
calls: Arc<AtomicUsize>,
}
impl Endpoint {
async fn started() -> Self {
// First caller owns the process-wide fixture and its atexit cleanup, including RED panics.
let _ =
crank_test_support::postgres_schema_url_for_task("rmcp_runtime_owner", "t_c559abb1")
.await;
let registry = test_registry().await;
let calls = Arc::new(AtomicUsize::new(0));
let upstream = serve(
Router::new()
.route("/crm/leads", post(result_serialization::upstream))
.with_state(calls.clone()),
)
.await;
let operation = test_operation(&upstream.1);
registry
.create_operation(&workspace_id(), &operation, Some("alice"))
.await
.unwrap();
registry
.publish_operation(PublishRequest {
workspace_id: &workspace_id(),
operation_id: &operation.id,
version: 1,
published_at: &test_time(),
published_by: Some("alice"),
})
.await
.unwrap();
publish_agent(&registry, &operation).await;
let key = create_api_key(&registry).await;
let counters = Arc::new(CounterSet::default());
let app = crate::build_app(
registry.clone(),
Duration::ZERO,
Some("https://crank.example.com".into()),
SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap(),
crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build(),
RequestRateLimiter::new(RequestRateLimitConfig::new(10_000, 10_000).unwrap()),
Arc::new(InMemoryCoordinationStateStore::default()),
Arc::new(crate::session::InMemorySessionStore::default()),
Arc::new(crate::auth::CommunityMachineCredentialVerifier),
);
let app = observe_requests(app, counters.clone(), None).layer(axum::middleware::from_fn({
let counters = counters.clone();
move |request: axum::extract::Request, next: axum::middleware::Next| {
let counters = counters.clone();
async move {
let response = next.run(request).await;
result_serialization::observe_body(response, counters)
}
}
}));
let server = serve(app).await;
Self {
server,
_upstream: upstream,
registry,
key,
counters,
calls,
}
}
async fn post(&self, revision: &str, session: Option<&str>, body: &Value) -> reqwest::Response {
self.post_accept(revision, session, body, "application/json")
.await
}
async fn post_accept(
&self,
revision: &str,
session: Option<&str>,
body: &Value,
accept: &str,
) -> reqwest::Response {
let mut request = reqwest::Client::new()
.post(format!(
"{}/v1/{WORKSPACE_SLUG}/{AGENT_SLUG}",
self.server.1
))
.bearer_auth(&self.key)
// Also guards the legacy JSON-only contract; no SSE parsing fallback here.
.header("Accept", accept)
.header("x-request-id", "sdk-result-wire")
.header(
"traceparent",
"00-11111111111111111111111111111111-2222222222222222-01",
)
.header("MCP-Protocol-Version", revision);
if let Some(session) = session {
request = request.header("MCP-Session-Id", session);
}
if revision == MODERN_REVISION {
request = request.header("Mcp-Method", body["method"].as_str().unwrap());
if let Some(name) = body["params"]["name"].as_str() {
request = request.header("Mcp-Name", name);
}
}
request.json(body).send().await.unwrap()
}
async fn session(&self, revision: &str) -> Option<String> {
if revision == MODERN_REVISION {
return None;
}
let response = self.post(revision, None, &legacy_initialize(1)).await;
assert_eq!(response.status(), 200);
let session = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
let body = json_body(response).await;
assert_eq!(body["result"]["protocolVersion"], revision);
assert!(body["result"]["capabilities"]["tools"].is_object());
let response = self
.post(revision, Some(&session), &initialized_notification())
.await;
assert_eq!(response.status(), 202);
Some(session)
}
async fn list(&self, revision: &str, session: Option<&str>, expected_name: &str) -> usize {
let before = self.counters.sdk_tools_dispatch();
let mut body = json!({"jsonrpc":"2.0", "id":10, "method":"tools/list", "params":{}});
if revision == MODERN_REVISION {
body["params"]["_meta"] = modern_meta();
}
let calls = self.calls.load(Ordering::SeqCst);
let response = self.post(revision, session, &body).await;
assert_eq!(response.status(), 200);
assert!(
response.headers()["content-type"]
.to_str()
.unwrap()
.starts_with("application/json")
);
let body = json_body(response).await;
assert_eq!(body["id"], 10);
assert_eq!(body["result"]["tools"].as_array().unwrap().len(), 1);
assert_eq!(body["result"]["tools"][0]["name"], expected_name);
assert!(body["error"].is_null(), "{body}");
assert_eq!(
self.calls.load(Ordering::SeqCst),
calls,
"listing must not execute upstream"
);
self.counters.sdk_tools_dispatch() - before
}
async fn call(&self, revision: &str, session: Option<&str>, name: &str, email: &str) -> usize {
let before = self.counters.sdk_tools_dispatch();
let arrivals = self.calls.load(Ordering::SeqCst);
let mut body = if revision == MODERN_REVISION {
modern_tool_call(11, email)
} else {
legacy_tool_call(11, email)
};
body["params"]["name"] = json!(name);
let response = self.post(revision, session, &body).await;
assert_eq!(response.status(), 200);
assert!(
response.headers()["content-type"]
.to_str()
.unwrap()
.starts_with("application/json")
);
let body = json_body(response).await;
assert_eq!(body["id"], 11);
assert!(body["error"].is_null(), "{body}");
assert_ne!(body["result"]["isError"], true, "{body}");
assert_scoped_output(revision, &body, email);
if revision == MODERN_REVISION {
assert_eq!(body["result"]["resultType"], "complete");
}
assert_eq!(self.calls.load(Ordering::SeqCst) - arrivals, 1);
self.counters.sdk_tools_dispatch() - before
}
async fn publish_renamed_tool(&self) {
let now = time::OffsetDateTime::now_utc();
let version = AgentVersion {
agent_id: agent_id(),
version: 2,
status: AgentStatus::Draft,
instructions: json!({}),
tool_selection_policy: ToolSelectionPolicy::default(),
created_at: now,
};
let bindings = [AgentOperationBinding {
operation_id: crank_core::OperationId::new("op_create_lead"),
operation_version: 1,
tool_name: "new_publication_tool".into(),
tool_title: "New Tool".into(),
tool_description_override: None,
enabled: true,
}];
self.registry
.create_agent_draft_version(CreateAgentDraftVersionRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
version: &version,
bindings: &bindings,
updated_at: &now,
})
.await
.unwrap();
self.registry
.publish_agent(PublishAgentRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
version: 2,
published_at: &now,
published_by: Some("alice"),
expected_state: None,
})
.await
.unwrap();
}
}
async fn endpoint_control(revision: &str) -> [usize; 2] {
let h = Endpoint::started().await;
let session = h.session(revision).await;
let session = session.as_deref();
let list_dispatches = h.list(revision, session, TOOL_NAME).await;
let call_dispatches = h
.call(
revision,
session,
TOOL_NAME,
&format!("{revision}@example.test"),
)
.await;
// A fresh publication must be visible without reinitialize or a new endpoint.
h.publish_renamed_tool().await;
h.list(revision, session, "new_publication_tool").await;
h.call(
revision,
session,
"new_publication_tool",
"new-publication@example.test",
)
.await;
h.registry
.revoke_platform_api_key(
&workspace_id(),
&PlatformApiKeyId::new("pk_dispatch_observer"),
&time::OffsetDateTime::now_utc(),
)
.await
.unwrap();
let before_sdk = h.counters.sdk_tools_dispatch();
let arrivals = h.calls.load(Ordering::SeqCst);
let mut denied = if revision == MODERN_REVISION {
modern_tool_call(12, "denied@example.test")
} else {
legacy_tool_call(12, "denied@example.test")
};
denied["params"]["name"] = json!("new_publication_tool");
assert_eq!(h.post(revision, session, &denied).await.status(), 401);
assert_eq!(
h.calls.load(Ordering::SeqCst),
arrivals,
"revoked credential must not execute upstream"
);
assert_eq!(
h.counters.sdk_tools_dispatch(),
before_sdk,
"revocation must reject before SDK dispatch"
);
eprintln!(
"existing GREEN {revision}: JSON list/call, scoped output, upstream=2, publication refresh, revoked=401/no outbound; SDK list/call deltas={:?}",
[list_dispatches, call_dispatches]
);
[list_dispatches, call_dispatches]
}
#[tokio::test]
async fn rmcp_runtime_existing_wire_modern() {
endpoint_control(MODERN_REVISION).await;
}
#[tokio::test]
async fn rmcp_runtime_existing_wire_legacy() {
endpoint_control(LEGACY_REVISION).await;
}
#[tokio::test]
async fn rmcp_runtime_sdk_dispatch_modern() {
let counts = endpoint_control(MODERN_REVISION).await;
assert_eq!(
counts,
[1, 1],
"real endpoint tools/list and tools/call must each enter SDK typed dispatch once (structural seam oracle)"
);
}
#[tokio::test]
async fn rmcp_runtime_sdk_dispatch_legacy() {
let counts = endpoint_control(LEGACY_REVISION).await;
assert_eq!(
counts,
[1, 1],
"real endpoint tools/list and tools/call must each enter SDK typed dispatch once (structural seam oracle)"
);
}
#[tokio::test]
async fn rmcp_runtime_observer_is_request_scoped() {
let counters = Arc::new(CounterSet::default());
sdk_tools_dispatch_enter();
assert_eq!(counters.sdk_tools_dispatch(), 0);
observe(counters.clone(), async {
sdk_tools_dispatch_enter();
})
.await;
assert_eq!(counters.sdk_tools_dispatch(), 1);
let other = Arc::new(CounterSet::default());
observe(other.clone(), async {
sdk_tools_dispatch_enter();
})
.await;
assert_eq!(other.sdk_tools_dispatch(), 1);
assert_eq!(counters.sdk_tools_dispatch(), 1);
}
@@ -0,0 +1,312 @@
//! #107: HTTP wire controls are independent of the SDK execution oracle.
//! A real committed Tool removal exercises the watcher; existing mcp_signals::scenarios covers
//! Resource/Prompt publications, isolation, coalescing, reconnect and authoritative recovery.
use super::{CounterSet, support::*};
use serde_json::{Value, json};
use std::{sync::Arc, time::Duration};
#[path = "rmcp_signals_test_support.rs"]
mod fixtures;
use fixtures::{Endpoint, terminal};
const SUB_ID: &str = "io.modelcontextprotocol/subscriptionId";
const ACK: &str = "notifications/subscriptions/acknowledged";
const TOOLS: &str = "notifications/tools/list_changed";
const INVALID_FILTER: &str = "invalid subscriptions/listen notification filter";
fn counts(c: &CounterSet) -> [usize; 3] {
[
c.sdk_subscriptions_listen(),
c.sdk_subscriptions_acknowledged(),
c.sdk_signal_notification(),
]
}
fn ack(id: &Value, notifications: Value) -> Value {
json!({"jsonrpc":"2.0", "method":ACK,
"params":{"_meta":{SUB_ID:id},"notifications":notifications}})
}
fn listen_body(id: Value, params: Value) -> Value {
json!({"jsonrpc":"2.0","id":id,"method":"subscriptions/listen","params":params})
}
async fn catalog_control(revision: &str) -> [usize; 3] {
let h = Endpoint::started().await;
let session = if revision == LEGACY_REVISION {
Some(h.session().await)
} else {
None
};
let admission_before = h.counters.snapshot().admission;
let before_list = h.tools(revision, session.as_deref()).await;
assert_eq!(
h.counters.snapshot().admission - admission_before,
1,
"positive control: the HTTP request must carry this observer"
);
assert_eq!(before_list["result"]["tools"].as_array().unwrap().len(), 1);
assert_eq!(before_list["result"]["tools"][0]["name"], TOOL_NAME);
let id = json!(u64::MAX);
let before = counts(&h.counters);
let mut stream = if revision == MODERN_REVISION {
let mut stream = h
.listen(
id.clone(),
json!({
"toolsListChanged":true,"resourcesListChanged":true,"promptsListChanged":true,
"resourceSubscriptions":["crank://CANARY-URI"], "unknown":null
}),
)
.await;
// Whole-frame equality proves ack first, exact id, no result/id/URI/payload leakage.
assert_eq!(
stream.frame().await,
ack(
&id,
json!({
"toolsListChanged":true,"resourcesListChanged":true,"promptsListChanged":true
})
)
);
stream
} else {
h.legacy_stream(session.as_deref().unwrap()).await
};
// Enough for the existing one-second watcher to establish its initial committed fingerprint.
stream.silent().await;
h.draft_without_tools().await;
stream.silent().await;
assert_eq!(
h.tools(revision, session.as_deref()).await["result"]["tools"]
.as_array()
.unwrap()
.len(),
1
);
h.publish_without_tools().await;
let expected = if revision == MODERN_REVISION {
json!({"jsonrpc":"2.0","method":TOOLS,"params":{"_meta":{SUB_ID:id}}})
} else {
json!({"jsonrpc":"2.0","method":TOOLS})
};
assert_eq!(
stream.frame().await,
expected,
"exact committed signal, no catalog payload"
);
stream.silent().await;
assert_eq!(
h.tools(revision, session.as_deref()).await["result"]["tools"],
json!([])
);
let after = counts(&h.counters);
std::array::from_fn(|i| after[i] - before[i])
}
#[tokio::test]
async fn modern_wire_control() {
catalog_control(MODERN_REVISION).await;
}
#[tokio::test]
async fn legacy_wire_control() {
catalog_control(LEGACY_REVISION).await;
}
#[tokio::test]
async fn modern_sdk_runtime_oracle() {
// All real HTTP/commit assertions above must pass before missing SDK behavior can be RED.
assert_eq!(
catalog_control(MODERN_REVISION).await,
[1, 1, 1],
"real HTTP listen decode / ack serialization / watcher signal serialization SDK entries"
);
}
#[tokio::test]
async fn legacy_sdk_runtime_oracle() {
assert_eq!(
catalog_control(LEGACY_REVISION).await,
[0, 0, 1],
"legacy GET must use SDK signal serialization but not modern listen/ack"
);
}
#[tokio::test]
async fn modern_false_absent_and_uri_only_do_not_opt_in() {
let h = Endpoint::started().await;
let mut absent = h.listen(json!("absent"), json!({})).await;
assert_eq!(absent.frame().await, ack(&json!("absent"), json!({})));
let mut disabled = h
.listen(
json!("disabled"),
json!({
"toolsListChanged":false,"resourcesListChanged":false,"promptsListChanged":false,
"resourceSubscriptions":["crank://CANARY-URI"]
}),
)
.await;
assert_eq!(disabled.frame().await, ack(&json!("disabled"), json!({})));
// A simultaneous positive stream proves the publication was visible to the running watchers.
let mut enabled = h
.listen(json!("enabled"), json!({"toolsListChanged":true}))
.await;
assert_eq!(
enabled.frame().await,
ack(&json!("enabled"), json!({"toolsListChanged":true}))
);
enabled.silent().await;
h.draft_without_tools().await;
h.publish_without_tools().await;
assert_eq!(
enabled.frame().await,
json!({"jsonrpc":"2.0","method":TOOLS,"params":{"_meta":{SUB_ID:"enabled"}}})
);
absent.silent().await;
disabled.silent().await;
let body = terminal(
h.post(
MODERN_REVISION,
None,
json!({
"jsonrpc":"2.0","id":4,"method":"resources/subscribe",
"params":{"_meta":modern_meta(),"uri":"crank://CANARY-URI"}
}),
)
.await,
)
.await;
assert_eq!(body["error"]["code"], -32601);
assert!(body.get("result").is_none());
assert!(!body.to_string().contains("CANARY"));
}
#[tokio::test]
async fn modern_http_filter_shape_and_bounds_stay_compatible() {
let h = Endpoint::started().await;
let mut invalid = vec![
json!({}),
json!({"notifications":null}),
json!({"notifications":[]}),
json!({"notifications":{"toolsListChanged":"CANARY"}}),
json!({"notifications":{"resourceSubscriptions":[null]}}),
json!({"notifications":{"resourceSubscriptions":vec!["CANARY";33]}}),
json!({"notifications":{"resourceSubscriptions":["X".repeat(2049)]}}),
];
for key in [
"toolsListChanged",
"resourcesListChanged",
"promptsListChanged",
"resourceSubscriptions",
] {
let mut filter = json!({});
filter[key] = Value::Null;
invalid.push(json!({"notifications":filter}));
}
for mut params in invalid {
params["_meta"] = modern_meta();
let body = terminal(
h.post(MODERN_REVISION, None, listen_body(json!(7), params))
.await,
)
.await;
assert_eq!(body["id"], 7);
assert_eq!(body["error"]["code"], -32602);
assert_eq!(body["error"]["message"], INVALID_FILTER);
assert!(body.get("result").is_none());
assert!(!body.to_string().contains("CANARY"));
assert!(body.to_string().len() < 4096);
}
// Inclusive bounds, unknown null extension and explicit false are accepted, never acknowledged
// as a URI-content subscription or an extra list-change type.
let mut stream = h
.listen(
json!(8),
json!({
"resourceSubscriptions":vec!["é".repeat(1024);32], "toolsListChanged":false,
"unknown":null
}),
)
.await;
assert_eq!(stream.frame().await, ack(&json!(8), json!({})));
}
#[tokio::test]
async fn modern_http_ids_are_lossless_and_bounded() {
let h = Endpoint::started().await;
for id in [
json!("a".repeat(62)),
json!("é".repeat(31)),
json!(u64::MAX),
json!(9_007_199_254_740_993_u64),
json!(1.25),
json!(1.0),
] {
let mut stream = h.listen(id.clone(), json!({})).await;
let frame = stream.frame().await;
assert_eq!(frame, ack(&id, json!({})));
assert_eq!(frame["params"]["_meta"][SUB_ID].to_string(), id.to_string());
}
for id in [json!("a".repeat(63)), json!("é".repeat(32))] {
let body = terminal(
h.post(
MODERN_REVISION,
None,
listen_body(
id.clone(),
json!({"_meta":modern_meta(),"notifications":{}}),
),
)
.await,
)
.await;
assert_eq!(body["id"], id);
assert_eq!(body["error"]["code"], -32600);
assert!(body.get("result").is_none());
}
}
#[tokio::test]
async fn modern_http_meta_admission_precedes_sdk_decode() {
let h = Endpoint::started().await;
for meta in [
Value::Null,
json!([]),
json!({META_PROTOCOL_VERSION:MODERN_REVISION,
META_CLIENT_CAPABILITIES:null}),
] {
let before = counts(&h.counters);
let response = h
.post(
MODERN_REVISION,
None,
listen_body(json!(9), json!({"_meta":meta,"notifications":{}})),
)
.await;
// Admission rejections are HTTP 400, not a successful SSE response.
assert_eq!(response.status(), 400);
let raw = tokio::time::timeout(Duration::from_secs(6), response.text())
.await
.unwrap()
.unwrap();
let body: Value = serde_json::from_str(&raw).unwrap();
assert_eq!(body["error"]["code"], -32020);
assert!(body.get("result").is_none());
assert_eq!(
counts(&h.counters),
before,
"admission must reject before any SDK entry"
);
}
// clientInfo is optional at HTTP admission; SDK decoding must not newly require it.
let mut meta = modern_meta();
meta.as_object_mut().unwrap().remove(META_CLIENT_INFO);
let response = h
.post(
MODERN_REVISION,
None,
listen_body(
json!(10),
json!({"_meta":meta,"notifications":{},"extension":null}),
),
)
.await;
let mut stream = fixtures::Events::open(response);
assert_eq!(stream.frame().await, ack(&json!(10), json!({})));
}
@@ -0,0 +1,280 @@
//! Small real catalog fixture; broader Resource/Prompt mutations stay in mcp_signals::scenarios.
use super::*;
use crank_core::{AgentStatus, AgentVersion, ToolSelectionPolicy};
use crank_registry::{CreateAgentDraftVersionRequest, PublishAgentRequest, PublishRequest};
use crank_runtime::{
InMemoryCoordinationStateStore, RequestRateLimitConfig, RequestRateLimiter, SecretCrypto,
};
pub(super) struct Endpoint {
pub registry: crank_registry::PostgresRegistry,
pub counters: Arc<CounterSet>,
key: String,
url: String,
server: tokio::task::JoinHandle<()>,
}
impl Drop for Endpoint {
fn drop(&mut self) {
self.server.abort();
}
}
impl Endpoint {
pub async fn started() -> Self {
// First caller labels the shared, exit-cleaned disposable PostgreSQL with this owner.
crank_test_support::postgres_schema_url_for_task("signals_owner", "t_7d5f77a6").await;
let registry = test_registry().await;
// This fixture only lists/removes a Tool; no upstream execution is needed.
let operation = test_operation("http://127.0.0.1:9");
registry
.create_operation(&workspace_id(), &operation, Some("alice"))
.await
.unwrap();
registry
.publish_operation(PublishRequest {
workspace_id: &workspace_id(),
operation_id: &operation.id,
version: 1,
published_at: &test_time(),
published_by: Some("alice"),
})
.await
.unwrap();
publish_agent(&registry, &operation).await;
let key = create_api_key(&registry).await;
let counters = Arc::new(CounterSet::default());
let app = crate::build_app(
registry.clone(),
Duration::ZERO,
None,
SecretCrypto::new("test-master-key-00000000000000000000000000000000").unwrap(),
crank_runtime::community_with_outbound_policy(
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
)
.build(),
RequestRateLimiter::new(RequestRateLimitConfig::new(10_000, 10_000).unwrap()),
Arc::new(InMemoryCoordinationStateStore::default()),
Arc::new(crate::session::InMemorySessionStore::default()),
Arc::new(crate::auth::CommunityMachineCredentialVerifier),
);
let app = observe_requests(app, counters.clone(), None);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!(
"http://{}/v1/{WORKSPACE_SLUG}/{AGENT_SLUG}",
listener.local_addr().unwrap()
);
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
Self {
registry,
counters,
key,
url,
server,
}
}
pub async fn post(
&self,
revision: &str,
session: Option<&str>,
body: Value,
) -> reqwest::Response {
let mut request = reqwest::Client::new()
.post(&self.url)
.bearer_auth(&self.key)
.header("Accept", "application/json, text/event-stream")
.header("MCP-Protocol-Version", revision);
if revision == MODERN_REVISION {
request = request.header("Mcp-Method", body["method"].as_str().unwrap());
}
if let Some(session) = session {
request = request.header("MCP-Session-Id", session);
}
tokio::time::timeout(Duration::from_secs(6), request.json(&body).send())
.await
.unwrap()
.unwrap()
}
pub async fn listen(&self, id: Value, notifications: Value) -> Events {
Events::open(
self.post(
MODERN_REVISION,
None,
json!({
"jsonrpc":"2.0", "id":id, "method":"subscriptions/listen",
"params":{"_meta":modern_meta(), "notifications":notifications}
}),
)
.await,
)
}
pub async fn session(&self) -> String {
let response = self.post(LEGACY_REVISION, None, legacy_initialize(1)).await;
assert_eq!(response.status(), 200);
let session = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
let body = terminal(response).await;
assert_eq!(body["result"]["protocolVersion"], LEGACY_REVISION);
let response = self
.post(LEGACY_REVISION, Some(&session), initialized_notification())
.await;
assert_eq!(response.status(), 202);
session
}
pub async fn legacy_stream(&self, session: &str) -> Events {
let response = tokio::time::timeout(
Duration::from_secs(6),
reqwest::Client::new()
.get(&self.url)
.bearer_auth(&self.key)
.header("Accept", "text/event-stream")
.header("MCP-Protocol-Version", LEGACY_REVISION)
.header("MCP-Session-Id", session)
.send(),
)
.await
.unwrap()
.unwrap();
Events::open(response)
}
pub async fn tools(&self, revision: &str, session: Option<&str>) -> Value {
let params = if revision == MODERN_REVISION {
json!({"_meta":modern_meta()})
} else {
json!({})
};
terminal(
self.post(
revision,
session,
json!({"jsonrpc":"2.0","id":2,"method":"tools/list","params":params}),
)
.await,
)
.await
}
pub async fn draft_without_tools(&self) {
self.registry
.create_agent_draft_version(CreateAgentDraftVersionRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
version: &AgentVersion {
agent_id: agent_id(),
version: 2,
status: AgentStatus::Draft,
instructions: json!({}),
tool_selection_policy: ToolSelectionPolicy::default(),
created_at: test_time(),
},
bindings: &[],
updated_at: &test_time(),
})
.await
.unwrap();
}
pub async fn publish_without_tools(&self) {
self.registry
.publish_agent(PublishAgentRequest {
workspace_id: &workspace_id(),
agent_id: &agent_id(),
version: 2,
published_at: &test_time(),
published_by: Some("alice"),
expected_state: None,
})
.await
.unwrap();
}
}
pub(super) struct Events {
response: reqwest::Response,
buffer: Vec<u8>,
}
impl Events {
pub fn open(response: reqwest::Response) -> Self {
assert_eq!(response.status(), 200, "notification stream must open");
assert!(
response.headers()["content-type"]
.to_str()
.unwrap()
.starts_with("text/event-stream")
);
Self {
response,
buffer: Vec::new(),
}
}
// One absolute deadline: keepalive comments cannot extend a missing-frame assertion forever.
pub async fn next(&mut self, wait: Duration) -> Option<Value> {
let deadline = tokio::time::Instant::now() + wait;
loop {
if let Some(index) = self.buffer.windows(2).position(|w| w == b"\n\n") {
let frame: Vec<u8> = self.buffer.drain(..index + 2).collect();
let frame = std::str::from_utf8(&frame).unwrap();
if let Some(data) = frame.lines().find_map(|line| line.strip_prefix("data:")) {
return Some(serde_json::from_str(data.trim()).expect("SSE data must be JSON"));
}
continue;
}
match tokio::time::timeout_at(deadline, self.response.chunk()).await {
Err(_) => return None,
Ok(Err(error)) => panic!("SSE transport failure, not silence: {error}"),
Ok(Ok(None)) => panic!("SSE closed unexpectedly, not silence"),
Ok(Ok(Some(bytes))) => self.buffer.extend_from_slice(&bytes),
}
}
}
pub async fn frame(&mut self) -> Value {
self.next(Duration::from_secs(6))
.await
.expect("notification must arrive before deadline")
}
pub async fn silent(&mut self) {
assert_eq!(
self.next(Duration::from_millis(2200)).await,
None,
"no duplicate/unrequested signal"
);
}
}
pub(super) async fn terminal(response: reqwest::Response) -> Value {
let status = response.status();
assert!(
status == 200 || status == 404,
"unexpected terminal status {status}"
);
let sse = response.headers()["content-type"]
.to_str()
.unwrap()
.starts_with("text/event-stream");
let raw = tokio::time::timeout(Duration::from_secs(6), response.text())
.await
.unwrap()
.unwrap();
let body: Value = if sse {
let frames: Vec<Value> = raw
.lines()
.filter_map(|line| line.strip_prefix("data:"))
.map(|data| serde_json::from_str(data.trim()).unwrap())
.collect();
assert_eq!(
frames.len(),
1,
"one terminal frame, no notification: {raw}"
);
frames.into_iter().next().unwrap()
} else {
serde_json::from_str(&raw).unwrap()
};
assert_eq!(
status.as_u16(),
if body["error"]["code"] == -32601 {
404
} else {
200
}
);
body
}
Loaded 100 of 219 files, more files were not shown because too many files have changed in this diff. Show more