Compare commits
68
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e056e1487e | ||
|
|
ca3fff0f44 | ||
|
|
1ce3c2bb97 | ||
|
|
48436538f1 | ||
|
|
8f494679b8 | ||
|
|
0755e6e8e8 | ||
|
|
d01bdf019c | ||
|
|
22637861c3 | ||
|
|
7aab666aaa | ||
|
|
fd76aaf676 | ||
|
|
986cd78b96 | ||
|
|
bcdf57074b | ||
|
|
b39e4a3470 | ||
|
|
3b30aad28e | ||
|
|
5611e81579 | ||
|
|
b769a2ed2e | ||
|
|
0139eeaac6 | ||
|
|
5cce1b09df | ||
|
|
5b0af84d5a | ||
|
|
9b3e0248d1 | ||
|
|
163b519979 | ||
|
|
65144fd8c6 | ||
|
|
66bb135398 | ||
|
|
0de1e94df4 | ||
|
|
7a1fc0d761 | ||
|
|
156a849087 | ||
|
|
a4cf5ed8cf | ||
|
|
326f209bc5 | ||
|
|
c32e24b268 | ||
|
|
8068fd7910 | ||
|
|
ca126004af | ||
|
|
2f9c4cb568 | ||
|
|
61378224b2 | ||
|
|
1b51d44121 | ||
|
|
077be3b069 | ||
|
|
42e7728b3a | ||
|
|
541b135baf | ||
|
|
835947e9fc | ||
|
|
2b36add550 | ||
|
|
4832878468 | ||
|
|
5bf4e3e9bf | ||
|
|
1ac5b10c67 | ||
|
|
07a9ef0a4a | ||
|
|
4f83d8d24c | ||
|
|
2a57800f63 | ||
|
|
e359077f6e | ||
|
|
c0117ff6b8 | ||
|
|
24bba2cf33 | ||
|
|
f56553c4d6 | ||
|
|
027e2cb911 | ||
|
|
adabb2065a | ||
|
|
bf773d61b8 | ||
|
|
74a98d9362 | ||
|
|
7f18bce679 | ||
|
|
4ec9d9201b | ||
|
|
cf2909273b | ||
|
|
4a7b230f35 | ||
|
|
cfe11a0a04 | ||
|
|
35a21f8ab5 | ||
|
|
2d0deb8963 | ||
|
|
f0a79362eb | ||
|
|
27f86d4916 | ||
|
|
f728d31dbf | ||
|
|
29ee01b6ef | ||
|
|
bcf8b43516 | ||
|
|
1c256e6c75 | ||
|
|
e4953abcd2 | ||
|
|
90e5357a08 |
No files matched your search
+40
-20
@@ -2,7 +2,8 @@ name: CI
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, synchronize, reopened, labeled, unlabeled]
|
||||
branches: [main]
|
||||
types: [opened, synchronize, reopened]
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
validation_level:
|
||||
@@ -17,9 +18,6 @@ on:
|
||||
options: [none, production]
|
||||
default: none
|
||||
required: true
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
env:
|
||||
CARGO_BUILD_JOBS: "2"
|
||||
@@ -40,6 +38,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: ${{ gitea.event_name == 'pull_request' || inputs.validation_level == 'feature' }}
|
||||
run: python3 scripts/ci-release-tooling-scope.py
|
||||
|
||||
- name: Install Rust toolchain
|
||||
run: |
|
||||
set -eu
|
||||
@@ -48,9 +55,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
|
||||
@@ -74,15 +85,18 @@ 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 .
|
||||
@@ -138,6 +152,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
|
||||
@@ -170,8 +185,7 @@ jobs:
|
||||
needs: rust
|
||||
name: Rust Feature Validation
|
||||
if: >-
|
||||
inputs.validation_level == 'feature' ||
|
||||
contains(gitea.event.pull_request.labels.*.name, 'feature-ready')
|
||||
gitea.event_name == 'pull_request' || inputs.validation_level == 'feature'
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
@@ -187,9 +201,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
|
||||
@@ -230,8 +248,7 @@ jobs:
|
||||
frontend-e2e:
|
||||
needs: [rust, ui]
|
||||
if: >-
|
||||
inputs.validation_level == 'feature' ||
|
||||
contains(gitea.event.pull_request.labels.*.name, 'feature-ready')
|
||||
gitea.event_name == 'pull_request' || inputs.validation_level == 'feature'
|
||||
name: Frontend E2E
|
||||
runs-on: ubuntu-latest
|
||||
services:
|
||||
@@ -266,9 +283,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
|
||||
@@ -336,8 +357,7 @@ jobs:
|
||||
deployment:
|
||||
needs: [rust, ui]
|
||||
if: >-
|
||||
inputs.validation_level == 'feature' ||
|
||||
contains(gitea.event.pull_request.labels.*.name, 'feature-ready')
|
||||
gitea.event_name == 'pull_request' || inputs.validation_level == 'feature'
|
||||
name: Community Image Smoke
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
@@ -471,7 +491,7 @@ jobs:
|
||||
steps:
|
||||
- name: Require story gates and mode-appropriate feature gates
|
||||
env:
|
||||
FEATURE_VALIDATION: ${{ inputs.validation_level == 'feature' || contains(gitea.event.pull_request.labels.*.name, 'feature-ready') }}
|
||||
FEATURE_VALIDATION: ${{ gitea.event_name == 'pull_request' || inputs.validation_level == 'feature' }}
|
||||
RUST_RESULT: ${{ needs.rust.result }}
|
||||
UI_RESULT: ${{ needs.ui.result }}
|
||||
RUST_FEATURE_RESULT: ${{ needs.rust-feature.result }}
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,52 @@
|
||||
name: Story CI
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: ['epic/**']
|
||||
types: [opened, synchronize, reopened]
|
||||
|
||||
jobs:
|
||||
linters:
|
||||
name: Linters
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Install Rust toolchain
|
||||
run: |
|
||||
set -eu
|
||||
toolchain="$(sed -n 's/^channel = "\(.*\)"/\1/p' rust-toolchain.toml | head -n1)"
|
||||
if [ -z "$toolchain" ]; then
|
||||
echo "Unable to read Rust toolchain channel from rust-toolchain.toml" >&2
|
||||
exit 1
|
||||
fi
|
||||
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
|
||||
echo "Rust $toolchain was not installed at $toolchain_dir." >&2
|
||||
exit 1
|
||||
fi
|
||||
printf '%s\n' "$toolchain_bin" >> "$GITHUB_PATH"
|
||||
"$toolchain_bin/rustc" --version
|
||||
"$toolchain_bin/cargo" --version
|
||||
"$toolchain_bin/rustfmt" --version
|
||||
"$toolchain_bin/cargo-clippy" --version
|
||||
|
||||
- name: Check formatting
|
||||
run: cargo fmt --all --check
|
||||
|
||||
- name: Check Rust code health
|
||||
run: scripts/check-rust-code-health.sh
|
||||
|
||||
- name: Check Rust boundaries
|
||||
run: scripts/check-rust-boundaries.sh
|
||||
Generated
+215
-65
@@ -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",
|
||||
@@ -850,7 +863,7 @@ dependencies = [
|
||||
"aes-gcm",
|
||||
"async-trait",
|
||||
"axum",
|
||||
"base64",
|
||||
"base64 0.22.1",
|
||||
"crank-adapter-rest",
|
||||
"crank-core",
|
||||
"crank-import",
|
||||
@@ -992,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]]
|
||||
@@ -1006,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]]
|
||||
@@ -1015,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]]
|
||||
@@ -1057,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",
|
||||
]
|
||||
@@ -1069,7 +1117,7 @@ checksum = "1ac70aa55017e108007fbaf5aa0f54b021c98f92ff8af59d42eda9da96e3dd4f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1078,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",
|
||||
]
|
||||
@@ -1147,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]]
|
||||
@@ -1309,7 +1357,7 @@ checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1661,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",
|
||||
@@ -1672,7 +1720,7 @@ dependencies = [
|
||||
"libc",
|
||||
"percent-encoding",
|
||||
"pin-project-lite",
|
||||
"socket2 0.5.10",
|
||||
"socket2 0.6.4",
|
||||
"tokio",
|
||||
"tower-service",
|
||||
"tracing",
|
||||
@@ -1920,7 +1968,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38c0b942f458fe50cdac086d2f946512305e5631e720728f2a61aabcd47a6264"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2056,7 +2104,7 @@ version = "0.3.1"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"axum",
|
||||
"base64",
|
||||
"base64 0.22.1",
|
||||
"crank-artifacts",
|
||||
"crank-community-mcp",
|
||||
"crank-config",
|
||||
@@ -2121,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",
|
||||
@@ -2457,7 +2505,7 @@ dependencies = [
|
||||
"regex",
|
||||
"regex-syntax",
|
||||
"structmeta",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2471,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"
|
||||
@@ -2494,7 +2548,7 @@ checksum = "c96395f0a926bc13b1c17622aaddda1ecb55d49c8f1bf9777e4d877800a43f8b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2580,7 +2634,7 @@ dependencies = [
|
||||
"itertools",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2636,7 +2690,7 @@ dependencies = [
|
||||
"quinn-udp",
|
||||
"rustc-hash",
|
||||
"rustls",
|
||||
"socket2 0.5.10",
|
||||
"socket2 0.6.4",
|
||||
"thiserror 2.0.18",
|
||||
"tokio",
|
||||
"tracing",
|
||||
@@ -2675,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]]
|
||||
@@ -2852,7 +2906,7 @@ checksum = "b7186006dcb21920990093f30e3dea63b7d6e977bf1256be20c3563a5db070da"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2890,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",
|
||||
@@ -2934,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",
|
||||
@@ -2981,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"
|
||||
@@ -3008,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]]
|
||||
@@ -3075,7 +3176,7 @@ dependencies = [
|
||||
"security-framework",
|
||||
"security-framework-sys",
|
||||
"webpki-root-certs",
|
||||
"windows-sys 0.52.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3144,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"
|
||||
@@ -3293,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]]
|
||||
@@ -3328,7 +3454,7 @@ checksum = "175ee3e80ae9982737ca543e96133087cbd9a485eecc3bc4de9c1a37b47ea59c"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3349,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",
|
||||
@@ -3369,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]]
|
||||
@@ -3410,7 +3536,7 @@ checksum = "94e153fc76e1c6a068703d6d29c508a0b15c061c4b7e43da59cc097bc342673c"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3544,7 +3670,7 @@ dependencies = [
|
||||
name = "sqlx-core"
|
||||
version = "0.9.0"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"base64 0.22.1",
|
||||
"bytes",
|
||||
"cfg-if",
|
||||
"crc",
|
||||
@@ -3586,7 +3712,7 @@ dependencies = [
|
||||
"quote",
|
||||
"sqlx-core",
|
||||
"sqlx-macros-core",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3609,7 +3735,7 @@ dependencies = [
|
||||
"sqlx-mysql",
|
||||
"sqlx-postgres",
|
||||
"sqlx-sqlite",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
"thiserror 2.0.18",
|
||||
"tokio",
|
||||
"url",
|
||||
@@ -3650,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",
|
||||
@@ -3706,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"
|
||||
@@ -3738,7 +3877,7 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"structmeta-derive",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3749,7 +3888,7 @@ checksum = "152a0b65a590ff6c3da95cabe2353ee04e6167c896b28e3b14478c2636c922fc"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3769,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"
|
||||
@@ -3786,7 +3936,7 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3855,7 +4005,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3866,7 +4016,7 @@ checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3957,7 +4107,7 @@ checksum = "385a6cb71ab9ab790c5fe8d67f1645e6c450a7ce006a33de03daa956cf70a496"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4002,7 +4152,7 @@ checksum = "ac2a5518c70fa84342385732db33fb3f44bc4cc748936eb5833d2df34d6445ef"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"axum",
|
||||
"base64",
|
||||
"base64 0.22.1",
|
||||
"bytes",
|
||||
"h2",
|
||||
"http",
|
||||
@@ -4103,7 +4253,7 @@ checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4232,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",
|
||||
@@ -4248,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",
|
||||
@@ -4385,7 +4535,7 @@ dependencies = [
|
||||
"bumpalo",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
@@ -4477,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]]
|
||||
@@ -4507,7 +4657,7 @@ checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4518,7 +4668,7 @@ checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4712,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]]
|
||||
@@ -4734,7 +4884,7 @@ checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
"synstructure",
|
||||
]
|
||||
|
||||
@@ -4755,7 +4905,7 @@ checksum = "4714fd92cf900833d49538023a9b3915155210801d1c1169eba513b2addefd71"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4775,7 +4925,7 @@ checksum = "11532158c46691caf0f2593ea8358fed6bbf68a0315e80aae9bd41fbade684a1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
"synstructure",
|
||||
]
|
||||
|
||||
@@ -4815,7 +4965,7 @@ checksum = "625dc425cab0dca6dc3c3319506e6593dcb08a9f387ea3b284dbd52a92c40555"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -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(®istry, &upstream, policy).await;
|
||||
let caller = principal(®istry, "input-happy").await;
|
||||
let app = application(®istry).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(®istry, &app);
|
||||
assert!(
|
||||
wait_for_state(
|
||||
®istry,
|
||||
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(®istry, &app);
|
||||
assert!(
|
||||
wait_for_state(®istry, 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(®istry, &format!("http://{address}"), policy).await;
|
||||
let caller = principal(®istry, "input-scope").await;
|
||||
let app = application(®istry).await;
|
||||
let (task_id, wait_id, expected_version) =
|
||||
start_and_suspend(®istry, &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(®istry, task_id).await.as_deref(),
|
||||
Some("input_required")
|
||||
);
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 0);
|
||||
assert!(
|
||||
audit_events(®istry, 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(®istry, "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(®istry, 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(®istry, &app);
|
||||
assert!(
|
||||
wait_for_state(®istry, 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(®istry, "input-revoked").await;
|
||||
let (revoked_task, revoked_wait, revoked_version) =
|
||||
start_and_suspend(®istry, &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(®istry, 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(®istry, "http://127.0.0.1:9", policy).await;
|
||||
let caller = principal(®istry, "input-replay").await;
|
||||
let app = application(®istry).await;
|
||||
let (task_id, wait_id, expected_version) =
|
||||
start_and_suspend(®istry, &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(®istry, task_id).await.as_deref(),
|
||||
Some("working")
|
||||
);
|
||||
let accepted_audit = audit_events(®istry, 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(®istry, &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(®istry, &format!("http://{address}"), policy).await;
|
||||
let caller = principal(®istry, "input-ttl").await;
|
||||
let app = application(®istry).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(®istry, &app);
|
||||
assert!(
|
||||
wait_for_state(®istry, task_id, "input_required", Duration::from_secs(5)).await,
|
||||
"worker must suspend"
|
||||
);
|
||||
assert!(
|
||||
wait_for_state(®istry, 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(®istry, task_id).await;
|
||||
let events = audit_events(®istry, 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(®istry, 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(®istry, 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(®istry, &format!("http://{address}"), policy).await;
|
||||
let caller = principal(®istry, "input-race").await;
|
||||
let app = application(®istry).await;
|
||||
let (task_id, wait_id, expected_version) =
|
||||
start_and_suspend(®istry, &app, &caller, "input-key-race").await;
|
||||
assert_eq!(
|
||||
admission_entries(®istry, 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(®istry, task_id)
|
||||
.await
|
||||
.into_iter()
|
||||
.filter(|(event, _)| event == "input_accepted")
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
admission_entries(®istry, 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(®istry, &app);
|
||||
assert!(
|
||||
wait_for_state(®istry, 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(®istry, 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(®istry, &format!("http://{address}"), policy).await;
|
||||
let caller = principal(®istry, "deadline-cancel").await;
|
||||
let app = application(®istry).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(®istry, &app);
|
||||
assert!(
|
||||
wait_for_state(®istry, task_a, "input_required", Duration::from_secs(10)).await,
|
||||
"a must suspend"
|
||||
);
|
||||
assert!(
|
||||
wait_for_state(®istry, 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(®istry, task_a).await.as_deref(),
|
||||
Some("cancelled")
|
||||
);
|
||||
assert_eq!(admission_entries(®istry, 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(®istry, &app);
|
||||
assert!(
|
||||
wait_for_state(®istry, 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(®istry, 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(®istry, &format!("http://{address}"), policy).await;
|
||||
let caller = principal(®istry, "cancel-wait").await;
|
||||
let app = application(®istry).await;
|
||||
|
||||
let (task_id, wait_id, expected_version) =
|
||||
start_and_suspend(®istry, &app, &caller, "input-key-cancel").await;
|
||||
assert_eq!(
|
||||
admission_entries(®istry, 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(®istry, task_id).await.as_deref(),
|
||||
Some("cancelled")
|
||||
);
|
||||
assert_eq!(admission_entries(®istry, 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(®istry, task_id).await.as_deref(),
|
||||
Some("cancelled")
|
||||
);
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 0);
|
||||
|
||||
let events = audit_events(®istry, 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(®istry, &format!("http://{address}"), policy).await;
|
||||
let caller = principal(®istry, "stale-fence").await;
|
||||
let app = application(®istry).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(®istry, 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(®istry, task_id).await.as_deref(),
|
||||
Some("input_required")
|
||||
);
|
||||
assert_eq!(
|
||||
admission_entries(®istry, 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();
|
||||
}
|
||||
@@ -653,5 +653,7 @@ pub(crate) fn completed_outcome(value: serde_json::Value) -> crank_core::Attempt
|
||||
mod attempt_guards;
|
||||
mod attempt_trace;
|
||||
mod cancel_chain;
|
||||
mod input_continuation;
|
||||
mod input_continuation_guards;
|
||||
mod shutdown_window;
|
||||
mod task_worker;
|
||||
@@ -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"
|
||||
|
||||
@@ -34,6 +34,34 @@ pub(crate) mod lifecycle;
|
||||
mod metrics;
|
||||
mod request;
|
||||
mod response;
|
||||
mod rmcp_completion;
|
||||
mod rmcp_dispatch;
|
||||
#[cfg(test)]
|
||||
mod rmcp_http;
|
||||
#[cfg(test)]
|
||||
#[path = "rmcp_http_tests.rs"]
|
||||
mod rmcp_http_tests;
|
||||
mod rmcp_logging;
|
||||
mod rmcp_negotiation;
|
||||
mod rmcp_notifications;
|
||||
#[cfg(test)]
|
||||
mod rmcp_progress_cancel;
|
||||
mod rmcp_progress_peer;
|
||||
mod rmcp_prompts;
|
||||
mod rmcp_prompts_projection;
|
||||
mod rmcp_resources;
|
||||
mod rmcp_resources_projection;
|
||||
#[cfg(test)]
|
||||
mod rmcp_session_tests;
|
||||
mod rmcp_subscriptions;
|
||||
mod rmcp_subscriptions_output;
|
||||
#[cfg(test)]
|
||||
mod rmcp_tools;
|
||||
mod rmcp_tools_projection;
|
||||
mod rmcp_tools_runtime;
|
||||
#[cfg(test)]
|
||||
#[path = "rmcp_tools_tests.rs"]
|
||||
mod rmcp_tools_tests;
|
||||
mod stages;
|
||||
mod tool_resolution;
|
||||
pub(crate) use self::approval_policy::{
|
||||
|
||||
@@ -5,21 +5,20 @@
|
||||
//! 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,
|
||||
ServiceContext, ToolCallCommand, ToolsCommand, ToolsListCommand, declared_mcp_method,
|
||||
validate_modern_request_name,
|
||||
},
|
||||
session::SessionState,
|
||||
tool_error::execution_locale,
|
||||
@@ -39,12 +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, tools_list_result,
|
||||
resource_templates_list_result, resources_list_result,
|
||||
};
|
||||
use super::{AgentRoutePath, AppState, TRANSPORT_SESSION_TTL_MS};
|
||||
use crate::protocol::{ResourceTemplatesListError, list_resource_templates};
|
||||
const SERVER_NAME: &str = "crank-mcp-server";
|
||||
const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
use crate::protocol::ResourceTemplatesListError;
|
||||
|
||||
/// Serve a request of the modern stateless era.
|
||||
/// No session is created, read or required; identity comes from the per-request `_meta` metadata that
|
||||
@@ -79,7 +76,20 @@ pub(super) async fn handle_modern_request(
|
||||
// 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();
|
||||
}
|
||||
|
||||
@@ -87,45 +97,55 @@ 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,
|
||||
..
|
||||
} => transport_response(
|
||||
StatusCode::OK,
|
||||
jsonrpc_result(
|
||||
request_id(message),
|
||||
discover_result(version, SERVER_NAME, SERVER_VERSION),
|
||||
),
|
||||
response_mode,
|
||||
None,
|
||||
Some(version),
|
||||
),
|
||||
} => {
|
||||
let result = match super::rmcp_negotiation::discover(version).await {
|
||||
Ok(result) => result,
|
||||
Err(error) => return sdk_error_response(message, response_mode, version, error),
|
||||
};
|
||||
transport_response(
|
||||
StatusCode::OK,
|
||||
jsonrpc_result(
|
||||
request_id(message),
|
||||
crate::protocol::project_modern_cacheable_result(version, result),
|
||||
),
|
||||
response_mode,
|
||||
None,
|
||||
Some(version),
|
||||
)
|
||||
}
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::Ping,
|
||||
..
|
||||
} => transport_response(
|
||||
StatusCode::OK,
|
||||
jsonrpc_result(
|
||||
request_id(message),
|
||||
crate::protocol::project_modern_result(
|
||||
version,
|
||||
json!({ "protocolVersion": version }),
|
||||
} => {
|
||||
if let Err(error) = super::rmcp_negotiation::ping().await {
|
||||
return sdk_error_response(message, response_mode, version, error);
|
||||
}
|
||||
transport_response(
|
||||
StatusCode::OK,
|
||||
jsonrpc_result(
|
||||
request_id(message),
|
||||
crate::protocol::project_modern_result(
|
||||
version,
|
||||
json!({"protocolVersion": version}),
|
||||
),
|
||||
),
|
||||
),
|
||||
response_mode,
|
||||
None,
|
||||
Some(version),
|
||||
),
|
||||
response_mode,
|
||||
None,
|
||||
Some(version),
|
||||
)
|
||||
}
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::ToolList,
|
||||
..
|
||||
@@ -159,37 +179,7 @@ pub(super) async fn handle_modern_request(
|
||||
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),
|
||||
),
|
||||
),
|
||||
response_mode,
|
||||
None,
|
||||
Some(version),
|
||||
),
|
||||
ToolsOutcome::InvalidParams(message_text) => transport_response(
|
||||
StatusCode::OK,
|
||||
jsonrpc_error(request_id(message), -32602, message_text),
|
||||
response_mode,
|
||||
None,
|
||||
Some(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",
|
||||
),
|
||||
}
|
||||
super::response::sdk_tools_response(message, response_mode, version, outcome)
|
||||
}
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::ToolCall,
|
||||
@@ -286,10 +276,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,
|
||||
@@ -345,10 +335,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,
|
||||
@@ -405,8 +395,7 @@ pub(super) async fn handle_modern_request(
|
||||
};
|
||||
let (templates, next_cursor) = match list_resource_templates(
|
||||
state,
|
||||
&path.workspace_slug,
|
||||
&path.agent_slug,
|
||||
resource_service_context(path, credential, transport_correlation, message),
|
||||
version,
|
||||
command,
|
||||
)
|
||||
@@ -434,10 +423,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,
|
||||
@@ -466,7 +455,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,
|
||||
@@ -483,7 +472,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,
|
||||
@@ -525,7 +517,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,
|
||||
@@ -542,7 +534,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,
|
||||
@@ -616,7 +611,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,
|
||||
@@ -696,8 +691,8 @@ pub(super) async fn handle_modern_request(
|
||||
);
|
||||
}
|
||||
};
|
||||
let accepted = match filter.notifications.accepted() {
|
||||
crate::protocol::FilterOutcome::Accepted(accepted) => accepted,
|
||||
match filter.notifications.accepted() {
|
||||
crate::protocol::FilterOutcome::Accepted(_) => {}
|
||||
crate::protocol::FilterOutcome::Invalid(detail) => {
|
||||
return transport_response(
|
||||
StatusCode::OK,
|
||||
@@ -724,18 +719,19 @@ pub(super) async fn handle_modern_request(
|
||||
else {
|
||||
return StatusCode::TOO_MANY_REQUESTS.into_response();
|
||||
};
|
||||
let acknowledged = crate::protocol::acknowledged_frame(&subscription_id, accepted);
|
||||
let receiver = crate::protocol::spawn_signal_watch(
|
||||
let (acknowledged, receiver) = match super::rmcp_subscriptions::start(
|
||||
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,
|
||||
},
|
||||
);
|
||||
path.workspace_slug.clone(),
|
||||
path.agent_slug.clone(),
|
||||
subscription_id,
|
||||
filter.notifications.into_sdk(),
|
||||
super::rmcp_subscriptions::project_meta(message),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(stream) => stream,
|
||||
Err(error) => return sdk_error_response(message, response_mode, version, error),
|
||||
};
|
||||
let stream = crate::protocol::frames_stream(vec![acknowledged], receiver, permit);
|
||||
|
||||
sse_response(StatusCode::OK, stream, None, Some(version))
|
||||
@@ -746,6 +742,19 @@ pub(super) async fn handle_modern_request(
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn sdk_error_response(
|
||||
message: &Value,
|
||||
mode: ResponseMode,
|
||||
revision: &str,
|
||||
error: rmcp::ErrorData,
|
||||
) -> Response {
|
||||
let mut envelope = jsonrpc_error(request_id(message), i64::from(error.code.0), error.message);
|
||||
if let Some(data) = error.data {
|
||||
envelope["error"]["data"] = data;
|
||||
}
|
||||
transport_response(StatusCode::OK, envelope, mode, None, Some(revision))
|
||||
}
|
||||
|
||||
/// The verified, era-neutral context of a Resource command: the same workspace/Agent/credential/
|
||||
/// correlation the Tools service receives.
|
||||
fn resource_service_context<'a>(
|
||||
@@ -770,7 +779,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(
|
||||
@@ -782,7 +792,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.
|
||||
@@ -801,11 +814,15 @@ pub(super) async fn handle_initialize(
|
||||
None,
|
||||
);
|
||||
};
|
||||
let initialize_result = match super::rmcp_negotiation::initialize(protocol_version).await {
|
||||
Ok(result) => result,
|
||||
Err(error) => return sdk_error_response(message, response_mode, protocol_version, error),
|
||||
};
|
||||
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
|
||||
@@ -826,17 +843,7 @@ pub(super) async fn handle_initialize(
|
||||
|
||||
transport_response(
|
||||
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
|
||||
}
|
||||
}),
|
||||
),
|
||||
jsonrpc_result(request_id(message), initialize_result),
|
||||
response_mode,
|
||||
Some(&session_id),
|
||||
Some(protocol_version),
|
||||
@@ -847,6 +854,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 {
|
||||
@@ -854,15 +862,18 @@ pub(super) async fn handle_initialized_notification(
|
||||
if session.workspace_slug == path.workspace_slug
|
||||
&& session.agent_slug == path.agent_slug =>
|
||||
{
|
||||
match state
|
||||
.sessions
|
||||
.mark_initialized(&session_id, OffsetDateTime::now_utc())
|
||||
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;
|
||||
super::rmcp_notifications::initialized(state, session_id)
|
||||
.await
|
||||
{
|
||||
Ok(true) => StatusCode::ACCEPTED.into_response(),
|
||||
Ok(false) => StatusCode::NOT_FOUND.into_response(),
|
||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
||||
}
|
||||
.into_response()
|
||||
}
|
||||
Ok(Some(_)) => StatusCode::NOT_FOUND.into_response(),
|
||||
Ok(None) => StatusCode::NOT_FOUND.into_response(),
|
||||
|
||||
@@ -51,9 +51,306 @@ 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_tools_list_handler_completed: AtomicUsize,
|
||||
sdk_tools_list_handler_consumed: AtomicUsize,
|
||||
sdk_tools_call_handler_completed: AtomicUsize,
|
||||
sdk_tools_call_handler_consumed: AtomicUsize,
|
||||
sdk_resources_dispatch: AtomicUsize,
|
||||
sdk_resources_list_handler_completed: AtomicUsize,
|
||||
sdk_resources_read_handler_completed: AtomicUsize,
|
||||
sdk_resources_templates_handler_completed: AtomicUsize,
|
||||
sdk_resources_list_handler_consumed: AtomicUsize,
|
||||
sdk_resources_read_handler_consumed: AtomicUsize,
|
||||
sdk_resources_templates_handler_consumed: AtomicUsize,
|
||||
sdk_resources_list_result: AtomicUsize,
|
||||
sdk_resources_read_result: AtomicUsize,
|
||||
sdk_resource_templates_list_result: AtomicUsize,
|
||||
sdk_prompts_list_handler_completed: AtomicUsize,
|
||||
sdk_prompts_get_handler_completed: AtomicUsize,
|
||||
sdk_prompts_list_handler_consumed: AtomicUsize,
|
||||
sdk_prompts_get_handler_consumed: AtomicUsize,
|
||||
sdk_prompts_dispatch: AtomicUsize,
|
||||
sdk_prompts_list_result: AtomicUsize,
|
||||
sdk_prompts_get_result: AtomicUsize,
|
||||
sdk_completion_dispatch: AtomicUsize,
|
||||
sdk_completion_result: AtomicUsize,
|
||||
sdk_completion_handler_completed: AtomicUsize,
|
||||
sdk_completion_handler_consumed: AtomicUsize,
|
||||
sdk_logging_dispatch: AtomicUsize,
|
||||
sdk_logging_level: AtomicUsize,
|
||||
sdk_logging_notification: AtomicUsize,
|
||||
sdk_logging_handler_completed: AtomicUsize,
|
||||
sdk_logging_handler_consumed: AtomicUsize,
|
||||
sdk_progress_notification: AtomicUsize,
|
||||
sdk_cancelled_params: AtomicUsize,
|
||||
sdk_subscriptions_listen: AtomicUsize,
|
||||
sdk_subscriptions_acknowledged: AtomicUsize,
|
||||
sdk_signal_notification: AtomicUsize,
|
||||
sdk_subscription_listen_entered: AtomicUsize,
|
||||
sdk_subscription_handler_completed: AtomicUsize,
|
||||
sdk_subscription_handler_consumed: AtomicUsize,
|
||||
sdk_subscription_notification_consumed: AtomicUsize,
|
||||
sdk_subscription_owner_completed: AtomicUsize,
|
||||
sdk_jsonrpc_response: AtomicUsize,
|
||||
sdk_jsonrpc_error: AtomicUsize,
|
||||
|
||||
sdk_inbound_response_decoded: AtomicUsize,
|
||||
sdk_inbound_error_decoded: AtomicUsize,
|
||||
sdk_inbound_response_consumed: AtomicUsize,
|
||||
sdk_inbound_error_consumed: AtomicUsize,
|
||||
sdk_dispatch_decoded: AtomicUsize,
|
||||
sdk_dispatch_consumed: AtomicUsize,
|
||||
sdk_initialized_decoded: AtomicUsize,
|
||||
sdk_initialized_consumed: AtomicUsize,
|
||||
sdk_initialized_handler_completed: AtomicUsize,
|
||||
sdk_initialized_handler_consumed: AtomicUsize,
|
||||
sdk_cancelled_handler_completed: AtomicUsize,
|
||||
sdk_cancelled_handler_consumed: AtomicUsize,
|
||||
sdk_initialize_input: AtomicUsize,
|
||||
|
||||
sdk_initialize_handler_completed: AtomicUsize,
|
||||
sdk_initialize_handler_consumed: AtomicUsize,
|
||||
sdk_discover_handler_completed: AtomicUsize,
|
||||
sdk_discover_handler_consumed: AtomicUsize,
|
||||
sdk_ping_handler_completed: AtomicUsize,
|
||||
sdk_ping_handler_consumed: AtomicUsize,
|
||||
}
|
||||
|
||||
impl CounterSet {
|
||||
pub(crate) fn sdk_tools_list_handler_completed(&self) -> usize {
|
||||
self.sdk_tools_list_handler_completed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_tools_list_handler_consumed(&self) -> usize {
|
||||
self.sdk_tools_list_handler_consumed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_tools_call_handler_completed(&self) -> usize {
|
||||
self.sdk_tools_call_handler_completed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_tools_call_handler_consumed(&self) -> usize {
|
||||
self.sdk_tools_call_handler_consumed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_prompts_list_handler_completed(&self) -> usize {
|
||||
self.sdk_prompts_list_handler_completed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_prompts_get_handler_completed(&self) -> usize {
|
||||
self.sdk_prompts_get_handler_completed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_prompts_list_handler_consumed(&self) -> usize {
|
||||
self.sdk_prompts_list_handler_consumed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_prompts_get_handler_consumed(&self) -> usize {
|
||||
self.sdk_prompts_get_handler_consumed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_resources_list_handler_completed(&self) -> usize {
|
||||
self.sdk_resources_list_handler_completed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_resources_read_handler_completed(&self) -> usize {
|
||||
self.sdk_resources_read_handler_completed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_resources_templates_handler_completed(&self) -> usize {
|
||||
self.sdk_resources_templates_handler_completed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_resources_list_handler_consumed(&self) -> usize {
|
||||
self.sdk_resources_list_handler_consumed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_resources_read_handler_consumed(&self) -> usize {
|
||||
self.sdk_resources_read_handler_consumed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_resources_templates_handler_consumed(&self) -> usize {
|
||||
self.sdk_resources_templates_handler_consumed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_inbound_response_decoded(&self) -> usize {
|
||||
self.sdk_inbound_response_decoded.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_inbound_error_decoded(&self) -> usize {
|
||||
self.sdk_inbound_error_decoded.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_inbound_response_consumed(&self) -> usize {
|
||||
self.sdk_inbound_response_consumed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_inbound_error_consumed(&self) -> usize {
|
||||
self.sdk_inbound_error_consumed.load(Ordering::SeqCst)
|
||||
}
|
||||
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_initialized_handler_completed(&self) -> usize {
|
||||
self.sdk_initialized_handler_completed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_initialized_handler_consumed(&self) -> usize {
|
||||
self.sdk_initialized_handler_consumed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_cancelled_handler_completed(&self) -> usize {
|
||||
self.sdk_cancelled_handler_completed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_cancelled_handler_consumed(&self) -> usize {
|
||||
self.sdk_cancelled_handler_consumed.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
pub(crate) fn sdk_initialize_input(&self) -> usize {
|
||||
self.sdk_initialize_input.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
pub(crate) fn sdk_initialize_handler_completed(&self) -> usize {
|
||||
self.sdk_initialize_handler_completed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_initialize_handler_consumed(&self) -> usize {
|
||||
self.sdk_initialize_handler_consumed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_discover_handler_completed(&self) -> usize {
|
||||
self.sdk_discover_handler_completed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_discover_handler_consumed(&self) -> usize {
|
||||
self.sdk_discover_handler_consumed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_ping_handler_completed(&self) -> usize {
|
||||
self.sdk_ping_handler_completed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_ping_handler_consumed(&self) -> usize {
|
||||
self.sdk_ping_handler_consumed.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_subscription_listen_entered(&self) -> usize {
|
||||
self.sdk_subscription_listen_entered.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_subscription_handler_completed(&self) -> usize {
|
||||
self.sdk_subscription_handler_completed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_subscription_handler_consumed(&self) -> usize {
|
||||
self.sdk_subscription_handler_consumed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_subscription_notification_consumed(&self) -> usize {
|
||||
self.sdk_subscription_notification_consumed
|
||||
.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_subscription_owner_completed(&self) -> usize {
|
||||
self.sdk_subscription_owner_completed.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_logging_handler_completed(&self) -> usize {
|
||||
self.sdk_logging_handler_completed.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
pub(crate) fn sdk_logging_handler_consumed(&self) -> usize {
|
||||
self.sdk_logging_handler_consumed.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_completion_handler_completed(&self) -> usize {
|
||||
self.sdk_completion_handler_completed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sdk_completion_handler_consumed(&self) -> usize {
|
||||
self.sdk_completion_handler_consumed.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 +431,363 @@ 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 after the SDK Tools handler awaits the domain operation and builds its typed result.
|
||||
pub(crate) fn sdk_tools_list_handler_completed() {
|
||||
increment(|counters| &counters.sdk_tools_list_handler_completed);
|
||||
}
|
||||
/// Mark only after the caller consumes the actual SDK handler result.
|
||||
pub(crate) fn sdk_tools_list_handler_consumed() {
|
||||
increment(|counters| &counters.sdk_tools_list_handler_consumed);
|
||||
}
|
||||
/// Mark after the SDK Tools handler awaits the domain operation and builds its typed result.
|
||||
pub(crate) fn sdk_tools_call_handler_completed() {
|
||||
increment(|counters| &counters.sdk_tools_call_handler_completed);
|
||||
}
|
||||
/// Mark only after the caller consumes the actual SDK handler result.
|
||||
pub(crate) fn sdk_tools_call_handler_consumed() {
|
||||
increment(|counters| &counters.sdk_tools_call_handler_consumed);
|
||||
}
|
||||
|
||||
/// Mark inside the ServerHandler override after domain await and typed result/error construction.
|
||||
pub(crate) fn sdk_resources_list_handler_completed() {
|
||||
increment(|c| &c.sdk_resources_list_handler_completed);
|
||||
}
|
||||
/// Mark inside the ServerHandler override after domain await and typed result/error construction.
|
||||
pub(crate) fn sdk_resources_read_handler_completed() {
|
||||
increment(|c| &c.sdk_resources_read_handler_completed);
|
||||
}
|
||||
/// Mark inside the ServerHandler override after domain await and typed result/error construction.
|
||||
pub(crate) fn sdk_resources_templates_handler_completed() {
|
||||
increment(|c| &c.sdk_resources_templates_handler_completed);
|
||||
}
|
||||
/// Mark in the caller only after matching and consuming the actual trait Result.
|
||||
pub(crate) fn sdk_resources_list_handler_consumed() {
|
||||
increment(|c| &c.sdk_resources_list_handler_consumed);
|
||||
}
|
||||
/// Mark in the caller only after matching and consuming the actual trait Result.
|
||||
pub(crate) fn sdk_resources_read_handler_consumed() {
|
||||
increment(|c| &c.sdk_resources_read_handler_consumed);
|
||||
}
|
||||
/// Mark in the caller only after matching and consuming the actual trait Result.
|
||||
pub(crate) fn sdk_resources_templates_handler_consumed() {
|
||||
increment(|c| &c.sdk_resources_templates_handler_consumed);
|
||||
}
|
||||
|
||||
/// 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;
|
||||
|
||||
#[path = "../rmcp_resources_handler_runtime_tests.rs"]
|
||||
mod rmcp_resources_handler_runtime_tests;
|
||||
|
||||
#[path = "../rmcp_prompts_handler_runtime_tests.rs"]
|
||||
mod rmcp_prompts_handler_runtime_tests;
|
||||
|
||||
/// Mark inside the override after domain await and typed Result construction.
|
||||
pub(crate) fn sdk_prompts_list_handler_completed() {
|
||||
increment(|c| &c.sdk_prompts_list_handler_completed);
|
||||
}
|
||||
/// Mark inside the override after domain await and typed Result construction.
|
||||
pub(crate) fn sdk_prompts_get_handler_completed() {
|
||||
increment(|c| &c.sdk_prompts_get_handler_completed);
|
||||
}
|
||||
/// Mark after matching/restoring the actual trait Result in the caller.
|
||||
pub(crate) fn sdk_prompts_list_handler_consumed() {
|
||||
increment(|c| &c.sdk_prompts_list_handler_consumed);
|
||||
}
|
||||
/// Mark after matching/restoring the actual trait Result in the caller.
|
||||
pub(crate) fn sdk_prompts_get_handler_consumed() {
|
||||
increment(|c| &c.sdk_prompts_get_handler_consumed);
|
||||
}
|
||||
|
||||
/// 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);
|
||||
}
|
||||
|
||||
/// Mark inside the ServerHandler override after domain await and typed Result construction.
|
||||
pub(crate) fn sdk_completion_handler_completed() {
|
||||
increment(|counters| &counters.sdk_completion_handler_completed);
|
||||
}
|
||||
|
||||
/// Mark in the caller only after matching and consuming the actual trait Result.
|
||||
pub(crate) fn sdk_completion_handler_consumed() {
|
||||
increment(|counters| &counters.sdk_completion_handler_consumed);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_completion_runtime_tests.rs"]
|
||||
mod rmcp_completion_runtime_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_completion_handler_runtime_tests.rs"]
|
||||
mod rmcp_completion_handler_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);
|
||||
}
|
||||
|
||||
/// Mark after the real logging handler has completed its awaited operation and built its result.
|
||||
pub(crate) fn sdk_logging_handler_completed() {
|
||||
increment(|counters| &counters.sdk_logging_handler_completed);
|
||||
}
|
||||
|
||||
/// Mark only after the caller consumes the actual SDK handler Result.
|
||||
pub(crate) fn sdk_logging_handler_consumed() {
|
||||
increment(|counters| &counters.sdk_logging_handler_consumed);
|
||||
}
|
||||
|
||||
#[path = "../rmcp_logging_runtime_tests.rs"]
|
||||
mod rmcp_logging_runtime_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_logging_handler_runtime_tests.rs"]
|
||||
mod rmcp_logging_handler_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_subscription_listen_entered() {
|
||||
increment(|c| &c.sdk_subscription_listen_entered);
|
||||
}
|
||||
pub(crate) fn sdk_subscription_handler_completed() {
|
||||
increment(|c| &c.sdk_subscription_handler_completed);
|
||||
}
|
||||
pub(crate) fn sdk_subscription_handler_consumed() {
|
||||
increment(|c| &c.sdk_subscription_handler_consumed);
|
||||
}
|
||||
pub(crate) fn sdk_subscription_notification_consumed() {
|
||||
increment(|c| &c.sdk_subscription_notification_consumed);
|
||||
}
|
||||
pub(crate) fn sdk_subscription_owner_completed() {
|
||||
increment(|c| &c.sdk_subscription_owner_completed);
|
||||
}
|
||||
|
||||
#[path = "../rmcp_signals_test_support.rs"]
|
||||
mod signals_test_support;
|
||||
|
||||
#[path = "../rmcp_subscription_handler_runtime_tests.rs"]
|
||||
mod rmcp_subscription_handler_runtime_tests;
|
||||
|
||||
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;
|
||||
|
||||
pub(crate) fn sdk_initialize_handler_completed() {
|
||||
increment(|c| &c.sdk_initialize_handler_completed);
|
||||
}
|
||||
pub(crate) fn sdk_initialize_handler_consumed() {
|
||||
increment(|c| &c.sdk_initialize_handler_consumed);
|
||||
}
|
||||
pub(crate) fn sdk_discover_handler_completed() {
|
||||
increment(|c| &c.sdk_discover_handler_completed);
|
||||
}
|
||||
pub(crate) fn sdk_discover_handler_consumed() {
|
||||
increment(|c| &c.sdk_discover_handler_consumed);
|
||||
}
|
||||
pub(crate) fn sdk_ping_handler_completed() {
|
||||
increment(|c| &c.sdk_ping_handler_completed);
|
||||
}
|
||||
pub(crate) fn sdk_ping_handler_consumed() {
|
||||
increment(|c| &c.sdk_ping_handler_consumed);
|
||||
}
|
||||
|
||||
#[path = "../rmcp_lifecycle_negotiation_runtime_tests.rs"]
|
||||
mod rmcp_lifecycle_negotiation_runtime_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_negotiation_handler_runtime_tests.rs"]
|
||||
mod rmcp_negotiation_handler_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;
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
||||
pub(crate) fn sdk_initialized_handler_completed() {
|
||||
increment(|c| &c.sdk_initialized_handler_completed);
|
||||
}
|
||||
|
||||
pub(crate) fn sdk_initialized_handler_consumed() {
|
||||
increment(|c| &c.sdk_initialized_handler_consumed);
|
||||
}
|
||||
|
||||
pub(crate) fn sdk_cancelled_handler_completed() {
|
||||
increment(|c| &c.sdk_cancelled_handler_completed);
|
||||
}
|
||||
|
||||
pub(crate) fn sdk_cancelled_handler_consumed() {
|
||||
increment(|c| &c.sdk_cancelled_handler_consumed);
|
||||
}
|
||||
|
||||
#[path = "../rmcp_initialized_runtime_tests.rs"]
|
||||
mod rmcp_initialized_runtime_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_notification_handler_runtime_tests.rs"]
|
||||
mod rmcp_notification_handler_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;
|
||||
|
||||
/// Mark only after a real SDK success-response from_value succeeds.
|
||||
pub(crate) fn sdk_inbound_response_decoded() {
|
||||
increment(|c| &c.sdk_inbound_response_decoded);
|
||||
}
|
||||
/// Mark only after a real SDK error-response from_value succeeds.
|
||||
pub(crate) fn sdk_inbound_error_decoded() {
|
||||
increment(|c| &c.sdk_inbound_error_decoded);
|
||||
}
|
||||
/// Mark inside the matched typed SDK success variant that selects ACK.
|
||||
pub(crate) fn sdk_inbound_response_consumed() {
|
||||
increment(|c| &c.sdk_inbound_response_consumed);
|
||||
}
|
||||
/// Mark inside the matched typed SDK error variant that selects ACK.
|
||||
pub(crate) fn sdk_inbound_error_consumed() {
|
||||
increment(|c| &c.sdk_inbound_error_consumed);
|
||||
}
|
||||
|
||||
#[path = "../rmcp_response_runtime_tests.rs"]
|
||||
mod rmcp_response_runtime_tests;
|
||||
|
||||
fn increment(counter: impl Fn(&CounterSet) -> &AtomicUsize) {
|
||||
let _ = OBSERVER.try_with(|counters| {
|
||||
counter(counters).fetch_add(1, Ordering::SeqCst);
|
||||
@@ -144,7 +798,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 +807,7 @@ mod support;
|
||||
#[cfg(test)]
|
||||
#[path = "../dispatch_observer_harness.rs"]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_runtime_tests.rs"]
|
||||
mod rmcp_runtime_tests;
|
||||
@@ -278,6 +278,17 @@ async fn mcp_post_response(
|
||||
Err(_status) => return ambiguous_session_carrier_response(headers, message),
|
||||
};
|
||||
|
||||
// Responses ACK before era/session resolution, but must not bypass fresh credentials.
|
||||
// Method-bearing messages retain their existing scoped authorization path below.
|
||||
if message
|
||||
.as_object()
|
||||
.is_some_and(|object| !object.contains_key("method"))
|
||||
&& let Err(error) =
|
||||
require_traced_machine_access(&state, path, headers, PlatformApiKeyScope::Read).await
|
||||
{
|
||||
return error.into_response();
|
||||
}
|
||||
|
||||
// Single JSON-RPC envelope guard: only a well-formed message reaches era resolution, dispatch,
|
||||
// approval or any outbound request. Syntactically valid JSON is not the same as a valid envelope.
|
||||
let envelope = match crate::protocol::validate_jsonrpc_envelope(message) {
|
||||
|
||||
@@ -22,23 +22,20 @@ 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, is_notification, is_request, jsonrpc_error, jsonrpc_result,
|
||||
method_name, params, request_id,
|
||||
},
|
||||
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_completion_command,
|
||||
handle_logging_command, handle_prompts_command, handle_resources_command,
|
||||
handle_tools_command,
|
||||
CompletionCommand, CompletionOutcome, ContinuationSupport, DispatchDecision, INVALID_LEVEL,
|
||||
Kind, MethodHandler, PromptGetCommand, PromptsCommand, PromptsListCommand, PromptsOutcome,
|
||||
RequestIdentity, ResourceReadCommand, ResourcesCommand, ResourcesListCommand,
|
||||
ResourcesOutcome, ServiceContext, SetLevelCommand, ToolCallCommand, ToolsCommand,
|
||||
ToolsListCommand,
|
||||
},
|
||||
tool_error::execution_locale,
|
||||
transport::{ResponseMode, session_id_from_headers, transport_response},
|
||||
@@ -64,10 +61,20 @@ pub(super) async fn handle_legacy_request(
|
||||
// `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 dispatch = crate::protocol::dispatch_decision(
|
||||
method_name(message).unwrap_or_default(),
|
||||
registration_revision,
|
||||
);
|
||||
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
|
||||
@@ -77,27 +84,42 @@ pub(super) async fn handle_legacy_request(
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::Lifecycle,
|
||||
kind: Kind::Method,
|
||||
} if is_request(message) => handle_initialize(state, path, message, response_mode).await,
|
||||
} if envelope.is_request() => 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
|
||||
} if envelope.is_notification() => {
|
||||
handle_initialized_notification(
|
||||
state,
|
||||
path,
|
||||
headers,
|
||||
crate::protocol::initialized_notification(message),
|
||||
)
|
||||
.await
|
||||
}
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::Ping,
|
||||
..
|
||||
} if is_request(message) => {
|
||||
} if envelope.is_request() => {
|
||||
let session = match require_initialized_session(&state, path, headers, message).await {
|
||||
Ok(session) => session,
|
||||
Err(response) => return response,
|
||||
};
|
||||
|
||||
if let Err(error) = super::rmcp_negotiation::ping().await {
|
||||
return super::dispatch::sdk_error_response(
|
||||
message,
|
||||
response_mode,
|
||||
&session.protocol_version,
|
||||
error,
|
||||
);
|
||||
}
|
||||
|
||||
transport_response(
|
||||
StatusCode::OK,
|
||||
jsonrpc_result(
|
||||
request_id(message),
|
||||
json!({ "protocolVersion": session.protocol_version }),
|
||||
json!({"protocolVersion": session.protocol_version}),
|
||||
),
|
||||
response_mode,
|
||||
None,
|
||||
@@ -107,7 +129,7 @@ pub(super) async fn handle_legacy_request(
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::ToolList,
|
||||
..
|
||||
} if is_request(message) => {
|
||||
} if envelope.is_request() => {
|
||||
let session = match require_initialized_session(&state, path, headers, message).await {
|
||||
Ok(session) => session,
|
||||
Err(response) => return response,
|
||||
@@ -142,36 +164,17 @@ pub(super) async fn handle_legacy_request(
|
||||
None,
|
||||
)
|
||||
.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",
|
||||
),
|
||||
}
|
||||
super::response::sdk_tools_response(
|
||||
message,
|
||||
response_mode,
|
||||
&session.protocol_version,
|
||||
outcome,
|
||||
)
|
||||
}
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::ToolCall,
|
||||
..
|
||||
} if is_request(message) => {
|
||||
} if envelope.is_request() => {
|
||||
let session = match require_initialized_session(&state, path, headers, message).await {
|
||||
Ok(session) => session,
|
||||
Err(response) => return response,
|
||||
@@ -229,13 +232,13 @@ pub(super) async fn handle_legacy_request(
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::Cancelled,
|
||||
kind: Kind::Notification,
|
||||
} if is_notification(message) => {
|
||||
} if envelope.is_notification() => {
|
||||
handle_cancelled_notification(&state, path, headers, message, credential).await
|
||||
}
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::ResourceList,
|
||||
..
|
||||
} if is_request(message) => {
|
||||
} if envelope.is_request() => {
|
||||
let session = match require_initialized_session(&state, path, headers, message).await {
|
||||
Ok(session) => session,
|
||||
Err(response) => return response,
|
||||
@@ -278,7 +281,14 @@ pub(super) async fn handle_legacy_request(
|
||||
StatusCode::OK,
|
||||
jsonrpc_result(
|
||||
request_id(message),
|
||||
resources_list_result(resources, next_cursor),
|
||||
match resources_list_result(
|
||||
resources,
|
||||
next_cursor,
|
||||
&session.protocol_version,
|
||||
) {
|
||||
Ok(result) => result,
|
||||
Err(error) => return internal_jsonrpc_error(message, error),
|
||||
},
|
||||
),
|
||||
response_mode,
|
||||
None,
|
||||
@@ -297,7 +307,7 @@ pub(super) async fn handle_legacy_request(
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::ResourceRead,
|
||||
..
|
||||
} if is_request(message) => {
|
||||
} if envelope.is_request() => {
|
||||
let session = match require_initialized_session(&state, path, headers, message).await {
|
||||
Ok(session) => session,
|
||||
Err(response) => return response,
|
||||
@@ -335,7 +345,13 @@ pub(super) async fn handle_legacy_request(
|
||||
match outcome {
|
||||
ResourcesOutcome::Read { contents } => transport_response(
|
||||
StatusCode::OK,
|
||||
jsonrpc_result(request_id(message), resource_read_result(contents)),
|
||||
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),
|
||||
@@ -372,7 +388,7 @@ pub(super) async fn handle_legacy_request(
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::ResourceTemplateList,
|
||||
..
|
||||
} if is_request(message) => {
|
||||
} if envelope.is_request() => {
|
||||
let session = match require_initialized_session(&state, path, headers, message).await {
|
||||
Ok(session) => session,
|
||||
Err(response) => return response,
|
||||
@@ -394,10 +410,16 @@ pub(super) async fn handle_legacy_request(
|
||||
);
|
||||
}
|
||||
};
|
||||
let (templates, next_cursor) = match crate::protocol::list_resource_templates(
|
||||
let (templates, next_cursor) = match list_resource_templates(
|
||||
&state,
|
||||
&path.workspace_slug,
|
||||
&path.agent_slug,
|
||||
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,
|
||||
)
|
||||
@@ -425,7 +447,14 @@ pub(super) async fn handle_legacy_request(
|
||||
StatusCode::OK,
|
||||
jsonrpc_result(
|
||||
request_id(message),
|
||||
resource_templates_list_result(templates, next_cursor),
|
||||
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,
|
||||
@@ -437,7 +466,7 @@ pub(super) async fn handle_legacy_request(
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::PromptList,
|
||||
..
|
||||
} if is_request(message) => {
|
||||
} if envelope.is_request() => {
|
||||
let session = match require_initialized_session(&state, path, headers, message).await {
|
||||
Ok(session) => session,
|
||||
Err(response) => return response,
|
||||
@@ -458,7 +487,7 @@ pub(super) async fn handle_legacy_request(
|
||||
);
|
||||
}
|
||||
};
|
||||
let outcome = handle_prompts_command(
|
||||
let outcome = super::rmcp_dispatch::handle_prompts_command(
|
||||
&state,
|
||||
ServiceContext {
|
||||
workspace_slug: &session.workspace_slug,
|
||||
@@ -480,7 +509,10 @@ pub(super) async fn handle_legacy_request(
|
||||
StatusCode::OK,
|
||||
jsonrpc_result(
|
||||
request_id(message),
|
||||
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,
|
||||
None,
|
||||
@@ -499,7 +531,7 @@ pub(super) async fn handle_legacy_request(
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::PromptGet,
|
||||
..
|
||||
} if is_request(message) => {
|
||||
} if envelope.is_request() => {
|
||||
let session = match require_initialized_session(&state, path, headers, message).await {
|
||||
Ok(session) => session,
|
||||
Err(response) => return response,
|
||||
@@ -520,7 +552,7 @@ pub(super) async fn handle_legacy_request(
|
||||
);
|
||||
}
|
||||
};
|
||||
let outcome = handle_prompts_command(
|
||||
let outcome = super::rmcp_dispatch::handle_prompts_command(
|
||||
&state,
|
||||
ServiceContext {
|
||||
workspace_slug: &session.workspace_slug,
|
||||
@@ -542,7 +574,10 @@ pub(super) async fn handle_legacy_request(
|
||||
StatusCode::OK,
|
||||
jsonrpc_result(
|
||||
request_id(message),
|
||||
prompt_get_result(description, messages),
|
||||
match prompt_get_result(description, messages) {
|
||||
Ok(result) => result,
|
||||
Err(error) => return internal_jsonrpc_error(message, error),
|
||||
},
|
||||
),
|
||||
response_mode,
|
||||
None,
|
||||
@@ -594,7 +629,7 @@ pub(super) async fn handle_legacy_request(
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::Completion,
|
||||
..
|
||||
} if is_request(message) => {
|
||||
} if envelope.is_request() => {
|
||||
let session = match require_initialized_session(&state, path, headers, message).await {
|
||||
Ok(session) => session,
|
||||
Err(response) => return response,
|
||||
@@ -615,7 +650,7 @@ pub(super) async fn handle_legacy_request(
|
||||
);
|
||||
}
|
||||
};
|
||||
let outcome = handle_completion_command(
|
||||
let outcome = super::rmcp_dispatch::handle_completion_command(
|
||||
&state,
|
||||
ServiceContext {
|
||||
workspace_slug: &session.workspace_slug,
|
||||
@@ -673,7 +708,7 @@ pub(super) async fn handle_legacy_request(
|
||||
DispatchDecision::Allowed {
|
||||
handler: MethodHandler::LoggingSetLevel,
|
||||
..
|
||||
} if is_request(message) => {
|
||||
} if envelope.is_request() => {
|
||||
let session = match require_initialized_session(&state, path, headers, message).await {
|
||||
Ok(session) => session,
|
||||
Err(response) => return response,
|
||||
@@ -692,37 +727,30 @@ pub(super) async fn handle_legacy_request(
|
||||
);
|
||||
}
|
||||
};
|
||||
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!({})),
|
||||
let request = match command.validate() {
|
||||
Ok(request) => request,
|
||||
Err(detail) => {
|
||||
return super::dispatch::sdk_error_response(
|
||||
message,
|
||||
response_mode,
|
||||
None,
|
||||
Some(&session.protocol_version),
|
||||
)
|
||||
&session.protocol_version,
|
||||
rmcp::ErrorData::invalid_params(detail, None),
|
||||
);
|
||||
}
|
||||
LoggingOutcome::InvalidParams(detail) => transport_response(
|
||||
StatusCode::OK,
|
||||
jsonrpc_error(request_id(message), -32602, detail),
|
||||
response_mode,
|
||||
None,
|
||||
Some(&session.protocol_version),
|
||||
),
|
||||
}
|
||||
};
|
||||
super::rmcp_logging::response(
|
||||
message,
|
||||
response_mode,
|
||||
&session.protocol_version,
|
||||
super::rmcp_logging::set_level(&state, &session.id, request).await,
|
||||
)
|
||||
}
|
||||
// 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(),
|
||||
_ 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_name(message) {
|
||||
_ => match method {
|
||||
Some(method) => transport_response(
|
||||
StatusCode::OK,
|
||||
jsonrpc_error(
|
||||
@@ -767,28 +795,19 @@ async fn handle_cancelled_notification(
|
||||
message: &Value,
|
||||
credential: &VerifiedMachineCredential,
|
||||
) -> Response {
|
||||
let request_id = params(message).get("requestId").cloned();
|
||||
let Some((typed, raw_number)) = super::rmcp_notifications::cancelled_params(¶ms(message))
|
||||
else {
|
||||
return StatusCode::ACCEPTED.into_response();
|
||||
};
|
||||
let session_id = session_id_from_headers(headers).ok().flatten();
|
||||
if let (Some(request_id), Some(session_id)) = (request_id, session_id) {
|
||||
// Only a string or a number is a pinned `RequestId`; anything else cannot name a call.
|
||||
if matches!(request_id, Value::String(_) | Value::Number(_)) {
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
super::rmcp_notifications::cancelled(
|
||||
Arc::clone(state),
|
||||
path.clone(),
|
||||
credential.clone(),
|
||||
session_id,
|
||||
typed,
|
||||
raw_number,
|
||||
)
|
||||
.await;
|
||||
StatusCode::ACCEPTED.into_response()
|
||||
}
|
||||
@@ -34,19 +34,17 @@ use axum::{
|
||||
};
|
||||
use crank_core::{CorrelationContext, ExecutionLocale, VerifiedMachineCredential};
|
||||
use futures_util::stream;
|
||||
use serde_json::{Number, Value, json};
|
||||
use serde_json::{Number, Value};
|
||||
use tokio::sync::{mpsc, watch};
|
||||
|
||||
use super::rmcp_dispatch::handle_tools_command;
|
||||
use super::{
|
||||
AppState,
|
||||
response::{ToolCommandPayload, tool_command_payload, tool_command_response},
|
||||
response::{ToolCommandPayload, sdk_tools_payload, sdk_tools_response},
|
||||
};
|
||||
use crate::{
|
||||
jsonrpc::request_id,
|
||||
protocol::{
|
||||
ContinuationSupport, ServiceContext, ToolCallCommand, ToolsCommand, ToolsOutcome,
|
||||
handle_tools_command,
|
||||
},
|
||||
protocol::{ContinuationSupport, ServiceContext, ToolCallCommand, ToolsCommand},
|
||||
transport::{ResponseMode, sse_response},
|
||||
};
|
||||
|
||||
@@ -84,7 +82,18 @@ impl ProgressToken {
|
||||
}
|
||||
}
|
||||
|
||||
fn as_value(&self) -> Value {
|
||||
#[cfg(test)]
|
||||
pub(super) fn sdk_token(&self) -> rmcp::model::NumberOrString {
|
||||
match self {
|
||||
Self::Text(text) => rmcp::model::NumberOrString::String(text.clone().into()),
|
||||
Self::Number(number) => number.as_i64().map_or_else(
|
||||
|| rmcp::model::NumberOrString::String(number.to_string().into()),
|
||||
rmcp::model::NumberOrString::Number,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn raw(&self) -> Value {
|
||||
match self {
|
||||
Self::Text(text) => Value::String(text.clone()),
|
||||
Self::Number(number) => Value::Number(number.clone()),
|
||||
@@ -131,18 +140,18 @@ pub(crate) struct ProgressEvent {
|
||||
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.
|
||||
fn progress_frame(token: &ProgressToken, event: &ProgressEvent) -> Value {
|
||||
json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": "notifications/progress",
|
||||
"params": {
|
||||
"progressToken": token.as_value(),
|
||||
"progress": event.progress,
|
||||
"message": event.message,
|
||||
}
|
||||
})
|
||||
#[cfg(test)]
|
||||
impl ProgressEvent {
|
||||
pub(super) fn sdk_params(
|
||||
&self,
|
||||
token: &ProgressToken,
|
||||
) -> rmcp::model::ProgressNotificationParam {
|
||||
rmcp::model::ProgressNotificationParam::new(
|
||||
rmcp::model::ProgressToken(token.sdk_token()),
|
||||
self.progress as f64,
|
||||
)
|
||||
.with_message(self.message)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -381,7 +390,7 @@ pub(crate) async fn run_tool_call(
|
||||
});
|
||||
|
||||
let message = wire.message.clone();
|
||||
let correlation = request.correlation.clone();
|
||||
|
||||
let protocol_version = wire.protocol_version.clone();
|
||||
let response_mode = wire.response_mode;
|
||||
|
||||
@@ -399,13 +408,7 @@ pub(crate) async fn run_tool_call(
|
||||
outcome = &mut call => outcome,
|
||||
_ = lifecycle.cancelled() => return cancelled_response(),
|
||||
};
|
||||
return tool_command_response(
|
||||
&message,
|
||||
response_mode,
|
||||
&protocol_version,
|
||||
&correlation,
|
||||
outcome,
|
||||
);
|
||||
return sdk_tools_response(&message, response_mode, &protocol_version, outcome);
|
||||
};
|
||||
|
||||
debug_assert!(matches!(response_mode, ResponseMode::Sse));
|
||||
@@ -415,10 +418,10 @@ pub(crate) async fn run_tool_call(
|
||||
call,
|
||||
events: progress_events,
|
||||
cancel: lifecycle.cancel.clone(),
|
||||
token,
|
||||
peer: Some(super::rmcp_progress_peer::ProgressPeer::start(token.raw())),
|
||||
outcome: None,
|
||||
message,
|
||||
protocol_version: protocol_version.clone(),
|
||||
correlation,
|
||||
pending: VecDeque::new(),
|
||||
finished: false,
|
||||
logging: crate::protocol::carry_scope(),
|
||||
@@ -439,7 +442,7 @@ async fn call_future(
|
||||
request: ToolCallRequest,
|
||||
lifecycle: ToolCallLifecycle,
|
||||
command: ToolCallCommand,
|
||||
) -> ToolsOutcome {
|
||||
) -> super::rmcp_tools_runtime::Outcome {
|
||||
handle_tools_command(
|
||||
&state,
|
||||
ServiceContext {
|
||||
@@ -466,13 +469,13 @@ fn cancelled_response() -> Response {
|
||||
}
|
||||
|
||||
struct CallStream {
|
||||
call: Pin<Box<dyn Future<Output = ToolsOutcome> + Send>>,
|
||||
call: Pin<Box<dyn Future<Output = super::rmcp_tools_runtime::Outcome> + Send>>,
|
||||
events: mpsc::Receiver<ProgressEvent>,
|
||||
cancel: CancelSignal,
|
||||
token: ProgressToken,
|
||||
peer: Option<super::rmcp_progress_peer::ProgressPeer>,
|
||||
outcome: Option<super::rmcp_tools_runtime::Outcome>,
|
||||
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
|
||||
@@ -484,7 +487,7 @@ struct CallStream {
|
||||
|
||||
enum Step {
|
||||
Progress(ProgressEvent),
|
||||
Outcome(ToolsOutcome),
|
||||
Outcome(super::rmcp_tools_runtime::Outcome),
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
@@ -507,60 +510,101 @@ async fn next_event_in_scope(
|
||||
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,
|
||||
}
|
||||
};
|
||||
loop {
|
||||
let step = if let Some(outcome) = call.outcome.take() {
|
||||
Step::Outcome(outcome)
|
||||
} else {
|
||||
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() {
|
||||
match step {
|
||||
Step::Progress(event) => {
|
||||
let Some(peer) = &mut call.peer else { continue };
|
||||
// One send in flight; keep driving the original domain future/deadline, never a task.
|
||||
let delivery = {
|
||||
let notify = peer.notify(event.progress, event.message);
|
||||
tokio::pin!(notify);
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased;
|
||||
result = &mut notify => break Some(result),
|
||||
outcome = &mut call.call, if call.outcome.is_none() => {
|
||||
let expired = is_deadline(&outcome);
|
||||
call.outcome = Some(outcome);
|
||||
if expired { break None; }
|
||||
}
|
||||
_ = call.cancel.cancelled() => return None,
|
||||
}
|
||||
}
|
||||
};
|
||||
match delivery {
|
||||
Some(Ok(frame)) => {
|
||||
return Some((Ok(Event::default().data(frame.to_string())), call));
|
||||
}
|
||||
// Progress is optional: no retry, handwritten fallback or replay of the Tool.
|
||||
Some(Err(_)) | None => {
|
||||
if let Some(peer) = call.peer.take() {
|
||||
peer.finish().await;
|
||||
}
|
||||
call.events.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Step::Outcome(outcome) => {
|
||||
call.finished = true;
|
||||
if let Some(peer) = call.peer.take() {
|
||||
peer.finish().await;
|
||||
}
|
||||
let payload =
|
||||
match sdk_tools_payload(&call.message, &call.protocol_version, 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(notification.to_string()));
|
||||
.push_back(Event::default().data(payload.to_string()));
|
||||
return Some((Ok(call.pending.pop_front().expect("queued frame")), call));
|
||||
}
|
||||
Step::Cancelled => {
|
||||
call.finished = true;
|
||||
call.pending.clear();
|
||||
return None;
|
||||
}
|
||||
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 is_deadline(outcome: &super::rmcp_tools_runtime::Outcome) -> bool {
|
||||
matches!(outcome, super::rmcp_tools_runtime::Outcome::Call(result)
|
||||
if result.structured_content.as_ref().is_some_and(|value|
|
||||
value["error"]["code"] == "upstream_timeout"))
|
||||
}
|
||||
|
||||
fn jsonrpc_internal_error(message: &Value) -> Value {
|
||||
crate::jsonrpc::jsonrpc_error(request_id(message), -32603, "internal server error")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "rmcp_progress_stream_tests.rs"]
|
||||
mod stream_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "lifecycle_tests.rs"]
|
||||
mod tests;
|
||||
@@ -2,11 +2,46 @@
|
||||
//! progress queue, the pinned frame shape and the ownership of the in-flight cancellation table.
|
||||
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn token(value: Value) -> Option<ProgressToken> {
|
||||
progress_token(&json!({ "_meta": { "progressToken": value } }))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async 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 mut peer = super::super::rmcp_progress_peer::ProgressPeer::start(value.clone());
|
||||
let frame = peer.notify(progress, "execution_dispatched").await.unwrap();
|
||||
peer.finish().await;
|
||||
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!(
|
||||
@@ -85,14 +120,15 @@ async fn without_a_token_no_progress_is_produced_at_all() {
|
||||
/// 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() {
|
||||
#[tokio::test]
|
||||
async fn the_progress_frame_matches_the_pinned_notification_shape() {
|
||||
let event = ProgressEvent {
|
||||
progress: 2,
|
||||
message: Phase::Authorized.message(),
|
||||
};
|
||||
let mut peer = super::super::rmcp_progress_peer::ProgressPeer::start(json!("tok-2"));
|
||||
assert_eq!(
|
||||
progress_frame(&ProgressToken::Text("tok-2".to_owned()), &event),
|
||||
peer.notify(event.progress, event.message).await.unwrap(),
|
||||
json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": "notifications/progress",
|
||||
@@ -103,10 +139,13 @@ fn the_progress_frame_matches_the_pinned_notification_shape() {
|
||||
}
|
||||
})
|
||||
);
|
||||
peer.finish().await;
|
||||
let mut peer = super::super::rmcp_progress_peer::ProgressPeer::start(json!(7));
|
||||
assert_eq!(
|
||||
progress_frame(&ProgressToken::Number(7.into()), &event)["params"]["progressToken"],
|
||||
peer.notify(event.progress, event.message).await.unwrap()["params"]["progressToken"],
|
||||
json!(7)
|
||||
);
|
||||
peer.finish().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -1,14 +1,6 @@
|
||||
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,
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
use axum::{http::StatusCode, response::Response};
|
||||
#[cfg(test)]
|
||||
use rmcp::model::{CacheScope, ListToolsResult, Tool};
|
||||
use rmcp::model::{CallToolResult, ContentBlock, ResultType};
|
||||
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},
|
||||
};
|
||||
|
||||
use crate::jsonrpc::jsonrpc_error;
|
||||
#[cfg(test)]
|
||||
use crate::protocol::{ToolCallOutcome, ToolsOutcome};
|
||||
#[cfg(test)]
|
||||
use crate::tool_error::{control_tool_error_contract, execution_locale};
|
||||
#[cfg(test)]
|
||||
use crank_core::CorrelationContext;
|
||||
|
||||
pub(crate) fn take_confirmation_token(arguments: &mut Value) -> Option<String> {
|
||||
@@ -25,27 +30,13 @@ pub(crate) fn take_confirmation_token(arguments: &mut Value) -> Option<String> {
|
||||
|
||||
/// 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).
|
||||
#[cfg(test)]
|
||||
pub(super) fn success_tool_payload(
|
||||
message: &Value,
|
||||
protocol_version: &str,
|
||||
output: Value,
|
||||
) -> Value {
|
||||
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
|
||||
}),
|
||||
),
|
||||
)
|
||||
call_result_payload(message, protocol_version, sdk_success_tool_result(output))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -64,90 +55,148 @@ pub(crate) fn tool_error_response(
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(super) fn tool_error_payload(
|
||||
message: &Value,
|
||||
protocol_version: &str,
|
||||
error: ToolErrorContract,
|
||||
) -> Value {
|
||||
let error_message = tool_error_text(&error);
|
||||
let error_value = tool_error_value(&error);
|
||||
|
||||
jsonrpc_result(
|
||||
request_id(message),
|
||||
project_modern_result(
|
||||
protocol_version,
|
||||
json!({
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": error_message
|
||||
}
|
||||
],
|
||||
"structuredContent": {
|
||||
"error": error_value
|
||||
},
|
||||
"isError": true
|
||||
}),
|
||||
),
|
||||
)
|
||||
call_result_payload(message, protocol_version, sdk_error_tool_result(error))
|
||||
}
|
||||
|
||||
/// 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 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)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
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.
|
||||
#[cfg(test)]
|
||||
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 (result, extras) =
|
||||
super::rmcp_resources_projection::list(&resources, next_cursor, version)?;
|
||||
let result = super::rmcp_resources_projection::serialize(result, extras, "resources")?;
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_resources_list_result_serialized();
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(super) fn resource_read_result(
|
||||
contents: Value,
|
||||
version: &str,
|
||||
) -> Result<Value, serde_json::Error> {
|
||||
let (result, extras) = super::rmcp_resources_projection::read(&contents, version)?;
|
||||
let result = super::rmcp_resources_projection::serialize(result, extras, "contents")?;
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_resources_read_result_serialized();
|
||||
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 (result, extras) =
|
||||
super::rmcp_resources_projection::templates(&templates, next_cursor, version)?;
|
||||
let result = super::rmcp_resources_projection::serialize(result, extras, "resourceTemplates")?;
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_resource_templates_list_result_serialized();
|
||||
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);
|
||||
}
|
||||
result
|
||||
/// 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, extras) = super::rmcp_prompts_projection::list(&prompts, next_cursor)?;
|
||||
let result = super::rmcp_prompts_projection::serialize(result, extras, "prompts")?;
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_prompts_list_result_serialized();
|
||||
Ok(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);
|
||||
}
|
||||
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 (result, extras) = super::rmcp_prompts_projection::get(description, &messages)?;
|
||||
let result = super::rmcp_prompts_projection::serialize(result, extras, "messages")?;
|
||||
#[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
|
||||
@@ -158,14 +207,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`
|
||||
@@ -212,6 +265,7 @@ pub(super) enum ToolCommandPayload {
|
||||
/// 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.
|
||||
#[cfg(test)]
|
||||
pub(super) fn tool_command_payload(
|
||||
message: &Value,
|
||||
protocol_version: &str,
|
||||
@@ -256,22 +310,53 @@ pub(super) fn tool_command_payload(
|
||||
}
|
||||
}
|
||||
|
||||
/// The single-response form of [`tool_command_payload`].
|
||||
pub(super) fn tool_command_response(
|
||||
pub(super) fn sdk_tools_payload(
|
||||
message: &Value,
|
||||
response_mode: ResponseMode,
|
||||
protocol_version: &str,
|
||||
correlation: &CorrelationContext,
|
||||
outcome: ToolsOutcome,
|
||||
version: &str,
|
||||
outcome: super::rmcp_tools_runtime::Outcome,
|
||||
) -> ToolCommandPayload {
|
||||
use super::rmcp_tools_runtime::Outcome;
|
||||
let payload = match outcome {
|
||||
Outcome::List { result, extras } => {
|
||||
match super::rmcp_tools_projection::serialize_list(result, extras, version) {
|
||||
Ok(result) => jsonrpc_result(request_id(message), result),
|
||||
Err(_) => return ToolCommandPayload::Internal("invalid tool catalog"),
|
||||
}
|
||||
}
|
||||
Outcome::Call(result) => call_result_payload(message, version, result),
|
||||
Outcome::Error(error) => sdk_tools_error_payload(message, error),
|
||||
};
|
||||
ToolCommandPayload::Ok(payload)
|
||||
}
|
||||
pub(super) fn sdk_tools_error_payload(message: &Value, error: rmcp::ErrorData) -> Value {
|
||||
let mut payload = jsonrpc_error(
|
||||
request_id(message),
|
||||
i64::from(error.code.0),
|
||||
error.message.to_string(),
|
||||
);
|
||||
if let Some(data) = error.data {
|
||||
payload["error"]["data"] = data;
|
||||
}
|
||||
payload
|
||||
}
|
||||
pub(super) fn sdk_tools_response(
|
||||
message: &Value,
|
||||
mode: ResponseMode,
|
||||
version: &str,
|
||||
outcome: super::rmcp_tools_runtime::Outcome,
|
||||
) -> Response {
|
||||
match tool_command_payload(message, protocol_version, correlation, outcome) {
|
||||
ToolCommandPayload::Ok(payload) => transport_response(
|
||||
StatusCode::OK,
|
||||
let internal = matches!(&outcome, super::rmcp_tools_runtime::Outcome::Error(error) if error.code == rmcp::model::ErrorCode::INTERNAL_ERROR);
|
||||
match sdk_tools_payload(message, version, outcome) {
|
||||
ToolCommandPayload::Ok(payload) if internal => transport_response(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
payload,
|
||||
response_mode,
|
||||
ResponseMode::Json,
|
||||
None,
|
||||
None,
|
||||
Some(protocol_version),
|
||||
),
|
||||
ToolCommandPayload::Ok(payload) => {
|
||||
transport_response(StatusCode::OK, payload, mode, None, Some(version))
|
||||
}
|
||||
ToolCommandPayload::Internal(detail) => internal_jsonrpc_error(message, detail),
|
||||
}
|
||||
}
|
||||
@@ -310,3 +395,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,215 @@
|
||||
//! Request-owned SDK completion handler; the domain future remains in its caller.
|
||||
use std::sync::Arc;
|
||||
|
||||
use crank_core::{CorrelationContext, ExecutionLocale};
|
||||
use rmcp::{ErrorData, RoleServer, ServerHandler, model::*, service::RequestContext};
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
use super::AppState;
|
||||
use crate::{
|
||||
auth::VerifiedMachineCredential,
|
||||
protocol::{self, CompletionCommand, CompletionOutcome, CompletionReference, ServiceContext},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Scope {
|
||||
workspace: String,
|
||||
agent: String,
|
||||
credential: VerifiedMachineCredential,
|
||||
correlation: CorrelationContext,
|
||||
continuation: protocol::ContinuationSupport,
|
||||
locale: ExecutionLocale,
|
||||
revision: String,
|
||||
context: Option<Map<String, Value>>,
|
||||
}
|
||||
impl Scope {
|
||||
fn service(&self) -> ServiceContext<'_> {
|
||||
ServiceContext {
|
||||
workspace_slug: &self.workspace,
|
||||
agent_slug: &self.agent,
|
||||
credential: &self.credential,
|
||||
correlation: &self.correlation,
|
||||
continuation: self.continuation,
|
||||
locale: self.locale,
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Clone)]
|
||||
struct CompletionHandler {
|
||||
state: Arc<AppState>,
|
||||
#[cfg(test)]
|
||||
domain: Option<Arc<tests::Domain>>,
|
||||
}
|
||||
fn internal() -> ErrorData {
|
||||
ErrorData::internal_error("completion handler unavailable", None)
|
||||
}
|
||||
impl CompletionHandler {
|
||||
async fn domain(&self, scope: &Scope, command: CompletionCommand) -> CompletionOutcome {
|
||||
#[cfg(test)]
|
||||
if let Some(domain) = &self.domain {
|
||||
return domain.complete(command).await;
|
||||
}
|
||||
protocol::handle_completion_command(&self.state, scope.service(), &scope.revision, command)
|
||||
.await
|
||||
}
|
||||
fn scope(context: &RequestContext<RoleServer>) -> Result<Scope, ErrorData> {
|
||||
context
|
||||
.extensions
|
||||
.get::<Scope>()
|
||||
.cloned()
|
||||
.ok_or_else(internal)
|
||||
}
|
||||
async fn execute(
|
||||
&self,
|
||||
scope: &Scope,
|
||||
params: CompleteRequestParams,
|
||||
) -> Result<CompleteResult, ErrorData> {
|
||||
let reference = match params.r#ref {
|
||||
Reference::Prompt(prompt) => CompletionReference::Prompt { name: prompt.name },
|
||||
Reference::Resource(resource) => CompletionReference::Resource { uri: resource.uri },
|
||||
_ => {
|
||||
return Err(ErrorData::invalid_params(
|
||||
"invalid completion/complete parameters",
|
||||
None,
|
||||
));
|
||||
}
|
||||
};
|
||||
// SDK context only accepts strings; retain the original JSON for domain validation.
|
||||
let context = scope
|
||||
.context
|
||||
.clone()
|
||||
.map(|arguments| serde_json::from_value(serde_json::json!({"arguments": arguments})))
|
||||
.transpose()
|
||||
.map_err(|_| internal())?;
|
||||
let command = CompletionCommand {
|
||||
reference,
|
||||
argument: crate::protocol::CompletionArgument {
|
||||
name: params.argument.name,
|
||||
value: params.argument.value,
|
||||
},
|
||||
context,
|
||||
};
|
||||
match self.domain(scope, command).await {
|
||||
CompletionOutcome::Completed {
|
||||
values,
|
||||
total,
|
||||
has_more,
|
||||
} => {
|
||||
let mut info = CompletionInfo::new(values).map_err(|_| internal())?;
|
||||
info.total = total.and_then(|v| u32::try_from(v).ok());
|
||||
info.has_more = Some(has_more);
|
||||
Ok(CompleteResult::new(info))
|
||||
}
|
||||
CompletionOutcome::InvalidParams(detail) => {
|
||||
Err(ErrorData::invalid_params(detail, None))
|
||||
}
|
||||
CompletionOutcome::NotAvailable => Err(ErrorData::invalid_params(
|
||||
"the completion reference is not available",
|
||||
None,
|
||||
)),
|
||||
CompletionOutcome::CatalogUnavailable => Err(internal()),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ServerHandler for CompletionHandler {
|
||||
async fn complete(
|
||||
&self,
|
||||
request: CompleteRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<CompleteResult, ErrorData> {
|
||||
let scope = Self::scope(&context)?;
|
||||
let result = self.execute(&scope, request).await;
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_completion_handler_completed();
|
||||
result
|
||||
}
|
||||
}
|
||||
async fn request_context(
|
||||
handler: &CompletionHandler,
|
||||
scope: Scope,
|
||||
) -> Result<RequestContext<RoleServer>, ErrorData> {
|
||||
let ping =
|
||||
serde_json::from_value(serde_json::json!({"jsonrpc":"2.0", "id":1, "method":"ping"}))
|
||||
.map_err(|_| internal())?;
|
||||
let (transport, receiver) = rmcp::transport::OneshotTransport::<RoleServer>::new(ping);
|
||||
let revision =
|
||||
serde_json::from_value(Value::String(scope.revision.clone())).map_err(|_| internal())?;
|
||||
let info = InitializeRequestParams::new(
|
||||
ClientCapabilities::default(),
|
||||
Implementation::new("crank-completion-context", "1"),
|
||||
)
|
||||
.with_protocol_version(revision);
|
||||
let mut running = rmcp::service::serve_directly(handler.clone(), transport, Some(info));
|
||||
let peer = running.peer().clone();
|
||||
running.close().await.map_err(|_| internal())?;
|
||||
drop(receiver);
|
||||
let mut context = RequestContext::new(RequestId::Number(1), peer);
|
||||
context.extensions.insert(scope);
|
||||
Ok(context)
|
||||
}
|
||||
pub(super) async fn handle(
|
||||
state: &Arc<AppState>,
|
||||
context: ServiceContext<'_>,
|
||||
revision: &str,
|
||||
command: CompletionCommand,
|
||||
) -> CompletionOutcome {
|
||||
let (reference, context_args) = match command.reference {
|
||||
CompletionReference::Prompt { name } => (
|
||||
Reference::for_prompt(name),
|
||||
command.context.and_then(|c| c.arguments),
|
||||
),
|
||||
CompletionReference::Resource { uri } => (
|
||||
Reference::for_resource(uri),
|
||||
command.context.and_then(|c| c.arguments),
|
||||
),
|
||||
};
|
||||
let scope = Scope {
|
||||
workspace: context.workspace_slug.to_owned(),
|
||||
agent: context.agent_slug.to_owned(),
|
||||
credential: context.credential.clone(),
|
||||
correlation: context.correlation.clone(),
|
||||
continuation: context.continuation,
|
||||
locale: context.locale,
|
||||
revision: revision.to_owned(),
|
||||
context: context_args,
|
||||
};
|
||||
let handler = CompletionHandler {
|
||||
state: state.clone(),
|
||||
#[cfg(test)]
|
||||
domain: None,
|
||||
};
|
||||
let sdk_context = match request_context(&handler, scope).await {
|
||||
Ok(context) => context,
|
||||
Err(_) => return CompletionOutcome::CatalogUnavailable,
|
||||
};
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_completion_dispatch_enter();
|
||||
let params = CompleteRequestParams::new(
|
||||
reference,
|
||||
ArgumentInfo::new(command.argument.name, command.argument.value),
|
||||
);
|
||||
let result = ServerHandler::complete(&handler, params, sdk_context).await;
|
||||
restore(result)
|
||||
}
|
||||
fn restore(result: Result<CompleteResult, ErrorData>) -> CompletionOutcome {
|
||||
let outcome = match result {
|
||||
Ok(result) => CompletionOutcome::Completed {
|
||||
values: result.completion.values,
|
||||
total: result.completion.total.map(|v| v as usize),
|
||||
has_more: result.completion.has_more.unwrap_or(false),
|
||||
},
|
||||
Err(error) if error.message == "the completion reference is not available" => {
|
||||
CompletionOutcome::NotAvailable
|
||||
}
|
||||
Err(error) if error.code == ErrorCode::INVALID_PARAMS => {
|
||||
CompletionOutcome::InvalidParams("invalid completion/complete parameters")
|
||||
}
|
||||
Err(_) => CompletionOutcome::CatalogUnavailable,
|
||||
};
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_completion_handler_consumed();
|
||||
outcome
|
||||
}
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_completion_handler_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,53 @@
|
||||
//! Runtime SDK dispatch; admission, lifecycle and response projection stay with the caller.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use super::{AppState, lifecycle::ToolCallLifecycle};
|
||||
use crate::protocol::{ServiceContext, ToolsCommand};
|
||||
|
||||
pub(super) async fn handle_tools_command(
|
||||
state: &Arc<AppState>,
|
||||
context: ServiceContext<'_>,
|
||||
command: ToolsCommand,
|
||||
lifecycle: Option<ToolCallLifecycle>,
|
||||
) -> super::rmcp_tools_runtime::Outcome {
|
||||
super::rmcp_tools_runtime::handle(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 {
|
||||
super::rmcp_prompts::handle(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 {
|
||||
super::rmcp_resources::handle(state, context, revision, command).await
|
||||
}
|
||||
|
||||
pub(super) async fn list_resource_templates(
|
||||
state: &Arc<AppState>,
|
||||
context: ServiceContext<'_>,
|
||||
revision: &str,
|
||||
command: crate::protocol::ResourcesListCommand,
|
||||
) -> Result<(Vec<Value>, Option<String>), crate::protocol::ResourceTemplatesListError> {
|
||||
super::rmcp_resources::templates(state, context, revision, command).await
|
||||
}
|
||||
|
||||
pub(super) async fn handle_completion_command(
|
||||
state: &Arc<AppState>,
|
||||
context: ServiceContext<'_>,
|
||||
revision: &str,
|
||||
command: crate::protocol::CompletionCommand,
|
||||
) -> crate::protocol::CompletionOutcome {
|
||||
super::rmcp_completion::handle(state, context, revision, command).await
|
||||
}
|
||||
@@ -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,107 @@
|
||||
//! Request-owned SDK implementation of the legacy `logging/setLevel` handler.
|
||||
// The pinned legacy revision still requires this deprecated SDK method.
|
||||
#![allow(deprecated)]
|
||||
use super::AppState;
|
||||
use crate::protocol::{self, LogLevel};
|
||||
use rmcp::{ErrorData, RoleServer, ServerHandler, model::*, service::RequestContext};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct LoggingHandler {
|
||||
store: Arc<protocol::LogLevelStore>,
|
||||
session_id: String,
|
||||
}
|
||||
|
||||
impl ServerHandler for LoggingHandler {
|
||||
async fn set_level(
|
||||
&self,
|
||||
request: SetLevelRequestParams,
|
||||
_context: RequestContext<RoleServer>,
|
||||
) -> Result<(), ErrorData> {
|
||||
#[allow(deprecated)]
|
||||
let level: LogLevel = request.level.into();
|
||||
self.store.set(&self.session_id, level);
|
||||
protocol::set_active_level(Some(level));
|
||||
protocol::note(LogLevel::Notice, protocol::EVENT_LEVEL_APPLIED, "applied");
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_logging_handler_completed();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn request_context() -> Result<RequestContext<RoleServer>, ErrorData> {
|
||||
let ping: ClientJsonRpcMessage = serde_json::from_value(serde_json::json!({
|
||||
"jsonrpc":"2.0", "id":1, "method":"ping"
|
||||
}))
|
||||
.map_err(|_| ErrorData::internal_error("logging handler unavailable", None))?;
|
||||
let (transport, receiver) = rmcp::transport::OneshotTransport::<RoleServer>::new(ping);
|
||||
let info = InitializeRequestParams::new(
|
||||
ClientCapabilities::default(),
|
||||
Implementation::new("crank-logging-context", "1"),
|
||||
);
|
||||
#[derive(Clone)]
|
||||
struct Bootstrap;
|
||||
impl ServerHandler for Bootstrap {}
|
||||
let mut running = rmcp::service::serve_directly(Bootstrap, transport, Some(info));
|
||||
let peer = running.peer().clone();
|
||||
running
|
||||
.close()
|
||||
.await
|
||||
.map_err(|_| ErrorData::internal_error("logging handler unavailable", None))?;
|
||||
drop(receiver);
|
||||
Ok(RequestContext::new(RequestId::Number(1), peer))
|
||||
}
|
||||
|
||||
pub(super) async fn set_level(
|
||||
state: &Arc<AppState>,
|
||||
session_id: &str,
|
||||
request: SetLevelRequestParams,
|
||||
) -> Result<(), ErrorData> {
|
||||
let handler = LoggingHandler {
|
||||
store: Arc::clone(&state.logging_levels),
|
||||
session_id: session_id.to_owned(),
|
||||
};
|
||||
let context = request_context().await?;
|
||||
invoke(&handler, request, context).await
|
||||
}
|
||||
|
||||
async fn invoke(
|
||||
handler: &impl ServerHandler,
|
||||
request: SetLevelRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<(), ErrorData> {
|
||||
ServerHandler::set_level(handler, request, context).await
|
||||
}
|
||||
|
||||
pub(super) fn response(
|
||||
message: &serde_json::Value,
|
||||
mode: crate::transport::ResponseMode,
|
||||
revision: &str,
|
||||
result: Result<(), ErrorData>,
|
||||
) -> axum::response::Response {
|
||||
match result {
|
||||
Ok(()) => {
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_logging_handler_consumed();
|
||||
crate::transport::transport_response(
|
||||
axum::http::StatusCode::OK,
|
||||
crate::jsonrpc::jsonrpc_result(
|
||||
crate::jsonrpc::request_id(message),
|
||||
serde_json::json!({}),
|
||||
),
|
||||
mode,
|
||||
None,
|
||||
Some(revision),
|
||||
)
|
||||
}
|
||||
Err(error) => {
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_logging_handler_consumed();
|
||||
super::dispatch::sdk_error_response(message, mode, revision, error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_logging_handler_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,176 @@
|
||||
//! Request-owned SDK handlers for the existing HTTP negotiation and ping routes.
|
||||
use std::borrow::Cow;
|
||||
|
||||
use rmcp::{ErrorData, RoleServer, ServerHandler, model::*, service::RequestContext};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::{jsonrpc::SUPPORTED_PROTOCOL_VERSIONS, protocol::advertised_server_capabilities};
|
||||
|
||||
const SERVER_NAME: &str = "crank-mcp-server";
|
||||
const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
#[derive(Clone)]
|
||||
struct NegotiationHandler;
|
||||
|
||||
impl ServerHandler for NegotiationHandler {
|
||||
fn supported_protocol_versions(&self) -> Cow<'static, [ProtocolVersion]> {
|
||||
Cow::Owned(
|
||||
SUPPORTED_PROTOCOL_VERSIONS
|
||||
.iter()
|
||||
.map(|v| version(v))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
async fn initialize(
|
||||
&self,
|
||||
request: InitializeRequestParams,
|
||||
_context: RequestContext<RoleServer>,
|
||||
) -> Result<InitializeResult, ErrorData> {
|
||||
let revision = request.protocol_version.as_str();
|
||||
if !SUPPORTED_PROTOCOL_VERSIONS.contains(&revision) {
|
||||
return Err(ErrorData::new(
|
||||
ErrorCode(-32602),
|
||||
format!("unsupported protocol version {revision}"),
|
||||
None,
|
||||
));
|
||||
}
|
||||
let caps = capabilities(&advertised_server_capabilities(revision));
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_initialize_handler_completed();
|
||||
Ok(InitializeResult::new(caps)
|
||||
.with_protocol_version(version(revision))
|
||||
.with_server_info(Implementation::new(SERVER_NAME, SERVER_VERSION)))
|
||||
}
|
||||
|
||||
async fn discover(
|
||||
&self,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<DiscoverResult, ErrorData> {
|
||||
let revision = context
|
||||
.extensions
|
||||
.get::<ProtocolVersion>()
|
||||
.ok_or_else(|| ErrorData::internal_error("discovery revision unavailable", None))?;
|
||||
let revision = revision.as_str();
|
||||
let result = DiscoverResult::new(
|
||||
SUPPORTED_PROTOCOL_VERSIONS
|
||||
.iter()
|
||||
.map(|v| version(v))
|
||||
.collect(),
|
||||
capabilities(&advertised_server_capabilities(revision)),
|
||||
)
|
||||
.with_server_info(Implementation::new(SERVER_NAME, SERVER_VERSION))
|
||||
.with_ttl_ms(0)
|
||||
.with_cache_scope(CacheScope::Private);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_discover_handler_completed();
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
async fn ping(&self, _context: RequestContext<RoleServer>) -> Result<(), ErrorData> {
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_ping_handler_completed();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn version(revision: &str) -> ProtocolVersion {
|
||||
serde_json::from_value(Value::String(revision.to_owned()))
|
||||
.expect("registered revisions are valid SDK versions")
|
||||
}
|
||||
|
||||
fn capabilities(value: &Value) -> ServerCapabilities {
|
||||
// Registry capabilities are the allowlist; the SDK must not supply defaults.
|
||||
serde_json::from_value(value.clone()).expect("registered capabilities match SDK shapes")
|
||||
}
|
||||
|
||||
async fn request_context() -> Result<RequestContext<RoleServer>, ErrorData> {
|
||||
let ping: rmcp::model::ClientJsonRpcMessage =
|
||||
serde_json::from_value(json!({"jsonrpc":"2.0", "id":1, "method":"ping"}))
|
||||
.map_err(|_| ErrorData::internal_error("negotiation context unavailable", None))?;
|
||||
let (transport, receiver) = rmcp::transport::OneshotTransport::<RoleServer>::new(ping);
|
||||
let info = InitializeRequestParams::new(
|
||||
ClientCapabilities::default(),
|
||||
Implementation::new("crank-negotiation-context", "1"),
|
||||
);
|
||||
// Bootstrap only obtains a closed Peer; it must never run the request handler.
|
||||
#[derive(Clone)]
|
||||
struct Bootstrap;
|
||||
impl ServerHandler for Bootstrap {}
|
||||
let mut running = rmcp::service::serve_directly(Bootstrap, transport, Some(info));
|
||||
let peer = running.peer().clone();
|
||||
running
|
||||
.close()
|
||||
.await
|
||||
.map_err(|_| ErrorData::internal_error("negotiation handler unavailable", None))?;
|
||||
drop(receiver);
|
||||
Ok(RequestContext::new(RequestId::Number(1), peer))
|
||||
}
|
||||
|
||||
pub(super) async fn initialize(revision: &str) -> Result<Value, ErrorData> {
|
||||
initialize_with(&NegotiationHandler, revision).await
|
||||
}
|
||||
|
||||
async fn initialize_with(handler: &impl ServerHandler, revision: &str) -> Result<Value, ErrorData> {
|
||||
let context = request_context().await?;
|
||||
let params =
|
||||
InitializeRequestParams::new(ClientCapabilities::default(), Implementation::new("", ""))
|
||||
.with_protocol_version(version(revision));
|
||||
let result = ServerHandler::initialize(handler, params, context).await?;
|
||||
let mut value = serde_json::to_value(result)
|
||||
.map_err(|_| ErrorData::internal_error("initialize result unavailable", None))?;
|
||||
restore_capabilities(&mut value, revision);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_initialize_handler_consumed();
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub(super) async fn discover(revision: &str) -> Result<Value, ErrorData> {
|
||||
discover_with(&NegotiationHandler, revision).await
|
||||
}
|
||||
|
||||
async fn discover_with(handler: &impl ServerHandler, revision: &str) -> Result<Value, ErrorData> {
|
||||
let mut context = request_context().await?;
|
||||
context.extensions.insert(version(revision));
|
||||
let result = ServerHandler::discover(handler, context).await?;
|
||||
let mut value = serde_json::to_value(result)
|
||||
.map_err(|_| ErrorData::internal_error("discover result unavailable", None))?;
|
||||
restore_capabilities(&mut value, revision);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_discover_handler_consumed();
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub(super) async fn ping() -> Result<(), ErrorData> {
|
||||
ping_with(&NegotiationHandler).await
|
||||
}
|
||||
|
||||
async fn ping_with(handler: &impl ServerHandler) -> Result<(), ErrorData> {
|
||||
let context = request_context().await?;
|
||||
let result = ServerHandler::ping(handler, context).await;
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_ping_handler_consumed();
|
||||
result
|
||||
}
|
||||
|
||||
fn restore_capabilities(result: &mut Value, revision: &str) {
|
||||
let advertised = advertised_server_capabilities(revision);
|
||||
let represented =
|
||||
serde_json::to_value(capabilities(&advertised)).expect("SDK capabilities serialize");
|
||||
let caps = result["capabilities"]
|
||||
.as_object_mut()
|
||||
.expect("SDK capabilities serialize as object");
|
||||
for (key, value) in advertised
|
||||
.as_object()
|
||||
.expect("registry capabilities are object")
|
||||
{
|
||||
// Restore only fields the SDK cannot represent, not removed typed capabilities.
|
||||
if represented.get(key).is_none() {
|
||||
caps.entry(key.clone()).or_insert_with(|| value.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_negotiation_handler_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,237 @@
|
||||
//! Request-owned SDK execution for legacy notifications/initialized and notifications/cancelled.
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use axum::http::StatusCode;
|
||||
use rmcp::{RoleServer, ServerHandler, model::*, service::NotificationContext};
|
||||
use serde_json::{Map, Value};
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
use super::{AgentRoutePath, AppState};
|
||||
use crate::auth::VerifiedMachineCredential;
|
||||
|
||||
struct OutcomeHandler {
|
||||
operation: Operation,
|
||||
outcome: Mutex<Option<StatusCode>>,
|
||||
}
|
||||
|
||||
enum Operation {
|
||||
Initialized {
|
||||
state: Arc<AppState>,
|
||||
session_id: String,
|
||||
},
|
||||
Cancelled {
|
||||
state: Arc<AppState>,
|
||||
path: AgentRoutePath,
|
||||
credential: VerifiedMachineCredential,
|
||||
session_id: Option<String>,
|
||||
request_id: Option<Value>,
|
||||
},
|
||||
}
|
||||
|
||||
impl OutcomeHandler {
|
||||
fn publish(&self, status: StatusCode) {
|
||||
if let Ok(mut outcome) = self.outcome.lock() {
|
||||
*outcome = Some(status);
|
||||
}
|
||||
}
|
||||
|
||||
fn take(&self) -> Option<StatusCode> {
|
||||
self.outcome.lock().ok()?.take()
|
||||
}
|
||||
}
|
||||
|
||||
impl ServerHandler for OutcomeHandler {
|
||||
async fn on_initialized(&self, _context: NotificationContext<RoleServer>) {
|
||||
if let Operation::Initialized { state, session_id } = &self.operation {
|
||||
let status = match state
|
||||
.sessions
|
||||
.mark_initialized(session_id, time::OffsetDateTime::now_utc())
|
||||
.await
|
||||
{
|
||||
Ok(true) => StatusCode::ACCEPTED,
|
||||
Ok(false) => StatusCode::NOT_FOUND,
|
||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
};
|
||||
self.publish(status);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_initialized_handler_completed();
|
||||
}
|
||||
}
|
||||
|
||||
async fn on_cancelled(
|
||||
&self,
|
||||
params: CancelledNotificationParam,
|
||||
_context: NotificationContext<RoleServer>,
|
||||
) {
|
||||
if let Operation::Cancelled {
|
||||
state,
|
||||
path,
|
||||
credential,
|
||||
session_id,
|
||||
request_id,
|
||||
} = &self.operation
|
||||
{
|
||||
let target = params.request_id.map(|id| {
|
||||
let typed = id.into_json_value();
|
||||
match (request_id, typed) {
|
||||
(Some(Value::Number(original)), Value::String(projected))
|
||||
if original.to_string() == projected =>
|
||||
{
|
||||
Value::Number(original.clone())
|
||||
}
|
||||
(_, typed) => typed,
|
||||
}
|
||||
});
|
||||
if let (Some(target), Some(session_id)) = (target, session_id.as_ref()) {
|
||||
let owned = 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 {
|
||||
state.in_flight.cancel(
|
||||
&crate::app::lifecycle::InFlightScope {
|
||||
session_id: session_id.clone(),
|
||||
owner: credential
|
||||
.platform_api_key_id
|
||||
.as_ref()
|
||||
.map_or_else(String::new, |id| id.as_str().to_owned()),
|
||||
},
|
||||
&target,
|
||||
);
|
||||
}
|
||||
}
|
||||
self.publish(StatusCode::ACCEPTED);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_cancelled_handler_completed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Capture(Mutex<Option<oneshot::Sender<NotificationContext<RoleServer>>>>);
|
||||
|
||||
impl ServerHandler for Capture {
|
||||
async fn on_initialized(&self, context: NotificationContext<RoleServer>) {
|
||||
if let Ok(mut sender) = self.0.lock()
|
||||
&& let Some(sender) = sender.take()
|
||||
{
|
||||
let _ = sender.send(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn notification_context() -> Result<NotificationContext<RoleServer>, ()> {
|
||||
let message: ClientJsonRpcMessage = serde_json::from_value(serde_json::json!({
|
||||
"jsonrpc":"2.0", "method":"notifications/initialized"
|
||||
}))
|
||||
.map_err(|_| ())?;
|
||||
let (transport, receiver) = rmcp::transport::OneshotTransport::<RoleServer>::new(message);
|
||||
let (sender, captured) = oneshot::channel();
|
||||
let info = InitializeRequestParams::new(
|
||||
ClientCapabilities::default(),
|
||||
Implementation::new("crank-internal", "1"),
|
||||
);
|
||||
let mut running =
|
||||
rmcp::service::serve_directly(Capture(Mutex::new(Some(sender))), transport, Some(info));
|
||||
let context = captured.await;
|
||||
let closed = running.close().await.is_ok();
|
||||
drop(receiver);
|
||||
if !closed {
|
||||
return Err(());
|
||||
}
|
||||
context.map_err(|_| ())
|
||||
}
|
||||
|
||||
pub(super) async fn initialized(state: Arc<AppState>, session_id: String) -> StatusCode {
|
||||
let handler = OutcomeHandler {
|
||||
operation: Operation::Initialized { state, session_id },
|
||||
outcome: Mutex::new(None),
|
||||
};
|
||||
initialized_with(&handler, &handler, notification_context()).await
|
||||
}
|
||||
|
||||
async fn initialized_with(
|
||||
sdk: &impl ServerHandler,
|
||||
outcome: &OutcomeHandler,
|
||||
context: impl std::future::Future<Output = Result<NotificationContext<RoleServer>, ()>>,
|
||||
) -> StatusCode {
|
||||
let Ok(context) = context.await else {
|
||||
return StatusCode::INTERNAL_SERVER_ERROR;
|
||||
};
|
||||
ServerHandler::on_initialized(sdk, context).await;
|
||||
let status = outcome.take();
|
||||
#[cfg(test)]
|
||||
if status.is_some() {
|
||||
super::dispatch_observer::sdk_initialized_handler_consumed();
|
||||
}
|
||||
status.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
|
||||
// Retain the SDK's typed interpretation and the exact original number used by its safe projection.
|
||||
pub(super) fn cancelled_params(
|
||||
value: &Value,
|
||||
) -> Option<(CancelledNotificationParam, Option<Value>)> {
|
||||
let original = value.get("requestId");
|
||||
let mut projected = Map::new();
|
||||
if let Some(id) = original {
|
||||
let safe = match id {
|
||||
Value::Number(number) if number.as_i64().is_none() => Value::String(number.to_string()),
|
||||
_ => id.clone(),
|
||||
};
|
||||
projected.insert("requestId".to_owned(), safe);
|
||||
}
|
||||
let typed = serde_json::from_value(Value::Object(projected)).ok()?;
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_cancelled_params_decoded();
|
||||
let raw_number = original.filter(|id| id.is_number()).cloned();
|
||||
Some((typed, raw_number))
|
||||
}
|
||||
|
||||
pub(super) async fn cancelled(
|
||||
state: Arc<AppState>,
|
||||
path: AgentRoutePath,
|
||||
credential: VerifiedMachineCredential,
|
||||
session_id: Option<String>,
|
||||
params: CancelledNotificationParam,
|
||||
raw_number: Option<Value>,
|
||||
) {
|
||||
let request_id = raw_number;
|
||||
let handler = OutcomeHandler {
|
||||
operation: Operation::Cancelled {
|
||||
state,
|
||||
path,
|
||||
credential,
|
||||
session_id,
|
||||
request_id,
|
||||
},
|
||||
outcome: Mutex::new(None),
|
||||
};
|
||||
cancelled_with(&handler, &handler, params, notification_context()).await;
|
||||
}
|
||||
|
||||
async fn cancelled_with(
|
||||
sdk: &impl ServerHandler,
|
||||
outcome: &OutcomeHandler,
|
||||
params: CancelledNotificationParam,
|
||||
context: impl std::future::Future<Output = Result<NotificationContext<RoleServer>, ()>>,
|
||||
) {
|
||||
let Ok(context) = context.await else {
|
||||
return;
|
||||
};
|
||||
// Cancellation is intentionally opaque: no outcome or bootstrap details reach the client.
|
||||
ServerHandler::on_cancelled(sdk, params, context).await;
|
||||
let status = outcome.take();
|
||||
if status.is_none() {
|
||||
tracing::warn!(name: "mcp.cancelled_handler.missing_outcome", "SDK cancellation handler did not publish an outcome");
|
||||
}
|
||||
#[cfg(test)]
|
||||
if status.is_some() {
|
||||
super::dispatch_observer::sdk_cancelled_handler_consumed();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_notification_handler_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,11 @@
|
||||
//! Test compatibility entry for the production notification decoder.
|
||||
use serde_json::Value;
|
||||
|
||||
pub(super) fn decode_cancelled_request_id(params: &Value) -> Option<Value> {
|
||||
let (typed, raw_number) = super::rmcp_notifications::cancelled_params(params)?;
|
||||
let id = typed.request_id?;
|
||||
Some(raw_number.unwrap_or_else(|| id.into_json_value()))
|
||||
}
|
||||
|
||||
#[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(¶ms).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(¶ms), None, "{params}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
//! Request-owned outbound SDK peer for progress notifications.
|
||||
use std::{
|
||||
io,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use futures_util::{sink, stream};
|
||||
use rmcp::{
|
||||
RoleServer, ServerHandler,
|
||||
model::{
|
||||
ClientJsonRpcMessage, ProgressNotificationParam, ServerJsonRpcMessage, ServerNotification,
|
||||
},
|
||||
};
|
||||
use serde_json::Value;
|
||||
use tokio::sync::{mpsc, oneshot, watch};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "rmcp_progress_peer_controls.rs"]
|
||||
pub(crate) mod controls;
|
||||
|
||||
const PROGRESS_TOKEN: &str = "progressToken";
|
||||
// The SDK traces outbound params; client tokens must stay outside its pump.
|
||||
const SDK_PROGRESS_TOKEN: &str = "crank-internal-progress";
|
||||
|
||||
struct Bootstrap;
|
||||
impl ServerHandler for Bootstrap {}
|
||||
|
||||
struct SinkState {
|
||||
sender: mpsc::Sender<Value>,
|
||||
#[cfg(test)]
|
||||
control: Option<controls::Controls>,
|
||||
}
|
||||
|
||||
pub(super) struct ProgressPeer {
|
||||
token: Value,
|
||||
receiver: mpsc::Receiver<Value>,
|
||||
shutdown: Option<oneshot::Sender<()>>,
|
||||
owner: Option<tokio::task::JoinHandle<()>>,
|
||||
peer: watch::Receiver<Option<rmcp::service::Peer<RoleServer>>>,
|
||||
#[cfg(test)]
|
||||
controls: Option<controls::Controls>,
|
||||
progress_sidecar: Arc<Mutex<Option<u64>>>,
|
||||
}
|
||||
|
||||
impl ProgressPeer {
|
||||
pub(super) fn start(token: Value) -> Self {
|
||||
#[cfg(test)]
|
||||
let controls = controls::Controls::current();
|
||||
let (sender, receiver) = mpsc::channel(1);
|
||||
let owner_sender = sender.clone();
|
||||
let (shutdown, shutdown_rx) = oneshot::channel();
|
||||
let (peer_tx, peer) = watch::channel(None);
|
||||
let progress_sidecar = Arc::new(Mutex::new(None::<u64>));
|
||||
let sink_sidecar = progress_sidecar.clone();
|
||||
let raw_token = token.clone();
|
||||
let output = sink::unfold(
|
||||
SinkState {
|
||||
sender: sender.clone(),
|
||||
#[cfg(test)]
|
||||
control: controls.clone(),
|
||||
},
|
||||
move |state, message| {
|
||||
let progress_sidecar = sink_sidecar.clone();
|
||||
let raw_token = raw_token.clone();
|
||||
async move {
|
||||
#[cfg(test)]
|
||||
if let Some(control) = &state.control {
|
||||
control.entered();
|
||||
if control.should_fail_send() {
|
||||
return Err(io::Error::other("controlled progress send failure"));
|
||||
}
|
||||
}
|
||||
let ServerJsonRpcMessage::Notification(notification) = &message else {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"unexpected SDK progress output",
|
||||
));
|
||||
};
|
||||
let ServerNotification::ProgressNotification(_) = ¬ification.notification
|
||||
else {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"unexpected SDK progress notification",
|
||||
));
|
||||
};
|
||||
let mut actual = serde_json::to_value(&message).map_err(io::Error::other)?;
|
||||
if actual["params"][PROGRESS_TOKEN] != SDK_PROGRESS_TOKEN {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"unexpected SDK progress token",
|
||||
));
|
||||
}
|
||||
let progress = progress_sidecar
|
||||
.lock()
|
||||
.expect("progress sidecar lock")
|
||||
.take()
|
||||
.ok_or_else(|| {
|
||||
io::Error::new(io::ErrorKind::InvalidData, "missing progress sidecar")
|
||||
})?;
|
||||
actual["params"]["progress"] = Value::from(progress);
|
||||
actual["params"][PROGRESS_TOKEN] = raw_token;
|
||||
#[cfg(test)]
|
||||
if state
|
||||
.control
|
||||
.as_ref()
|
||||
.is_some_and(controls::Controls::should_mutate_message)
|
||||
{
|
||||
actual["params"]["message"] = Value::from("controlled output mutation");
|
||||
}
|
||||
state.sender.try_send(actual).map_err(|error| match error {
|
||||
mpsc::error::TrySendError::Full(_) => {
|
||||
io::Error::new(io::ErrorKind::WouldBlock, "progress output full")
|
||||
}
|
||||
mpsc::error::TrySendError::Closed(_) => {
|
||||
io::Error::new(io::ErrorKind::BrokenPipe, "progress output closed")
|
||||
}
|
||||
})?;
|
||||
#[cfg(test)]
|
||||
if let Some(control) = &state.control {
|
||||
control.queued();
|
||||
control.wait_gate().await;
|
||||
}
|
||||
Ok::<_, io::Error>(state)
|
||||
}
|
||||
},
|
||||
);
|
||||
#[cfg(test)]
|
||||
let owner_controls = controls.clone();
|
||||
let owner = tokio::spawn(async move {
|
||||
let running = rmcp::service::serve_directly(
|
||||
Bootstrap,
|
||||
(Box::pin(output), stream::pending::<ClientJsonRpcMessage>()),
|
||||
None,
|
||||
);
|
||||
let peer = running.peer().clone();
|
||||
let _ = peer_tx.send(Some(peer));
|
||||
let stop = running.cancellation_token();
|
||||
let pump = running.waiting();
|
||||
tokio::pin!(pump);
|
||||
let mut pump_done = false;
|
||||
tokio::select! {
|
||||
_ = owner_sender.closed() => {},
|
||||
_ = shutdown_rx => {},
|
||||
_ = &mut pump => { pump_done = true; },
|
||||
}
|
||||
#[cfg(test)]
|
||||
if let Some(control) = &owner_controls {
|
||||
control.release_sink();
|
||||
}
|
||||
stop.cancel();
|
||||
if !pump_done {
|
||||
let _ = pump.await;
|
||||
}
|
||||
#[cfg(test)]
|
||||
if let Some(control) = owner_controls {
|
||||
control.completed();
|
||||
}
|
||||
});
|
||||
Self {
|
||||
token,
|
||||
receiver,
|
||||
shutdown: Some(shutdown),
|
||||
owner: Some(owner),
|
||||
peer,
|
||||
#[cfg(test)]
|
||||
controls,
|
||||
progress_sidecar,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn notify(
|
||||
&mut self,
|
||||
progress: u64,
|
||||
message: &'static str,
|
||||
) -> Result<Value, io::Error> {
|
||||
if !matches!(self.token, Value::String(_) | Value::Number(_)) {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"invalid progress token",
|
||||
));
|
||||
}
|
||||
let sdk_token = rmcp::model::ProgressToken(rmcp::model::NumberOrString::String(
|
||||
SDK_PROGRESS_TOKEN.into(),
|
||||
));
|
||||
let params =
|
||||
ProgressNotificationParam::new(sdk_token, progress as f64).with_message(message);
|
||||
*self.progress_sidecar.lock().expect("progress sidecar lock") = Some(progress);
|
||||
let mut peer = self.peer.clone();
|
||||
while peer.borrow().is_none() {
|
||||
peer.changed().await.map_err(io::Error::other)?;
|
||||
}
|
||||
let peer = peer.borrow().as_ref().expect("peer initialized").clone();
|
||||
#[cfg(test)]
|
||||
if let Some(control) = &self.controls {
|
||||
control.sent();
|
||||
}
|
||||
let send = peer.notify_progress(params);
|
||||
tokio::pin!(send);
|
||||
let mut output = None;
|
||||
let send_result = loop {
|
||||
tokio::select! {
|
||||
result = &mut send => break result,
|
||||
received = self.receiver.recv(), if output.is_none() => {
|
||||
output = Some(received.ok_or_else(|| io::Error::new(io::ErrorKind::BrokenPipe, "progress output closed"))?);
|
||||
#[cfg(test)]
|
||||
if let Some(control) = &self.controls { control.received(); }
|
||||
}
|
||||
}
|
||||
};
|
||||
send_result.map_err(io::Error::other)?;
|
||||
let actual = match output {
|
||||
Some(output) => output,
|
||||
None => self.receiver.recv().await.ok_or_else(|| {
|
||||
io::Error::new(io::ErrorKind::BrokenPipe, "progress output closed")
|
||||
})?,
|
||||
};
|
||||
#[cfg(test)]
|
||||
if let Some(control) = &self.controls {
|
||||
control.consumed();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_progress_notification_serialized();
|
||||
Ok(actual)
|
||||
}
|
||||
|
||||
pub(super) async fn finish(mut self) {
|
||||
self.close();
|
||||
if let Some(owner) = self.owner.take() {
|
||||
let _ = owner.await;
|
||||
}
|
||||
}
|
||||
|
||||
fn close(&mut self) {
|
||||
self.receiver.close();
|
||||
if let Some(shutdown) = self.shutdown.take() {
|
||||
let _ = shutdown.send(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ProgressPeer {
|
||||
fn drop(&mut self) {
|
||||
self.close();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_progress_peer_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,103 @@
|
||||
//! Request-local test controls for the actual SDK progress peer.
|
||||
#![cfg(test)]
|
||||
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicBool, AtomicUsize, Ordering},
|
||||
};
|
||||
use tokio::sync::Semaphore;
|
||||
|
||||
tokio::task_local! {
|
||||
static CURRENT: Controls;
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub(crate) struct Controls(Arc<State>);
|
||||
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
actual_send: AtomicUsize,
|
||||
sink_entered: AtomicUsize,
|
||||
output_consumed: AtomicUsize,
|
||||
output_received: AtomicUsize,
|
||||
output_queued: AtomicUsize,
|
||||
owner_completed: AtomicUsize,
|
||||
fail_send: AtomicBool,
|
||||
mutate_message: AtomicBool,
|
||||
gate: std::sync::OnceLock<Arc<Semaphore>>,
|
||||
}
|
||||
|
||||
impl Controls {
|
||||
/// Installs controls only for this request future; `ProgressPeer::start` captures them.
|
||||
pub(crate) async fn scope<F: std::future::Future>(controls: Controls, future: F) -> F::Output {
|
||||
CURRENT.scope(controls, future).await
|
||||
}
|
||||
|
||||
pub(crate) fn actual_send(&self) -> usize {
|
||||
self.0.actual_send.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn sink_entered(&self) -> usize {
|
||||
self.0.sink_entered.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn output_consumed(&self) -> usize {
|
||||
self.0.output_consumed.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn output_queued(&self) -> usize {
|
||||
self.0.output_queued.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn output_received(&self) -> usize {
|
||||
self.0.output_received.load(Ordering::SeqCst)
|
||||
}
|
||||
pub(crate) fn owner_completed(&self) -> usize {
|
||||
self.0.owner_completed.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
pub(crate) fn fail_next_send(&self) {
|
||||
self.0.fail_send.store(true, Ordering::SeqCst);
|
||||
}
|
||||
pub(crate) fn mutate_output_message(&self) {
|
||||
self.0.mutate_message.store(true, Ordering::SeqCst);
|
||||
}
|
||||
pub(crate) fn hold_sink(&self) {
|
||||
let _ = self.gate();
|
||||
}
|
||||
pub(crate) fn release_sink(&self) {
|
||||
self.gate().close();
|
||||
}
|
||||
|
||||
pub(super) fn current() -> Option<Self> {
|
||||
CURRENT.try_with(Clone::clone).ok()
|
||||
}
|
||||
pub(super) fn sent(&self) {
|
||||
self.0.actual_send.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
pub(super) fn entered(&self) {
|
||||
self.0.sink_entered.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
pub(super) fn consumed(&self) {
|
||||
self.0.output_consumed.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
pub(super) fn queued(&self) {
|
||||
self.0.output_queued.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
pub(super) fn received(&self) {
|
||||
self.0.output_received.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
pub(super) fn completed(&self) {
|
||||
self.0.owner_completed.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
pub(super) fn should_fail_send(&self) -> bool {
|
||||
self.0.fail_send.swap(false, Ordering::SeqCst)
|
||||
}
|
||||
pub(super) fn should_mutate_message(&self) -> bool {
|
||||
self.0.mutate_message.swap(false, Ordering::SeqCst)
|
||||
}
|
||||
pub(super) async fn wait_gate(&self) {
|
||||
if let Some(gate) = self.0.gate.get() {
|
||||
let _ = gate.acquire().await;
|
||||
}
|
||||
}
|
||||
fn gate(&self) -> &Arc<Semaphore> {
|
||||
self.0.gate.get_or_init(|| Arc::new(Semaphore::new(0)))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
//! Request-stream ownership controls without an external service or a second domain task.
|
||||
use super::super::rmcp_progress_peer::{ProgressPeer, controls::Controls};
|
||||
use super::*;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::time::Duration;
|
||||
|
||||
struct DomainLease(Arc<AtomicBool>);
|
||||
impl Drop for DomainLease {
|
||||
fn drop(&mut self) {
|
||||
self.0.store(true, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
fn stream_call(
|
||||
controls: &Controls,
|
||||
deadline: bool,
|
||||
) -> (
|
||||
CallStream,
|
||||
Arc<AtomicBool>,
|
||||
Arc<InFlightCalls>,
|
||||
InFlightScope,
|
||||
) {
|
||||
controls.hold_sink();
|
||||
let dropped = Arc::new(AtomicBool::new(false));
|
||||
let lease = DomainLease(dropped.clone());
|
||||
let registry = Arc::new(InFlightCalls::new(1));
|
||||
let scope = InFlightScope {
|
||||
session_id: "owned".into(),
|
||||
owner: "key".into(),
|
||||
};
|
||||
let entry = registry.register(&scope, &serde_json::json!(1)).unwrap();
|
||||
let (lifecycle, events) = ToolCallLifecycle::new(Some(ProgressToken::Text("owned".into())));
|
||||
let domain = async move {
|
||||
let _lease = lease;
|
||||
lifecycle.advance(Phase::Resolved);
|
||||
if deadline {
|
||||
tokio::time::sleep(Duration::from_millis(30)).await;
|
||||
let mut result = rmcp::model::CallToolResult::error(vec![]);
|
||||
result.structured_content =
|
||||
Some(serde_json::json!({"error":{"code":"upstream_timeout"}}));
|
||||
super::super::rmcp_tools_runtime::Outcome::Call(result)
|
||||
} else {
|
||||
std::future::pending().await
|
||||
}
|
||||
};
|
||||
let call = CallStream {
|
||||
call: Box::pin(domain),
|
||||
events: events.unwrap(),
|
||||
cancel: entry.cancel_signal(),
|
||||
peer: Some(ProgressPeer::start(serde_json::json!("owned"))),
|
||||
outcome: None,
|
||||
message: serde_json::json!({"jsonrpc":"2.0","id":1}),
|
||||
protocol_version: crate::jsonrpc::LEGACY_PROTOCOL_VERSION.into(),
|
||||
pending: VecDeque::new(),
|
||||
finished: false,
|
||||
logging: crate::protocol::carry_scope(),
|
||||
_in_flight: Some(entry),
|
||||
};
|
||||
(call, dropped, registry, scope)
|
||||
}
|
||||
|
||||
async fn joined(controls: &Controls) {
|
||||
tokio::time::timeout(Duration::from_secs(2), async {
|
||||
while controls.owner_completed() != 1 {
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("request owner must join SDK pump");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_progress_peer_unpolled_stream_drop_releases_owner_domain_and_registration() {
|
||||
let controls = Controls::default();
|
||||
Controls::scope(controls.clone(), async {
|
||||
let (call, dropped, registry, scope) = stream_call(&controls, false);
|
||||
drop(call);
|
||||
joined(&controls).await;
|
||||
assert!(dropped.load(Ordering::SeqCst));
|
||||
assert_eq!(controls.actual_send(), 0);
|
||||
assert!(registry.register(&scope, &serde_json::json!(1)).is_some());
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_progress_peer_pending_send_cancel_stops_domain_and_joins_pump() {
|
||||
let controls = Controls::default();
|
||||
Controls::scope(controls.clone(), async {
|
||||
let (call, dropped, registry, scope) = stream_call(&controls, false);
|
||||
let next = next_event(call);
|
||||
tokio::pin!(next);
|
||||
tokio::select! {
|
||||
_ = &mut next => panic!("held send cannot produce a frame"),
|
||||
_ = async { while controls.sink_entered() == 0 { tokio::task::yield_now().await; } } => {}
|
||||
}
|
||||
assert!(registry.cancel(&scope, &serde_json::json!(1)));
|
||||
assert!(tokio::time::timeout(Duration::from_secs(2), &mut next).await.unwrap().is_none());
|
||||
joined(&controls).await;
|
||||
assert!(dropped.load(Ordering::SeqCst));
|
||||
assert_eq!(controls.output_consumed(), 0);
|
||||
assert!(registry.register(&scope, &serde_json::json!(1)).is_some());
|
||||
}).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_progress_peer_pending_send_body_drop_stops_domain_and_joins_pump() {
|
||||
let controls = Controls::default();
|
||||
Controls::scope(controls.clone(), async {
|
||||
let (call, dropped, registry, scope) = stream_call(&controls, false);
|
||||
let mut next = Box::pin(next_event(call));
|
||||
tokio::time::timeout(Duration::from_secs(2), async {
|
||||
tokio::select! {
|
||||
_ = &mut next => panic!("held send cannot produce a frame"),
|
||||
_ = async { while controls.sink_entered() == 0 { tokio::task::yield_now().await; } } => {}
|
||||
}
|
||||
}).await.unwrap();
|
||||
drop(next);
|
||||
joined(&controls).await;
|
||||
assert!(dropped.load(Ordering::SeqCst));
|
||||
assert_eq!(controls.output_consumed(), 0);
|
||||
assert!(registry.register(&scope, &serde_json::json!(1)).is_some());
|
||||
}).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_progress_peer_failure_after_first_frame_preserves_terminal_without_fallback() {
|
||||
let controls = Controls::default();
|
||||
Controls::scope(controls.clone(), async {
|
||||
let (call, _, _, _) = stream_call(&controls, false);
|
||||
controls.release_sink();
|
||||
let (_, mut call) = next_event(call).await.expect("first actual SDK progress");
|
||||
assert_eq!(controls.output_consumed(), 1);
|
||||
controls.fail_next_send();
|
||||
let (sender, events) = mpsc::channel(PROGRESS_CAPACITY);
|
||||
sender
|
||||
.try_send(ProgressEvent {
|
||||
progress: 2,
|
||||
message: "execution_dispatched",
|
||||
})
|
||||
.unwrap();
|
||||
call.events = events;
|
||||
call.call = Box::pin(async {
|
||||
super::super::rmcp_tools_runtime::Outcome::Call(rmcp::model::CallToolResult::success(
|
||||
vec![],
|
||||
))
|
||||
});
|
||||
let (_, call) = next_event(call)
|
||||
.await
|
||||
.expect("original terminal after send error");
|
||||
assert!(
|
||||
call.finished,
|
||||
"no fallback progress in place of failed SDK send"
|
||||
);
|
||||
assert!(call.peer.is_none());
|
||||
assert_eq!(controls.actual_send(), 2);
|
||||
assert_eq!(controls.output_consumed(), 1);
|
||||
assert_eq!(controls.owner_completed(), 1);
|
||||
assert!(next_event(call).await.is_none(), "terminal stays last");
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_progress_peer_pending_send_keeps_original_domain_deadline() {
|
||||
let controls = Controls::default();
|
||||
Controls::scope(controls.clone(), async {
|
||||
let (call, dropped, registry, scope) = stream_call(&controls, true);
|
||||
let (_, call) = tokio::time::timeout(Duration::from_secs(2), next_event(call))
|
||||
.await
|
||||
.expect("domain deadline must not wait for held send")
|
||||
.unwrap();
|
||||
assert!(call.finished);
|
||||
assert!(call.peer.is_none());
|
||||
assert_eq!(controls.sink_entered(), 1);
|
||||
assert_eq!(controls.output_consumed(), 0);
|
||||
joined(&controls).await;
|
||||
assert!(dropped.load(Ordering::SeqCst));
|
||||
drop(call);
|
||||
assert!(registry.register(&scope, &serde_json::json!(1)).is_some());
|
||||
})
|
||||
.await;
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
//! Request-owned Prompts trait adapter; domain execution stays in the parent HTTP future.
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crank_core::{CorrelationContext, ExecutionLocale};
|
||||
use rmcp::{ErrorData, RoleServer, ServerHandler, model::*, service::RequestContext};
|
||||
use serde_json::Value;
|
||||
|
||||
use super::{AppState, rmcp_prompts_projection as projection};
|
||||
use crate::{
|
||||
auth::VerifiedMachineCredential,
|
||||
protocol::{
|
||||
self, ContinuationSupport, PromptGetCommand, PromptsCommand, PromptsListCommand,
|
||||
PromptsOutcome, ServiceContext,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct PromptScope {
|
||||
workspace_slug: String,
|
||||
agent_slug: String,
|
||||
credential: VerifiedMachineCredential,
|
||||
correlation: CorrelationContext,
|
||||
continuation: ContinuationSupport,
|
||||
locale: ExecutionLocale,
|
||||
revision: String,
|
||||
limit: Option<usize>,
|
||||
}
|
||||
impl PromptScope {
|
||||
fn new(context: ServiceContext<'_>, revision: &str, limit: Option<usize>) -> Self {
|
||||
Self {
|
||||
workspace_slug: context.workspace_slug.to_owned(),
|
||||
agent_slug: context.agent_slug.to_owned(),
|
||||
credential: context.credential.clone(),
|
||||
correlation: context.correlation.clone(),
|
||||
continuation: context.continuation,
|
||||
locale: context.locale,
|
||||
revision: revision.to_owned(),
|
||||
limit,
|
||||
}
|
||||
}
|
||||
fn 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum ReplyExtras {
|
||||
List(projection::Projection),
|
||||
Get(projection::Projection),
|
||||
DomainError(PromptsOutcome),
|
||||
}
|
||||
#[derive(Clone)]
|
||||
struct PromptHandler {
|
||||
state: Arc<AppState>,
|
||||
reply: Arc<Mutex<Option<ReplyExtras>>>,
|
||||
#[cfg(test)]
|
||||
domain: Option<Arc<tests::Domain>>,
|
||||
}
|
||||
fn internal() -> ErrorData {
|
||||
ErrorData::internal_error("prompt handler unavailable", None)
|
||||
}
|
||||
|
||||
impl PromptHandler {
|
||||
fn new(state: &Arc<AppState>) -> Self {
|
||||
Self {
|
||||
state: state.clone(),
|
||||
reply: Arc::new(Mutex::new(None)),
|
||||
#[cfg(test)]
|
||||
domain: None,
|
||||
}
|
||||
}
|
||||
async fn execute(&self, scope: &PromptScope, command: PromptsCommand) -> PromptsOutcome {
|
||||
#[cfg(test)]
|
||||
if let Some(domain) = &self.domain {
|
||||
return domain.prompts(command).await;
|
||||
}
|
||||
protocol::handle_prompts_command(&self.state, scope.context(), &scope.revision, command)
|
||||
.await
|
||||
}
|
||||
fn save(&self, extras: ReplyExtras) -> Result<(), ErrorData> {
|
||||
let mut slot = self.reply.lock().map_err(|_| internal())?;
|
||||
if slot.is_some() {
|
||||
return Err(internal());
|
||||
}
|
||||
*slot = Some(extras);
|
||||
Ok(())
|
||||
}
|
||||
fn take(&self) -> Option<ReplyExtras> {
|
||||
self.reply.lock().ok()?.take()
|
||||
}
|
||||
fn scope(context: &RequestContext<RoleServer>) -> Result<PromptScope, ErrorData> {
|
||||
context
|
||||
.extensions
|
||||
.get::<PromptScope>()
|
||||
.cloned()
|
||||
.ok_or_else(internal)
|
||||
}
|
||||
fn domain_error(&self, outcome: PromptsOutcome) -> ErrorData {
|
||||
let error = match &outcome {
|
||||
PromptsOutcome::Listed { .. } | PromptsOutcome::Got { .. } => return internal(),
|
||||
PromptsOutcome::InvalidParams(detail) => {
|
||||
ErrorData::invalid_params(detail.clone(), None)
|
||||
}
|
||||
PromptsOutcome::NotAvailable => {
|
||||
ErrorData::invalid_params("the requested prompt is not available", None)
|
||||
}
|
||||
PromptsOutcome::ArgumentsRejected { .. } => {
|
||||
ErrorData::invalid_params("the prompt arguments were rejected", None)
|
||||
}
|
||||
_ => internal(),
|
||||
};
|
||||
if self.save(ReplyExtras::DomainError(outcome)).is_err() {
|
||||
return internal();
|
||||
}
|
||||
error
|
||||
}
|
||||
fn project_list(&self, outcome: PromptsOutcome) -> Result<ListPromptsResult, ErrorData> {
|
||||
match outcome {
|
||||
PromptsOutcome::Listed {
|
||||
prompts,
|
||||
next_cursor,
|
||||
} => {
|
||||
let (result, extras) =
|
||||
projection::list(&prompts, next_cursor).map_err(|_| internal())?;
|
||||
self.save(ReplyExtras::List(extras))?;
|
||||
Ok(result)
|
||||
}
|
||||
other => Err(self.domain_error(other)),
|
||||
}
|
||||
}
|
||||
fn project_get(&self, outcome: PromptsOutcome) -> Result<GetPromptResult, ErrorData> {
|
||||
match outcome {
|
||||
PromptsOutcome::Got {
|
||||
description,
|
||||
messages,
|
||||
} => {
|
||||
let (result, extras) =
|
||||
projection::get(description, &messages).map_err(|_| internal())?;
|
||||
self.save(ReplyExtras::Get(extras))?;
|
||||
Ok(result)
|
||||
}
|
||||
other => Err(self.domain_error(other)),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ServerHandler for PromptHandler {
|
||||
async fn list_prompts(
|
||||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<ListPromptsResult, ErrorData> {
|
||||
let scope = Self::scope(&context)?;
|
||||
let command = PromptsCommand::List(PromptsListCommand {
|
||||
cursor: request.unwrap_or_default().cursor,
|
||||
limit: scope.limit,
|
||||
});
|
||||
let outcome = self.execute(&scope, command).await;
|
||||
let result = self.project_list(outcome);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_prompts_list_handler_completed();
|
||||
result
|
||||
}
|
||||
async fn get_prompt(
|
||||
&self,
|
||||
request: GetPromptRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<GetPromptResponse, ErrorData> {
|
||||
let scope = Self::scope(&context)?;
|
||||
let command = PromptsCommand::Get(PromptGetCommand {
|
||||
name: request.name,
|
||||
arguments: request.arguments,
|
||||
});
|
||||
let outcome = self.execute(&scope, command).await;
|
||||
let result = self.project_get(outcome).map(Into::into);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_prompts_get_handler_completed();
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
/// Public SDK context seed only; close the inert service before any domain execution.
|
||||
/// shortcut: the closed Peer cannot send callbacks, revisit if Prompts need peer messaging.
|
||||
async fn request_context(
|
||||
handler: &PromptHandler,
|
||||
scope: PromptScope,
|
||||
) -> Result<RequestContext<RoleServer>, ErrorData> {
|
||||
let ping =
|
||||
serde_json::from_value(serde_json::json!({"jsonrpc":"2.0", "id":1, "method":"ping"}))
|
||||
.map_err(|_| internal())?;
|
||||
let (transport, receiver) = rmcp::transport::OneshotTransport::<RoleServer>::new(ping);
|
||||
let revision =
|
||||
serde_json::from_value(Value::String(scope.revision.clone())).map_err(|_| internal())?;
|
||||
let info = InitializeRequestParams::new(
|
||||
ClientCapabilities::default(),
|
||||
Implementation::new("crank-prompt-context", "1"),
|
||||
)
|
||||
.with_protocol_version(revision);
|
||||
let mut running = rmcp::service::serve_directly(handler.clone(), transport, Some(info));
|
||||
let peer = running.peer().clone();
|
||||
running.close().await.map_err(|_| internal())?;
|
||||
drop(receiver);
|
||||
let mut context = RequestContext::new(RequestId::Number(1), peer);
|
||||
context.extensions.insert(scope);
|
||||
Ok(context)
|
||||
}
|
||||
fn restore_list(
|
||||
handler: &PromptHandler,
|
||||
result: Result<ListPromptsResult, ErrorData>,
|
||||
) -> PromptsOutcome {
|
||||
match (result, handler.take()) {
|
||||
(Ok(result), Some(ReplyExtras::List(extras))) => {
|
||||
let cursor = result.next_cursor.clone();
|
||||
match projection::serialize(result, extras, "prompts") {
|
||||
Ok(mut result) => match result["prompts"].take() {
|
||||
Value::Array(prompts) => PromptsOutcome::Listed {
|
||||
prompts,
|
||||
next_cursor: cursor,
|
||||
},
|
||||
_ => PromptsOutcome::CatalogUnavailable,
|
||||
},
|
||||
Err(_) => PromptsOutcome::CatalogUnavailable,
|
||||
}
|
||||
}
|
||||
(Err(_), Some(ReplyExtras::DomainError(error))) => error,
|
||||
_ => PromptsOutcome::CatalogUnavailable,
|
||||
}
|
||||
}
|
||||
fn restore_get(
|
||||
handler: &PromptHandler,
|
||||
result: Result<GetPromptResponse, ErrorData>,
|
||||
) -> PromptsOutcome {
|
||||
match (result, handler.take()) {
|
||||
(Ok(GetPromptResponse::Complete(result)), Some(ReplyExtras::Get(extras))) => {
|
||||
let description = result.description.clone();
|
||||
match projection::serialize(result, extras, "messages") {
|
||||
Ok(mut result) => match result["messages"].take() {
|
||||
Value::Array(messages) => PromptsOutcome::Got {
|
||||
description,
|
||||
messages,
|
||||
},
|
||||
_ => PromptsOutcome::CatalogUnavailable,
|
||||
},
|
||||
Err(_) => PromptsOutcome::CatalogUnavailable,
|
||||
}
|
||||
}
|
||||
(Err(_), Some(ReplyExtras::DomainError(error))) => error,
|
||||
_ => PromptsOutcome::CatalogUnavailable,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn handle(
|
||||
state: &Arc<AppState>,
|
||||
context: ServiceContext<'_>,
|
||||
revision: &str,
|
||||
command: PromptsCommand,
|
||||
) -> PromptsOutcome {
|
||||
let limit = match &command {
|
||||
PromptsCommand::List(list) => list.limit,
|
||||
_ => None,
|
||||
};
|
||||
let handler = PromptHandler::new(state);
|
||||
let context = match request_context(&handler, PromptScope::new(context, revision, limit)).await
|
||||
{
|
||||
Ok(context) => context,
|
||||
Err(_) => return PromptsOutcome::CatalogUnavailable,
|
||||
};
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_prompts_dispatch_enter();
|
||||
match command {
|
||||
PromptsCommand::List(list) => {
|
||||
let result = ServerHandler::list_prompts(
|
||||
&handler,
|
||||
Some(PaginatedRequestParams::default().with_cursor(list.cursor)),
|
||||
context,
|
||||
)
|
||||
.await;
|
||||
let outcome = restore_list(&handler, result);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_prompts_list_handler_consumed();
|
||||
outcome
|
||||
}
|
||||
PromptsCommand::Get(get) => {
|
||||
let mut request = GetPromptRequestParams::new(get.name);
|
||||
request.arguments = get.arguments;
|
||||
let result = ServerHandler::get_prompt(&handler, request, context).await;
|
||||
let outcome = restore_get(&handler, result);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_prompts_get_handler_consumed();
|
||||
outcome
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_prompts_handler_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,194 @@
|
||||
//! Prompt-only lossless projection: SDK fields remain sourced from the actual trait result.
|
||||
use rmcp::model::{GetPromptResult, ListPromptsResult};
|
||||
use serde::Serialize;
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
pub(super) fn invalid() -> serde_json::Error {
|
||||
<serde_json::Error as serde::de::Error>::custom("invalid prompt projection")
|
||||
}
|
||||
|
||||
// Store only differences from SDK serialization, not an alternate successful response.
|
||||
pub(super) enum Projection {
|
||||
Same,
|
||||
Opaque(Value),
|
||||
Object {
|
||||
extras: Map<String, Value>,
|
||||
absent: Vec<String>,
|
||||
children: Vec<(String, Projection)>,
|
||||
identity: Map<String, Value>,
|
||||
},
|
||||
Array(Vec<Projection>),
|
||||
}
|
||||
|
||||
impl Projection {
|
||||
fn between(raw: &Value, sdk: &Value) -> Self {
|
||||
match (raw, sdk) {
|
||||
(Value::Object(raw), Value::Object(sdk)) => {
|
||||
let extras = raw
|
||||
.iter()
|
||||
.filter(|(k, _)| !sdk.contains_key(*k))
|
||||
.map(|(k, v)| (k.clone(), v.clone()))
|
||||
.collect();
|
||||
let absent = sdk
|
||||
.keys()
|
||||
.filter(|k| !raw.contains_key(*k))
|
||||
.cloned()
|
||||
.collect();
|
||||
let children = raw
|
||||
.iter()
|
||||
.filter_map(|(k, v)| sdk.get(k).map(|s| (k.clone(), Self::between(v, s))))
|
||||
.collect();
|
||||
let identity = sdk
|
||||
.iter()
|
||||
.filter(|(k, _)| matches!(k.as_str(), "name" | "role" | "type" | "uri"))
|
||||
.map(|(k, v)| (k.clone(), v.clone()))
|
||||
.collect();
|
||||
Self::Object {
|
||||
extras,
|
||||
absent,
|
||||
children,
|
||||
identity,
|
||||
}
|
||||
}
|
||||
(Value::Array(raw), Value::Array(sdk)) => Self::Array(
|
||||
raw.iter()
|
||||
.zip(sdk)
|
||||
.map(|(r, s)| Self::between(r, s))
|
||||
.collect(),
|
||||
),
|
||||
_ if raw == sdk => Self::Same,
|
||||
_ => Self::Opaque(raw.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
fn restore(self, sdk: &mut Value) -> Result<(), serde_json::Error> {
|
||||
match self {
|
||||
Self::Same => {}
|
||||
Self::Opaque(value) => *sdk = value,
|
||||
Self::Object {
|
||||
extras,
|
||||
absent,
|
||||
children,
|
||||
identity,
|
||||
} => {
|
||||
let object = sdk.as_object_mut().ok_or_else(invalid)?;
|
||||
if identity.iter().any(|(k, v)| object.get(k) != Some(v)) {
|
||||
return Err(invalid());
|
||||
}
|
||||
for (key, projection) in children {
|
||||
projection.restore(object.get_mut(&key).ok_or_else(invalid)?)?;
|
||||
}
|
||||
for key in absent {
|
||||
object.remove(&key);
|
||||
}
|
||||
object.extend(extras);
|
||||
}
|
||||
Self::Array(children) => {
|
||||
let array = sdk.as_array_mut().ok_or_else(invalid)?;
|
||||
if array.len() != children.len() {
|
||||
return Err(invalid());
|
||||
}
|
||||
for (projection, item) in children.into_iter().zip(array) {
|
||||
projection.restore(item)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn visible(raw: &Value, fields: &[&str]) -> Result<Value, serde_json::Error> {
|
||||
let raw = raw.as_object().ok_or_else(invalid)?;
|
||||
Ok(Value::Object(
|
||||
raw.iter()
|
||||
.filter(|(key, _)| fields.contains(&key.as_str()))
|
||||
.map(|(key, value)| (key.clone(), value.clone()))
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
pub(super) fn list(
|
||||
prompts: &[Value],
|
||||
next_cursor: Option<String>,
|
||||
) -> Result<(ListPromptsResult, Projection), serde_json::Error> {
|
||||
let mut typed = Vec::with_capacity(prompts.len());
|
||||
for prompt in prompts {
|
||||
let mut prompt = visible(
|
||||
prompt,
|
||||
&["name", "title", "description", "arguments", "_meta"],
|
||||
)?;
|
||||
if let Some(Value::Array(arguments)) = prompt.get_mut("arguments") {
|
||||
for argument in arguments {
|
||||
*argument = visible(argument, &["name", "title", "description", "required"])?;
|
||||
}
|
||||
}
|
||||
typed.push(serde_json::from_value(prompt)?);
|
||||
}
|
||||
let result = ListPromptsResult {
|
||||
prompts: typed,
|
||||
next_cursor,
|
||||
result_type: None,
|
||||
ttl_ms: None,
|
||||
cache_scope: None,
|
||||
meta: None,
|
||||
};
|
||||
let extras = Projection::between(
|
||||
&Value::Array(prompts.to_vec()),
|
||||
&serde_json::to_value(&result)?["prompts"],
|
||||
);
|
||||
Ok((result, extras))
|
||||
}
|
||||
|
||||
pub(super) fn get(
|
||||
description: Option<String>,
|
||||
messages: &[Value],
|
||||
) -> Result<(GetPromptResult, Projection), serde_json::Error> {
|
||||
let mut typed = Vec::with_capacity(messages.len());
|
||||
for message in messages {
|
||||
let mut message = visible(message, &["role", "content"])?;
|
||||
if message["role"] == "system" {
|
||||
message["role"] = Value::String("user".into());
|
||||
}
|
||||
let content = message.get_mut("content").ok_or_else(invalid)?;
|
||||
// Opaque annotations and newly known SDK extensions must not add validation to Crank.
|
||||
let fields: &[&str] = match content["type"].as_str() {
|
||||
Some("text") => &["type", "text", "_meta"],
|
||||
Some("image" | "audio") => &["type", "data", "mimeType", "_meta"],
|
||||
Some("resource") => &["type", "resource", "_meta"],
|
||||
Some("resource_link") => &[
|
||||
"type",
|
||||
"uri",
|
||||
"name",
|
||||
"title",
|
||||
"description",
|
||||
"mimeType",
|
||||
"size",
|
||||
"_meta",
|
||||
],
|
||||
_ => return Err(invalid()),
|
||||
};
|
||||
*content = visible(content, fields)?;
|
||||
if let Some(resource) = content.get_mut("resource") {
|
||||
*resource = visible(resource, &["uri", "mimeType", "text", "blob", "_meta"])?;
|
||||
}
|
||||
typed.push(serde_json::from_value(message)?);
|
||||
}
|
||||
let mut result = GetPromptResult::new(typed);
|
||||
result.description = description;
|
||||
result.result_type = None;
|
||||
let extras = Projection::between(
|
||||
&Value::Array(messages.to_vec()),
|
||||
&serde_json::to_value(&result)?["messages"],
|
||||
);
|
||||
Ok((result, extras))
|
||||
}
|
||||
|
||||
pub(super) fn serialize(
|
||||
result: impl Serialize,
|
||||
extras: Projection,
|
||||
field: &str,
|
||||
) -> Result<Value, serde_json::Error> {
|
||||
let mut result = serde_json::to_value(result)?;
|
||||
extras.restore(result.get_mut(field).ok_or_else(invalid)?)?;
|
||||
Ok(result)
|
||||
}
|
||||
@@ -0,0 +1,423 @@
|
||||
//! Request-owned Resources trait adapter. Crank remains the only domain executor.
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crank_core::{CorrelationContext, ExecutionLocale};
|
||||
use rmcp::{ErrorData, RoleServer, ServerHandler, model::*, service::RequestContext};
|
||||
use serde_json::Value;
|
||||
|
||||
use super::{AppState, rmcp_resources_projection as projection};
|
||||
use crate::{
|
||||
auth::VerifiedMachineCredential,
|
||||
protocol::{
|
||||
self, ContinuationSupport, ResourceReadCommand, ResourceTemplatesListError,
|
||||
ResourcesCommand, ResourcesListCommand, ResourcesOutcome, ServiceContext,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ResourceScope {
|
||||
workspace_slug: String,
|
||||
agent_slug: String,
|
||||
credential: VerifiedMachineCredential,
|
||||
correlation: CorrelationContext,
|
||||
continuation: ContinuationSupport,
|
||||
locale: ExecutionLocale,
|
||||
revision: String,
|
||||
limit: Option<usize>,
|
||||
}
|
||||
|
||||
impl ResourceScope {
|
||||
fn new(context: ServiceContext<'_>, revision: &str, limit: Option<usize>) -> Self {
|
||||
Self {
|
||||
workspace_slug: context.workspace_slug.to_owned(),
|
||||
agent_slug: context.agent_slug.to_owned(),
|
||||
credential: context.credential.clone(),
|
||||
correlation: context.correlation.clone(),
|
||||
continuation: context.continuation,
|
||||
locale: context.locale,
|
||||
revision: revision.to_owned(),
|
||||
limit,
|
||||
}
|
||||
}
|
||||
|
||||
fn 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum ReplyExtras {
|
||||
List(projection::Projection),
|
||||
Read(projection::Projection),
|
||||
Templates(projection::Projection),
|
||||
DomainError(ResourcesOutcome),
|
||||
TemplateError(ResourceTemplatesListError),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ResourceHandler {
|
||||
state: Arc<AppState>,
|
||||
reply: Arc<Mutex<Option<ReplyExtras>>>,
|
||||
#[cfg(test)]
|
||||
domain: Option<Arc<tests::Domain>>,
|
||||
}
|
||||
|
||||
fn internal() -> ErrorData {
|
||||
ErrorData::internal_error("resource handler unavailable", None)
|
||||
}
|
||||
|
||||
impl ResourceHandler {
|
||||
fn new(state: &Arc<AppState>) -> Self {
|
||||
Self {
|
||||
state: state.clone(),
|
||||
reply: Arc::new(Mutex::new(None)),
|
||||
#[cfg(test)]
|
||||
domain: None,
|
||||
}
|
||||
}
|
||||
|
||||
async fn execute(&self, scope: &ResourceScope, command: ResourcesCommand) -> ResourcesOutcome {
|
||||
#[cfg(test)]
|
||||
if let Some(domain) = &self.domain {
|
||||
return domain.resources(command).await;
|
||||
}
|
||||
protocol::handle_resources_command(&self.state, scope.context(), &scope.revision, command)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn execute_templates(
|
||||
&self,
|
||||
scope: &ResourceScope,
|
||||
command: ResourcesListCommand,
|
||||
) -> Result<(Vec<Value>, Option<String>), ResourceTemplatesListError> {
|
||||
#[cfg(test)]
|
||||
if let Some(domain) = &self.domain {
|
||||
return domain.templates(command).await;
|
||||
}
|
||||
protocol::list_resource_templates(
|
||||
&self.state,
|
||||
&scope.workspace_slug,
|
||||
&scope.agent_slug,
|
||||
&scope.revision,
|
||||
command,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
fn save(&self, extras: ReplyExtras) -> Result<(), ErrorData> {
|
||||
let mut slot = self.reply.lock().map_err(|_| internal())?;
|
||||
if slot.is_some() {
|
||||
return Err(internal());
|
||||
}
|
||||
*slot = Some(extras);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn take(&self) -> Option<ReplyExtras> {
|
||||
self.reply.lock().ok()?.take()
|
||||
}
|
||||
|
||||
fn scope(context: &RequestContext<RoleServer>) -> Result<ResourceScope, ErrorData> {
|
||||
context
|
||||
.extensions
|
||||
.get::<ResourceScope>()
|
||||
.cloned()
|
||||
.ok_or_else(internal)
|
||||
}
|
||||
|
||||
fn domain_error(&self, outcome: ResourcesOutcome) -> ErrorData {
|
||||
let error = match &outcome {
|
||||
ResourcesOutcome::Listed { .. } | ResourcesOutcome::Read { .. } => return internal(),
|
||||
ResourcesOutcome::InvalidParams(detail) => {
|
||||
ErrorData::invalid_params(detail.clone(), None)
|
||||
}
|
||||
ResourcesOutcome::NotAvailable => {
|
||||
ErrorData::invalid_params("the requested resource is not available", None)
|
||||
}
|
||||
_ => internal(),
|
||||
};
|
||||
if self.save(ReplyExtras::DomainError(outcome)).is_err() {
|
||||
return internal();
|
||||
}
|
||||
error
|
||||
}
|
||||
|
||||
fn project_list(
|
||||
&self,
|
||||
outcome: ResourcesOutcome,
|
||||
revision: &str,
|
||||
) -> Result<ListResourcesResult, ErrorData> {
|
||||
match outcome {
|
||||
ResourcesOutcome::Listed {
|
||||
resources,
|
||||
next_cursor,
|
||||
} => {
|
||||
let (result, extras) =
|
||||
projection::list(&resources, next_cursor, revision).map_err(|_| internal())?;
|
||||
self.save(ReplyExtras::List(extras))?;
|
||||
Ok(result)
|
||||
}
|
||||
other => Err(self.domain_error(other)),
|
||||
}
|
||||
}
|
||||
|
||||
fn project_read(
|
||||
&self,
|
||||
outcome: ResourcesOutcome,
|
||||
revision: &str,
|
||||
) -> Result<ReadResourceResponse, ErrorData> {
|
||||
match outcome {
|
||||
ResourcesOutcome::Read { contents } => {
|
||||
let (result, extras) =
|
||||
projection::read(&contents, revision).map_err(|_| internal())?;
|
||||
self.save(ReplyExtras::Read(extras))?;
|
||||
Ok(result.into())
|
||||
}
|
||||
other => Err(self.domain_error(other)),
|
||||
}
|
||||
}
|
||||
|
||||
fn project_templates(
|
||||
&self,
|
||||
outcome: Result<(Vec<Value>, Option<String>), ResourceTemplatesListError>,
|
||||
revision: &str,
|
||||
) -> Result<ListResourceTemplatesResult, ErrorData> {
|
||||
match outcome {
|
||||
Ok((templates, next_cursor)) => {
|
||||
let (result, extras) = projection::templates(&templates, next_cursor, revision)
|
||||
.map_err(|_| internal())?;
|
||||
self.save(ReplyExtras::Templates(extras))?;
|
||||
Ok(result)
|
||||
}
|
||||
Err(error) => {
|
||||
let sdk_error = match error {
|
||||
ResourceTemplatesListError::InvalidCursor => {
|
||||
ErrorData::invalid_params("invalid resources/templates/list cursor", None)
|
||||
}
|
||||
ResourceTemplatesListError::Unavailable => internal(),
|
||||
};
|
||||
self.save(ReplyExtras::TemplateError(error))?;
|
||||
Err(sdk_error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ServerHandler for ResourceHandler {
|
||||
async fn list_resources(
|
||||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<ListResourcesResult, ErrorData> {
|
||||
let scope = Self::scope(&context)?;
|
||||
let command = ResourcesCommand::List(ResourcesListCommand {
|
||||
cursor: request.unwrap_or_default().cursor,
|
||||
limit: scope.limit,
|
||||
});
|
||||
let outcome = self.execute(&scope, command).await;
|
||||
let result = self.project_list(outcome, &scope.revision);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_resources_list_handler_completed();
|
||||
result
|
||||
}
|
||||
|
||||
async fn read_resource(
|
||||
&self,
|
||||
request: ReadResourceRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<ReadResourceResponse, ErrorData> {
|
||||
let scope = Self::scope(&context)?;
|
||||
let command = ResourcesCommand::Read(ResourceReadCommand { uri: request.uri });
|
||||
let outcome = self.execute(&scope, command).await;
|
||||
let result = self.project_read(outcome, &scope.revision);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_resources_read_handler_completed();
|
||||
result
|
||||
}
|
||||
|
||||
async fn list_resource_templates(
|
||||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<ListResourceTemplatesResult, ErrorData> {
|
||||
let scope = Self::scope(&context)?;
|
||||
let command = ResourcesListCommand {
|
||||
cursor: request.unwrap_or_default().cursor,
|
||||
limit: scope.limit,
|
||||
};
|
||||
let outcome = self.execute_templates(&scope, command).await;
|
||||
let result = self.project_templates(outcome, &scope.revision);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_resources_templates_handler_completed();
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
/// Public SDK Peer construction only; the inert seed service closes before domain execution.
|
||||
/// shortcut: the closed Peer cannot send callbacks, revisit if Resources require peer messaging.
|
||||
async fn request_context(
|
||||
handler: &ResourceHandler,
|
||||
scope: ResourceScope,
|
||||
) -> Result<RequestContext<RoleServer>, ErrorData> {
|
||||
let ping =
|
||||
serde_json::from_value(serde_json::json!({"jsonrpc":"2.0", "id":1, "method":"ping"}))
|
||||
.map_err(|_| internal())?;
|
||||
let (transport, receiver) = rmcp::transport::OneshotTransport::<RoleServer>::new(ping);
|
||||
let revision =
|
||||
serde_json::from_value(Value::String(scope.revision.clone())).map_err(|_| internal())?;
|
||||
let info = InitializeRequestParams::new(
|
||||
ClientCapabilities::default(),
|
||||
Implementation::new("crank-resource-context", "1"),
|
||||
)
|
||||
.with_protocol_version(revision);
|
||||
let mut running = rmcp::service::serve_directly(handler.clone(), transport, Some(info));
|
||||
let peer = running.peer().clone();
|
||||
running.close().await.map_err(|_| internal())?;
|
||||
drop(receiver);
|
||||
let mut context = RequestContext::new(RequestId::Number(1), peer);
|
||||
context.extensions.insert(scope);
|
||||
Ok(context)
|
||||
}
|
||||
|
||||
fn restore_list(
|
||||
handler: &ResourceHandler,
|
||||
result: Result<ListResourcesResult, ErrorData>,
|
||||
) -> ResourcesOutcome {
|
||||
match (result, handler.take()) {
|
||||
(Ok(result), Some(ReplyExtras::List(extras))) => {
|
||||
let cursor = result.next_cursor.clone();
|
||||
match projection::serialize(result, extras, "resources") {
|
||||
Ok(mut result) => match result["resources"].take() {
|
||||
Value::Array(resources) => ResourcesOutcome::Listed {
|
||||
resources,
|
||||
next_cursor: cursor,
|
||||
},
|
||||
_ => ResourcesOutcome::CatalogUnavailable,
|
||||
},
|
||||
Err(_) => ResourcesOutcome::CatalogUnavailable,
|
||||
}
|
||||
}
|
||||
(Err(_), Some(ReplyExtras::DomainError(error))) => error,
|
||||
_ => ResourcesOutcome::CatalogUnavailable,
|
||||
}
|
||||
}
|
||||
|
||||
fn restore_read(
|
||||
handler: &ResourceHandler,
|
||||
result: Result<ReadResourceResponse, ErrorData>,
|
||||
) -> ResourcesOutcome {
|
||||
match (result, handler.take()) {
|
||||
(Ok(ReadResourceResponse::Complete(result)), Some(ReplyExtras::Read(extras))) => {
|
||||
match projection::serialize(result, extras, "contents") {
|
||||
Ok(mut result) => ResourcesOutcome::Read {
|
||||
contents: result["contents"].take(),
|
||||
},
|
||||
Err(_) => ResourcesOutcome::ArtifactUnavailable,
|
||||
}
|
||||
}
|
||||
(Err(_), Some(ReplyExtras::DomainError(error))) => error,
|
||||
_ => ResourcesOutcome::ArtifactUnavailable,
|
||||
}
|
||||
}
|
||||
|
||||
fn restore_templates(
|
||||
handler: &ResourceHandler,
|
||||
result: Result<ListResourceTemplatesResult, ErrorData>,
|
||||
) -> Result<(Vec<Value>, Option<String>), ResourceTemplatesListError> {
|
||||
match (result, handler.take()) {
|
||||
(Ok(result), Some(ReplyExtras::Templates(extras))) => {
|
||||
let cursor = result.next_cursor.clone();
|
||||
let mut result = projection::serialize(result, extras, "resourceTemplates")
|
||||
.map_err(|_| ResourceTemplatesListError::Unavailable)?;
|
||||
match result["resourceTemplates"].take() {
|
||||
Value::Array(templates) => Ok((templates, cursor)),
|
||||
_ => Err(ResourceTemplatesListError::Unavailable),
|
||||
}
|
||||
}
|
||||
(Err(_), Some(ReplyExtras::TemplateError(error))) => Err(error),
|
||||
_ => Err(ResourceTemplatesListError::Unavailable),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn handle(
|
||||
state: &Arc<AppState>,
|
||||
context: ServiceContext<'_>,
|
||||
revision: &str,
|
||||
command: ResourcesCommand,
|
||||
) -> ResourcesOutcome {
|
||||
let limit = match &command {
|
||||
ResourcesCommand::List(list) => list.limit,
|
||||
_ => None,
|
||||
};
|
||||
let handler = ResourceHandler::new(state);
|
||||
let context =
|
||||
match request_context(&handler, ResourceScope::new(context, revision, limit)).await {
|
||||
Ok(context) => context,
|
||||
Err(_) => return ResourcesOutcome::CatalogUnavailable,
|
||||
};
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_resources_dispatch_enter();
|
||||
match command {
|
||||
ResourcesCommand::List(list) => {
|
||||
let result = ServerHandler::list_resources(
|
||||
&handler,
|
||||
Some(PaginatedRequestParams::default().with_cursor(list.cursor)),
|
||||
context,
|
||||
)
|
||||
.await;
|
||||
let outcome = restore_list(&handler, result);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_resources_list_handler_consumed();
|
||||
outcome
|
||||
}
|
||||
ResourcesCommand::Read(read) => {
|
||||
let result = ServerHandler::read_resource(
|
||||
&handler,
|
||||
ReadResourceRequestParams::new(read.uri),
|
||||
context,
|
||||
)
|
||||
.await;
|
||||
let outcome = restore_read(&handler, result);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_resources_read_handler_consumed();
|
||||
outcome
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn templates(
|
||||
state: &Arc<AppState>,
|
||||
context: ServiceContext<'_>,
|
||||
revision: &str,
|
||||
command: ResourcesListCommand,
|
||||
) -> Result<(Vec<Value>, Option<String>), ResourceTemplatesListError> {
|
||||
let handler = ResourceHandler::new(state);
|
||||
let context = request_context(
|
||||
&handler,
|
||||
ResourceScope::new(context, revision, command.limit),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| ResourceTemplatesListError::Unavailable)?;
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_resources_dispatch_enter();
|
||||
let result = ServerHandler::list_resource_templates(
|
||||
&handler,
|
||||
Some(PaginatedRequestParams::default().with_cursor(command.cursor)),
|
||||
context,
|
||||
)
|
||||
.await;
|
||||
let outcome = restore_templates(&handler, result);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_resources_templates_handler_consumed();
|
||||
outcome
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_resources_handler_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,148 @@
|
||||
//! Lossless Resource-only projection shared by the handler and existing wire builders.
|
||||
use rmcp::model::{
|
||||
CacheScope, ListResourceTemplatesResult, ListResourcesResult, ReadResourceResult, ResultType,
|
||||
};
|
||||
use serde::{Serialize, de::DeserializeOwned};
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
pub(super) const RESOURCE_FIELDS: &[&str] = &[
|
||||
"uri",
|
||||
"name",
|
||||
"title",
|
||||
"description",
|
||||
"mimeType",
|
||||
"size",
|
||||
"_meta",
|
||||
];
|
||||
pub(super) const TEMPLATE_FIELDS: &[&str] = &[
|
||||
"uriTemplate",
|
||||
"name",
|
||||
"title",
|
||||
"description",
|
||||
"mimeType",
|
||||
"_meta",
|
||||
];
|
||||
pub(super) const CONTENT_FIELDS: &[&str] = &["uri", "mimeType", "text", "blob", "_meta"];
|
||||
|
||||
/// Only unrepresentable fields plus order/identity witnesses, never a saved success response.
|
||||
pub(super) struct Projection {
|
||||
extras: Vec<Map<String, Value>>,
|
||||
identities: Vec<Map<String, Value>>,
|
||||
}
|
||||
|
||||
fn invalid() -> serde_json::Error {
|
||||
<serde_json::Error as serde::de::Error>::custom("invalid resource projection")
|
||||
}
|
||||
|
||||
impl Projection {
|
||||
fn project<T: DeserializeOwned>(
|
||||
raw: &[Value],
|
||||
fields: &[&str],
|
||||
) -> Result<(Vec<T>, Self), serde_json::Error> {
|
||||
let mut extras = Vec::with_capacity(raw.len());
|
||||
let mut identities = Vec::with_capacity(raw.len());
|
||||
let mut typed = Vec::with_capacity(raw.len());
|
||||
for raw in raw {
|
||||
let mut visible = raw.as_object().ok_or_else(invalid)?.clone();
|
||||
let extra = visible
|
||||
.iter()
|
||||
.filter(|(key, value)| !fields.contains(&key.as_str()) || value.is_null())
|
||||
.map(|(key, value)| (key.clone(), value.clone()))
|
||||
.collect();
|
||||
let identity = visible
|
||||
.iter()
|
||||
.filter(|(key, _)| matches!(key.as_str(), "uri" | "uriTemplate" | "name"))
|
||||
.map(|(key, value)| (key.clone(), value.clone()))
|
||||
.collect();
|
||||
// Opaque descriptor JSON must not acquire SDK annotation/icon schema validation.
|
||||
visible.remove("annotations");
|
||||
visible.remove("icons");
|
||||
typed.push(serde_json::from_value(Value::Object(visible))?);
|
||||
extras.push(extra);
|
||||
identities.push(identity);
|
||||
}
|
||||
Ok((typed, Self { extras, identities }))
|
||||
}
|
||||
|
||||
pub(super) fn restore(self, serialized: &mut Value) -> Result<(), serde_json::Error> {
|
||||
let array = serialized.as_array_mut().ok_or_else(invalid)?;
|
||||
if array.len() != self.extras.len() {
|
||||
return Err(invalid());
|
||||
}
|
||||
for ((item, extras), identity) in array.iter_mut().zip(self.extras).zip(self.identities) {
|
||||
let item = item.as_object_mut().ok_or_else(invalid)?;
|
||||
if identity
|
||||
.iter()
|
||||
.any(|(key, value)| item.get(key) != Some(value))
|
||||
{
|
||||
return Err(invalid());
|
||||
}
|
||||
item.extend(extras);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn list(
|
||||
resources: &[Value],
|
||||
next_cursor: Option<String>,
|
||||
version: &str,
|
||||
) -> Result<(ListResourcesResult, Projection), serde_json::Error> {
|
||||
let (resources, extras) = Projection::project(resources, RESOURCE_FIELDS)?;
|
||||
let modern = version == crate::jsonrpc::MODERN_PROTOCOL_VERSION;
|
||||
Ok((
|
||||
ListResourcesResult {
|
||||
resources,
|
||||
next_cursor,
|
||||
result_type: modern.then_some(ResultType::COMPLETE),
|
||||
ttl_ms: modern.then_some(0),
|
||||
cache_scope: modern.then_some(CacheScope::Private),
|
||||
meta: None,
|
||||
},
|
||||
extras,
|
||||
))
|
||||
}
|
||||
|
||||
pub(super) fn templates(
|
||||
templates: &[Value],
|
||||
next_cursor: Option<String>,
|
||||
version: &str,
|
||||
) -> Result<(ListResourceTemplatesResult, Projection), serde_json::Error> {
|
||||
let (resource_templates, extras) = Projection::project(templates, TEMPLATE_FIELDS)?;
|
||||
let modern = version == crate::jsonrpc::MODERN_PROTOCOL_VERSION;
|
||||
Ok((
|
||||
ListResourceTemplatesResult {
|
||||
resource_templates,
|
||||
next_cursor,
|
||||
result_type: modern.then_some(ResultType::COMPLETE),
|
||||
ttl_ms: modern.then_some(0),
|
||||
cache_scope: modern.then_some(CacheScope::Private),
|
||||
meta: None,
|
||||
},
|
||||
extras,
|
||||
))
|
||||
}
|
||||
|
||||
pub(super) fn read(
|
||||
contents: &Value,
|
||||
version: &str,
|
||||
) -> Result<(ReadResourceResult, Projection), serde_json::Error> {
|
||||
let (contents, extras) =
|
||||
Projection::project(contents.as_array().ok_or_else(invalid)?, CONTENT_FIELDS)?;
|
||||
let modern = version == crate::jsonrpc::MODERN_PROTOCOL_VERSION;
|
||||
let mut result = ReadResourceResult::new(contents);
|
||||
result.result_type = modern.then_some(ResultType::COMPLETE);
|
||||
result.ttl_ms = modern.then_some(0);
|
||||
result.cache_scope = modern.then_some(CacheScope::Private);
|
||||
Ok((result, extras))
|
||||
}
|
||||
|
||||
pub(super) fn serialize(
|
||||
result: impl Serialize,
|
||||
extras: Projection,
|
||||
field: &str,
|
||||
) -> Result<Value, serde_json::Error> {
|
||||
let mut result = serde_json::to_value(result)?;
|
||||
extras.restore(result.get_mut(field).ok_or_else(invalid)?)?;
|
||||
Ok(result)
|
||||
}
|
||||
@@ -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,243 @@
|
||||
//! Request-owned SDK subscription execution; the existing SSE receiver owns cancellation.
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use futures_util::{sink, stream};
|
||||
use rmcp::{
|
||||
ErrorData, RoleServer, ServerHandler, Service,
|
||||
model::*,
|
||||
service::{RequestContext, SubscriptionContext},
|
||||
};
|
||||
use serde_json::Value;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::protocol::signals::{self, CatalogFingerprint, ReadError, Reader, Watch};
|
||||
|
||||
type PreparedWatch = (Watch, Reader, Result<CatalogFingerprint, ReadError>);
|
||||
|
||||
struct Listener {
|
||||
capabilities: ServerCapabilities,
|
||||
watch: Mutex<Option<PreparedWatch>>,
|
||||
}
|
||||
|
||||
impl ServerHandler for Listener {
|
||||
fn supported_protocol_versions(&self) -> std::borrow::Cow<'static, [ProtocolVersion]> {
|
||||
std::borrow::Cow::Owned(
|
||||
crate::jsonrpc::SUPPORTED_PROTOCOL_VERSIONS
|
||||
.iter()
|
||||
.map(|revision| {
|
||||
serde_json::from_value(Value::String((*revision).to_owned()))
|
||||
.expect("registered revisions match SDK versions")
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_info(&self) -> ServerConfig {
|
||||
let mut info = ServerConfig::default();
|
||||
info.capabilities = self.capabilities.clone();
|
||||
info
|
||||
}
|
||||
|
||||
fn accepted_subscription_filter(&self, _: &SubscriptionFilter) -> Option<SubscriptionFilter> {
|
||||
Some(
|
||||
serde_json::from_value(serde_json::json!({
|
||||
"toolsListChanged": true, "resourcesListChanged": true, "promptsListChanged": true
|
||||
}))
|
||||
.expect("closed subscription candidate matches SDK"),
|
||||
)
|
||||
}
|
||||
|
||||
async fn listen(&self, context: SubscriptionContext) -> Result<(), ErrorData> {
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_subscription_listen_entered();
|
||||
let (mut watch, read, baseline) = self
|
||||
.watch
|
||||
.lock()
|
||||
.expect("request-local watch lock")
|
||||
.take()
|
||||
.ok_or_else(unavailable)?;
|
||||
let accepted = context.accepted();
|
||||
watch.signals = signals::Signals {
|
||||
tools: accepted.tools_list_changed == Some(true),
|
||||
resources: accepted.resources_list_changed == Some(true),
|
||||
prompts: accepted.prompts_list_changed == Some(true),
|
||||
};
|
||||
let result = signals::run_delivery(
|
||||
watch,
|
||||
signals::Delivery::Subscription(context.sink().clone()),
|
||||
read,
|
||||
baseline,
|
||||
)
|
||||
.await;
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_subscription_handler_completed();
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn project_meta(message: &Value) -> RequestMetaObject {
|
||||
use crate::jsonrpc::{
|
||||
META_CLIENT_CAPABILITIES_KEY, META_CLIENT_INFO_KEY, META_PROTOCOL_VERSION_KEY,
|
||||
};
|
||||
let raw = &message["params"]["_meta"];
|
||||
// Subscription delivery uses no client features; do not revalidate ignored capability extensions.
|
||||
let mut meta = serde_json::json!({
|
||||
META_PROTOCOL_VERSION_KEY: raw[META_PROTOCOL_VERSION_KEY],
|
||||
META_CLIENT_CAPABILITIES_KEY: {},
|
||||
});
|
||||
if let Some(info) = raw.get(META_CLIENT_INFO_KEY) {
|
||||
meta[META_CLIENT_INFO_KEY] =
|
||||
serde_json::json!({"name": info["name"], "version": info["version"]});
|
||||
}
|
||||
serde_json::from_value(meta).expect("admitted identity projection matches SDK metadata")
|
||||
}
|
||||
|
||||
fn unavailable() -> ErrorData {
|
||||
ErrorData::internal_error("subscription handler unavailable", None)
|
||||
}
|
||||
|
||||
pub(super) async fn start(
|
||||
state: Arc<super::AppState>,
|
||||
workspace_slug: String,
|
||||
agent_slug: String,
|
||||
original_id: Value,
|
||||
requested: SubscriptionFilter,
|
||||
meta: RequestMetaObject,
|
||||
) -> Result<(String, mpsc::Receiver<String>), ErrorData> {
|
||||
let watch = Watch {
|
||||
workspace_slug,
|
||||
agent_slug,
|
||||
signals: signals::Signals::ALL,
|
||||
wire: signals::Wire::Legacy,
|
||||
legacy_stream: None,
|
||||
deadline: None,
|
||||
};
|
||||
let mut read = signals::reader(state, &watch);
|
||||
// Keep the single baseline across ACK setup, including Retry/Gone semantics.
|
||||
let baseline = read().await;
|
||||
let capabilities = serde_json::from_value(crate::protocol::advertised_server_capabilities(
|
||||
crate::jsonrpc::MODERN_PROTOCOL_VERSION,
|
||||
))
|
||||
.expect("registered capabilities match SDK shapes");
|
||||
start_with(
|
||||
Listener {
|
||||
capabilities,
|
||||
watch: Mutex::new(Some((watch, read, baseline))),
|
||||
},
|
||||
requested,
|
||||
meta,
|
||||
original_id,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn start_with(
|
||||
handler: impl ServerHandler,
|
||||
requested: SubscriptionFilter,
|
||||
meta: RequestMetaObject,
|
||||
original_id: Value,
|
||||
) -> Result<(String, mpsc::Receiver<String>), ErrorData> {
|
||||
let (sender, mut receiver) = mpsc::channel(signals::SIGNAL_QUEUE_CAPACITY);
|
||||
let owner = own(handler, requested, meta, original_id, sender);
|
||||
#[cfg(test)]
|
||||
let observer = super::dispatch_observer::installed_observer();
|
||||
#[cfg(test)]
|
||||
let owner = async move {
|
||||
match observer {
|
||||
Some(observer) => super::dispatch_observer::observe(observer, owner).await,
|
||||
None => owner.await,
|
||||
}
|
||||
};
|
||||
let mut task = tokio::spawn(owner);
|
||||
// Dropping this future also drops receiver: the owner closes and joins the SDK pump.
|
||||
tokio::select! {
|
||||
biased;
|
||||
frame = receiver.recv() => match frame {
|
||||
Some(ack) => Ok((ack, receiver)),
|
||||
None => { task.await.map_err(|_| unavailable())??; Err(unavailable()) }
|
||||
},
|
||||
result = &mut task => {
|
||||
result.map_err(|_| unavailable())??;
|
||||
receiver.try_recv().map(|ack| (ack, receiver)).map_err(|_| unavailable())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_subscription_handler_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
async fn own(
|
||||
handler: impl ServerHandler,
|
||||
requested: SubscriptionFilter,
|
||||
meta: RequestMetaObject,
|
||||
original_id: Value,
|
||||
sender: mpsc::Sender<String>,
|
||||
) -> Result<(), ErrorData> {
|
||||
struct Bootstrap;
|
||||
impl ServerHandler for Bootstrap {}
|
||||
let output_sender = sender.clone();
|
||||
#[cfg(test)]
|
||||
let observer = super::dispatch_observer::installed_observer();
|
||||
let output = sink::unfold(output_sender, move |sender, message| {
|
||||
let id = original_id.clone();
|
||||
#[cfg(test)]
|
||||
let observer = observer.clone();
|
||||
async move {
|
||||
let send = async { super::rmcp_subscriptions_output::send(&sender, &id, message) };
|
||||
#[cfg(test)]
|
||||
let send = async {
|
||||
match observer {
|
||||
Some(observer) => super::dispatch_observer::observe(observer, send).await,
|
||||
None => send.await,
|
||||
}
|
||||
};
|
||||
send.await?;
|
||||
Ok::<_, std::io::Error>(sender)
|
||||
}
|
||||
});
|
||||
let running = rmcp::service::serve_directly(
|
||||
Bootstrap,
|
||||
(Box::pin(output), stream::pending::<ClientJsonRpcMessage>()),
|
||||
None,
|
||||
);
|
||||
let mut context =
|
||||
RequestContext::<RoleServer>::new(RequestId::Number(1), running.peer().clone());
|
||||
context.meta = meta;
|
||||
let ct = context.ct.clone();
|
||||
let stop = running.cancellation_token();
|
||||
let pump = running.waiting();
|
||||
tokio::pin!(pump);
|
||||
let mut pump_done = false;
|
||||
let result = {
|
||||
let request = ClientRequest::SubscriptionsListenRequest(SubscriptionsListenRequest::new(
|
||||
SubscriptionsListenRequestParams::new(requested),
|
||||
));
|
||||
let operation = Service::handle_request(&handler, request, context);
|
||||
tokio::pin!(operation);
|
||||
tokio::select! {
|
||||
result = &mut operation => {
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_subscription_handler_consumed();
|
||||
match result {
|
||||
Ok(ServerResult::SubscriptionsListenResult(_)) => Ok(()),
|
||||
Ok(_) => Err(unavailable()),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
},
|
||||
_ = sender.closed() => Ok(()),
|
||||
_ = &mut pump => { pump_done = true; Err(unavailable()) },
|
||||
}
|
||||
};
|
||||
ct.cancel();
|
||||
stop.cancel();
|
||||
if !pump_done {
|
||||
pump.await.map_err(|_| unavailable())?;
|
||||
}
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_subscription_owner_completed();
|
||||
if result.is_err() {
|
||||
tracing::warn!(name: "mcp.subscription.ended", "subscription handler ended with an error");
|
||||
}
|
||||
result
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
use std::io;
|
||||
|
||||
use rmcp::model::{ServerJsonRpcMessage, ServerNotification};
|
||||
use serde_json::Value;
|
||||
|
||||
const SUBSCRIPTION_ID: &str = "io.modelcontextprotocol/subscriptionId";
|
||||
const ACK: &str = "notifications/subscriptions/acknowledged";
|
||||
const RESOURCES: &str = "notifications/resources/list_changed";
|
||||
const PROMPTS: &str = "notifications/prompts/list_changed";
|
||||
const TOOLS: &str = "notifications/tools/list_changed";
|
||||
|
||||
pub(super) fn send(
|
||||
sender: &tokio::sync::mpsc::Sender<String>,
|
||||
original_id: &Value,
|
||||
message: ServerJsonRpcMessage,
|
||||
) -> Result<(), io::Error> {
|
||||
let ServerJsonRpcMessage::Notification(notification) = message else {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"subscription output accepts notifications only",
|
||||
));
|
||||
};
|
||||
|
||||
let (method, is_ack) = match ¬ification.notification {
|
||||
ServerNotification::SubscriptionsAcknowledgedNotification(_) => (ACK, true),
|
||||
ServerNotification::ResourceListChangedNotification(_) => (RESOURCES, false),
|
||||
ServerNotification::PromptListChangedNotification(_) => (PROMPTS, false),
|
||||
ServerNotification::ToolListChangedNotification(_) => (TOOLS, false),
|
||||
_ => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"unsupported subscription notification",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let mut encoded = serde_json::to_value(ServerJsonRpcMessage::Notification(notification))
|
||||
.map_err(io::Error::other)?;
|
||||
if encoded["method"] != method {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"subscription notification method does not match its SDK variant",
|
||||
));
|
||||
}
|
||||
encoded["params"]["_meta"][SUBSCRIPTION_ID] = original_id.clone();
|
||||
let frame = serde_json::to_string(&encoded).map_err(io::Error::other)?;
|
||||
#[cfg(test)]
|
||||
if is_ack {
|
||||
crate::app::dispatch_observer::sdk_subscriptions_acknowledged_serialized();
|
||||
} else {
|
||||
crate::app::dispatch_observer::sdk_signal_notification_serialized();
|
||||
}
|
||||
|
||||
if is_ack {
|
||||
sender.try_send(frame).map_err(|error| match error {
|
||||
tokio::sync::mpsc::error::TrySendError::Full(_) => {
|
||||
io::Error::new(io::ErrorKind::WouldBlock, "subscription ACK queue is full")
|
||||
}
|
||||
tokio::sync::mpsc::error::TrySendError::Closed(_) => {
|
||||
io::Error::new(io::ErrorKind::BrokenPipe, "subscription output is closed")
|
||||
}
|
||||
})?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
match sender.try_send(frame) {
|
||||
Ok(()) | Err(tokio::sync::mpsc::error::TrySendError::Full(_)) => Ok(()),
|
||||
Err(tokio::sync::mpsc::error::TrySendError::Closed(_)) => Err(io::Error::new(
|
||||
io::ErrorKind::BrokenPipe,
|
||||
"subscription output is closed",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod sdk_controls {
|
||||
use super::*;
|
||||
use futures_util::{sink, stream};
|
||||
use rmcp::{
|
||||
ErrorData, RoleServer, ServerHandler, Service,
|
||||
model::{ClientJsonRpcMessage, RequestId, RequestMetaObject, SubscriptionFilter},
|
||||
service::{RequestContext, SubscriptionContext},
|
||||
};
|
||||
use serde_json::json;
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
struct Bootstrap;
|
||||
impl ServerHandler for Bootstrap {}
|
||||
|
||||
struct Listener {
|
||||
entered: Arc<AtomicUsize>,
|
||||
count: usize,
|
||||
fail: bool,
|
||||
}
|
||||
impl ServerHandler for Listener {
|
||||
fn get_info(&self) -> rmcp::model::ServerConfig {
|
||||
let mut info = rmcp::model::ServerConfig::default();
|
||||
info.capabilities =
|
||||
serde_json::from_value(json!({"tools":{"listChanged":true}})).unwrap();
|
||||
info
|
||||
}
|
||||
fn accepted_subscription_filter(
|
||||
&self,
|
||||
_: &SubscriptionFilter,
|
||||
) -> Option<SubscriptionFilter> {
|
||||
Some(
|
||||
serde_json::from_value(json!({"toolsListChanged":true,"promptsListChanged":true}))
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
async fn listen(&self, context: SubscriptionContext) -> Result<(), ErrorData> {
|
||||
self.entered.fetch_add(1, Ordering::SeqCst);
|
||||
assert_eq!(
|
||||
serde_json::to_value(context.accepted()).unwrap(),
|
||||
json!({"toolsListChanged":true})
|
||||
);
|
||||
assert!(context.sink().notify_prompt_list_changed().await.is_err());
|
||||
if self.fail {
|
||||
return Err(ErrorData::internal_error("listener rejected", None));
|
||||
}
|
||||
for _ in 0..self.count {
|
||||
context.sink().notify_tool_list_changed().await.unwrap();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn exercise(original_id: Value, count: usize, fail: bool, closed: bool) {
|
||||
let (sender, mut receiver) = mpsc::channel(4);
|
||||
let entered = Arc::new(AtomicUsize::new(0));
|
||||
let handler = Listener {
|
||||
entered: entered.clone(),
|
||||
count,
|
||||
fail,
|
||||
};
|
||||
let output_id = original_id.clone();
|
||||
let output = sink::unfold(sender, move |sender, message| {
|
||||
let id = output_id.clone();
|
||||
async move {
|
||||
send(&sender, &id, message)?;
|
||||
Ok::<_, io::Error>(sender)
|
||||
}
|
||||
});
|
||||
let mut running = rmcp::service::serve_directly(
|
||||
Bootstrap,
|
||||
(Box::pin(output), stream::pending::<ClientJsonRpcMessage>()),
|
||||
None,
|
||||
);
|
||||
let mut context =
|
||||
RequestContext::<RoleServer>::new(RequestId::Number(1), running.peer().clone());
|
||||
context.meta = RequestMetaObject::with_client_context(
|
||||
rmcp::model::ProtocolVersion::V_2026_07_28,
|
||||
rmcp::model::Implementation::new("output-test", "1"),
|
||||
rmcp::model::ClientCapabilities::default(),
|
||||
);
|
||||
let request = serde_json::from_value(json!({"method":"subscriptions/listen", "params":{"notifications":{"toolsListChanged":true,"promptsListChanged":true,"resourcesListChanged":true,"resourceSubscriptions":["crank://excluded"]}}})).unwrap();
|
||||
|
||||
if closed {
|
||||
drop(receiver);
|
||||
let result = Service::handle_request(&handler, request, context).await;
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
entered.load(Ordering::SeqCst),
|
||||
0,
|
||||
"ACK send failure must prevent listen"
|
||||
);
|
||||
} else {
|
||||
let result = tokio::time::timeout(
|
||||
std::time::Duration::from_secs(3),
|
||||
Service::handle_request(&handler, request, context),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(entered.load(Ordering::SeqCst), 1);
|
||||
let mut frames = Vec::new();
|
||||
while let Ok(frame) = receiver.try_recv() {
|
||||
frames.push(serde_json::from_str::<Value>(&frame).unwrap());
|
||||
}
|
||||
assert_eq!(frames[0]["method"], ACK);
|
||||
assert_eq!(
|
||||
frames[0]["params"]["notifications"],
|
||||
json!({"toolsListChanged":true})
|
||||
);
|
||||
assert_eq!(frames[0]["params"]["_meta"][SUBSCRIPTION_ID], original_id);
|
||||
assert_eq!(
|
||||
frames[0]["params"]["_meta"][SUBSCRIPTION_ID].to_string(),
|
||||
original_id.to_string()
|
||||
);
|
||||
assert_eq!(frames.len(), (count + 1).min(4));
|
||||
for frame in &frames[1..] {
|
||||
assert_eq!(frame["method"], TOOLS);
|
||||
assert_eq!(
|
||||
frame["params"]["_meta"][SUBSCRIPTION_ID].to_string(),
|
||||
original_id.to_string()
|
||||
);
|
||||
}
|
||||
if fail {
|
||||
assert_eq!(result.unwrap_err().message, "listener rejected");
|
||||
} else {
|
||||
assert!(matches!(
|
||||
result.unwrap(),
|
||||
rmcp::model::ServerResult::SubscriptionsListenResult(_)
|
||||
));
|
||||
}
|
||||
}
|
||||
let peer = running.peer().clone();
|
||||
running.close().await.unwrap();
|
||||
assert!(peer.is_transport_closed());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn actual_sdk_ack_notifications_filters_ids_outcomes_and_bounded_delivery() {
|
||||
for encoded in ["18446744073709551615", "1.234567890123456789e+25"] {
|
||||
exercise(serde_json::from_str(encoded).unwrap(), 1, false, false).await;
|
||||
}
|
||||
exercise(json!("burst"), 10, false, false).await;
|
||||
exercise(json!("no-op"), 0, false, false).await;
|
||||
exercise(json!("error"), 0, true, false).await;
|
||||
exercise(json!("closed"), 1, false, true).await;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,241 @@
|
||||
//! 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 _ = context.peer.notify_progress(event.sdk_params(token)).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,85 @@
|
||||
//! Lossless catalog extras only; represented SDK fields remain authoritative.
|
||||
use rmcp::{ErrorData, model::*};
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
pub(super) type Extras = Vec<Map<String, Value>>;
|
||||
pub(super) enum Outcome {
|
||||
List {
|
||||
result: ListToolsResult,
|
||||
extras: Extras,
|
||||
},
|
||||
Call(CallToolResult),
|
||||
Error(ErrorData),
|
||||
}
|
||||
pub(super) fn internal() -> ErrorData {
|
||||
ErrorData::internal_error("internal server error", None)
|
||||
}
|
||||
pub(super) fn list(
|
||||
tools: Vec<Value>,
|
||||
next_cursor: Option<String>,
|
||||
) -> Result<(ListToolsResult, Extras), ErrorData> {
|
||||
let mut extras = Vec::with_capacity(tools.len());
|
||||
let mut typed = Vec::with_capacity(tools.len());
|
||||
for tool in tools {
|
||||
let mut raw = tool.as_object().cloned().ok_or_else(internal)?;
|
||||
let mut extra = Map::new();
|
||||
for key in raw.keys().cloned().collect::<Vec<_>>() {
|
||||
if !matches!(
|
||||
key.as_str(),
|
||||
"name" | "title" | "description" | "inputSchema" | "outputSchema" | "_meta"
|
||||
) || raw[&key].is_null()
|
||||
{
|
||||
extra.insert(key.clone(), raw.remove(&key).expect("existing field"));
|
||||
}
|
||||
}
|
||||
typed.push(serde_json::from_value(Value::Object(raw)).map_err(|_| internal())?);
|
||||
extras.push(extra);
|
||||
}
|
||||
Ok((
|
||||
ListToolsResult {
|
||||
tools: typed,
|
||||
next_cursor,
|
||||
..Default::default()
|
||||
},
|
||||
extras,
|
||||
))
|
||||
}
|
||||
pub(super) fn consume_list(result: Result<ListToolsResult, ErrorData>, extras: Extras) -> Outcome {
|
||||
match result {
|
||||
Ok(result) => Outcome::List { result, extras },
|
||||
Err(error) => Outcome::Error(error),
|
||||
}
|
||||
}
|
||||
pub(super) fn serialize_list(
|
||||
mut result: ListToolsResult,
|
||||
extras: Extras,
|
||||
version: &str,
|
||||
) -> Result<Value, serde_json::Error> {
|
||||
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)?;
|
||||
for (tool, extras) in result["tools"]
|
||||
.as_array_mut()
|
||||
.expect("SDK tools array")
|
||||
.iter_mut()
|
||||
.zip(extras)
|
||||
{
|
||||
let tool = tool.as_object_mut().expect("SDK Tool object");
|
||||
for (key, value) in extras {
|
||||
tool.entry(key).or_insert(value);
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_tools_list_result_serialized();
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(super) fn consume_call(result: Result<CallToolResponse, ErrorData>) -> Outcome {
|
||||
match result {
|
||||
Ok(CallToolResponse::Complete(result)) => Outcome::Call(result),
|
||||
Ok(_) => Outcome::Error(internal()),
|
||||
Err(error) => Outcome::Error(error),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
//! Request-owned Tools trait execution. The caller retains admission and lifecycle ownership.
|
||||
use super::{AppState, lifecycle::ToolCallLifecycle, rmcp_tools_projection as projection};
|
||||
use crate::{
|
||||
auth::VerifiedMachineCredential,
|
||||
protocol::{self, *},
|
||||
};
|
||||
use crank_core::{CorrelationContext, ExecutionLocale};
|
||||
pub(super) use projection::Outcome;
|
||||
use rmcp::{ErrorData, RoleServer, ServerHandler, model::*, service::RequestContext};
|
||||
use serde_json::Value;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Scope {
|
||||
workspace: String,
|
||||
agent: String,
|
||||
credential: VerifiedMachineCredential,
|
||||
correlation: CorrelationContext,
|
||||
continuation: ContinuationSupport,
|
||||
locale: ExecutionLocale,
|
||||
limit: Option<usize>,
|
||||
confirmation: Option<String>,
|
||||
lifecycle: Option<ToolCallLifecycle>,
|
||||
}
|
||||
impl Scope {
|
||||
fn context(&self) -> ServiceContext<'_> {
|
||||
ServiceContext {
|
||||
workspace_slug: &self.workspace,
|
||||
agent_slug: &self.agent,
|
||||
credential: &self.credential,
|
||||
correlation: &self.correlation,
|
||||
continuation: self.continuation,
|
||||
locale: self.locale,
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Clone)]
|
||||
struct Handler {
|
||||
state: Arc<AppState>,
|
||||
extras: Arc<Mutex<projection::Extras>>,
|
||||
#[cfg(test)]
|
||||
domain: Option<Arc<tests::Domain>>,
|
||||
}
|
||||
impl Handler {
|
||||
fn new(state: &Arc<AppState>) -> Self {
|
||||
Self {
|
||||
state: state.clone(),
|
||||
extras: Arc::new(Mutex::new(Vec::new())),
|
||||
#[cfg(test)]
|
||||
domain: None,
|
||||
}
|
||||
}
|
||||
async fn execute(&self, scope: &Scope, command: ToolsCommand) -> ToolsOutcome {
|
||||
#[cfg(test)]
|
||||
if let Some(domain) = &self.domain {
|
||||
return domain.execute(scope, command).await;
|
||||
}
|
||||
protocol::handle_tools_command(
|
||||
&self.state,
|
||||
scope.context(),
|
||||
command,
|
||||
scope.lifecycle.clone(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
fn scope(context: &RequestContext<RoleServer>) -> Result<Scope, ErrorData> {
|
||||
context
|
||||
.extensions
|
||||
.get::<Scope>()
|
||||
.cloned()
|
||||
.ok_or_else(projection::internal)
|
||||
}
|
||||
fn take_extras(&self) -> projection::Extras {
|
||||
self.extras
|
||||
.lock()
|
||||
.expect("request-local extras")
|
||||
.drain(..)
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
impl ServerHandler for Handler {
|
||||
async fn list_tools(
|
||||
&self,
|
||||
request: Option<PaginatedRequestParams>,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<ListToolsResult, ErrorData> {
|
||||
let scope = Self::scope(&context)?;
|
||||
let outcome = self
|
||||
.execute(
|
||||
&scope,
|
||||
ToolsCommand::List(ToolsListCommand {
|
||||
cursor: request.unwrap_or_default().cursor,
|
||||
limit: scope.limit,
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
let result = match outcome {
|
||||
ToolsOutcome::List { tools, next_cursor } => projection::list(tools, next_cursor)
|
||||
.and_then(|(result, extras)| {
|
||||
*self.extras.lock().map_err(|_| projection::internal())? = extras;
|
||||
Ok(result)
|
||||
}),
|
||||
ToolsOutcome::InvalidParams(detail) => Err(ErrorData::invalid_params(detail, None)),
|
||||
_ => Err(projection::internal()),
|
||||
};
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_tools_list_handler_completed();
|
||||
result
|
||||
}
|
||||
async fn call_tool(
|
||||
&self,
|
||||
request: CallToolRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<CallToolResponse, ErrorData> {
|
||||
let scope = Self::scope(&context)?;
|
||||
let outcome = self
|
||||
.execute(
|
||||
&scope,
|
||||
ToolsCommand::Call(ToolCallCommand {
|
||||
tool_name: request.name.into_owned(),
|
||||
arguments: Value::Object(request.arguments.unwrap_or_default()),
|
||||
confirmation_token: scope.confirmation.clone(),
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
let result = match outcome {
|
||||
ToolsOutcome::Call(
|
||||
ToolCallOutcome::Completed(output) | ToolCallOutcome::ApprovalPending(output),
|
||||
) => Ok(super::response::sdk_success_tool_result(output).into()),
|
||||
ToolsOutcome::Call(ToolCallOutcome::Error(error)) => {
|
||||
Ok(super::response::sdk_error_tool_result(error).into())
|
||||
}
|
||||
ToolsOutcome::Call(ToolCallOutcome::ApprovalUnavailable(error)) => {
|
||||
Ok(super::response::sdk_error_tool_result(
|
||||
crate::tool_error::control_tool_error_contract(
|
||||
error,
|
||||
scope.correlation.request_id().as_str(),
|
||||
scope.correlation.trace_id().as_str(),
|
||||
scope.locale,
|
||||
),
|
||||
)
|
||||
.into())
|
||||
}
|
||||
ToolsOutcome::Call(ToolCallOutcome::InvalidParams(detail))
|
||||
| ToolsOutcome::InvalidParams(detail) => Err(ErrorData::invalid_params(detail, None)),
|
||||
_ => Err(projection::internal()),
|
||||
};
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_tools_call_handler_completed();
|
||||
result
|
||||
}
|
||||
}
|
||||
// Context seed only: close the inert transport before running domain code. No Peer callbacks.
|
||||
async fn request_context(
|
||||
handler: &Handler,
|
||||
scope: Scope,
|
||||
) -> Result<RequestContext<RoleServer>, ErrorData> {
|
||||
let ping = serde_json::from_value(serde_json::json!({"jsonrpc":"2.0","id":1,"method":"ping"}))
|
||||
.map_err(|_| projection::internal())?;
|
||||
let (transport, receiver) = rmcp::transport::OneshotTransport::<RoleServer>::new(ping);
|
||||
let info = InitializeRequestParams::new(
|
||||
ClientCapabilities::default(),
|
||||
Implementation::new("crank-tools-context", "1"),
|
||||
)
|
||||
.with_protocol_version(ProtocolVersion::V_2025_11_25);
|
||||
let mut running = rmcp::service::serve_directly(handler.clone(), transport, Some(info));
|
||||
let peer = running.peer().clone();
|
||||
running.close().await.map_err(|_| projection::internal())?;
|
||||
drop(receiver);
|
||||
let mut context = RequestContext::new(RequestId::Number(1), peer);
|
||||
context.extensions.insert(scope);
|
||||
Ok(context)
|
||||
}
|
||||
pub(super) async fn handle(
|
||||
state: &Arc<AppState>,
|
||||
context: ServiceContext<'_>,
|
||||
command: ToolsCommand,
|
||||
lifecycle: Option<ToolCallLifecycle>,
|
||||
) -> Outcome {
|
||||
let (limit, confirmation) = match &command {
|
||||
ToolsCommand::List(list) => (list.limit, None),
|
||||
ToolsCommand::Call(call) => (None, call.confirmation_token.clone()),
|
||||
};
|
||||
let scope = Scope {
|
||||
workspace: context.workspace_slug.to_owned(),
|
||||
agent: context.agent_slug.to_owned(),
|
||||
credential: context.credential.clone(),
|
||||
correlation: context.correlation.clone(),
|
||||
continuation: context.continuation,
|
||||
locale: context.locale,
|
||||
limit,
|
||||
confirmation,
|
||||
lifecycle,
|
||||
};
|
||||
let handler = Handler::new(state);
|
||||
let context = match request_context(&handler, scope).await {
|
||||
Ok(context) => context,
|
||||
Err(error) => return Outcome::Error(error),
|
||||
};
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_tools_dispatch_enter();
|
||||
match command {
|
||||
ToolsCommand::List(list) => {
|
||||
let result = ServerHandler::list_tools(
|
||||
&handler,
|
||||
Some(PaginatedRequestParams::default().with_cursor(list.cursor)),
|
||||
context,
|
||||
)
|
||||
.await;
|
||||
let outcome = projection::consume_list(result, handler.take_extras());
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_tools_list_handler_consumed();
|
||||
outcome
|
||||
}
|
||||
ToolsCommand::Call(call) => {
|
||||
let Value::Object(arguments) = call.arguments else {
|
||||
return Outcome::Error(ErrorData::invalid_params(
|
||||
"tool arguments must be an object",
|
||||
None,
|
||||
));
|
||||
};
|
||||
let result = ServerHandler::call_tool(
|
||||
&handler,
|
||||
CallToolRequestParams::new(call.tool_name).with_arguments(arguments),
|
||||
context,
|
||||
)
|
||||
.await;
|
||||
let outcome = projection::consume_call(result);
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_tools_call_handler_consumed();
|
||||
outcome
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
#[path = "../rmcp_tools_handler_tests.rs"]
|
||||
mod tests;
|
||||
@@ -16,9 +16,9 @@ use crank_core::{
|
||||
use crank_registry::PostgresRegistry;
|
||||
use crank_registry::PublishedAgentTool;
|
||||
use crank_runtime::{
|
||||
ExecutionAuthorization, RuntimeExecutor, RuntimeOperation, SecretCrypto, TaskAttemptAuthority,
|
||||
TaskAttemptContext, TaskAttemptOutcome, TaskAttemptPlan, TaskAttemptRefusal, TaskHandler,
|
||||
TaskService, TaskWorkerError,
|
||||
ExecutionAuthorization, PrepareAttempt, RuntimeExecutor, RuntimeOperation, SecretCrypto,
|
||||
TaskAttemptAuthority, TaskAttemptContext, TaskAttemptOutcome, TaskAttemptPlan,
|
||||
TaskAttemptRefusal, TaskHandler, TaskService, TaskWorkerError,
|
||||
};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
@@ -233,6 +233,39 @@ impl TaskWorkerHandler {
|
||||
(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 {
|
||||
@@ -264,7 +297,7 @@ impl TaskAttemptAuthority for TaskWorkerHandler {
|
||||
async fn prepare_attempt(
|
||||
&self,
|
||||
lease: &TaskLease,
|
||||
) -> Result<TaskAttemptPlan, TaskAttemptRefusal> {
|
||||
) -> Result<PrepareAttempt, TaskAttemptRefusal> {
|
||||
let tool = self.authorize(lease).await.map_err(Self::refusal)?;
|
||||
let input = self
|
||||
.service
|
||||
@@ -281,6 +314,24 @@ impl TaskAttemptAuthority for TaskWorkerHandler {
|
||||
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());
|
||||
@@ -313,7 +364,7 @@ impl TaskAttemptAuthority for TaskWorkerHandler {
|
||||
);
|
||||
let (retry_allowed, upstream_key_digest) =
|
||||
self.retry_classification(&operation, &input, deadline_ms - now_ms);
|
||||
Ok(TaskAttemptPlan {
|
||||
Ok(PrepareAttempt::Ready(Box::new(TaskAttemptPlan {
|
||||
workspace_id,
|
||||
origin: ExecutionOrigin::AgentSnapshot,
|
||||
agent_id: Some(agent_id),
|
||||
@@ -325,6 +376,6 @@ impl TaskAttemptAuthority for TaskWorkerHandler {
|
||||
deadline,
|
||||
retry_allowed,
|
||||
upstream_key_digest,
|
||||
})
|
||||
})))
|
||||
}
|
||||
}
|
||||
@@ -299,6 +299,43 @@ impl TaskApplication {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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).
|
||||
@@ -60,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 {
|
||||
@@ -104,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));
|
||||
}
|
||||
}
|
||||
@@ -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 use the separate response SDK classifier.
|
||||
/// 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_do_not_enter_method_dispatch() {
|
||||
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,69 @@
|
||||
//! Typed negotiation results; admission and session lifecycle remain in the HTTP adapter.
|
||||
|
||||
use rmcp::model::InitializeRequestParams;
|
||||
#[cfg(test)]
|
||||
use rmcp::model::{InitializeResult, ServerCapabilities};
|
||||
use serde_json::Value;
|
||||
|
||||
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": ""}
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn capabilities(advertised: &Value) -> ServerCapabilities {
|
||||
serde_json::from_value(advertised.clone())
|
||||
.expect("registry capability fragments match SDK capability shapes")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
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(¬ification);
|
||||
notification.into()
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,9 @@ use std::{
|
||||
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;
|
||||
@@ -61,7 +64,8 @@ pub(crate) enum LogLevel {
|
||||
}
|
||||
|
||||
impl LogLevel {
|
||||
/// The exact wire literal of this level.
|
||||
/// 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",
|
||||
@@ -80,17 +84,47 @@ impl LogLevel {
|
||||
/// 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> {
|
||||
match value {
|
||||
"debug" => Some(Self::Debug),
|
||||
"info" => Some(Self::Info),
|
||||
"notice" => Some(Self::Notice),
|
||||
"warning" => Some(Self::Warning),
|
||||
"error" => Some(Self::Error),
|
||||
"critical" => Some(Self::Critical),
|
||||
"alert" => Some(Self::Alert),
|
||||
"emergency" => Some(Self::Emergency),
|
||||
_ => None,
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -135,15 +169,25 @@ pub(crate) struct SetLevelCommand {
|
||||
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.
|
||||
pub(crate) fn validate(&self) -> Result<LogLevel, &'static str> {
|
||||
#[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);
|
||||
}
|
||||
LogLevel::parse(&self.level).ok_or(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.
|
||||
/// Compatibility outcome for the original protocol unit tests; runtime uses the SDK handler.
|
||||
#[cfg(test)]
|
||||
pub(crate) enum LoggingOutcome {
|
||||
/// The level of this client context was accepted.
|
||||
Applied(LogLevel),
|
||||
@@ -282,19 +326,22 @@ pub(crate) fn take_notifications() -> Vec<Value> {
|
||||
}
|
||||
std::mem::take(&mut log.pending)
|
||||
.into_iter()
|
||||
.map(|event| {
|
||||
json!({
|
||||
.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": {
|
||||
"level": event.level.as_str(),
|
||||
"logger": LOGGER_NAME,
|
||||
"data": {
|
||||
"event": event.event,
|
||||
"outcome": event.outcome,
|
||||
}
|
||||
}
|
||||
})
|
||||
"params": params,
|
||||
}))
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
@@ -348,13 +395,16 @@ impl LogLevelStore {
|
||||
///
|
||||
/// 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.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn handle_logging_command(
|
||||
store: &LogLevelStore,
|
||||
session_id: &str,
|
||||
command: SetLevelCommand,
|
||||
) -> LoggingOutcome {
|
||||
match command.validate() {
|
||||
Ok(level) => {
|
||||
Ok(request) => {
|
||||
#[allow(deprecated)]
|
||||
let level = request.level.into();
|
||||
store.set(session_id, level);
|
||||
LoggingOutcome::Applied(level)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@ use super::*;
|
||||
/// 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()?;
|
||||
Some(command.validate())
|
||||
#[allow(deprecated)]
|
||||
Some(command.validate().map(|request| request.level.into()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -291,8 +292,11 @@ fn the_modern_level_is_read_from_the_pinned_meta_key_only() {
|
||||
fn the_command_records_the_level_of_its_own_session_only() {
|
||||
let store = LogLevelStore::new();
|
||||
store.set("other-session", LogLevel::Debug);
|
||||
let command: SetLevelCommand =
|
||||
serde_json::from_value(json!({ "level": "critical" })).expect("a pinned level parses");
|
||||
// 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,
|
||||
|
||||
@@ -26,22 +26,30 @@ 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;
|
||||
mod response_sdk;
|
||||
pub(crate) 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::{initialize_params, initialized_notification};
|
||||
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,
|
||||
LogLevel, LogLevelStore, SetLevelCommand, carry_scope, 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,
|
||||
@@ -52,7 +60,7 @@ pub(crate) use resources::{
|
||||
};
|
||||
pub(crate) use signals::{
|
||||
FilterOutcome, INVALID_LISTEN_FILTER, ListenParams, Signals, StreamRegistry, Watch, Wire,
|
||||
acknowledged_frame, frames_stream, spawn as spawn_signal_watch, subscription_id,
|
||||
frames_stream, spawn as spawn_signal_watch, subscription_id,
|
||||
};
|
||||
|
||||
/// Resolved era of an incoming POST request.
|
||||
@@ -81,50 +89,6 @@ fn is_valid_request_id(id: &Value) -> bool {
|
||||
id.is_string() || id.is_number()
|
||||
}
|
||||
|
||||
/// The pinned error object: `Error { code: number; message: string; data?: unknown }`.
|
||||
fn is_valid_error_object(error: Option<&Value>) -> bool {
|
||||
let Some(Value::Object(error)) = error else {
|
||||
return false;
|
||||
};
|
||||
error.get("code").is_some_and(Value::is_number)
|
||||
&& error.get("message").is_some_and(Value::is_string)
|
||||
}
|
||||
|
||||
/// Classify a wire response (a message without `method`).
|
||||
///
|
||||
/// Pinned shapes are exclusive: a success response carries `result: Result` (an object) and a required
|
||||
/// `id: RequestId`, an error response carries `error: Error` and may omit the id (a present id must
|
||||
/// still be a `RequestId`). Both together, neither, a foreign id, a non-object `result` or a malformed
|
||||
/// error object are all invalid requests rather than an acknowledgement.
|
||||
///
|
||||
/// The `result` value is required to be an object because the pinned `JSONRPCResultResponse` types it as
|
||||
/// `Result`; the modern `resultType` member is deliberately **not** required here, because an incoming
|
||||
/// response may come from a client implementing an earlier revision.
|
||||
fn classify_wire_response(object: &serde_json::Map<String, Value>) -> Option<EnvelopeKind> {
|
||||
match (object.contains_key("result"), object.contains_key("error")) {
|
||||
(true, false) => {
|
||||
if !matches!(object.get("result"), Some(Value::Object(_))) {
|
||||
return None;
|
||||
}
|
||||
object
|
||||
.get("id")
|
||||
.filter(|id| is_valid_request_id(id))
|
||||
.map(|_| EnvelopeKind::Response)
|
||||
}
|
||||
(false, true) => {
|
||||
if !is_valid_error_object(object.get("error")) {
|
||||
return None;
|
||||
}
|
||||
match object.get("id") {
|
||||
None => Some(EnvelopeKind::Response),
|
||||
Some(id) if is_valid_request_id(id) => Some(EnvelopeKind::Response),
|
||||
Some(_) => None,
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Pinned modern wire projection of a success result.
|
||||
///
|
||||
/// `JSONRPCResultResponse.result` is a `Result`, and this revision requires the `resultType` member
|
||||
@@ -250,7 +214,7 @@ pub(crate) fn validate_jsonrpc_envelope(message: &Value) -> Result<EnvelopeKind,
|
||||
}
|
||||
}
|
||||
Some(_) => Err(rejected(invalid_request_response())),
|
||||
None => match classify_wire_response(object) {
|
||||
None => match response_sdk::classify_response(message) {
|
||||
Some(kind) => Ok(kind),
|
||||
None => Err(rejected(invalid_request_response())),
|
||||
},
|
||||
@@ -841,20 +805,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"
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
//! SDK classification of inbound responses, without narrowing the admitted wire numbers.
|
||||
|
||||
use rmcp::model::{JsonRpcError, JsonRpcResponse};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::EnvelopeKind;
|
||||
|
||||
enum ResponseEnvelope {
|
||||
Success(JsonRpcResponse<Value>),
|
||||
Error(JsonRpcError),
|
||||
}
|
||||
|
||||
/// Only the no-method branch of the envelope guard calls this helper.
|
||||
pub(super) fn classify_response(message: &Value) -> Option<EnvelopeKind> {
|
||||
match decode_response(message)? {
|
||||
ResponseEnvelope::Success(JsonRpcResponse { result, .. }) => {
|
||||
// The generic SDK result must still satisfy the pinned object-only contract.
|
||||
if !result.is_object() {
|
||||
return None;
|
||||
}
|
||||
#[cfg(test)]
|
||||
crate::app::dispatch_observer::sdk_inbound_response_consumed();
|
||||
Some(EnvelopeKind::Response)
|
||||
}
|
||||
ResponseEnvelope::Error(JsonRpcError { .. }) => {
|
||||
#[cfg(test)]
|
||||
crate::app::dispatch_observer::sdk_inbound_error_consumed();
|
||||
Some(EnvelopeKind::Response)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_response(message: &Value) -> Option<ResponseEnvelope> {
|
||||
let object = message.as_object()?;
|
||||
if object.get("jsonrpc")?.as_str()? != "2.0" || object.contains_key("method") {
|
||||
return None;
|
||||
}
|
||||
let id = object.get("id");
|
||||
if id.is_some_and(|id| !id.is_string() && !id.is_number()) {
|
||||
return None;
|
||||
}
|
||||
let mut projection = json!({"jsonrpc": "2.0"});
|
||||
if let Some(id) = id {
|
||||
// SDK ids are string/i64; this surrogate stays in the classification-only clone.
|
||||
projection["id"] = if id.is_number() && id.as_i64().is_none() {
|
||||
json!(0)
|
||||
} else {
|
||||
id.clone()
|
||||
};
|
||||
}
|
||||
match (object.get("result"), object.get("error")) {
|
||||
(Some(result), None) if result.is_object() && id.is_some() => {
|
||||
projection["result"] = result.clone();
|
||||
let response = serde_json::from_value::<JsonRpcResponse<Value>>(projection).ok()?;
|
||||
#[cfg(test)]
|
||||
crate::app::dispatch_observer::sdk_inbound_response_decoded();
|
||||
Some(ResponseEnvelope::Success(response))
|
||||
}
|
||||
(None, Some(error)) if error.is_object() => {
|
||||
let code = error.get("code")?;
|
||||
if !code.is_number() || !error.get("message")?.is_string() {
|
||||
return None;
|
||||
}
|
||||
projection["error"] = error.clone();
|
||||
// SDK codes are i32; validate the original number before projecting only that field.
|
||||
if code
|
||||
.as_i64()
|
||||
.and_then(|code| i32::try_from(code).ok())
|
||||
.is_none()
|
||||
{
|
||||
projection["error"]["code"] = json!(0);
|
||||
}
|
||||
let error = serde_json::from_value::<JsonRpcError>(projection).ok()?;
|
||||
#[cfg(test)]
|
||||
crate::app::dispatch_observer::sdk_inbound_error_decoded();
|
||||
Some(ResponseEnvelope::Error(error))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn accepts_response_shapes_without_constraining_params_or_extensions() {
|
||||
for message in [
|
||||
json!({"jsonrpc": "2.0", "id": "", "result": {}}),
|
||||
json!({"jsonrpc": "2.0", "id": 1, "result": {"resultType": false},
|
||||
"params": false, "extension": [null, 1]}),
|
||||
json!({"jsonrpc": "2.0", "error": {"code": -1, "message": ""}}),
|
||||
json!({"jsonrpc": "2.0", "id": "id", "error": {
|
||||
"code": -1, "message": "error", "data": [null, {"nested": true}]
|
||||
}, "params": [], "extension": false}),
|
||||
] {
|
||||
let original = message.to_string();
|
||||
assert_eq!(classify_response(&message), Some(EnvelopeKind::Response));
|
||||
assert_eq!(message.to_string(), original);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_shapes_before_sdk_projection() {
|
||||
for message in [
|
||||
Value::Null,
|
||||
json!([]),
|
||||
json!({"id": 1, "result": {}}),
|
||||
json!({"jsonrpc": "1.0", "id": 1, "result": {}}),
|
||||
json!({"jsonrpc": "2.0"}),
|
||||
json!({"jsonrpc": "2.0", "result": {}}),
|
||||
json!({"jsonrpc": "2.0", "id": 1, "result": {}, "error": null}),
|
||||
json!({"jsonrpc": "2.0", "id": 1, "result": null}),
|
||||
json!({"jsonrpc": "2.0", "id": 1, "result": []}),
|
||||
json!({"jsonrpc": "2.0", "id": 1, "result": true}),
|
||||
json!({"jsonrpc": "2.0", "error": null}),
|
||||
json!({"jsonrpc": "2.0", "error": []}),
|
||||
json!({"jsonrpc": "2.0", "error": {"message": "error"}}),
|
||||
json!({"jsonrpc": "2.0", "error": {"code": "-1", "message": "error"}}),
|
||||
json!({"jsonrpc": "2.0", "error": {"code": null, "message": "error"}}),
|
||||
json!({"jsonrpc": "2.0", "error": {"code": true, "message": "error"}}),
|
||||
json!({"jsonrpc": "2.0", "error": {"code": -1}}),
|
||||
json!({"jsonrpc": "2.0", "error": {"code": -1, "message": false}}),
|
||||
json!({"jsonrpc": "2.0", "method": null, "id": 1, "result": {}}),
|
||||
json!({"jsonrpc": "2.0", "method": "request", "id": 1, "result": {}}),
|
||||
] {
|
||||
assert_eq!(classify_response(&message), None, "{message}");
|
||||
}
|
||||
for id in [Value::Null, json!(true), json!([]), json!({})] {
|
||||
for payload in [
|
||||
json!({"result": {}}),
|
||||
json!({"error": {"code": -1, "message": "e"}}),
|
||||
] {
|
||||
let mut message = payload;
|
||||
message["jsonrpc"] = json!("2.0");
|
||||
message["id"] = id.clone();
|
||||
assert_eq!(classify_response(&message), None, "{message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numeric_projection_preserves_raw_json_and_executes_typed_variants() {
|
||||
let numbers = [
|
||||
json!(i64::MIN),
|
||||
json!(i64::MAX),
|
||||
json!(u64::MAX),
|
||||
json!(i32::MIN),
|
||||
json!(i32::MAX),
|
||||
json!(2147483648_i64),
|
||||
json!(-2147483649_i64),
|
||||
json!(1.25),
|
||||
json!(1.0),
|
||||
json!(-0.0),
|
||||
json!(f64::MAX),
|
||||
];
|
||||
for number in numbers {
|
||||
let sdk_id = if number.as_i64().is_some() {
|
||||
number.clone()
|
||||
} else {
|
||||
json!(0)
|
||||
};
|
||||
let sdk_code = if number
|
||||
.as_i64()
|
||||
.and_then(|n| i32::try_from(n).ok())
|
||||
.is_some()
|
||||
{
|
||||
number.clone()
|
||||
} else {
|
||||
json!(0)
|
||||
};
|
||||
for error in [false, true] {
|
||||
let mut message = json!({"jsonrpc": "2.0", "id": number});
|
||||
if error {
|
||||
message["error"] = json!({"code": number, "message": "e", "data": number});
|
||||
} else {
|
||||
message["result"] = json!({"nested": [number, null]});
|
||||
}
|
||||
let original = message.to_string();
|
||||
match decode_response(&message).expect("numeric wire shape decodes") {
|
||||
ResponseEnvelope::Success(response) => {
|
||||
assert!(!error);
|
||||
assert_eq!(serde_json::to_value(response.id).unwrap(), sdk_id);
|
||||
assert_eq!(response.result, message["result"]);
|
||||
}
|
||||
ResponseEnvelope::Error(response) => {
|
||||
assert!(error);
|
||||
assert_eq!(serde_json::to_value(response.id).unwrap(), sdk_id);
|
||||
assert_eq!(serde_json::to_value(response.error.code).unwrap(), sdk_code);
|
||||
assert_eq!(response.error.data.as_ref(), Some(&number));
|
||||
}
|
||||
}
|
||||
assert_eq!(classify_response(&message), Some(EnvelopeKind::Response));
|
||||
assert_eq!(message.to_string(), original);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_error_keeps_optional_id_and_arbitrary_data() {
|
||||
for data in [
|
||||
Value::Null,
|
||||
json!(true),
|
||||
json!(1.0),
|
||||
json!("text"),
|
||||
json!([]),
|
||||
json!({}),
|
||||
] {
|
||||
let message =
|
||||
json!({"jsonrpc": "2.0", "error": {"code": -1, "message": "e", "data": data}});
|
||||
let ResponseEnvelope::Error(error) = decode_response(&message).unwrap() else {
|
||||
panic!("error must retain its SDK variant");
|
||||
};
|
||||
assert_eq!(error.id, None);
|
||||
// SDK Option<Value> maps explicit null to None; classification never rewrites the wire.
|
||||
assert_eq!(
|
||||
error.error.data,
|
||||
if data.is_null() { None } else { Some(data) }
|
||||
);
|
||||
assert_eq!(classify_response(&message), Some(EnvelopeKind::Response));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,8 +42,12 @@ use std::{
|
||||
use axum::response::sse::Event;
|
||||
use crank_registry::PublishedAgentCatalog;
|
||||
use futures_util::stream;
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Map, Value, json};
|
||||
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;
|
||||
|
||||
@@ -60,6 +64,7 @@ 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
|
||||
@@ -113,78 +118,94 @@ impl Signals {
|
||||
}
|
||||
}
|
||||
|
||||
/// The pinned modern `SubscriptionFilter`, as far as this server reads it.
|
||||
///
|
||||
/// Unknown members are ignored and a malformed shape is a deserialization error, so a client cannot
|
||||
/// reach the watcher with anything but the reviewed field set.
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct ListenFilter {
|
||||
#[serde(default)]
|
||||
tools_list_changed: bool,
|
||||
#[serde(default)]
|
||||
prompts_list_changed: bool,
|
||||
#[serde(default)]
|
||||
resources_list_changed: bool,
|
||||
#[serde(default)]
|
||||
resource_subscriptions: Vec<String>,
|
||||
/// 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(())
|
||||
}
|
||||
|
||||
/// The pinned `SubscriptionsListenRequestParams`: the notification filter is a required member.
|
||||
///
|
||||
/// Only the reviewed member is read; `_meta` and any other member of the request params is not part
|
||||
/// of this contract.
|
||||
#[derive(Debug, Deserialize)]
|
||||
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.
|
||||
// Compatibility projection retained for the existing filter controls; SDK owns runtime acceptance.
|
||||
#[cfg_attr(not(test), allow(dead_code))]
|
||||
Accepted(Signals),
|
||||
/// One safe detail: no URI, name or value the client sent is echoed back.
|
||||
Invalid(&'static str),
|
||||
}
|
||||
|
||||
impl ListenFilter {
|
||||
pub(crate) fn into_sdk(self) -> SubscriptionFilter {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
if self.resource_subscriptions.len() > MAX_FILTER_URIS
|
||||
|| self
|
||||
.resource_subscriptions
|
||||
.iter()
|
||||
.any(|uri| uri.len() > MAX_FILTER_URI_BYTES)
|
||||
{
|
||||
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.resources_list_changed,
|
||||
prompts: self.prompts_list_changed,
|
||||
tools: self.tools_list_changed,
|
||||
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 `notifications` object of the acknowledgment: exactly the subset that will be delivered.
|
||||
fn acknowledged_filter_wire(accepted: Signals) -> Value {
|
||||
let mut filter = Map::new();
|
||||
if accepted.prompts {
|
||||
filter.insert("promptsListChanged".to_owned(), Value::Bool(true));
|
||||
}
|
||||
if accepted.resources {
|
||||
filter.insert("resourcesListChanged".to_owned(), Value::Bool(true));
|
||||
}
|
||||
if accepted.tools {
|
||||
filter.insert("toolsListChanged".to_owned(), Value::Bool(true));
|
||||
}
|
||||
Value::Object(filter)
|
||||
}
|
||||
|
||||
/// 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
|
||||
@@ -254,6 +275,7 @@ 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.
|
||||
#[cfg(test)]
|
||||
Modern(Value),
|
||||
}
|
||||
|
||||
@@ -268,31 +290,21 @@ impl Wire {
|
||||
if !changed {
|
||||
continue;
|
||||
}
|
||||
let frame = match self {
|
||||
Wire::Legacy => json!({ "jsonrpc": "2.0", "method": method }),
|
||||
Wire::Modern(subscription_id) => json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": method,
|
||||
"params": { "_meta": { SUBSCRIPTION_ID_KEY: subscription_id } }
|
||||
}),
|
||||
let subscription_id = match self {
|
||||
Wire::Legacy => None,
|
||||
#[cfg(test)]
|
||||
Wire::Modern(subscription_id) => Some(subscription_id),
|
||||
};
|
||||
frames.push(frame.to_string());
|
||||
frames.push(sdk::signal_frame(method, subscription_id));
|
||||
}
|
||||
frames
|
||||
}
|
||||
}
|
||||
|
||||
/// The first modern frame: the accepted subset, before any list-change notification of the stream.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn acknowledged_frame(subscription_id: &Value, accepted: Signals) -> String {
|
||||
json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": SUBSCRIPTIONS_ACKNOWLEDGED,
|
||||
"params": {
|
||||
"_meta": { SUBSCRIPTION_ID_KEY: subscription_id },
|
||||
"notifications": acknowledged_filter_wire(accepted),
|
||||
}
|
||||
})
|
||||
.to_string()
|
||||
sdk::acknowledged_frame(subscription_id, accepted)
|
||||
}
|
||||
|
||||
/// The subscription id of a `subscriptions/listen` request, or `None` when its encoding is not a
|
||||
@@ -379,22 +391,87 @@ pub(crate) type Reader = Box<dyn FnMut() -> ReadFuture + Send>;
|
||||
///
|
||||
/// 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> {
|
||||
pub(crate) fn reader(state: Arc<AppState>, watch: &Watch) -> Reader {
|
||||
let scope = (watch.workspace_slug.clone(), watch.agent_slug.clone());
|
||||
let reader: Reader = Box::new(move || {
|
||||
Box::new(move || {
|
||||
let state = Arc::clone(&state);
|
||||
let scope = scope.clone();
|
||||
Box::pin(async move { read(&state, &scope).await })
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn spawn(state: Arc<AppState>, watch: Watch) -> mpsc::Receiver<String> {
|
||||
let reader = reader(state, &watch);
|
||||
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
|
||||
}
|
||||
|
||||
pub(crate) enum Delivery {
|
||||
Frames(mpsc::Sender<String>),
|
||||
Subscription(rmcp::service::SubscriptionSink),
|
||||
}
|
||||
|
||||
impl Delivery {
|
||||
async fn send(&self, wire: &Wire, signals: Signals) -> Result<(), rmcp::ErrorData> {
|
||||
match self {
|
||||
Self::Frames(sender) => {
|
||||
for frame in wire.signal_frames(signals) {
|
||||
let _ = sender.try_send(frame);
|
||||
}
|
||||
}
|
||||
Self::Subscription(sink) => {
|
||||
if signals.resources {
|
||||
sink.notify_resource_list_changed()
|
||||
.await
|
||||
.map_err(send_error)?;
|
||||
}
|
||||
if signals.prompts {
|
||||
sink.notify_prompt_list_changed()
|
||||
.await
|
||||
.map_err(send_error)?;
|
||||
}
|
||||
if signals.tools {
|
||||
sink.notify_tool_list_changed().await.map_err(send_error)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn send_error(_: rmcp::service::SubscriptionSendError) -> rmcp::ErrorData {
|
||||
rmcp::ErrorData::internal_error("subscription output unavailable", None)
|
||||
}
|
||||
|
||||
async fn run(watch: Watch, sender: mpsc::Sender<String>, mut read: Reader) {
|
||||
let mut baseline = match read().await {
|
||||
let initial = read().await;
|
||||
let _ = run_delivery(watch, Delivery::Frames(sender), read, initial).await;
|
||||
}
|
||||
|
||||
pub(crate) async fn run_delivery(
|
||||
watch: Watch,
|
||||
delivery: Delivery,
|
||||
mut read: Reader,
|
||||
initial: Result<CatalogFingerprint, ReadError>,
|
||||
) -> Result<(), rmcp::ErrorData> {
|
||||
let mut baseline = match initial {
|
||||
Ok(fingerprint) => Some(fingerprint),
|
||||
Err(ReadError::Gone) => return,
|
||||
Err(ReadError::Gone) => return Ok(()),
|
||||
Err(ReadError::Retry) => {
|
||||
observe_failure();
|
||||
None
|
||||
@@ -402,36 +479,33 @@ async fn run(watch: Watch, sender: mpsc::Sender<String>, mut read: Reader) {
|
||||
};
|
||||
|
||||
loop {
|
||||
if !still_owned(&watch) || sender.is_closed() {
|
||||
return;
|
||||
if !still_owned(&watch)
|
||||
|| matches!(&delivery, Delivery::Frames(sender) if sender.is_closed())
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
if watch
|
||||
.deadline
|
||||
.is_some_and(|deadline| Instant::now() >= deadline)
|
||||
{
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
tokio::time::sleep(POLL_INTERVAL).await;
|
||||
if !still_owned(&watch) {
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
delivery.send(&watch.wire, changes).await?;
|
||||
}
|
||||
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,
|
||||
Err(ReadError::Gone) => return Ok(()),
|
||||
// The committed catalog is untouched: the failure is observable and this tick is skipped.
|
||||
Err(_) => observe_failure(),
|
||||
}
|
||||
@@ -482,15 +556,39 @@ pub(crate) fn frames_stream<G: Send + 'static>(
|
||||
guard: G,
|
||||
) -> impl stream::Stream<Item = Result<Event, std::convert::Infallible>> {
|
||||
let pending: VecDeque<String> = first.into();
|
||||
#[cfg(test)]
|
||||
let observer = crate::app::dispatch_observer::installed_observer();
|
||||
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)));
|
||||
move |(mut pending, mut receiver, guard)| {
|
||||
#[cfg(test)]
|
||||
let observer = observer.clone();
|
||||
async move {
|
||||
loop {
|
||||
if let Some(frame) = pending.pop_front() {
|
||||
#[cfg(test)]
|
||||
if let Ok(value) = serde_json::from_str::<Value>(&frame)
|
||||
&& value["params"]["_meta"].get(SUBSCRIPTION_ID_KEY).is_some()
|
||||
&& matches!(
|
||||
value["method"].as_str(),
|
||||
Some(
|
||||
RESOURCE_LIST_CHANGED | PROMPT_LIST_CHANGED | TOOL_LIST_CHANGED
|
||||
)
|
||||
)
|
||||
&& let Some(observer) = observer.as_ref()
|
||||
{
|
||||
crate::app::dispatch_observer::observe(Arc::clone(observer), async {
|
||||
crate::app::dispatch_observer::sdk_subscription_notification_consumed();
|
||||
}).await;
|
||||
}
|
||||
return Some((
|
||||
Ok(Event::default().data(frame)),
|
||||
(pending, receiver, guard),
|
||||
));
|
||||
}
|
||||
let frame = receiver.recv().await?;
|
||||
pending.push_back(frame);
|
||||
}
|
||||
let frame = receiver.recv().await?;
|
||||
pending.push_back(frame);
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
//! SDK serialization for the stream-local list-change notifications.
|
||||
|
||||
use rmcp::model::{
|
||||
JsonRpcNotification, JsonRpcVersion2_0, NotificationMetaObject, PromptListChangedNotification,
|
||||
ResourceListChangedNotification, ToolListChangedNotification,
|
||||
};
|
||||
#[cfg(test)]
|
||||
use rmcp::model::{
|
||||
SubscriptionFilter, SubscriptionsAcknowledgedNotification,
|
||||
SubscriptionsAcknowledgedNotificationParams,
|
||||
};
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
#[cfg(test)]
|
||||
use super::Signals;
|
||||
use super::{PROMPT_LIST_CHANGED, RESOURCE_LIST_CHANGED, 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
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
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,30 @@
|
||||
//! Completion actual ServerHandler execution/consumption observer oracle.
|
||||
use super::{CounterSet, observe};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub(super) fn counts(c: &CounterSet) -> [usize; 2] {
|
||||
[
|
||||
c.sdk_completion_handler_completed(),
|
||||
c.sdk_completion_handler_consumed(),
|
||||
]
|
||||
}
|
||||
|
||||
pub(super) fn delta(c: &CounterSet, before: [usize; 2]) -> ([usize; 2], [usize; 2]) {
|
||||
let after = counts(c);
|
||||
(std::array::from_fn(|i| after[i] - before[i]), [1, 1])
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_completion_handler_observer_positive_control() {
|
||||
use super::{sdk_completion_handler_completed, sdk_completion_handler_consumed};
|
||||
let counters = Arc::new(CounterSet::default());
|
||||
sdk_completion_handler_completed();
|
||||
sdk_completion_handler_consumed();
|
||||
assert_eq!(counts(&counters), [0, 0], "out-of-scope markers are no-ops");
|
||||
observe(counters.clone(), async {
|
||||
sdk_completion_handler_completed();
|
||||
sdk_completion_handler_consumed();
|
||||
})
|
||||
.await;
|
||||
assert_eq!(counts(&counters), [1, 1]);
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
//! Real trait execution, lossless request context, typed result consumption and drop ownership.
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
use std::sync::{
|
||||
Mutex,
|
||||
atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst},
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct Domain {
|
||||
outcome: Mutex<Option<CompletionOutcome>>,
|
||||
calls: AtomicUsize,
|
||||
pending: bool,
|
||||
dropped: AtomicBool,
|
||||
finished: AtomicBool,
|
||||
}
|
||||
struct DropWitness<'a>(&'a AtomicBool);
|
||||
impl Drop for DropWitness<'_> {
|
||||
fn drop(&mut self) {
|
||||
self.0.store(true, SeqCst);
|
||||
}
|
||||
}
|
||||
impl Domain {
|
||||
pub(super) async fn complete(&self, command: CompletionCommand) -> CompletionOutcome {
|
||||
self.calls.fetch_add(1, SeqCst);
|
||||
assert!(
|
||||
matches!(command.reference, CompletionReference::Resource { uri } if uri == "crank://bound/{region}")
|
||||
);
|
||||
assert_eq!(command.argument.name, "region");
|
||||
assert_eq!(command.argument.value, "e");
|
||||
assert_eq!(command.context.unwrap().arguments.unwrap(), args());
|
||||
let _guard = DropWitness(&self.dropped);
|
||||
if self.pending {
|
||||
std::future::pending::<()>().await;
|
||||
}
|
||||
self.finished.store(true, SeqCst);
|
||||
self.outcome
|
||||
.lock()
|
||||
.unwrap()
|
||||
.take()
|
||||
.expect("single domain execution")
|
||||
}
|
||||
}
|
||||
fn args() -> Map<String, Value> {
|
||||
json!({"number":42,"bool":true,"null":null,"array":["x"],"object":{"a":1}})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone()
|
||||
}
|
||||
fn scope(revision: &str) -> Scope {
|
||||
Scope {
|
||||
workspace: "workspace".into(),
|
||||
agent: "agent".into(),
|
||||
credential: VerifiedMachineCredential {
|
||||
machine_access_mode: crank_core::MachineAccessMode::StaticAgentKey,
|
||||
max_security_level: crank_core::OperationSecurityLevel::Standard,
|
||||
scopes: vec![crank_core::PlatformApiKeyScope::Read],
|
||||
platform_api_key_id: Some("key".into()),
|
||||
},
|
||||
correlation: CorrelationContext::generate(),
|
||||
continuation: protocol::ContinuationSupport::from_supported(false),
|
||||
locale: ExecutionLocale::En,
|
||||
revision: revision.into(),
|
||||
context: Some(args()),
|
||||
}
|
||||
}
|
||||
fn params() -> CompleteRequestParams {
|
||||
CompleteRequestParams::new(
|
||||
Reference::for_resource("crank://bound/{region}"),
|
||||
ArgumentInfo::new("region", "e"),
|
||||
)
|
||||
}
|
||||
async fn state() -> Arc<AppState> {
|
||||
crank_test_support::postgres_schema_url_for_task("completion_handler_local", "t_7458fe3b")
|
||||
.await;
|
||||
super::super::rmcp_tools_tests::test_state().await
|
||||
}
|
||||
fn handler(state: &Arc<AppState>, outcome: CompletionOutcome, pending: bool) -> CompletionHandler {
|
||||
CompletionHandler {
|
||||
state: state.clone(),
|
||||
domain: Some(Arc::new(Domain {
|
||||
outcome: Mutex::new(Some(outcome)),
|
||||
pending,
|
||||
..Default::default()
|
||||
})),
|
||||
}
|
||||
}
|
||||
async fn context(h: &CompletionHandler, revision: &str) -> RequestContext<RoleServer> {
|
||||
let c = request_context(h, scope(revision)).await.unwrap();
|
||||
assert!(c.peer.is_transport_closed());
|
||||
assert_eq!(
|
||||
h.domain.as_ref().unwrap().calls.load(SeqCst),
|
||||
0,
|
||||
"bootstrap must not execute domain"
|
||||
);
|
||||
let s = CompletionHandler::scope(&c).unwrap();
|
||||
assert_eq!(s.workspace, "workspace");
|
||||
assert_eq!(s.agent, "agent");
|
||||
assert_eq!(s.revision, revision);
|
||||
assert_eq!(s.credential.platform_api_key_id.unwrap().as_str(), "key");
|
||||
c
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_completion_trait_result_is_consumed_and_domain_runs_once() {
|
||||
let state = state().await;
|
||||
for revision in ["2025-11-25", "2026-07-28"] {
|
||||
let h = handler(
|
||||
&state,
|
||||
CompletionOutcome::Completed {
|
||||
values: vec!["eu".into()],
|
||||
total: Some(1),
|
||||
has_more: false,
|
||||
},
|
||||
false,
|
||||
);
|
||||
let c = context(&h, revision).await;
|
||||
let mut result = ServerHandler::complete(&h, params(), c).await.unwrap();
|
||||
assert_eq!(result.completion.values, ["eu"]);
|
||||
result.completion.values = vec!["typed".into()];
|
||||
result.completion.total = None;
|
||||
result.completion.has_more = Some(true);
|
||||
match restore(Ok(result)) {
|
||||
CompletionOutcome::Completed {
|
||||
values,
|
||||
total,
|
||||
has_more,
|
||||
} => {
|
||||
assert_eq!(values, ["typed"]);
|
||||
assert_eq!(total, None);
|
||||
assert!(has_more);
|
||||
}
|
||||
_ => panic!("typed result not consumed"),
|
||||
}
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
let h = handler(
|
||||
&state,
|
||||
CompletionOutcome::Completed {
|
||||
values: vec![],
|
||||
total: None,
|
||||
has_more: false,
|
||||
},
|
||||
false,
|
||||
);
|
||||
let c = context(&h, revision).await;
|
||||
let result = ServerHandler::complete(&h, params(), c).await.unwrap();
|
||||
assert!(result.completion.values.is_empty());
|
||||
assert_eq!(result.completion.total, None);
|
||||
}
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_completion_dropping_trait_future_drops_pending_domain() {
|
||||
let state = state().await;
|
||||
for revision in ["2025-11-25", "2026-07-28"] {
|
||||
let h = handler(&state, CompletionOutcome::CatalogUnavailable, true);
|
||||
let c = context(&h, revision).await;
|
||||
let domain = h.domain.as_ref().unwrap();
|
||||
let mut future = Box::pin(ServerHandler::complete(&h, params(), c));
|
||||
assert!(futures_util::poll!(&mut future).is_pending());
|
||||
assert_eq!(domain.calls.load(SeqCst), 1);
|
||||
assert!(!domain.dropped.load(SeqCst));
|
||||
drop(future);
|
||||
assert!(domain.dropped.load(SeqCst));
|
||||
tokio::task::yield_now().await;
|
||||
assert!(!domain.finished.load(SeqCst));
|
||||
assert_eq!(domain.calls.load(SeqCst), 1);
|
||||
}
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_completion_trait_errors_remain_safe() {
|
||||
let state = state().await;
|
||||
for outcome in [
|
||||
CompletionOutcome::InvalidParams("invalid completion/complete parameters"),
|
||||
CompletionOutcome::NotAvailable,
|
||||
CompletionOutcome::CatalogUnavailable,
|
||||
] {
|
||||
let h = handler(&state, outcome, false);
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let result = ServerHandler::complete(&h, params(), c).await;
|
||||
assert!(result.is_err());
|
||||
let error = result.as_ref().unwrap_err();
|
||||
assert!(!error.to_string().contains("crank://bound"));
|
||||
match error.code {
|
||||
ErrorCode::INVALID_PARAMS => assert!(matches!(
|
||||
restore(result),
|
||||
CompletionOutcome::InvalidParams(_) | CompletionOutcome::NotAvailable
|
||||
)),
|
||||
ErrorCode::INTERNAL_ERROR => assert!(matches!(
|
||||
restore(result),
|
||||
CompletionOutcome::CatalogUnavailable
|
||||
)),
|
||||
_ => panic!("unexpected error"),
|
||||
}
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,357 @@
|
||||
//! #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 super::rmcp_completion_handler_runtime_tests as handler;
|
||||
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,
|
||||
require_handler: bool,
|
||||
) -> (Value, [usize; 2]) {
|
||||
let before = counts(&h.counters);
|
||||
let handler_before = handler::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);
|
||||
let handler_delta = handler::delta(&h.counters, handler_before);
|
||||
if require_handler {
|
||||
assert_eq!(
|
||||
handler_delta.0, handler_delta.1,
|
||||
"actual completion handler execution/consumption: {handler_delta:?}"
|
||||
);
|
||||
}
|
||||
(
|
||||
result["completion"].clone(),
|
||||
std::array::from_fn(|i| after[i] - before[i]),
|
||||
)
|
||||
}
|
||||
async fn control(revision: &str, require_handler: bool) -> 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, require_handler).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,
|
||||
require_handler,
|
||||
)
|
||||
.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, require_handler).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, false).await;
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_completion_wire_modern() {
|
||||
control(MODERN_REVISION, false).await;
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_completion_sdk_legacy() {
|
||||
let deltas = control(LEGACY_REVISION, true).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, true).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(®istry, "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(®istry, &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(®istry).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(¶ms);
|
||||
assert_eq!(c.sdk_initialize_input(), 1);
|
||||
assert_eq!(
|
||||
c.sdk_initialize_handler_consumed(),
|
||||
0,
|
||||
"input and output are independent"
|
||||
);
|
||||
})
|
||||
.await;
|
||||
assert_eq!(c.snapshot(), Snapshot::new(1, 0, 0, 0));
|
||||
super::sdk_initialize_input_consumed(¶ms);
|
||||
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(¶ms);
|
||||
})
|
||||
.await;
|
||||
assert_eq!(
|
||||
c.sdk_initialize_input(),
|
||||
1,
|
||||
"another request scope is isolated"
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
//! 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,
|
||||
delete_on_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(),
|
||||
});
|
||||
}
|
||||
if self.delete_on_mark.load(Ordering::SeqCst) {
|
||||
self.inner.delete(id).await?;
|
||||
}
|
||||
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),
|
||||
delete_on_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);
|
||||
|
||||
// Session can disappear after admission/scoped reads but before the durable UPDATE.
|
||||
store.delete_on_mark.store(true, Ordering::SeqCst);
|
||||
let completed = h.counters.sdk_initialized_handler_completed();
|
||||
let consumed = h.counters.sdk_initialized_handler_consumed();
|
||||
let response = post(
|
||||
&h,
|
||||
Some(&pending),
|
||||
"application/json",
|
||||
initialized_notification(),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(response.status(), 404);
|
||||
assert!(response.bytes().await.unwrap().is_empty());
|
||||
assert_eq!(
|
||||
h.counters.sdk_initialized_handler_completed(),
|
||||
completed + 1
|
||||
);
|
||||
assert_eq!(h.counters.sdk_initialized_handler_consumed(), consumed + 1);
|
||||
assert!(restarted.get(&pending).await.unwrap().is_none());
|
||||
}
|
||||
|
||||
#[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(¬ification);
|
||||
super::sdk_initialized_notification_consumed(¬ification);
|
||||
assert_eq!(sdk(&a), [0, 0]);
|
||||
tokio::join!(
|
||||
observe(a.clone(), async {
|
||||
super::sdk_initialized_notification_decoded(¬ification);
|
||||
}),
|
||||
observe(b.clone(), async {
|
||||
super::sdk_initialized_notification_consumed(¬ification);
|
||||
})
|
||||
);
|
||||
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,273 @@
|
||||
//! #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_handler_consumed(),
|
||||
c.sdk_discover_handler_consumed(),
|
||||
]
|
||||
}
|
||||
|
||||
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_handler_consumed();
|
||||
assert_eq!(
|
||||
sdk_counts(&c),
|
||||
[1, 0],
|
||||
"initialize and discovery are separate markers"
|
||||
);
|
||||
super::sdk_discover_handler_consumed();
|
||||
})
|
||||
.await;
|
||||
assert_eq!(sdk_counts(&c), [1, 1]);
|
||||
assert_eq!(c.snapshot(), super::Snapshot::new(1, 0, 0, 0));
|
||||
super::sdk_initialize_handler_consumed();
|
||||
super::sdk_discover_handler_consumed();
|
||||
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,12 @@
|
||||
//! Handler-specific acceptance oracle, distinct from existing wire-level GREEN controls.
|
||||
use super::rmcp_logging_runtime_tests;
|
||||
|
||||
#[tokio::test]
|
||||
async fn legacy_logging_http_completes_and_consumes_the_sdk_handler() {
|
||||
// Existing real build_app HTTP control proves the JSON/SSE contract independently.
|
||||
assert_eq!(
|
||||
rmcp_logging_runtime_tests::control("2025-11-25").await,
|
||||
[1, 1, 1, 1, 1],
|
||||
"legacy wire succeeded but the awaited SDK handler was not completed/consumed"
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
#![allow(deprecated)]
|
||||
use super::*;
|
||||
use crate::protocol::SetLevelCommand;
|
||||
use serde_json::json;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
fn request(level: &str) -> SetLevelRequestParams {
|
||||
serde_json::from_value(json!({"level": level})).expect("valid SDK logging level")
|
||||
}
|
||||
fn handler(store: &Arc<protocol::LogLevelStore>) -> LoggingHandler {
|
||||
LoggingHandler {
|
||||
store: store.clone(),
|
||||
session_id: "current".into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn typed_sdk_level_is_the_applied_level_and_execution_is_single() {
|
||||
let store = Arc::new(protocol::LogLevelStore::new());
|
||||
store.set("other", LogLevel::Debug);
|
||||
let counters = Arc::new(super::super::dispatch_observer::CounterSet::default());
|
||||
let events = super::super::dispatch_observer::observe(
|
||||
counters.clone(),
|
||||
protocol::logging_scope(async {
|
||||
// Mutate the typed value after validation: no raw command may override it later.
|
||||
let mut typed = SetLevelCommand {
|
||||
level: "critical".into(),
|
||||
}
|
||||
.validate()
|
||||
.unwrap();
|
||||
typed.level = LoggingLevel::Info;
|
||||
let result = invoke(&handler(&store), typed, request_context().await.unwrap()).await;
|
||||
assert!(result.is_ok());
|
||||
protocol::set_sse_acceptable();
|
||||
let events = protocol::take_notifications();
|
||||
let response = response(
|
||||
&json!({"id":91}),
|
||||
crate::transport::ResponseMode::Json,
|
||||
crate::jsonrpc::LEGACY_PROTOCOL_VERSION,
|
||||
result,
|
||||
);
|
||||
assert_eq!(response.status(), 200);
|
||||
assert_eq!(store.get("current"), Some(LogLevel::Info));
|
||||
assert_eq!(store.get("other"), Some(LogLevel::Debug));
|
||||
events
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(counters.sdk_logging_handler_completed(), 1);
|
||||
assert_eq!(counters.sdk_logging_handler_consumed(), 1);
|
||||
assert_eq!(events.len(), 1);
|
||||
assert_eq!(
|
||||
events[0]["params"]["data"],
|
||||
json!({
|
||||
"event": protocol::EVENT_LEVEL_APPLIED, "outcome": "applied"
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
struct Controlled {
|
||||
inner: LoggingHandler,
|
||||
entered: AtomicUsize,
|
||||
error: Option<ErrorData>,
|
||||
pending: bool,
|
||||
}
|
||||
impl ServerHandler for Controlled {
|
||||
async fn set_level(
|
||||
&self,
|
||||
request: SetLevelRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<(), ErrorData> {
|
||||
self.entered.fetch_add(1, Ordering::SeqCst);
|
||||
if let Some(error) = &self.error {
|
||||
return Err(error.clone());
|
||||
}
|
||||
if self.pending {
|
||||
std::future::pending::<()>().await;
|
||||
}
|
||||
ServerHandler::set_level(&self.inner, request, context).await
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn actual_trait_error_controls_the_http_response_without_store_effect() {
|
||||
let store = Arc::new(protocol::LogLevelStore::new());
|
||||
let error = ErrorData::invalid_params("controlled handler error", None);
|
||||
let controlled = Controlled {
|
||||
inner: handler(&store),
|
||||
entered: AtomicUsize::new(0),
|
||||
error: Some(error),
|
||||
pending: false,
|
||||
};
|
||||
for mode in [
|
||||
crate::transport::ResponseMode::Json,
|
||||
crate::transport::ResponseMode::Sse,
|
||||
] {
|
||||
let result = invoke(
|
||||
&controlled,
|
||||
request("notice"),
|
||||
request_context().await.unwrap(),
|
||||
)
|
||||
.await;
|
||||
let reply = response(
|
||||
&json!({"id":"controlled"}),
|
||||
mode,
|
||||
crate::jsonrpc::LEGACY_PROTOCOL_VERSION,
|
||||
result,
|
||||
);
|
||||
assert_eq!(reply.status(), 200);
|
||||
let bytes = axum::body::to_bytes(reply.into_body(), 4096).await.unwrap();
|
||||
let raw = std::str::from_utf8(&bytes).unwrap();
|
||||
let raw = raw
|
||||
.lines()
|
||||
.find_map(|l| l.strip_prefix("data:"))
|
||||
.unwrap_or(raw);
|
||||
let body: serde_json::Value = serde_json::from_str(raw).unwrap();
|
||||
assert_eq!(body["id"], "controlled");
|
||||
assert_eq!(body["error"]["code"], -32602);
|
||||
assert_eq!(body["error"]["message"], "controlled handler error");
|
||||
assert!(body.get("result").is_none());
|
||||
}
|
||||
assert_eq!(controlled.entered.load(Ordering::SeqCst), 2);
|
||||
assert_eq!(store.get("current"), None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dropping_pending_await_before_store_effect_does_not_detach_work() {
|
||||
let store = Arc::new(protocol::LogLevelStore::new());
|
||||
let controlled = Controlled {
|
||||
inner: handler(&store),
|
||||
entered: AtomicUsize::new(0),
|
||||
error: None,
|
||||
pending: true,
|
||||
};
|
||||
let context = request_context().await.unwrap();
|
||||
let mut future = Box::pin(invoke(&controlled, request("alert"), context));
|
||||
assert!(futures_util::poll!(future.as_mut()).is_pending());
|
||||
assert_eq!(controlled.entered.load(Ordering::SeqCst), 1);
|
||||
drop(future);
|
||||
tokio::task::yield_now().await;
|
||||
assert_eq!(store.get("current"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_command_maps_to_safe_invalid_params_error_data() {
|
||||
let error = SetLevelCommand {
|
||||
level: "not-a-level".to_owned(),
|
||||
}
|
||||
.validate()
|
||||
.unwrap_err();
|
||||
assert_eq!(error, protocol::INVALID_LEVEL);
|
||||
let sdk_error = ErrorData::invalid_params(error, None);
|
||||
assert_eq!(sdk_error.code.0, -32602);
|
||||
assert_eq!(sdk_error.message, protocol::INVALID_LEVEL);
|
||||
}
|
||||
@@ -0,0 +1,408 @@
|
||||
//! #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})
|
||||
}
|
||||
pub(super) async fn control(revision: &str) -> [usize; 5] {
|
||||
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);
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
handlers(&h.counters),
|
||||
[0, 0],
|
||||
"rejected input must not invoke the handler"
|
||||
);
|
||||
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: [usize; 3] = 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 before_handlers = handlers(&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);
|
||||
assert_eq!(handlers(&h.counters), before_handlers);
|
||||
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[0],
|
||||
delta[1],
|
||||
delta[2],
|
||||
h.counters.sdk_logging_handler_completed(),
|
||||
h.counters.sdk_logging_handler_consumed(),
|
||||
]
|
||||
}
|
||||
#[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, 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, 0, 0],
|
||||
"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]);
|
||||
}
|
||||
|
||||
fn handlers(c: &CounterSet) -> [usize; 2] {
|
||||
[
|
||||
c.sdk_logging_handler_completed(),
|
||||
c.sdk_logging_handler_consumed(),
|
||||
]
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_logging_all_levels_json_sse_and_ignored_extensions() {
|
||||
let h = Endpoint::started().await;
|
||||
let session = h.session(LEGACY_REVISION).await.unwrap();
|
||||
for accept in ["application/json", "text/event-stream, application/json"] {
|
||||
for level in [
|
||||
"debug",
|
||||
"info",
|
||||
"notice",
|
||||
"warning",
|
||||
"error",
|
||||
"critical",
|
||||
"alert",
|
||||
"emergency",
|
||||
] {
|
||||
let before = handlers(&h.counters);
|
||||
let response = h.post_with_accept(LEGACY_REVISION, Some(&session), "logging/setLevel",
|
||||
json!({"level":level,"extension":{"CANARY":"ignored"},"_meta":{"extension":true}}), accept).await;
|
||||
assert_eq!(response.headers()["MCP-Protocol-Version"], LEGACY_REVISION);
|
||||
assert_eq!(
|
||||
response.headers()["x-request-id"],
|
||||
"logging-handler-control"
|
||||
);
|
||||
let (body, events, raw) = reply(response).await;
|
||||
assert_eq!(body["result"], json!({}));
|
||||
assert!(!raw.contains("CANARY"));
|
||||
if accept == "application/json" || !["debug", "info", "notice"].contains(&level) {
|
||||
assert!(events.is_empty());
|
||||
} else {
|
||||
let mut expected = vec![notification(
|
||||
"notice",
|
||||
"mcp.logging.level_applied",
|
||||
"applied",
|
||||
)];
|
||||
if ["debug", "info"].contains(&level) {
|
||||
expected.push(notification("info", "mcp.request.served", "ok"));
|
||||
}
|
||||
assert_eq!(events, expected);
|
||||
}
|
||||
assert_eq!(handlers(&h.counters), before.map(|n| n + 1));
|
||||
}
|
||||
}
|
||||
let before = handlers(&h.counters);
|
||||
let response = h
|
||||
.post(
|
||||
LEGACY_REVISION,
|
||||
Some(&session),
|
||||
"logging/setLevel",
|
||||
json!({"level":"info","_meta":7}),
|
||||
)
|
||||
.await;
|
||||
assert!(response.status().is_client_error());
|
||||
assert!(!response.text().await.unwrap().contains("CANARY"));
|
||||
assert_eq!(handlers(&h.counters), before);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_logging_session_guards_do_not_enter_the_handler() {
|
||||
use crate::session::TransportSessionStore;
|
||||
let h = Endpoint::started().await;
|
||||
let uninitialized = h
|
||||
.sessions
|
||||
.create(
|
||||
LEGACY_REVISION,
|
||||
WORKSPACE_SLUG,
|
||||
AGENT_SLUG,
|
||||
false,
|
||||
time::OffsetDateTime::now_utc(),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let foreign = h
|
||||
.sessions
|
||||
.create(
|
||||
LEGACY_REVISION,
|
||||
WORKSPACE_SLUG,
|
||||
"another-agent",
|
||||
false,
|
||||
time::OffsetDateTime::now_utc(),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
h.sessions
|
||||
.mark_initialized(&foreign, time::OffsetDateTime::now_utc())
|
||||
.await
|
||||
.unwrap();
|
||||
for session in [
|
||||
None,
|
||||
Some("unknown"),
|
||||
Some(uninitialized.as_str()),
|
||||
Some(foreign.as_str()),
|
||||
] {
|
||||
let response = h
|
||||
.post(
|
||||
LEGACY_REVISION,
|
||||
session,
|
||||
"logging/setLevel",
|
||||
json!({"level":"debug"}),
|
||||
)
|
||||
.await;
|
||||
let status = response.status();
|
||||
let raw = response.text().await.unwrap();
|
||||
if session == Some(uninitialized.as_str()) {
|
||||
assert_eq!(status, 200);
|
||||
let body: Value = serde_json::from_str(&raw).unwrap();
|
||||
assert_eq!(body["error"]["code"], -32002);
|
||||
} else {
|
||||
assert_eq!(status.as_u16(), if session.is_none() { 400 } else { 404 });
|
||||
}
|
||||
assert!(!raw.contains("mcp.logging.level_applied"));
|
||||
assert_eq!(handlers(&h.counters), [0, 0]);
|
||||
assert_eq!(h.counters.sdk_logging_dispatch(), 0);
|
||||
}
|
||||
let session = h.session(LEGACY_REVISION).await;
|
||||
let (body, _, _) = reply(
|
||||
h.post(
|
||||
LEGACY_REVISION,
|
||||
session.as_deref(),
|
||||
"logging/setLevel",
|
||||
json!({"level":"notice"}),
|
||||
)
|
||||
.await,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(body["result"], json!({}));
|
||||
assert_eq!(handlers(&h.counters), [1, 1]);
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
//! 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>,
|
||||
pub sessions: Arc<crate::session::InMemorySessionStore>,
|
||||
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_handler", "t_dbf2bdff").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(®istry, &operation).await;
|
||||
let key = create_api_key(®istry).await;
|
||||
let counters = Arc::new(CounterSet::default());
|
||||
let sessions = Arc::new(crate::session::InMemorySessionStore::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()),
|
||||
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 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,
|
||||
sessions,
|
||||
key,
|
||||
url,
|
||||
server,
|
||||
}
|
||||
}
|
||||
pub async fn post(
|
||||
&self,
|
||||
revision: &str,
|
||||
session: Option<&str>,
|
||||
method: &str,
|
||||
params: Value,
|
||||
) -> reqwest::Response {
|
||||
self.post_with_accept(
|
||||
revision,
|
||||
session,
|
||||
method,
|
||||
params,
|
||||
"text/event-stream, application/json",
|
||||
)
|
||||
.await
|
||||
}
|
||||
pub async fn post_with_accept(
|
||||
&self,
|
||||
revision: &str,
|
||||
session: Option<&str>,
|
||||
method: &str,
|
||||
mut params: Value,
|
||||
accept: &str,
|
||||
) -> reqwest::Response {
|
||||
let mut request = reqwest::Client::new()
|
||||
.post(&self.url)
|
||||
.bearer_auth(&self.key)
|
||||
.header("Accept", accept)
|
||||
.header("x-request-id", "logging-handler-control")
|
||||
.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,139 @@
|
||||
//! Behavioral RED for real negotiation/ping handler execution and result consumption.
|
||||
use super::rmcp_progress_cancel_runtime_tests::fixtures::Endpoint;
|
||||
use super::{CounterSet, observe, support::*};
|
||||
use serde_json::{Value, json};
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
async fn request(
|
||||
h: &Endpoint,
|
||||
revision: &str,
|
||||
payload: Value,
|
||||
accept: &str,
|
||||
session: Option<&str>,
|
||||
) -> reqwest::Response {
|
||||
h.request(revision, session, &h.key, accept, payload)
|
||||
.header("x-request-id", "negotiation-handler-atdd")
|
||||
.header(
|
||||
"traceparent",
|
||||
"00-11111111111111111111111111111111-2222222222222222-01",
|
||||
)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn terminal(response: reqwest::Response, sse: bool) -> Value {
|
||||
assert_eq!(response.status(), 200);
|
||||
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, "single terminal response frame");
|
||||
frames.into_iter().next().unwrap()
|
||||
} else {
|
||||
serde_json::from_str(&raw).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
fn handler_counts(c: &CounterSet) -> [usize; 6] {
|
||||
[
|
||||
c.sdk_initialize_handler_completed(),
|
||||
c.sdk_initialize_handler_consumed(),
|
||||
c.sdk_discover_handler_completed(),
|
||||
c.sdk_discover_handler_consumed(),
|
||||
c.sdk_ping_handler_completed(),
|
||||
c.sdk_ping_handler_consumed(),
|
||||
]
|
||||
}
|
||||
|
||||
fn delta(after: [usize; 6], before: [usize; 6]) -> [usize; 6] {
|
||||
std::array::from_fn(|index| after[index] - before[index])
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_negotiation_handler_http_executes_and_consumes_initialize_discover_and_ping() {
|
||||
crank_test_support::postgres_schema_url_for_task("negotiation_handler_owner", "t_219756db")
|
||||
.await;
|
||||
let h = Endpoint::started().await;
|
||||
let session = h.session(LEGACY_REVISION).await;
|
||||
let before_all = handler_counts(&h.counters);
|
||||
for (revision, method, session) in [
|
||||
(LEGACY_REVISION, "initialize", None),
|
||||
(MODERN_REVISION, "server/discover", None),
|
||||
] {
|
||||
let mut payload = if method == "initialize" {
|
||||
legacy_initialize(1)
|
||||
} else {
|
||||
json!({"jsonrpc":"2.0","id":1,"method":method,"params":{"_meta":modern_meta()}})
|
||||
};
|
||||
for accept in ["application/json", "text/event-stream, application/json"] {
|
||||
if method == "initialize" {
|
||||
payload["id"] = json!(if accept.starts_with("text/event") {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
});
|
||||
}
|
||||
let response = request(&h, revision, payload.clone(), accept, session).await;
|
||||
let body = terminal(response, accept.starts_with("text/event")).await;
|
||||
assert_eq!(body["jsonrpc"], "2.0");
|
||||
}
|
||||
}
|
||||
for revision in [LEGACY_REVISION, MODERN_REVISION] {
|
||||
for accept in ["application/json", "text/event-stream, application/json"] {
|
||||
let payload = json!({"jsonrpc":"2.0","id":"ping","method":"ping","params":{"_meta":if revision == MODERN_REVISION { modern_meta() } else { json!({"future":true}) }}});
|
||||
let response = request(
|
||||
&h,
|
||||
revision,
|
||||
payload,
|
||||
accept,
|
||||
if revision == LEGACY_REVISION {
|
||||
session.as_deref()
|
||||
} else {
|
||||
None
|
||||
},
|
||||
)
|
||||
.await;
|
||||
let body = terminal(response, accept.starts_with("text/event")).await;
|
||||
assert_eq!(body["id"], "ping");
|
||||
assert!(body.get("error").is_none());
|
||||
if revision == LEGACY_REVISION {
|
||||
assert_eq!(body["result"]["protocolVersion"], LEGACY_REVISION);
|
||||
}
|
||||
}
|
||||
}
|
||||
let after = handler_counts(&h.counters);
|
||||
assert_eq!(
|
||||
delta(after, before_all),
|
||||
[2, 2, 2, 2, 4, 4],
|
||||
"successful initialize/discover/ping HTTP responses must complete in and consume their SDK handler results"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_negotiation_handler_observer_positive_control() {
|
||||
let c = Arc::new(CounterSet::default());
|
||||
observe(c.clone(), async {
|
||||
super::sdk_initialize_handler_completed();
|
||||
super::sdk_initialize_handler_consumed();
|
||||
super::sdk_discover_handler_completed();
|
||||
super::sdk_discover_handler_consumed();
|
||||
super::sdk_ping_handler_completed();
|
||||
super::sdk_ping_handler_consumed();
|
||||
assert_eq!(handler_counts(&c), [1; 6]);
|
||||
})
|
||||
.await;
|
||||
assert_eq!(handler_counts(&c), [1; 6]);
|
||||
super::sdk_initialize_handler_completed();
|
||||
assert_eq!(
|
||||
handler_counts(&c),
|
||||
[1; 6],
|
||||
"markers outside observer scope are inert"
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
//! The production adapters consume actual trait results and own their pending work.
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_negotiation_handler_qa_discover_uses_requested_revision_capabilities() {
|
||||
use crate::protocol::{
|
||||
CAPABILITY_DESCRIPTORS, CapabilityId, CapabilityRegistry, METHOD_DESCRIPTORS,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
let mut outcomes = Vec::new();
|
||||
for revisions in [&["2026-07-28"][..], &["2025-11-25"][..]] {
|
||||
let mut descriptors = CAPABILITY_DESCRIPTORS.to_vec();
|
||||
descriptors
|
||||
.iter_mut()
|
||||
.find(|d| d.id == CapabilityId::Tools)
|
||||
.unwrap()
|
||||
.revisions = revisions;
|
||||
let registration =
|
||||
Arc::new(CapabilityRegistry::new(&descriptors, METHOD_DESCRIPTORS).unwrap());
|
||||
crate::app::dispatch_observer::observe_with_registration(registration, async {
|
||||
let expected = advertised_server_capabilities("2026-07-28");
|
||||
let actual = discover("2026-07-28").await.unwrap();
|
||||
outcomes.push((
|
||||
expected["tools"].clone(),
|
||||
actual["capabilities"]["tools"].clone(),
|
||||
));
|
||||
})
|
||||
.await;
|
||||
}
|
||||
assert_eq!(
|
||||
outcomes,
|
||||
vec![
|
||||
(json!({"listChanged":true}), json!({"listChanged":true})),
|
||||
(Value::Null, Value::Null)
|
||||
],
|
||||
"modern-only tools must be advertised; legacy-only tools must not leak into modern discovery"
|
||||
);
|
||||
}
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
|
||||
|
||||
#[derive(Default)]
|
||||
struct Control {
|
||||
calls: AtomicUsize,
|
||||
dropped: AtomicBool,
|
||||
finished: AtomicBool,
|
||||
fail: bool,
|
||||
pending: bool,
|
||||
}
|
||||
struct Witness<'a>(&'a AtomicBool);
|
||||
impl Drop for Witness<'_> {
|
||||
fn drop(&mut self) {
|
||||
self.0.store(true, SeqCst);
|
||||
}
|
||||
}
|
||||
fn error() -> ErrorData {
|
||||
ErrorData::new(
|
||||
ErrorCode(-32042),
|
||||
"typed negotiation failure",
|
||||
Some(json!({"proof":42})),
|
||||
)
|
||||
}
|
||||
impl Control {
|
||||
async fn enter(&self, context: &RequestContext<RoleServer>) -> Result<(), ErrorData> {
|
||||
assert!(
|
||||
context.peer.is_transport_closed(),
|
||||
"bootstrap closes before handler"
|
||||
);
|
||||
self.calls.fetch_add(1, SeqCst);
|
||||
let _witness = Witness(&self.dropped);
|
||||
if self.pending {
|
||||
std::future::pending::<()>().await;
|
||||
}
|
||||
self.finished.store(true, SeqCst);
|
||||
if self.fail { Err(error()) } else { Ok(()) }
|
||||
}
|
||||
}
|
||||
impl ServerHandler for Control {
|
||||
async fn initialize(
|
||||
&self,
|
||||
request: InitializeRequestParams,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<InitializeResult, ErrorData> {
|
||||
self.enter(&context).await?;
|
||||
let mut result = ServerHandler::initialize(&NegotiationHandler, request, context).await?;
|
||||
result.server_info = Implementation::new("typed-initialize", "42");
|
||||
result.capabilities.tools = None;
|
||||
Ok(result)
|
||||
}
|
||||
async fn discover(
|
||||
&self,
|
||||
context: RequestContext<RoleServer>,
|
||||
) -> Result<DiscoverResult, ErrorData> {
|
||||
self.enter(&context).await?;
|
||||
let mut result = ServerHandler::discover(&NegotiationHandler, context).await?;
|
||||
result = result.with_server_info(Implementation::new("typed-discover", "42"));
|
||||
result.capabilities.tools = None;
|
||||
Ok(result)
|
||||
}
|
||||
async fn ping(&self, context: RequestContext<RoleServer>) -> Result<(), ErrorData> {
|
||||
self.enter(&context).await
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_negotiation_handler_mutated_typed_success_is_consumed() {
|
||||
let h = Control::default();
|
||||
let init = initialize_with(&h, "2025-11-25").await.unwrap();
|
||||
assert_eq!(init["serverInfo"]["name"], "typed-initialize");
|
||||
assert!(init["capabilities"].get("tools").is_none());
|
||||
let discover = discover_with(&h, "2026-07-28").await.unwrap();
|
||||
assert_eq!(
|
||||
discover["_meta"]["io.modelcontextprotocol/serverInfo"]["name"],
|
||||
"typed-discover"
|
||||
);
|
||||
assert!(discover["capabilities"].get("tools").is_none());
|
||||
ping_with(&h).await.unwrap();
|
||||
assert_eq!(h.calls.load(SeqCst), 3, "no bootstrap invocation");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_negotiation_handler_typed_errors_are_not_success() {
|
||||
let h = Control {
|
||||
fail: true,
|
||||
..Default::default()
|
||||
};
|
||||
for actual in [
|
||||
initialize_with(&h, "2025-11-25").await.unwrap_err(),
|
||||
discover_with(&h, "2026-07-28").await.unwrap_err(),
|
||||
ping_with(&h).await.unwrap_err(),
|
||||
] {
|
||||
assert_eq!(actual, error());
|
||||
for mode in [
|
||||
crate::transport::ResponseMode::Json,
|
||||
crate::transport::ResponseMode::Sse,
|
||||
] {
|
||||
let response = super::super::dispatch::sdk_error_response(
|
||||
&json!({"id":"typed-error"}),
|
||||
mode,
|
||||
"2025-11-25",
|
||||
actual.clone(),
|
||||
);
|
||||
assert_eq!(response.status(), 200);
|
||||
assert!(response.headers().get("mcp-session-id").is_none());
|
||||
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
|
||||
.await
|
||||
.unwrap();
|
||||
let raw = std::str::from_utf8(&bytes).unwrap();
|
||||
let body: Value = match mode {
|
||||
crate::transport::ResponseMode::Json => serde_json::from_str(raw).unwrap(),
|
||||
crate::transport::ResponseMode::Sse => serde_json::from_str(
|
||||
raw.lines()
|
||||
.find_map(|l| l.strip_prefix("data:"))
|
||||
.unwrap()
|
||||
.trim(),
|
||||
)
|
||||
.unwrap(),
|
||||
};
|
||||
assert_eq!(
|
||||
body,
|
||||
json!({"jsonrpc":"2.0","id":"typed-error","error":{"code":-32042,"message":"typed negotiation failure","data":{"proof":42}}})
|
||||
);
|
||||
}
|
||||
}
|
||||
assert_eq!(h.calls.load(SeqCst), 3);
|
||||
assert!(initialize("not-supported").await.is_err());
|
||||
assert_eq!(
|
||||
NegotiationHandler
|
||||
.supported_protocol_versions()
|
||||
.iter()
|
||||
.map(|v| v.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
SUPPORTED_PROTOCOL_VERSIONS
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_negotiation_handler_drop_cancels_request_owned_work() {
|
||||
for method in 0..3 {
|
||||
let h = Control {
|
||||
pending: true,
|
||||
..Default::default()
|
||||
};
|
||||
let mut future = Box::pin(async {
|
||||
match method {
|
||||
0 => {
|
||||
initialize_with(&h, "2025-11-25").await?;
|
||||
}
|
||||
1 => {
|
||||
discover_with(&h, "2026-07-28").await?;
|
||||
}
|
||||
_ => {
|
||||
ping_with(&h).await?;
|
||||
}
|
||||
}
|
||||
Ok::<_, ErrorData>(())
|
||||
});
|
||||
tokio::select! {
|
||||
result = &mut future => panic!("pending handler completed: {result:?}"),
|
||||
_ = async { while h.calls.load(SeqCst) == 0 { tokio::task::yield_now().await; } } => {}
|
||||
}
|
||||
assert!(!h.dropped.load(SeqCst));
|
||||
drop(future);
|
||||
assert!(h.dropped.load(SeqCst));
|
||||
tokio::task::yield_now().await;
|
||||
assert!(!h.finished.load(SeqCst));
|
||||
assert_eq!(h.calls.load(SeqCst), 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
//! #107 real HTTP contract for awaited legacy SDK notification handlers.
|
||||
use super::rmcp_progress_cancel_runtime_tests::fixtures::Endpoint;
|
||||
use super::support::*;
|
||||
use serde_json::{Value, json};
|
||||
use std::time::Duration;
|
||||
|
||||
async fn legacy_session(h: &Endpoint) -> String {
|
||||
let response = h
|
||||
.request(
|
||||
LEGACY_REVISION,
|
||||
None,
|
||||
&h.key,
|
||||
"application/json",
|
||||
legacy_initialize(801),
|
||||
)
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), 200);
|
||||
let session = response.headers()["MCP-Session-Id"]
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.to_owned();
|
||||
let body: Value = json_body(response).await;
|
||||
assert_eq!(body["result"]["protocolVersion"], LEGACY_REVISION);
|
||||
session
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn initialized_handler_executes_and_is_consumed_by_real_http_caller() {
|
||||
crank_test_support::postgres_schema_url_for_task("notification_handler", "t_2a52960d").await;
|
||||
let h = Endpoint::started().await;
|
||||
let session = legacy_session(&h).await;
|
||||
let response = h
|
||||
.request(
|
||||
LEGACY_REVISION,
|
||||
Some(&session),
|
||||
&h.key,
|
||||
"application/json",
|
||||
json!({"jsonrpc":"2.0","method":"notifications/initialized"}),
|
||||
)
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), 202, "legacy HTTP wire positive control");
|
||||
assert!(response.bytes().await.unwrap().is_empty());
|
||||
assert!(h.sessions.retained(&session).await.unwrap().initialized);
|
||||
assert_eq!(
|
||||
h.counters.sdk_initialized_handler_completed(),
|
||||
1,
|
||||
"real awaited on_initialized must complete its durable operation"
|
||||
);
|
||||
assert_eq!(
|
||||
h.counters.sdk_initialized_handler_consumed(),
|
||||
1,
|
||||
"HTTP caller must consume the outcome of that same SDK handler"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cancelled_handler_executes_and_is_consumed_for_owned_active_call() {
|
||||
crank_test_support::postgres_schema_url_for_task("notification_handler", "t_2a52960d").await;
|
||||
let h = Endpoint::started().await;
|
||||
let session = h.session(LEGACY_REVISION).await.unwrap();
|
||||
let call = h.call(
|
||||
LEGACY_REVISION,
|
||||
Some(&session),
|
||||
json!(802),
|
||||
Some(json!("cancel")),
|
||||
"text/event-stream",
|
||||
);
|
||||
let active = tokio::spawn(async move { call.send().await.unwrap().text().await.unwrap() });
|
||||
h.wait_started(1).await;
|
||||
h.cancel(&h.key, &session, json!({"requestId":802})).await;
|
||||
tokio::time::timeout(Duration::from_secs(2), active)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
h.counters.sdk_cancelled_handler_completed(),
|
||||
1,
|
||||
"real awaited on_cancelled must cancel the owned call"
|
||||
);
|
||||
assert_eq!(
|
||||
h.counters.sdk_cancelled_handler_consumed(),
|
||||
1,
|
||||
"HTTP caller must consume the outcome of that same SDK handler"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn modern_notifications_remain_ack_only_without_legacy_handlers() {
|
||||
crank_test_support::postgres_schema_url_for_task("notification_handler", "t_2a52960d").await;
|
||||
let h = Endpoint::started().await;
|
||||
let before = 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());
|
||||
let response = h.request(MODERN_REVISION, None, &h.key, "application/json",
|
||||
json!({"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":802,"_meta":modern_meta()}}))
|
||||
.send().await.unwrap();
|
||||
assert_eq!(response.status(), 202);
|
||||
assert!(response.bytes().await.unwrap().is_empty());
|
||||
assert_session_ops(
|
||||
"modern notification ACK",
|
||||
before,
|
||||
h.sessions.snapshot(),
|
||||
SessionOps {
|
||||
get: 0,
|
||||
create: 0,
|
||||
mark_initialized: 0,
|
||||
},
|
||||
);
|
||||
assert_eq!(h.counters.sdk_initialized_handler_completed(), 0);
|
||||
assert_eq!(h.counters.sdk_initialized_handler_consumed(), 0);
|
||||
assert_eq!(h.counters.sdk_cancelled_handler_completed(), 0);
|
||||
assert_eq!(h.counters.sdk_cancelled_handler_consumed(), 0);
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
//! Direct controls of the production awaited notification boundary.
|
||||
use super::*;
|
||||
use crate::session::{
|
||||
InMemorySessionStore, SessionState, SessionStoreError, TransportSessionStore,
|
||||
};
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Store {
|
||||
inner: InMemorySessionStore,
|
||||
marks: AtomicUsize,
|
||||
reads: AtomicUsize,
|
||||
entered: AtomicBool,
|
||||
dropped: AtomicBool,
|
||||
pending: AtomicBool,
|
||||
fail_get: AtomicBool,
|
||||
gate: tokio::sync::Notify,
|
||||
}
|
||||
struct Witness<'a>(&'a AtomicBool);
|
||||
impl Drop for Witness<'_> {
|
||||
fn drop(&mut self) {
|
||||
self.0.store(true, SeqCst);
|
||||
}
|
||||
}
|
||||
impl Store {
|
||||
async fn wait(&self) {
|
||||
if self.pending.load(SeqCst) {
|
||||
let _witness = Witness(&self.dropped);
|
||||
self.entered.store(true, SeqCst);
|
||||
self.gate.notified().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
#[async_trait::async_trait]
|
||||
impl TransportSessionStore for Store {
|
||||
async fn create(
|
||||
&self,
|
||||
v: &str,
|
||||
w: &str,
|
||||
a: &str,
|
||||
e: bool,
|
||||
n: OffsetDateTime,
|
||||
x: Option<OffsetDateTime>,
|
||||
) -> Result<String, SessionStoreError> {
|
||||
self.inner.create(v, w, a, e, n, x).await
|
||||
}
|
||||
async fn get(&self, id: &str) -> Result<Option<SessionState>, SessionStoreError> {
|
||||
self.wait().await;
|
||||
self.reads.fetch_add(1, SeqCst);
|
||||
if self.fail_get.load(SeqCst) {
|
||||
return Err(SessionStoreError {
|
||||
details: "test read refusal".into(),
|
||||
});
|
||||
}
|
||||
self.inner.get(id).await
|
||||
}
|
||||
async fn mark_initialized(
|
||||
&self,
|
||||
id: &str,
|
||||
n: OffsetDateTime,
|
||||
) -> Result<bool, SessionStoreError> {
|
||||
self.wait().await;
|
||||
self.marks.fetch_add(1, SeqCst);
|
||||
self.inner.mark_initialized(id, n).await
|
||||
}
|
||||
async fn delete(&self, id: &str) -> Result<bool, SessionStoreError> {
|
||||
self.inner.delete(id).await
|
||||
}
|
||||
async fn cleanup_expired(&self, n: OffsetDateTime) -> Result<u64, SessionStoreError> {
|
||||
self.inner.cleanup_expired(n).await
|
||||
}
|
||||
async fn active_count(&self, n: OffsetDateTime) -> Result<u64, SessionStoreError> {
|
||||
self.inner.active_count(n).await
|
||||
}
|
||||
}
|
||||
async fn fixture() -> (Arc<AppState>, Arc<Store>, String) {
|
||||
let mut state = (*super::super::rmcp_tools_tests::test_state().await).clone();
|
||||
let store = Arc::new(Store::default());
|
||||
let id = store
|
||||
.create(
|
||||
"2025-11-25",
|
||||
"w",
|
||||
"a",
|
||||
false,
|
||||
OffsetDateTime::now_utc(),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
state.sessions = store.clone();
|
||||
(Arc::new(state), store, id)
|
||||
}
|
||||
fn initialized_handler(state: Arc<AppState>, id: &str) -> OutcomeHandler {
|
||||
OutcomeHandler {
|
||||
operation: Operation::Initialized {
|
||||
state,
|
||||
session_id: id.to_owned(),
|
||||
},
|
||||
outcome: Mutex::new(None),
|
||||
}
|
||||
}
|
||||
fn cancelled_handler(state: Arc<AppState>, id: &str, raw: Option<Value>) -> OutcomeHandler {
|
||||
OutcomeHandler {
|
||||
operation: Operation::Cancelled {
|
||||
state,
|
||||
path: AgentRoutePath {
|
||||
workspace_slug: "w".into(),
|
||||
agent_slug: "a".into(),
|
||||
},
|
||||
credential: VerifiedMachineCredential {
|
||||
machine_access_mode: crank_core::MachineAccessMode::StaticAgentKey,
|
||||
max_security_level: crank_core::OperationSecurityLevel::Standard,
|
||||
scopes: vec![],
|
||||
platform_api_key_id: None,
|
||||
},
|
||||
session_id: Some(id.to_owned()),
|
||||
request_id: raw,
|
||||
},
|
||||
outcome: Mutex::new(None),
|
||||
}
|
||||
}
|
||||
struct Noop;
|
||||
impl ServerHandler for Noop {}
|
||||
|
||||
#[tokio::test]
|
||||
async fn notification_handler_noop_and_bootstrap_failure_have_no_effect() {
|
||||
let (state, store, id) = fixture().await;
|
||||
let init = initialized_handler(state.clone(), &id);
|
||||
assert_eq!(
|
||||
initialized_with(&Noop, &init, notification_context()).await,
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
);
|
||||
assert_eq!(
|
||||
initialized_with(&init, &init, async { Err(()) }).await,
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
);
|
||||
let cancel = cancelled_handler(state.clone(), &id, None);
|
||||
let scope = super::super::lifecycle::InFlightScope {
|
||||
session_id: id.clone(),
|
||||
owner: String::new(),
|
||||
};
|
||||
let entry = state
|
||||
.in_flight
|
||||
.register(&scope, &serde_json::json!(1))
|
||||
.unwrap();
|
||||
let (params, _) = cancelled_params(&serde_json::json!({"requestId":1})).unwrap();
|
||||
cancelled_with(&Noop, &cancel, params.clone(), notification_context()).await;
|
||||
cancelled_with(&cancel, &cancel, params, async { Err(()) }).await;
|
||||
assert!(cancel.take().is_none());
|
||||
assert_eq!(store.marks.load(SeqCst), 0);
|
||||
assert_eq!(store.reads.load(SeqCst), 0);
|
||||
assert!(!store.inner.get(&id).await.unwrap().unwrap().initialized);
|
||||
assert!(futures_util::poll!(Box::pin(entry.cancel_signal().cancelled())).is_pending());
|
||||
// Positive control: exactly one durable operation, and the caller drains its outcome.
|
||||
assert_eq!(
|
||||
initialized_with(&init, &init, notification_context()).await,
|
||||
StatusCode::ACCEPTED
|
||||
);
|
||||
assert_eq!(store.marks.load(SeqCst), 1);
|
||||
assert!(init.take().is_none());
|
||||
assert!(store.inner.get(&id).await.unwrap().unwrap().initialized);
|
||||
}
|
||||
|
||||
struct Mutate(Arc<OutcomeHandler>, NumberOrString);
|
||||
impl ServerHandler for Mutate {
|
||||
async fn on_cancelled(
|
||||
&self,
|
||||
mut p: CancelledNotificationParam,
|
||||
ctx: NotificationContext<RoleServer>,
|
||||
) {
|
||||
assert!(ctx.peer.is_transport_closed());
|
||||
p.request_id = Some(self.1.clone());
|
||||
ServerHandler::on_cancelled(self.0.as_ref(), p, ctx).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn notification_handler_typed_target_mutation_and_raw_number_separation() {
|
||||
let (state, store, id) = fixture().await;
|
||||
let scope = super::super::lifecycle::InFlightScope {
|
||||
session_id: id.clone(),
|
||||
owner: String::new(),
|
||||
};
|
||||
for raw in [
|
||||
serde_json::json!(18446744073709551615_u64),
|
||||
serde_json::json!(1.25),
|
||||
] {
|
||||
let numeric = state.in_flight.register(&scope, &raw).unwrap();
|
||||
let text = state
|
||||
.in_flight
|
||||
.register(&scope, &Value::String(raw.to_string()))
|
||||
.unwrap();
|
||||
let changed = state
|
||||
.in_flight
|
||||
.register(&scope, &serde_json::json!(42))
|
||||
.unwrap();
|
||||
let (params, original) = cancelled_params(&serde_json::json!({"requestId":raw})).unwrap();
|
||||
let h = Arc::new(cancelled_handler(state.clone(), &id, original));
|
||||
let mutation = Mutate(h.clone(), NumberOrString::Number(42));
|
||||
cancelled_with(
|
||||
&mutation,
|
||||
h.as_ref(),
|
||||
params.clone(),
|
||||
notification_context(),
|
||||
)
|
||||
.await;
|
||||
assert!(futures_util::poll!(Box::pin(changed.cancel_signal().cancelled())).is_ready());
|
||||
assert!(futures_util::poll!(Box::pin(numeric.cancel_signal().cancelled())).is_pending());
|
||||
assert!(futures_util::poll!(Box::pin(text.cancel_signal().cancelled())).is_pending());
|
||||
assert!(h.take().is_none());
|
||||
cancelled_with(h.as_ref(), h.as_ref(), params, notification_context()).await;
|
||||
assert!(futures_util::poll!(Box::pin(numeric.cancel_signal().cancelled())).is_ready());
|
||||
assert!(futures_util::poll!(Box::pin(text.cancel_signal().cancelled())).is_pending());
|
||||
}
|
||||
assert_eq!(
|
||||
store.reads.load(SeqCst),
|
||||
4,
|
||||
"one lookup per real callback, no bootstrap effects"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn notification_handler_store_read_refusal_does_not_cancel() {
|
||||
let (state, store, id) = fixture().await;
|
||||
let scope = super::super::lifecycle::InFlightScope {
|
||||
session_id: id.clone(),
|
||||
owner: String::new(),
|
||||
};
|
||||
let entry = state
|
||||
.in_flight
|
||||
.register(&scope, &serde_json::json!(7))
|
||||
.unwrap();
|
||||
let (params, _) = cancelled_params(&serde_json::json!({"requestId":7})).unwrap();
|
||||
let h = cancelled_handler(state, &id, None);
|
||||
store.fail_get.store(true, SeqCst);
|
||||
ServerHandler::on_cancelled(&h, params, notification_context().await.unwrap()).await;
|
||||
assert_eq!(h.take(), Some(StatusCode::ACCEPTED));
|
||||
assert_eq!(store.reads.load(SeqCst), 1);
|
||||
assert!(futures_util::poll!(Box::pin(entry.cancel_signal().cancelled())).is_pending());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn notification_handler_pending_store_drop_stops_initialized_and_cancelled() {
|
||||
let (state, store, id) = fixture().await;
|
||||
for cancel in [false, true] {
|
||||
store.pending.store(true, SeqCst);
|
||||
store.entered.store(false, SeqCst);
|
||||
store.dropped.store(false, SeqCst);
|
||||
let h = if cancel {
|
||||
cancelled_handler(state.clone(), &id, None)
|
||||
} else {
|
||||
initialized_handler(state.clone(), &id)
|
||||
};
|
||||
let scope = super::super::lifecycle::InFlightScope {
|
||||
session_id: id.clone(),
|
||||
owner: String::new(),
|
||||
};
|
||||
let entry = state
|
||||
.in_flight
|
||||
.register(&scope, &serde_json::json!(7))
|
||||
.unwrap();
|
||||
let (params, _) = cancelled_params(&serde_json::json!({"requestId":7})).unwrap();
|
||||
let mut future = Box::pin(async {
|
||||
if cancel {
|
||||
cancelled_with(&h, &h, params, notification_context()).await;
|
||||
} else {
|
||||
initialized_with(&h, &h, notification_context()).await;
|
||||
}
|
||||
});
|
||||
tokio::time::timeout(std::time::Duration::from_secs(2), async {
|
||||
tokio::select! {
|
||||
_ = &mut future => panic!("pending operation completed"),
|
||||
_ = async { while !store.entered.load(SeqCst) { tokio::task::yield_now().await; } } => {}
|
||||
}
|
||||
}).await.unwrap();
|
||||
assert!(!store.dropped.load(SeqCst));
|
||||
drop(future);
|
||||
assert!(
|
||||
store.dropped.load(SeqCst),
|
||||
"the actual store future belongs to this await"
|
||||
);
|
||||
store.gate.notify_waiters();
|
||||
tokio::task::yield_now().await;
|
||||
assert_eq!(store.marks.load(SeqCst), 0);
|
||||
assert_eq!(store.reads.load(SeqCst), 0);
|
||||
assert!(h.take().is_none());
|
||||
assert!(!store.inner.get(&id).await.unwrap().unwrap().initialized);
|
||||
assert!(futures_util::poll!(Box::pin(entry.cancel_signal().cancelled())).is_pending());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
//! #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_handler_completed(),
|
||||
h.counters.sdk_ping_handler_consumed(),
|
||||
);
|
||||
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_handler_completed() - before.0,
|
||||
h.counters.sdk_ping_handler_consumed() - 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_handler_completed(),
|
||||
h.counters.sdk_ping_handler_consumed()
|
||||
),
|
||||
(0, 0)
|
||||
);
|
||||
assert_eq!(
|
||||
(
|
||||
h.counters.sdk_ping_handler_completed(),
|
||||
h.counters.sdk_ping_handler_consumed()
|
||||
),
|
||||
(0, 0),
|
||||
"pre-handler guard rejection must not execute or consume the SDK ping handler"
|
||||
);
|
||||
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());
|
||||
observe(c.clone(), async {
|
||||
super::sdk_ping_handler_completed();
|
||||
assert_eq!(
|
||||
(
|
||||
c.sdk_ping_handler_completed(),
|
||||
c.sdk_ping_handler_consumed()
|
||||
),
|
||||
(1, 0)
|
||||
);
|
||||
super::sdk_ping_handler_consumed();
|
||||
assert_eq!(
|
||||
(
|
||||
c.sdk_ping_handler_completed(),
|
||||
c.sdk_ping_handler_consumed()
|
||||
),
|
||||
(1, 1)
|
||||
);
|
||||
})
|
||||
.await;
|
||||
super::sdk_ping_handler_completed();
|
||||
super::sdk_ping_handler_consumed();
|
||||
observe(Arc::new(CounterSet::default()), async {
|
||||
super::sdk_ping_handler_completed();
|
||||
super::sdk_ping_handler_consumed();
|
||||
})
|
||||
.await;
|
||||
assert_eq!(
|
||||
(
|
||||
c.sdk_ping_handler_completed(),
|
||||
c.sdk_ping_handler_consumed()
|
||||
),
|
||||
(1, 1),
|
||||
"scope isolation and out-of-scope no-op"
|
||||
);
|
||||
assert_eq!(c.snapshot(), Snapshot::new(0, 0, 0, 0));
|
||||
}
|
||||
@@ -0,0 +1,305 @@
|
||||
//! 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 peer_controls: crate::app::rmcp_progress_peer::controls::Controls,
|
||||
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(®istry, &operation).await;
|
||||
let key = create_api_key(®istry).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 peer_controls = crate::app::rmcp_progress_peer::controls::Controls::default();
|
||||
let peer_scope = peer_controls.clone();
|
||||
let app = app.layer(axum::middleware::from_fn(
|
||||
move |request: axum::extract::Request, next: axum::middleware::Next| {
|
||||
let c = scope.clone();
|
||||
let control = peer_scope.clone();
|
||||
async move {
|
||||
let response = crate::app::rmcp_progress_peer::controls::Controls::scope(
|
||||
control,
|
||||
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,
|
||||
peer_controls,
|
||||
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,460 @@
|
||||
//! #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;
|
||||
|
||||
#[path = "rmcp_progress_peer_runtime_tests.rs"]
|
||||
mod peer_runtime;
|
||||
|
||||
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,77 @@
|
||||
//! Actual SDK send/output controls through the ordinary HTTP/SSE consumer.
|
||||
use super::*;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
async fn peer_output_control(revision: &str) {
|
||||
let h = Endpoint::started().await;
|
||||
let session = h.session(revision).await;
|
||||
let controls = &h.peer_controls;
|
||||
controls.mutate_output_message();
|
||||
h.gate.add_permits(1);
|
||||
let reply = frames(
|
||||
h.call(
|
||||
revision,
|
||||
session.as_deref(),
|
||||
json!(61),
|
||||
Some(json!(1.0)),
|
||||
"text/event-stream",
|
||||
)
|
||||
.send()
|
||||
.await
|
||||
.unwrap(),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(reply.len(), 4);
|
||||
assert_eq!(
|
||||
reply[0]["params"]["message"], "controlled output mutation",
|
||||
"SSE must consume actual SDK output, not a parallel handwritten frame"
|
||||
);
|
||||
assert_eq!(reply[0]["params"]["progressToken"].to_string(), "1.0");
|
||||
terminal(&reply, revision, json!(61));
|
||||
assert_eq!(controls.actual_send(), 3);
|
||||
assert_eq!(controls.sink_entered(), 3);
|
||||
assert_eq!(controls.output_consumed(), 3);
|
||||
assert_eq!(controls.owner_completed(), 1);
|
||||
assert_eq!(h.started.load(Ordering::SeqCst), 1);
|
||||
|
||||
controls.fail_next_send();
|
||||
h.gate.add_permits(1);
|
||||
let reply = frames(
|
||||
h.call(
|
||||
revision,
|
||||
session.as_deref(),
|
||||
json!(62),
|
||||
Some(json!("failed-send")),
|
||||
"text/event-stream",
|
||||
)
|
||||
.send()
|
||||
.await
|
||||
.unwrap(),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
reply.len(),
|
||||
1,
|
||||
"failed SDK send disables progress without fallback"
|
||||
);
|
||||
terminal(&reply, revision, json!(62));
|
||||
assert_eq!(controls.actual_send(), 4, "no SDK retry/restart");
|
||||
assert_eq!(controls.sink_entered(), 4);
|
||||
assert_eq!(
|
||||
controls.output_consumed(),
|
||||
3,
|
||||
"failed output cannot count as delivered"
|
||||
);
|
||||
assert_eq!(controls.owner_completed(), 2);
|
||||
assert_eq!(h.started.load(Ordering::SeqCst), 2, "same Tool, no replay");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_progress_peer_runtime_legacy() {
|
||||
peer_output_control(LEGACY_REVISION).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_progress_peer_runtime_modern() {
|
||||
peer_output_control(MODERN_REVISION).await;
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
use super::ProgressPeer;
|
||||
use super::controls::Controls;
|
||||
use serde_json::{Value, json};
|
||||
use std::{
|
||||
io::Write,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct LogCapture(Arc<Mutex<Vec<u8>>>);
|
||||
|
||||
impl Write for LogCapture {
|
||||
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
|
||||
self.0.lock().unwrap().extend_from_slice(bytes);
|
||||
Ok(bytes.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for LogCapture {
|
||||
type Writer = Self;
|
||||
fn make_writer(&'a self) -> Self {
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sdk_trace_keeps_raw_string_and_numeric_tokens_private_without_disabling_logs() {
|
||||
let capture = LogCapture::default();
|
||||
let subscriber = tracing_subscriber::fmt()
|
||||
.with_max_level(tracing::Level::TRACE)
|
||||
.without_time()
|
||||
.with_ansi(false)
|
||||
.with_writer(capture.clone())
|
||||
.finish();
|
||||
let dispatch = tracing::Dispatch::new(subscriber);
|
||||
let _guard = tracing::dispatcher::set_default(&dispatch);
|
||||
// All spawned pump tasks run under this thread's TRACE capture, not just the caller.
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(async {
|
||||
for raw in [
|
||||
"\"private-progress-canary-716283941\"",
|
||||
"-716283941",
|
||||
"1.572839416",
|
||||
"1.0",
|
||||
"-0.0",
|
||||
"18446744073709551615",
|
||||
"7.182736451928374651e+27",
|
||||
] {
|
||||
let token: Value = serde_json::from_str(raw).unwrap();
|
||||
let mut peer = ProgressPeer::start(token.clone());
|
||||
let output = peer.notify(u64::MAX, "running").await.unwrap();
|
||||
assert_eq!(
|
||||
output["params"]["progressToken"].to_string(),
|
||||
token.to_string()
|
||||
);
|
||||
assert_eq!(output["params"]["progress"], json!(u64::MAX));
|
||||
peer.finish().await;
|
||||
}
|
||||
tracing::info!("application-log-still-enabled");
|
||||
});
|
||||
let logs = String::from_utf8(capture.0.lock().unwrap().clone()).unwrap();
|
||||
assert!(logs.contains("application-log-still-enabled"));
|
||||
let notifications: Vec<_> = logs
|
||||
.lines()
|
||||
.filter(|line| line.contains("ProxyMessage(Notification"))
|
||||
.collect();
|
||||
assert_eq!(notifications.len(), 7, "actual SDK TRACE must be captured");
|
||||
assert!(
|
||||
notifications
|
||||
.iter()
|
||||
.all(|line| line.contains("ProgressToken(String(\"crank-internal-progress\"))"))
|
||||
);
|
||||
for raw in [
|
||||
"private-progress-canary-716283941",
|
||||
"-716283941",
|
||||
"1.572839416",
|
||||
"18446744073709551615",
|
||||
"7.182736451928374651",
|
||||
] {
|
||||
assert!(!logs.contains(raw), "raw token leaked into SDK TRACE");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn emits_real_sdk_progress_notification_with_exact_raw_numbers() {
|
||||
let token: Value = serde_json::from_str("1.0").unwrap();
|
||||
let mut peer = ProgressPeer::start(token);
|
||||
let output = peer.notify(9_007_199_254_740_993, "running").await.unwrap();
|
||||
assert_eq!(output["method"], "notifications/progress");
|
||||
assert_eq!(output["params"]["progressToken"].to_string(), "1.0");
|
||||
assert_eq!(
|
||||
output["params"]["progress"],
|
||||
json!(9_007_199_254_740_993u64)
|
||||
);
|
||||
assert_eq!(output["params"]["message"], "running");
|
||||
peer.finish().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reports_output_send_failure_instead_of_claiming_delivery() {
|
||||
let mut peer = ProgressPeer::start(json!(4));
|
||||
peer.receiver.close();
|
||||
assert!(peer.notify(2, "running").await.is_err());
|
||||
peer.finish().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn request_controls_observe_mutated_actual_sdk_output_and_owner_shutdown() {
|
||||
let controls = Controls::default();
|
||||
controls.mutate_output_message();
|
||||
Controls::scope(controls.clone(), async {
|
||||
let mut peer = ProgressPeer::start(json!(4));
|
||||
let output = peer.notify(2, "running").await.unwrap();
|
||||
assert_eq!(output["params"]["message"], "controlled output mutation");
|
||||
peer.finish().await;
|
||||
})
|
||||
.await;
|
||||
assert_eq!(controls.actual_send(), 1);
|
||||
assert_eq!(controls.sink_entered(), 1);
|
||||
assert_eq!(controls.output_consumed(), 1);
|
||||
assert_eq!(controls.owner_completed(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn failed_sdk_send_does_not_claim_output() {
|
||||
let controls = Controls::default();
|
||||
controls.fail_next_send();
|
||||
Controls::scope(controls.clone(), async {
|
||||
let mut peer = ProgressPeer::start(json!(4));
|
||||
assert!(peer.notify(2, "running").await.is_err());
|
||||
peer.finish().await;
|
||||
})
|
||||
.await;
|
||||
assert_eq!(controls.actual_send(), 1);
|
||||
assert_eq!(controls.sink_entered(), 1);
|
||||
assert_eq!(controls.output_consumed(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sink_gate_holds_responder_after_output_and_cleans_up_on_release() {
|
||||
let controls = Controls::default();
|
||||
controls.hold_sink();
|
||||
Controls::scope(controls.clone(), async {
|
||||
let mut peer = ProgressPeer::start(json!(4));
|
||||
{
|
||||
let send = peer.notify(2, "running");
|
||||
tokio::pin!(send);
|
||||
tokio::time::timeout(std::time::Duration::from_secs(2), async {
|
||||
while controls.output_received() == 0 {
|
||||
tokio::select! {
|
||||
result = &mut send => panic!("send completed before gate release: {result:?}"),
|
||||
_ = tokio::task::yield_now() => {}
|
||||
}
|
||||
}
|
||||
}).await.expect("SDK output received while responder is gated");
|
||||
assert_eq!(controls.output_consumed(), 0);
|
||||
controls.release_sink();
|
||||
assert!(send.await.is_ok());
|
||||
}
|
||||
peer.finish().await;
|
||||
})
|
||||
.await;
|
||||
assert_eq!(controls.owner_completed(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dropping_pending_gated_send_releases_and_joins_owner() {
|
||||
let controls = Controls::default();
|
||||
controls.hold_sink();
|
||||
Controls::scope(controls.clone(), async {
|
||||
let mut peer = ProgressPeer::start(json!(4));
|
||||
{
|
||||
let send = peer.notify(2, "running");
|
||||
tokio::pin!(send);
|
||||
tokio::time::timeout(std::time::Duration::from_secs(2), async {
|
||||
while controls.output_queued() == 0 {
|
||||
tokio::select! {
|
||||
result = &mut send => panic!("send completed before drop: {result:?}"),
|
||||
_ = tokio::task::yield_now() => {}
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("SDK output queued before pending send drop");
|
||||
}
|
||||
peer.finish().await;
|
||||
})
|
||||
.await;
|
||||
assert_eq!(controls.owner_completed(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn raw_numeric_tokens_and_counts_survive_sdk_projection() {
|
||||
for token in ["-7", "1.5", "-0", "18446744073709551615"] {
|
||||
let token: Value = serde_json::from_str(token).unwrap();
|
||||
let mut peer = ProgressPeer::start(token.clone());
|
||||
let output = peer.notify(u64::MAX, "running").await.unwrap();
|
||||
assert_eq!(output["params"]["progressToken"], token);
|
||||
assert_eq!(output["params"]["progress"], json!(u64::MAX));
|
||||
peer.finish().await;
|
||||
}
|
||||
let token: Value = serde_json::from_str("1.0").unwrap();
|
||||
let mut peer = ProgressPeer::start(token.clone());
|
||||
let output = peer.notify(9_007_199_254_740_993, "running").await.unwrap();
|
||||
assert_eq!(output["params"]["progressToken"], token);
|
||||
assert_eq!(
|
||||
output["params"]["progress"],
|
||||
json!(9_007_199_254_740_993u64)
|
||||
);
|
||||
peer.finish().await;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
//! Handler completion/consumption is independent of the existing DTO wire oracle.
|
||||
use super::{CounterSet, observe, rmcp_prompts_runtime_tests::control, support::*};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub(super) fn counts(c: &CounterSet) -> [usize; 4] {
|
||||
[
|
||||
c.sdk_prompts_list_handler_completed(),
|
||||
c.sdk_prompts_get_handler_completed(),
|
||||
c.sdk_prompts_list_handler_consumed(),
|
||||
c.sdk_prompts_get_handler_consumed(),
|
||||
]
|
||||
}
|
||||
|
||||
pub(super) fn delta(c: &CounterSet, before: [usize; 4], method: &str) -> ([usize; 4], [usize; 4]) {
|
||||
let after = counts(c);
|
||||
let expected = match method {
|
||||
"prompts/list" => [1, 0, 1, 0],
|
||||
"prompts/get" => [0, 1, 0, 1],
|
||||
_ => panic!("not a Prompts method: {method}"),
|
||||
};
|
||||
(std::array::from_fn(|i| after[i] - before[i]), expected)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_prompts_handler_legacy() {
|
||||
control(LEGACY_REVISION, true).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_prompts_handler_modern() {
|
||||
control(MODERN_REVISION, true).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_prompts_handler_observer_positive_control() {
|
||||
use super::{
|
||||
sdk_prompts_get_handler_completed, sdk_prompts_get_handler_consumed,
|
||||
sdk_prompts_list_handler_completed, sdk_prompts_list_handler_consumed,
|
||||
};
|
||||
let a = Arc::new(CounterSet::default());
|
||||
let b = Arc::new(CounterSet::default());
|
||||
sdk_prompts_list_handler_completed();
|
||||
sdk_prompts_get_handler_completed();
|
||||
sdk_prompts_list_handler_consumed();
|
||||
sdk_prompts_get_handler_consumed();
|
||||
assert_eq!(counts(&a), [0; 4], "no-scope markers must be no-ops");
|
||||
let snapshot = a.snapshot();
|
||||
tokio::join!(
|
||||
observe(a.clone(), async {
|
||||
sdk_prompts_list_handler_completed();
|
||||
tokio::task::yield_now().await;
|
||||
sdk_prompts_get_handler_completed();
|
||||
sdk_prompts_list_handler_consumed();
|
||||
sdk_prompts_get_handler_consumed();
|
||||
}),
|
||||
observe(b.clone(), async {
|
||||
sdk_prompts_get_handler_completed();
|
||||
tokio::task::yield_now().await;
|
||||
sdk_prompts_get_handler_consumed();
|
||||
}),
|
||||
);
|
||||
assert_eq!(counts(&a), [1; 4]);
|
||||
assert_eq!(counts(&b), [0, 1, 0, 1]);
|
||||
assert_eq!(a.snapshot(), snapshot);
|
||||
assert_eq!(b.snapshot(), CounterSet::default().snapshot());
|
||||
for c in [&a, &b] {
|
||||
assert_eq!(c.sdk_prompts_dispatch(), 0);
|
||||
assert_eq!(c.sdk_prompts_list_result(), 0);
|
||||
assert_eq!(c.sdk_prompts_get_result(), 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,392 @@
|
||||
//! Synthetic replies exercise real trait results, restoration and structured cancellation.
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct Domain {
|
||||
outcome: Mutex<Option<PromptsOutcome>>,
|
||||
calls: AtomicUsize,
|
||||
pending: bool,
|
||||
started: tokio::sync::Notify,
|
||||
release: tokio::sync::Notify,
|
||||
dropped: AtomicBool,
|
||||
finished: AtomicBool,
|
||||
}
|
||||
struct DropWitness<'a>(&'a AtomicBool);
|
||||
impl Drop for DropWitness<'_> {
|
||||
fn drop(&mut self) {
|
||||
self.0.store(true, SeqCst);
|
||||
}
|
||||
}
|
||||
impl Domain {
|
||||
pub(super) async fn prompts(&self, command: PromptsCommand) -> PromptsOutcome {
|
||||
self.calls.fetch_add(1, SeqCst);
|
||||
match command {
|
||||
PromptsCommand::List(list) => {
|
||||
assert_eq!(list.limit, Some(7));
|
||||
assert_eq!(list.cursor.as_deref(), Some("opaque"));
|
||||
}
|
||||
PromptsCommand::Get(get) => {
|
||||
assert_eq!(get.name, "bound-prompt");
|
||||
assert_eq!(
|
||||
get.arguments,
|
||||
Some(
|
||||
json!({"number":42,"enabled":true,"nested":[null]})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
let _guard = DropWitness(&self.dropped);
|
||||
if self.pending {
|
||||
self.started.notify_one();
|
||||
self.release.notified().await;
|
||||
}
|
||||
self.finished.store(true, SeqCst);
|
||||
self.outcome
|
||||
.lock()
|
||||
.unwrap()
|
||||
.take()
|
||||
.expect("exactly one domain execution")
|
||||
}
|
||||
}
|
||||
fn scope(revision: &str) -> PromptScope {
|
||||
PromptScope {
|
||||
workspace_slug: "fresh-workspace".into(),
|
||||
agent_slug: "fresh-agent".into(),
|
||||
credential: VerifiedMachineCredential {
|
||||
machine_access_mode: crank_core::MachineAccessMode::StaticAgentKey,
|
||||
max_security_level: crank_core::OperationSecurityLevel::Standard,
|
||||
scopes: vec![crank_core::PlatformApiKeyScope::Read],
|
||||
platform_api_key_id: Some("fresh-key".into()),
|
||||
},
|
||||
correlation: CorrelationContext::generate(),
|
||||
continuation: ContinuationSupport::from_supported(false),
|
||||
locale: ExecutionLocale::En,
|
||||
revision: revision.into(),
|
||||
limit: Some(7),
|
||||
}
|
||||
}
|
||||
async fn state() -> Arc<AppState> {
|
||||
crank_test_support::postgres_schema_url_for_task("prompts_handler_local", "t_2d797361").await;
|
||||
super::super::rmcp_tools_tests::test_state().await
|
||||
}
|
||||
fn handler(state: &Arc<AppState>, outcome: PromptsOutcome) -> PromptHandler {
|
||||
let mut handler = PromptHandler::new(state);
|
||||
handler.domain = Some(Arc::new(Domain {
|
||||
outcome: Mutex::new(Some(outcome)),
|
||||
..Default::default()
|
||||
}));
|
||||
handler
|
||||
}
|
||||
fn page() -> Option<PaginatedRequestParams> {
|
||||
Some(PaginatedRequestParams::default().with_cursor(Some("opaque".into())))
|
||||
}
|
||||
fn get() -> GetPromptRequestParams {
|
||||
let mut request = GetPromptRequestParams::new("bound-prompt");
|
||||
request.arguments = Some(
|
||||
json!({"number":42,"enabled":true,"nested":[null]})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone(),
|
||||
);
|
||||
request
|
||||
}
|
||||
async fn context(handler: &PromptHandler, revision: &str) -> RequestContext<RoleServer> {
|
||||
let context = request_context(handler, scope(revision)).await.unwrap();
|
||||
assert!(context.peer.is_transport_closed());
|
||||
assert_eq!(
|
||||
Arc::strong_count(&handler.reply),
|
||||
1,
|
||||
"seed service released handler"
|
||||
);
|
||||
assert_eq!(handler.domain.as_ref().unwrap().calls.load(SeqCst), 0);
|
||||
let actual = PromptHandler::scope(&context).unwrap();
|
||||
assert_eq!(actual.workspace_slug, "fresh-workspace");
|
||||
assert_eq!(actual.agent_slug, "fresh-agent");
|
||||
assert_eq!(
|
||||
actual
|
||||
.credential
|
||||
.platform_api_key_id
|
||||
.as_ref()
|
||||
.map(|id| id.as_str()),
|
||||
Some("fresh-key")
|
||||
);
|
||||
assert_eq!(actual.revision, revision);
|
||||
context
|
||||
}
|
||||
fn assert_list(outcome: PromptsOutcome, raw: Vec<Value>, cursor: Option<&str>) {
|
||||
match outcome {
|
||||
PromptsOutcome::Listed {
|
||||
prompts,
|
||||
next_cursor,
|
||||
} => {
|
||||
assert_eq!(prompts, raw);
|
||||
assert_eq!(next_cursor.as_deref(), cursor);
|
||||
}
|
||||
_ => panic!("list restore failed"),
|
||||
}
|
||||
}
|
||||
fn assert_get(outcome: PromptsOutcome, raw: Vec<Value>, expected: Option<&str>) {
|
||||
match outcome {
|
||||
PromptsOutcome::Got {
|
||||
description,
|
||||
messages,
|
||||
} => {
|
||||
assert_eq!(messages, raw);
|
||||
assert_eq!(description.as_deref(), expected);
|
||||
}
|
||||
_ => panic!("get restore failed"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_prompts_trait_lossless_and_actual_typed_mutations_consumed() {
|
||||
let state = state().await;
|
||||
for revision in ["2025-11-25", "2026-07-28"] {
|
||||
let raw = vec![
|
||||
json!({"name":"a","description":null,"icons":{"not":"SDK array"},"annotations":false,"x":[null,{"nested":true}],"arguments":[{"name":"n","required":null,"description":null,"type":"integer","default":42,"enum":[1,42],"x":null}]}),
|
||||
json!({"name":"b","arguments":null}),
|
||||
json!({"name":"c"}),
|
||||
];
|
||||
let h = handler(
|
||||
&state,
|
||||
PromptsOutcome::Listed {
|
||||
prompts: raw.clone(),
|
||||
next_cursor: Some("next".into()),
|
||||
},
|
||||
);
|
||||
let c = context(&h, revision).await;
|
||||
let mut result = ServerHandler::list_prompts(&h, page(), c).await.unwrap();
|
||||
result.prompts[0].title = Some("typed-title".into());
|
||||
result.prompts[0].arguments.as_mut().unwrap()[0].title = Some("typed-argument".into());
|
||||
result.next_cursor = Some("typed-cursor".into());
|
||||
let mut expected = raw;
|
||||
expected[0]["title"] = json!("typed-title");
|
||||
expected[0]["arguments"][0]["title"] = json!("typed-argument");
|
||||
assert_list(restore_list(&h, Ok(result)), expected, Some("typed-cursor"));
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
let text = serde_json::to_string_pretty(&json!({"exact":[null,true]})).unwrap();
|
||||
let raw = vec![
|
||||
json!({"role":"system","content":{"type":"text","text":text,"annotations":[null,42],"x":null},"x":{"nested":true}}),
|
||||
json!({"role":"user","content":{"type":"image","data":"AAEC/w==","mimeType":"image/png","annotations":false,"x":[1,null]}}),
|
||||
json!({"role":"assistant","content":{"type":"audio","data":"AAEC/w==","mimeType":"audio/wav"}}),
|
||||
json!({"role":"user","content":{"type":"resource","resource":{"uri":"crank://text","text":"exact\nbytes","mimeType":null,"x":[null]},"annotations":{"arbitrary":true}}}),
|
||||
json!({"role":"user","content":{"type":"resource","resource":{"uri":"crank://blob","blob":"AAEC/w==","x":false},"x":null}}),
|
||||
json!({"role":"user","content":{"type":"resource_link","uri":"crank://link","name":"link","description":null,"icons":false,"annotations":42,"x":{"a":null}}}),
|
||||
];
|
||||
let h = handler(
|
||||
&state,
|
||||
PromptsOutcome::Got {
|
||||
description: None,
|
||||
messages: raw.clone(),
|
||||
},
|
||||
);
|
||||
let c = context(&h, revision).await;
|
||||
let GetPromptResponse::Complete(mut result) =
|
||||
ServerHandler::get_prompt(&h, get(), c).await.unwrap()
|
||||
else {
|
||||
panic!("expected complete prompt")
|
||||
};
|
||||
result.description = Some("typed-description".into());
|
||||
// Content is mutated through the SDK result, not the original domain JSON.
|
||||
let mut message = serde_json::to_value(&result.messages[0]).unwrap();
|
||||
message["content"]["text"] = json!("typed-text");
|
||||
result.messages[0] = serde_json::from_value(message).unwrap();
|
||||
let mut expected = raw;
|
||||
expected[0]["content"]["text"] = json!("typed-text");
|
||||
assert_get(
|
||||
restore_get(&h, Ok(result.into())),
|
||||
expected,
|
||||
Some("typed-description"),
|
||||
);
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
let h = handler(
|
||||
&state,
|
||||
PromptsOutcome::Got {
|
||||
description: None,
|
||||
messages: vec![],
|
||||
},
|
||||
);
|
||||
let c = context(&h, revision).await;
|
||||
let result = ServerHandler::get_prompt(&h, get(), c).await;
|
||||
assert_get(restore_get(&h, result), vec![], None);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_prompts_trait_errors_fail_closed_without_reexecution() {
|
||||
let state = state().await;
|
||||
for is_list in [true, false] {
|
||||
for outcome in [
|
||||
PromptsOutcome::InvalidParams("exact-detail".into()),
|
||||
PromptsOutcome::NotAvailable,
|
||||
PromptsOutcome::ArgumentsRejected {
|
||||
reason: "wrong_type",
|
||||
argument: Some("number".into()),
|
||||
},
|
||||
PromptsOutcome::ArgumentsRejected {
|
||||
reason: "missing",
|
||||
argument: None,
|
||||
},
|
||||
PromptsOutcome::DependencyUnavailable { position: 13 },
|
||||
PromptsOutcome::CatalogUnavailable,
|
||||
] {
|
||||
let expected = std::mem::discriminant(&outcome);
|
||||
let h = handler(&state, outcome);
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let actual = if is_list {
|
||||
let result = ServerHandler::list_prompts(&h, page(), c).await;
|
||||
assert!(result.is_err());
|
||||
restore_list(&h, result)
|
||||
} else {
|
||||
let result = ServerHandler::get_prompt(&h, get(), c).await;
|
||||
assert!(result.is_err());
|
||||
restore_get(&h, result)
|
||||
};
|
||||
assert_eq!(std::mem::discriminant(&actual), expected);
|
||||
match actual {
|
||||
PromptsOutcome::InvalidParams(detail) => assert_eq!(detail, "exact-detail"),
|
||||
PromptsOutcome::ArgumentsRejected { reason, argument } => {
|
||||
assert_eq!(
|
||||
reason,
|
||||
if argument.is_some() {
|
||||
"wrong_type"
|
||||
} else {
|
||||
"missing"
|
||||
}
|
||||
);
|
||||
if let Some(argument) = argument {
|
||||
assert_eq!(argument, "number");
|
||||
}
|
||||
}
|
||||
PromptsOutcome::DependencyUnavailable { position } => assert_eq!(position, 13),
|
||||
_ => {}
|
||||
}
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
}
|
||||
}
|
||||
let h = handler(
|
||||
&state,
|
||||
PromptsOutcome::Listed {
|
||||
prompts: vec![json!({"description":"missing name"})],
|
||||
next_cursor: None,
|
||||
},
|
||||
);
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let result = ServerHandler::list_prompts(&h, page(), c).await;
|
||||
assert!(result.is_err());
|
||||
assert!(matches!(
|
||||
restore_list(&h, result),
|
||||
PromptsOutcome::CatalogUnavailable
|
||||
));
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
for message in [
|
||||
json!({"role":"invalid","content":{"type":"text","text":"x"}}),
|
||||
json!({"role":"user","content":{"type":"text"}}),
|
||||
] {
|
||||
let h = handler(
|
||||
&state,
|
||||
PromptsOutcome::Got {
|
||||
description: None,
|
||||
messages: vec![message],
|
||||
},
|
||||
);
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let result = ServerHandler::get_prompt(&h, get(), c).await;
|
||||
assert!(result.is_err());
|
||||
assert!(matches!(
|
||||
restore_get(&h, result),
|
||||
PromptsOutcome::CatalogUnavailable
|
||||
));
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
}
|
||||
for change in 0..3 {
|
||||
let h = handler(
|
||||
&state,
|
||||
PromptsOutcome::Listed {
|
||||
prompts: vec![json!({"name":"a","x":1}), json!({"name":"b","x":2})],
|
||||
next_cursor: None,
|
||||
},
|
||||
);
|
||||
let mut c = context(&h, "2026-07-28").await;
|
||||
c.extensions = Default::default();
|
||||
assert!(ServerHandler::list_prompts(&h, page(), c).await.is_err());
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 0);
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let mut result = ServerHandler::list_prompts(&h, page(), c).await.unwrap();
|
||||
let result = match change {
|
||||
0 => {
|
||||
result.prompts.clear();
|
||||
Ok(result)
|
||||
}
|
||||
1 => {
|
||||
result.prompts.swap(0, 1);
|
||||
Ok(result)
|
||||
}
|
||||
_ => Err(internal()),
|
||||
};
|
||||
assert!(matches!(
|
||||
restore_list(&h, result),
|
||||
PromptsOutcome::CatalogUnavailable
|
||||
));
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
}
|
||||
let h = handler(
|
||||
&state,
|
||||
PromptsOutcome::Got {
|
||||
description: None,
|
||||
messages: vec![],
|
||||
},
|
||||
);
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let result = ServerHandler::list_prompts(&h, page(), c).await;
|
||||
assert!(result.is_err());
|
||||
assert!(matches!(
|
||||
restore_list(&h, result),
|
||||
PromptsOutcome::CatalogUnavailable
|
||||
));
|
||||
assert!(matches!(
|
||||
restore_get(&h, Err(internal())),
|
||||
PromptsOutcome::CatalogUnavailable
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_prompts_parent_drop_cancels_domain_and_bootstrap_is_closed() {
|
||||
let state = state().await;
|
||||
for is_list in [true, false] {
|
||||
let mut h = PromptHandler::new(&state);
|
||||
let domain = Arc::new(Domain {
|
||||
pending: true,
|
||||
..Default::default()
|
||||
});
|
||||
h.domain = Some(domain.clone());
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let mut parent = Box::pin(async {
|
||||
if is_list {
|
||||
let result = ServerHandler::list_prompts(&h, page(), c).await;
|
||||
restore_list(&h, result)
|
||||
} else {
|
||||
let result = ServerHandler::get_prompt(&h, get(), c).await;
|
||||
restore_get(&h, result)
|
||||
}
|
||||
});
|
||||
tokio::select! { _ = &mut parent => panic!("domain should wait"), _ = domain.started.notified() => {} }
|
||||
assert!(!domain.dropped.load(SeqCst));
|
||||
drop(parent);
|
||||
assert!(
|
||||
domain.dropped.load(SeqCst),
|
||||
"domain future dropped synchronously with parent"
|
||||
);
|
||||
domain.release.notify_one();
|
||||
tokio::task::yield_now().await;
|
||||
assert!(!domain.finished.load(SeqCst), "no detached continuation");
|
||||
assert!(h.take().is_none());
|
||||
assert_eq!(domain.calls.load(SeqCst), 1);
|
||||
assert_eq!(Arc::strong_count(&domain), 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,492 @@
|
||||
//! #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 super::rmcp_prompts_handler_runtime_tests as handler;
|
||||
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]),
|
||||
)
|
||||
}
|
||||
pub(super) async fn control(
|
||||
revision: &str,
|
||||
require_handler: bool,
|
||||
) -> 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 handlers = 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 before = handler::counts(&h.counters);
|
||||
let (result, delta) = success(&h, revision, session, "prompts/list", params, sse).await;
|
||||
handlers.push(handler::delta(&h.counters, before, "prompts/list"));
|
||||
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 before = handler::counts(&h.counters);
|
||||
let (result, delta) = success(
|
||||
&h,
|
||||
revision,
|
||||
session,
|
||||
"prompts/get",
|
||||
json!({"name":name,"arguments":args}),
|
||||
sse,
|
||||
)
|
||||
.await;
|
||||
handlers.push(handler::delta(&h.counters, before, "prompts/get"));
|
||||
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 before = handler::counts(&h.counters);
|
||||
let body = wire(h.post(revision, session, &req, sse).await, &req["id"], sse).await;
|
||||
handlers.push(handler::delta(&h.counters, before, "prompts/get"));
|
||||
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 before = handler::counts(&h.counters);
|
||||
let body = wire(h.post(revision, session, &req, sse).await, &req["id"], sse).await;
|
||||
handlers.push(handler::delta(&h.counters, before, "prompts/get"));
|
||||
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 before = handler::counts(&h.counters);
|
||||
let req = request(revision, "prompts/list", params.clone());
|
||||
let body = wire(
|
||||
h.post(revision, session, &req, false).await,
|
||||
&req["id"],
|
||||
false,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(body["error"]["code"], -32602, "{body}");
|
||||
if params.get("cursor").is_some() {
|
||||
handlers.push(handler::delta(&h.counters, before, "prompts/list"));
|
||||
} else {
|
||||
assert_eq!(
|
||||
handler::counts(&h.counters),
|
||||
before,
|
||||
"parsing rejects must not enter handlers"
|
||||
);
|
||||
}
|
||||
}
|
||||
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:?}"
|
||||
);
|
||||
sqlx::query(
|
||||
"update platform_api_keys set scopes_json = '[]'::jsonb where id = 'pk_dispatch_observer'",
|
||||
)
|
||||
.execute(h.registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
for (method, params) in [
|
||||
("prompts/list", json!({})),
|
||||
("prompts/get", json!({"name":"greeting"})),
|
||||
] {
|
||||
let before = handler::counts(&h.counters);
|
||||
let req = request(revision, method, params);
|
||||
assert_eq!(h.post(revision, session, &req, false).await.status(), 403);
|
||||
assert_eq!(
|
||||
handler::counts(&h.counters),
|
||||
before,
|
||||
"scope rejects must precede handlers"
|
||||
);
|
||||
}
|
||||
sqlx::query("update platform_api_keys set scopes_json = '[\"read\",\"write\"]'::jsonb where id = 'pk_dispatch_observer'")
|
||||
.execute(h.registry.pool()).await.unwrap();
|
||||
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"}}),
|
||||
),
|
||||
] {
|
||||
let before = handler::counts(&h.counters);
|
||||
assert_eq!(
|
||||
h.post(revision, session, &request(revision, method, params), false)
|
||||
.await
|
||||
.status(),
|
||||
401
|
||||
);
|
||||
assert_eq!(
|
||||
handler::counts(&h.counters),
|
||||
before,
|
||||
"revocation must precede handlers"
|
||||
);
|
||||
}
|
||||
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:?}"
|
||||
);
|
||||
if require_handler {
|
||||
assert!(
|
||||
handlers.iter().all(|(actual, expected)| actual == expected),
|
||||
"SDK Prompts handler completion/consumption missing after successful HTTP wire/error/guard checks: {handlers:?}"
|
||||
);
|
||||
}
|
||||
deltas
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_prompts_wire_legacy() {
|
||||
control(LEGACY_REVISION, false).await;
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_prompts_wire_modern() {
|
||||
control(MODERN_REVISION, false).await;
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_prompts_sdk_legacy() {
|
||||
let deltas = control(LEGACY_REVISION, false).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, false).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_handler", "t_7afaaafe").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(®istry).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,125 @@
|
||||
//! Actual handler completion and Result consumption, separate from DTO serialization.
|
||||
use super::{CounterSet, observe, rmcp_resources_runtime_tests::control, support::*};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub(super) async fn foreign_agent(registry: &crank_registry::PostgresRegistry) -> &'static str {
|
||||
use crank_core::{Agent, AgentId, AgentStatus, AgentVersion, ToolSelectionPolicy};
|
||||
use crank_registry::{CreateAgentRequest, PublishAgentRequest};
|
||||
let id = AgentId::new("agent_resources_foreign");
|
||||
registry
|
||||
.create_agent(CreateAgentRequest {
|
||||
agent: &Agent {
|
||||
id: id.clone(),
|
||||
workspace_id: workspace_id(),
|
||||
slug: "resources-foreign".to_owned(),
|
||||
display_name: "Other published Agent".to_owned(),
|
||||
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.clone(),
|
||||
version: 1,
|
||||
status: AgentStatus::Draft,
|
||||
instructions: serde_json::json!({}),
|
||||
tool_selection_policy: ToolSelectionPolicy::default(),
|
||||
created_at: test_time(),
|
||||
},
|
||||
bindings: &[],
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
registry
|
||||
.publish_agent(PublishAgentRequest {
|
||||
workspace_id: &workspace_id(),
|
||||
agent_id: &id,
|
||||
version: 1,
|
||||
published_at: &test_time(),
|
||||
published_by: Some("alice"),
|
||||
expected_state: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
"resources-foreign"
|
||||
}
|
||||
|
||||
pub(super) fn counts(c: &CounterSet) -> [usize; 6] {
|
||||
[
|
||||
c.sdk_resources_list_handler_completed(),
|
||||
c.sdk_resources_read_handler_completed(),
|
||||
c.sdk_resources_templates_handler_completed(),
|
||||
c.sdk_resources_list_handler_consumed(),
|
||||
c.sdk_resources_read_handler_consumed(),
|
||||
c.sdk_resources_templates_handler_consumed(),
|
||||
]
|
||||
}
|
||||
|
||||
pub(super) fn delta(c: &CounterSet, before: [usize; 6], method: &str) -> ([usize; 6], [usize; 6]) {
|
||||
let after = counts(c);
|
||||
let expected = match method {
|
||||
"resources/list" => [1, 0, 0, 1, 0, 0],
|
||||
"resources/read" => [0, 1, 0, 0, 1, 0],
|
||||
"resources/templates/list" => [0, 0, 1, 0, 0, 1],
|
||||
_ => panic!("not a Resources family: {method}"),
|
||||
};
|
||||
(std::array::from_fn(|i| after[i] - before[i]), expected)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_resources_handler_legacy() {
|
||||
control(LEGACY_REVISION, true).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_resources_handler_modern() {
|
||||
control(MODERN_REVISION, true).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_resources_handler_observer_positive_control() {
|
||||
use super::{
|
||||
sdk_resources_list_handler_completed, sdk_resources_list_handler_consumed,
|
||||
sdk_resources_read_handler_completed, sdk_resources_read_handler_consumed,
|
||||
sdk_resources_templates_handler_completed, sdk_resources_templates_handler_consumed,
|
||||
};
|
||||
let a = Arc::new(CounterSet::default());
|
||||
let b = Arc::new(CounterSet::default());
|
||||
sdk_resources_list_handler_completed();
|
||||
sdk_resources_read_handler_completed();
|
||||
sdk_resources_templates_handler_completed();
|
||||
sdk_resources_list_handler_consumed();
|
||||
sdk_resources_read_handler_consumed();
|
||||
sdk_resources_templates_handler_consumed();
|
||||
assert_eq!(counts(&a), [0; 6], "no-scope markers must be no-ops");
|
||||
let snapshot = a.snapshot();
|
||||
tokio::join!(
|
||||
observe(a.clone(), async {
|
||||
sdk_resources_list_handler_completed();
|
||||
tokio::task::yield_now().await;
|
||||
sdk_resources_read_handler_completed();
|
||||
sdk_resources_templates_handler_completed();
|
||||
sdk_resources_list_handler_consumed();
|
||||
sdk_resources_read_handler_consumed();
|
||||
sdk_resources_templates_handler_consumed();
|
||||
}),
|
||||
observe(b.clone(), async {
|
||||
sdk_resources_read_handler_completed();
|
||||
tokio::task::yield_now().await;
|
||||
sdk_resources_read_handler_consumed();
|
||||
}),
|
||||
);
|
||||
assert_eq!(counts(&a), [1; 6]);
|
||||
assert_eq!(counts(&b), [0, 1, 0, 0, 1, 0]);
|
||||
assert_eq!(a.snapshot(), snapshot);
|
||||
assert_eq!(b.snapshot(), CounterSet::default().snapshot());
|
||||
for c in [&a, &b] {
|
||||
assert_eq!(c.sdk_resources_dispatch(), 0);
|
||||
assert_eq!(c.sdk_resources_list_result(), 0);
|
||||
assert_eq!(c.sdk_resources_read_result(), 0);
|
||||
assert_eq!(c.sdk_resource_templates_list_result(), 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
//! Synthetic domain replies exercise the real trait/restore path, not wire producer coverage.
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
|
||||
|
||||
type TemplateReply = Result<(Vec<Value>, Option<String>), ResourceTemplatesListError>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct Domain {
|
||||
outcome: Mutex<Option<ResourcesOutcome>>,
|
||||
templates: Mutex<Option<TemplateReply>>,
|
||||
calls: AtomicUsize,
|
||||
pending: bool,
|
||||
started: tokio::sync::Notify,
|
||||
release: tokio::sync::Notify,
|
||||
dropped: AtomicBool,
|
||||
finished: AtomicBool,
|
||||
}
|
||||
struct DropWitness<'a>(&'a AtomicBool);
|
||||
impl Drop for DropWitness<'_> {
|
||||
fn drop(&mut self) {
|
||||
self.0.store(true, SeqCst);
|
||||
}
|
||||
}
|
||||
impl Domain {
|
||||
pub(super) async fn resources(&self, command: ResourcesCommand) -> ResourcesOutcome {
|
||||
self.calls.fetch_add(1, SeqCst);
|
||||
match command {
|
||||
ResourcesCommand::List(list) => {
|
||||
assert_eq!(list.limit, Some(7));
|
||||
assert_eq!(list.cursor.as_deref(), Some("opaque"));
|
||||
}
|
||||
ResourcesCommand::Read(read) => assert_eq!(read.uri, "crank://read"),
|
||||
}
|
||||
let _guard = DropWitness(&self.dropped);
|
||||
if self.pending {
|
||||
self.started.notify_one();
|
||||
self.release.notified().await;
|
||||
}
|
||||
self.finished.store(true, SeqCst);
|
||||
self.outcome
|
||||
.lock()
|
||||
.unwrap()
|
||||
.take()
|
||||
.expect("exactly one domain execution")
|
||||
}
|
||||
pub(super) async fn templates(
|
||||
&self,
|
||||
command: ResourcesListCommand,
|
||||
) -> Result<(Vec<Value>, Option<String>), ResourceTemplatesListError> {
|
||||
self.calls.fetch_add(1, SeqCst);
|
||||
assert_eq!(command.limit, Some(7));
|
||||
assert_eq!(command.cursor.as_deref(), Some("opaque"));
|
||||
self.templates
|
||||
.lock()
|
||||
.unwrap()
|
||||
.take()
|
||||
.expect("exactly one template execution")
|
||||
}
|
||||
}
|
||||
fn scope(revision: &str) -> ResourceScope {
|
||||
ResourceScope {
|
||||
workspace_slug: "fresh-workspace".into(),
|
||||
agent_slug: "fresh-agent".into(),
|
||||
credential: VerifiedMachineCredential {
|
||||
machine_access_mode: crank_core::MachineAccessMode::StaticAgentKey,
|
||||
max_security_level: crank_core::OperationSecurityLevel::Standard,
|
||||
scopes: vec![crank_core::PlatformApiKeyScope::Read],
|
||||
platform_api_key_id: Some("fresh-key".into()),
|
||||
},
|
||||
correlation: CorrelationContext::generate(),
|
||||
continuation: ContinuationSupport::from_supported(false),
|
||||
locale: ExecutionLocale::En,
|
||||
revision: revision.into(),
|
||||
limit: Some(7),
|
||||
}
|
||||
}
|
||||
async fn state() -> Arc<AppState> {
|
||||
crank_test_support::postgres_schema_url_for_task("resources_handler_local", "t_b2b08b44").await;
|
||||
super::super::rmcp_tools_tests::test_state().await
|
||||
}
|
||||
fn handler(state: &Arc<AppState>, outcome: ResourcesOutcome) -> ResourceHandler {
|
||||
let mut handler = ResourceHandler::new(state);
|
||||
handler.domain = Some(Arc::new(Domain {
|
||||
outcome: Mutex::new(Some(outcome)),
|
||||
..Default::default()
|
||||
}));
|
||||
handler
|
||||
}
|
||||
fn page() -> Option<PaginatedRequestParams> {
|
||||
Some(PaginatedRequestParams::default().with_cursor(Some("opaque".into())))
|
||||
}
|
||||
async fn context(handler: &ResourceHandler, revision: &str) -> RequestContext<RoleServer> {
|
||||
let context = request_context(handler, scope(revision)).await.unwrap();
|
||||
// Closed Peer and released seed handler ownership independently witness bootstrap cleanup.
|
||||
assert!(context.peer.is_transport_closed());
|
||||
assert_eq!(Arc::strong_count(&handler.reply), 1);
|
||||
assert_eq!(handler.domain.as_ref().unwrap().calls.load(SeqCst), 0);
|
||||
assert_eq!(
|
||||
ResourceHandler::scope(&context)
|
||||
.unwrap()
|
||||
.credential
|
||||
.platform_api_key_id
|
||||
.as_ref()
|
||||
.map(|id| id.as_str()),
|
||||
Some("fresh-key")
|
||||
);
|
||||
context
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_resources_trait_lossless_populated_results() {
|
||||
let state = state().await;
|
||||
for revision in ["2025-11-25", "2026-07-28"] {
|
||||
let raw = vec![
|
||||
json!({"uri":"crank://a", "name":"a", "description":null, "annotations":{"arbitrary":[null,{"nested":true}]}, "icons":{"not":"an SDK icon array"}, "x": [1,null]}),
|
||||
json!({"uri":"crank://b","name":"b"}),
|
||||
];
|
||||
let h = handler(
|
||||
&state,
|
||||
ResourcesOutcome::Listed {
|
||||
resources: raw.clone(),
|
||||
next_cursor: Some("next".into()),
|
||||
},
|
||||
);
|
||||
let c = context(&h, revision).await;
|
||||
let mut result = ServerHandler::list_resources(&h, page(), c).await.unwrap();
|
||||
// Mutating a known field demonstrates the actual typed result is consumed, not old success.
|
||||
result.resources[0].title = Some("from-sdk-result".into());
|
||||
result.next_cursor = Some("typed-cursor".into());
|
||||
match restore_list(&h, Ok(result)) {
|
||||
ResourcesOutcome::Listed {
|
||||
resources,
|
||||
next_cursor,
|
||||
} => {
|
||||
let mut expected = raw;
|
||||
expected[0]["title"] = json!("from-sdk-result");
|
||||
assert_eq!(resources, expected);
|
||||
assert_eq!(next_cursor.as_deref(), Some("typed-cursor"));
|
||||
}
|
||||
_ => panic!("list restore failed"),
|
||||
}
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
let text = serde_json::to_string_pretty(&json!({"exact":true})).unwrap();
|
||||
let raw = json!([{"uri":"crank://text", "text":text, "mimeType":null, "annotations": [null,42], "x":null}, {"uri":"crank://blob", "blob":"AAEC/w==", "icons":false}]);
|
||||
let h = handler(
|
||||
&state,
|
||||
ResourcesOutcome::Read {
|
||||
contents: raw.clone(),
|
||||
},
|
||||
);
|
||||
let c = context(&h, revision).await;
|
||||
let result =
|
||||
ServerHandler::read_resource(&h, ReadResourceRequestParams::new("crank://read"), c)
|
||||
.await;
|
||||
match restore_read(&h, result) {
|
||||
ResourcesOutcome::Read { contents } => assert_eq!(contents, raw),
|
||||
_ => panic!("read restore failed"),
|
||||
}
|
||||
let raw = vec![
|
||||
json!({"uriTemplate":"crank://{id}", "name":"template", "parameters":{"id":{"type":"string","default":null}}, "annotations":false, "icons":[{"unknown":null}], "description":null, "x":{"nested":[null]}}),
|
||||
];
|
||||
let mut h = ResourceHandler::new(&state);
|
||||
h.domain = Some(Arc::new(Domain {
|
||||
templates: Mutex::new(Some(Ok((raw.clone(), Some("next".into()))))),
|
||||
..Default::default()
|
||||
}));
|
||||
let c = context(&h, revision).await;
|
||||
let result = ServerHandler::list_resource_templates(&h, page(), c).await;
|
||||
let (actual, cursor) = restore_templates(&h, result).ok().unwrap();
|
||||
assert_eq!(actual, raw);
|
||||
assert_eq!(cursor.as_deref(), Some("next"));
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_resources_trait_errors_and_broken_projection_fail_closed() {
|
||||
let state = state().await;
|
||||
let contract = crate::tool_error::control_tool_error_contract(
|
||||
crate::tool_error::McpControlError::CatalogRevisionChanged,
|
||||
"request",
|
||||
"trace",
|
||||
ExecutionLocale::En,
|
||||
);
|
||||
for outcome in [
|
||||
ResourcesOutcome::InvalidParams("exact detail".into()),
|
||||
ResourcesOutcome::NotAvailable,
|
||||
ResourcesOutcome::CatalogUnavailable,
|
||||
ResourcesOutcome::ArtifactUnavailable,
|
||||
ResourcesOutcome::ExecutionError(contract.clone()),
|
||||
] {
|
||||
let expected = std::mem::discriminant(&outcome);
|
||||
let h = handler(&state, outcome);
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let result =
|
||||
ServerHandler::read_resource(&h, ReadResourceRequestParams::new("crank://read"), c)
|
||||
.await;
|
||||
assert!(result.is_err());
|
||||
let actual = restore_read(&h, result);
|
||||
assert_eq!(std::mem::discriminant(&actual), expected);
|
||||
match actual {
|
||||
ResourcesOutcome::ExecutionError(actual) => assert_eq!(actual, contract),
|
||||
ResourcesOutcome::InvalidParams(detail) => assert_eq!(detail, "exact detail"),
|
||||
_ => {}
|
||||
}
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
}
|
||||
for error in [
|
||||
ResourceTemplatesListError::InvalidCursor,
|
||||
ResourceTemplatesListError::Unavailable,
|
||||
] {
|
||||
let expected = std::mem::discriminant(&error);
|
||||
let mut h = ResourceHandler::new(&state);
|
||||
h.domain = Some(Arc::new(Domain {
|
||||
templates: Mutex::new(Some(Err(error))),
|
||||
..Default::default()
|
||||
}));
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let result = ServerHandler::list_resource_templates(&h, page(), c).await;
|
||||
assert!(result.is_err());
|
||||
assert_eq!(
|
||||
std::mem::discriminant(&restore_templates(&h, result).err().unwrap()),
|
||||
expected
|
||||
);
|
||||
}
|
||||
let h = handler(
|
||||
&state,
|
||||
ResourcesOutcome::Listed {
|
||||
resources: vec![json!({"name":"missing-required-uri"})],
|
||||
next_cursor: None,
|
||||
},
|
||||
);
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let result = ServerHandler::list_resources(&h, page(), c).await;
|
||||
assert!(result.is_err());
|
||||
assert!(matches!(
|
||||
restore_list(&h, result),
|
||||
ResourcesOutcome::CatalogUnavailable
|
||||
));
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
let h = handler(
|
||||
&state,
|
||||
ResourcesOutcome::Listed {
|
||||
resources: vec![json!({"uri":"crank://a","name":"a"})],
|
||||
next_cursor: None,
|
||||
},
|
||||
);
|
||||
let mut c = context(&h, "2026-07-28").await;
|
||||
c.extensions = Default::default();
|
||||
assert!(ServerHandler::list_resources(&h, page(), c).await.is_err());
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 0);
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let mut result = ServerHandler::list_resources(&h, page(), c).await.unwrap();
|
||||
result.resources.clear();
|
||||
assert!(matches!(
|
||||
restore_list(&h, Ok(result)),
|
||||
ResourcesOutcome::CatalogUnavailable
|
||||
));
|
||||
// An impossible domain variant or unpaired SDK Err cannot become a saved success.
|
||||
let h = handler(
|
||||
&state,
|
||||
ResourcesOutcome::Read {
|
||||
contents: json!([]),
|
||||
},
|
||||
);
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let result = ServerHandler::list_resources(&h, page(), c).await;
|
||||
assert!(result.is_err());
|
||||
assert!(matches!(
|
||||
restore_list(&h, result),
|
||||
ResourcesOutcome::CatalogUnavailable
|
||||
));
|
||||
assert!(matches!(
|
||||
restore_read(&h, Err(internal())),
|
||||
ResourcesOutcome::ArtifactUnavailable
|
||||
));
|
||||
let h = handler(
|
||||
&state,
|
||||
ResourcesOutcome::Listed {
|
||||
resources: vec![
|
||||
json!({"uri":"crank://a","name":"a","x":1}),
|
||||
json!({"uri":"crank://b","name":"b","x":2}),
|
||||
],
|
||||
next_cursor: None,
|
||||
},
|
||||
);
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let mut result = ServerHandler::list_resources(&h, page(), c).await.unwrap();
|
||||
result.resources.swap(0, 1);
|
||||
assert!(matches!(
|
||||
restore_list(&h, Ok(result)),
|
||||
ResourcesOutcome::CatalogUnavailable
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_resources_parent_drop_releases_domain_future_without_detached_execution() {
|
||||
let state = state().await;
|
||||
let mut h = ResourceHandler::new(&state);
|
||||
let domain = Arc::new(Domain {
|
||||
pending: true,
|
||||
..Default::default()
|
||||
});
|
||||
h.domain = Some(domain.clone());
|
||||
let c = context(&h, "2026-07-28").await;
|
||||
let mut parent = Box::pin(ServerHandler::read_resource(
|
||||
&h,
|
||||
ReadResourceRequestParams::new("crank://read"),
|
||||
c,
|
||||
));
|
||||
tokio::select! { _ = &mut parent => panic!("domain should be waiting"), _ = domain.started.notified() => {} }
|
||||
assert!(!domain.dropped.load(SeqCst));
|
||||
drop(parent);
|
||||
assert!(
|
||||
domain.dropped.load(SeqCst),
|
||||
"owned domain future must be dropped synchronously"
|
||||
);
|
||||
domain.release.notify_one();
|
||||
tokio::task::yield_now().await;
|
||||
assert!(
|
||||
!domain.finished.load(SeqCst),
|
||||
"no detached future may resume execution"
|
||||
);
|
||||
assert!(h.take().is_none());
|
||||
assert_eq!(domain.calls.load(SeqCst), 1);
|
||||
assert_eq!(Arc::strong_count(&domain), 2);
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
//! #107 Resources: wire GREEN is independent of actual ServerHandler execution.
|
||||
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};
|
||||
|
||||
use super::rmcp_resources_handler_runtime_tests as handler;
|
||||
|
||||
// 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]),
|
||||
)
|
||||
}
|
||||
pub(super) async fn control(
|
||||
revision: &str,
|
||||
require_handler: bool,
|
||||
) -> Vec<([usize; 4], [usize; 4])> {
|
||||
crank_test_support::postgres_schema_url_for_task("resources_handler", "t_48a39ca6").await;
|
||||
let mut h = Endpoint::started().await;
|
||||
let session = h.session(revision).await;
|
||||
let session = session.as_deref();
|
||||
let mut deltas = Vec::new();
|
||||
let mut handlers = 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 before = handler::counts(&h.counters);
|
||||
let (result, delta) =
|
||||
success(&h, revision, session, "resources/list", params, sse).await;
|
||||
handlers.push(handler::delta(&h.counters, before, "resources/list"));
|
||||
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 before = handler::counts(&h.counters);
|
||||
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}])
|
||||
);
|
||||
handlers.push(handler::delta(&h.counters, before, "resources/read"));
|
||||
deltas.push((delta, [1, 0, 1, 0]));
|
||||
let before = handler::counts(&h.counters);
|
||||
let (result, delta) = success(
|
||||
&h,
|
||||
revision,
|
||||
session,
|
||||
"resources/templates/list",
|
||||
json!({}),
|
||||
sse,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(result["resourceTemplates"], json!([]));
|
||||
assert!(result.get("nextCursor").is_none());
|
||||
handlers.push(handler::delta(
|
||||
&h.counters,
|
||||
before,
|
||||
"resources/templates/list",
|
||||
));
|
||||
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":42})),
|
||||
("resources/read", json!({})),
|
||||
("resources/read", json!({"uri":"crank://resources/unbound"})),
|
||||
] {
|
||||
let before = handler::counts(&h.counters);
|
||||
let parsing_reject = params.get("limit").is_some()
|
||||
|| (method == "resources/read" && !params["uri"].is_string());
|
||||
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}");
|
||||
let expected_message = if parsing_reject {
|
||||
format!("invalid {method} parameters")
|
||||
} else if method == "resources/read" {
|
||||
"the requested resource is not available".to_owned()
|
||||
} else {
|
||||
format!("invalid {method} cursor")
|
||||
};
|
||||
assert_eq!(body["error"]["message"], expected_message, "{body}");
|
||||
assert!(body.get("result").is_none(), "{body}");
|
||||
if parsing_reject {
|
||||
assert_eq!(
|
||||
handler::counts(&h.counters),
|
||||
before,
|
||||
"parsing reject: {req}"
|
||||
);
|
||||
} else {
|
||||
handlers.push(handler::delta(&h.counters, before, method));
|
||||
}
|
||||
}
|
||||
// Fresh scope and Agent binding are checked even after a successful handshake/read.
|
||||
let foreign_slug = handler::foreign_agent(&h.registry).await;
|
||||
let original_url = h.url.clone();
|
||||
h.url = format!(
|
||||
"{}/{foreign_slug}",
|
||||
original_url.rsplit_once('/').unwrap().0
|
||||
);
|
||||
for method in [
|
||||
"resources/list",
|
||||
"resources/read",
|
||||
"resources/templates/list",
|
||||
] {
|
||||
let before = handler::counts(&h.counters);
|
||||
let req = request(revision, method, json!({"uri":URI}));
|
||||
// Agent-bound credential lookup intentionally does not reveal the other Agent.
|
||||
assert_eq!(h.post(revision, session, &req, false).await.status(), 401);
|
||||
assert_eq!(handler::counts(&h.counters), before, "cross-agent: {req}");
|
||||
}
|
||||
h.url = original_url;
|
||||
sqlx::query(
|
||||
"update platform_api_keys set scopes_json = '[]'::jsonb where id = 'pk_dispatch_observer'",
|
||||
)
|
||||
.execute(h.registry.pool())
|
||||
.await
|
||||
.unwrap();
|
||||
for method in [
|
||||
"resources/list",
|
||||
"resources/read",
|
||||
"resources/templates/list",
|
||||
] {
|
||||
let before = handler::counts(&h.counters);
|
||||
let req = request(revision, method, json!({"uri":URI}));
|
||||
assert_eq!(h.post(revision, session, &req, false).await.status(), 403);
|
||||
assert_eq!(
|
||||
handler::counts(&h.counters),
|
||||
before,
|
||||
"read scope required: {req}"
|
||||
);
|
||||
}
|
||||
sqlx::query("update platform_api_keys set scopes_json = '[\"read\",\"write\"]'::jsonb where id = 'pk_dispatch_observer'")
|
||||
.execute(h.registry.pool()).await.unwrap();
|
||||
// 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);
|
||||
let before = handler::counts(&h.counters);
|
||||
assert_eq!(h.post(revision, session, &req, false).await.status(), 401);
|
||||
assert_eq!(handler::counts(&h.counters), before, "revoked: {req}");
|
||||
}
|
||||
eprintln!(
|
||||
"existing GREEN Resources wire {revision}: pagination, exact content URI/bytes, empty templates, JSON/SSE negotiation, InvalidParams, revoked401; actual/expected SDK deltas={deltas:?}"
|
||||
);
|
||||
eprintln!(
|
||||
"Resources handler {revision}: completed[list,read,templates], consumed[list,read,templates]; actual/expected per admitted request={handlers:?}; parsing/revoked guards=0"
|
||||
);
|
||||
if require_handler {
|
||||
assert!(
|
||||
handlers.iter().all(|(actual, expected)| actual == expected),
|
||||
"ServerHandler completion/Result consumption missing after successful HTTP wire/error/guard checks ({revision}): {handlers:?}"
|
||||
);
|
||||
}
|
||||
deltas
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_resources_wire_legacy() {
|
||||
control(LEGACY_REVISION, false).await;
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_resources_wire_modern() {
|
||||
control(MODERN_REVISION, false).await;
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_resources_sdk_legacy() {
|
||||
let deltas = control(LEGACY_REVISION, false).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, false).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(®istry).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,352 @@
|
||||
//! #107 inbound responses: wire compatibility and independent SDK execution 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, atomic::Ordering};
|
||||
|
||||
async fn endpoint() -> Endpoint {
|
||||
crank_test_support::postgres_schema_url_for_task("response_owner", "t_d427ee76").await;
|
||||
Endpoint::started().await
|
||||
}
|
||||
fn sdk(c: &CounterSet) -> [usize; 4] {
|
||||
[
|
||||
c.sdk_inbound_response_decoded(),
|
||||
c.sdk_inbound_error_decoded(),
|
||||
c.sdk_inbound_response_consumed(),
|
||||
c.sdk_inbound_error_consumed(),
|
||||
]
|
||||
}
|
||||
fn request(
|
||||
h: &Endpoint,
|
||||
revision: &str,
|
||||
session: Option<&str>,
|
||||
accept: &str,
|
||||
raw: &str,
|
||||
) -> reqwest::RequestBuilder {
|
||||
// Reuse the HTTP fixture without its request-only modern method/name headers.
|
||||
let mut req = h
|
||||
.request(LEGACY_REVISION, session, &h.key, accept, json!({}))
|
||||
.header("x-request-id", "response-atdd")
|
||||
.body(raw.to_owned())
|
||||
.build()
|
||||
.unwrap();
|
||||
req.headers_mut()
|
||||
.insert("MCP-Protocol-Version", revision.parse().unwrap());
|
||||
reqwest::RequestBuilder::from_parts(reqwest::Client::new(), req)
|
||||
}
|
||||
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());
|
||||
}
|
||||
async fn no_effects(h: &Endpoint, ops: SessionOps, entries: Snapshot) {
|
||||
assert_eq!(
|
||||
h.sessions.snapshot(),
|
||||
ops,
|
||||
"response ACK must not access sessions"
|
||||
);
|
||||
let after = h.counters.snapshot();
|
||||
assert_eq!(
|
||||
(after.era_modern, after.era_legacy, after.tools_service),
|
||||
(
|
||||
entries.era_modern,
|
||||
entries.era_legacy,
|
||||
entries.tools_service
|
||||
)
|
||||
);
|
||||
assert_eq!(h.started.load(Ordering::SeqCst), 0);
|
||||
assert!(
|
||||
h.history("response-atdd").await.is_empty(),
|
||||
"no domain invocation"
|
||||
);
|
||||
}
|
||||
async fn success_control(error: bool) -> Vec<[usize; 4]> {
|
||||
let h = endpoint().await;
|
||||
let mut deltas = vec![];
|
||||
let numbers = [
|
||||
"-9223372036854775808",
|
||||
"9223372036854775807",
|
||||
"18446744073709551615",
|
||||
"3.25",
|
||||
"1.0",
|
||||
"-0.0",
|
||||
"1.7976931348623157e308",
|
||||
"-1.7976931348623157e308",
|
||||
"5e-324",
|
||||
];
|
||||
let mut ids = vec!["\"reply/Ю\""];
|
||||
ids.extend(numbers);
|
||||
for revision in [LEGACY_REVISION, MODERN_REVISION] {
|
||||
for accept in ["application/json", "text/event-stream, application/json"] {
|
||||
for session in [None, Some("nonexistent-response-session")] {
|
||||
for (index, id) in ids.iter().enumerate() {
|
||||
let messages = if error {
|
||||
let code = numbers[index % numbers.len()];
|
||||
let data = [
|
||||
"null",
|
||||
"true",
|
||||
"3.25",
|
||||
"\"text\"",
|
||||
"[]",
|
||||
"{}",
|
||||
"[null,true,{\"nested\":1}]",
|
||||
][index % 7];
|
||||
vec![
|
||||
format!(
|
||||
r#"{{"jsonrpc":"2.0","id":{id},"error":{{"code":{code},"message":"failed","data":{data}}},"params":false,"extension":[]}}"#
|
||||
),
|
||||
format!(
|
||||
r#"{{"jsonrpc":"2.0","error":{{"code":{code},"message":"failed"}},"params":{{"_meta":false}}}}"#
|
||||
),
|
||||
]
|
||||
} else {
|
||||
vec![format!(
|
||||
r#"{{"jsonrpc":"2.0","id":{id},"result":{{"arbitrary":[null,true,3]}},"params":false,"extension":[]}}"#
|
||||
)]
|
||||
};
|
||||
for raw in messages {
|
||||
let before = sdk(&h.counters);
|
||||
let ops = h.sessions.snapshot();
|
||||
let entries = h.counters.snapshot();
|
||||
ack(request(&h, revision, session, accept, &raw)
|
||||
.send()
|
||||
.await
|
||||
.unwrap())
|
||||
.await;
|
||||
no_effects(&h, ops, entries).await;
|
||||
let after = sdk(&h.counters);
|
||||
deltas.push(std::array::from_fn(|i| after[i] - before[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
eprintln!("existing wire GREEN error={error}; SDK deltas={deltas:?}");
|
||||
deltas
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn wire_success_and_error_ack_without_session_or_modern_metadata() {
|
||||
success_control(false).await;
|
||||
success_control(true).await;
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn sdk_success_response_decoded_and_consumed_after_http_ack() {
|
||||
let deltas = success_control(false).await;
|
||||
assert!(
|
||||
deltas.iter().all(|d| *d == [1, 0, 1, 0]),
|
||||
"real SDK success decode/typed variant consumption required; actual={deltas:?}"
|
||||
);
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn sdk_error_response_decoded_and_consumed_after_http_ack() {
|
||||
let deltas = success_control(true).await;
|
||||
assert!(
|
||||
deltas.iter().all(|d| *d == [0, 1, 0, 1]),
|
||||
"real SDK error decode/typed variant consumption required; actual={deltas:?}"
|
||||
);
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn malformed_responses_reject_before_sdk_or_effects() {
|
||||
let h = endpoint().await;
|
||||
let mut cases = vec![
|
||||
json!({}),
|
||||
json!({"jsonrpc":"2.0","id":1}),
|
||||
json!({"jsonrpc":"2.0","id":1,"result":{},"error":{"code":1,"message":"x"}}),
|
||||
json!({"jsonrpc":"2.0","result":{}}),
|
||||
json!({"id":1,"result":{}}),
|
||||
json!({"jsonrpc":2,"id":1,"result":{}}),
|
||||
json!({"jsonrpc":"2.0","id":1,"result":3}),
|
||||
json!({"jsonrpc":"2.0","error":{}}),
|
||||
json!({"jsonrpc":"2.0","error":{"code":1,"message":3}}),
|
||||
];
|
||||
for value in [Value::Null, json!(true), json!([]), json!({}), json!("bad")] {
|
||||
for field in ["jsonrpc", "result", "id"] {
|
||||
if (field == "result" && value.is_object()) || (field == "id" && value.is_string()) {
|
||||
continue;
|
||||
}
|
||||
let mut m = json!({"jsonrpc":"2.0","id":1,"result":{}});
|
||||
m[field] = value.clone();
|
||||
cases.push(m);
|
||||
}
|
||||
for field in ["error", "code", "message", "id"] {
|
||||
if (field == "error" && value.is_object())
|
||||
|| (field == "message" && value.is_string())
|
||||
|| (field == "id" && value.is_string())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let mut m = json!({"jsonrpc":"2.0","id":1,"error":{"code":1,"message":"x"}});
|
||||
if ["code", "message"].contains(&field) {
|
||||
m["error"][field] = value.clone();
|
||||
} else {
|
||||
m[field] = value.clone();
|
||||
}
|
||||
cases.push(m);
|
||||
}
|
||||
}
|
||||
for missing in ["code", "message"] {
|
||||
let mut m = json!({"jsonrpc":"2.0","error":{"code":1,"message":"x"}});
|
||||
m["error"].as_object_mut().unwrap().remove(missing);
|
||||
cases.push(m);
|
||||
}
|
||||
for revision in [LEGACY_REVISION, MODERN_REVISION] {
|
||||
for m in &cases {
|
||||
let before = sdk(&h.counters);
|
||||
let ops = h.sessions.snapshot();
|
||||
let entries = h.counters.snapshot();
|
||||
let response = request(&h, revision, None, "application/json", &m.to_string())
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), 400, "{m}");
|
||||
let wire: Value = response.json().await.unwrap();
|
||||
assert_eq!(wire["error"]["code"], -32600, "{m}: {wire}");
|
||||
assert_eq!(wire["id"], Value::Null);
|
||||
assert!(wire.get("result").is_none());
|
||||
assert_eq!(sdk(&h.counters), before);
|
||||
no_effects(&h, ops, entries).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn admission_guards_precede_response_ack_and_sdk() {
|
||||
let h = endpoint().await;
|
||||
for revision in [LEGACY_REVISION, MODERN_REVISION] {
|
||||
for raw in [
|
||||
r#"{"jsonrpc":"2.0","id":1,"result":{}}"#,
|
||||
r#"{"jsonrpc":"2.0","error":{"code":1,"message":"x"}}"#,
|
||||
] {
|
||||
for guard in ["origin", "accept", "ambiguous"] {
|
||||
let ops = h.sessions.snapshot();
|
||||
let entries = h.counters.snapshot();
|
||||
let before = sdk(&h.counters);
|
||||
let mut req = request(&h, revision, None, "application/json", raw)
|
||||
.build()
|
||||
.unwrap();
|
||||
let expected = match guard {
|
||||
"origin" => {
|
||||
req.headers_mut()
|
||||
.insert("origin", "https://evil.example".parse().unwrap());
|
||||
403
|
||||
}
|
||||
"accept" => {
|
||||
req.headers_mut()
|
||||
.insert("accept", "text/plain".parse().unwrap());
|
||||
406
|
||||
}
|
||||
_ => {
|
||||
req.headers_mut()
|
||||
.append("MCP-Session-Id", "one".parse().unwrap());
|
||||
req.headers_mut()
|
||||
.append("MCP-Session-Id", "two".parse().unwrap());
|
||||
400
|
||||
}
|
||||
};
|
||||
let response = reqwest::Client::new().execute(req).await.unwrap();
|
||||
assert_eq!(response.status().as_u16(), expected, "{guard}");
|
||||
let _ = response.bytes().await.unwrap();
|
||||
assert_eq!(sdk(&h.counters), before);
|
||||
no_effects(&h, ops, entries).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn authentication_and_revocation_precede_response_ack() {
|
||||
let h = endpoint().await;
|
||||
let mut statuses = vec![];
|
||||
for revoked in [false, true] {
|
||||
if revoked {
|
||||
h.registry
|
||||
.revoke_platform_api_key(
|
||||
&workspace_id(),
|
||||
&crank_core::PlatformApiKeyId::new("pk_dispatch_observer"),
|
||||
&time::OffsetDateTime::now_utc(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
for revision in [LEGACY_REVISION, MODERN_REVISION] {
|
||||
for raw in [
|
||||
r#"{"jsonrpc":"2.0","id":1,"result":{}}"#,
|
||||
r#"{"jsonrpc":"2.0","error":{"code":1,"message":"x"}}"#,
|
||||
] {
|
||||
let before = sdk(&h.counters);
|
||||
let ops = h.sessions.snapshot();
|
||||
let entries = h.counters.snapshot();
|
||||
let mut req = request(&h, revision, None, "application/json", raw)
|
||||
.build()
|
||||
.unwrap();
|
||||
if !revoked {
|
||||
req.headers_mut().remove("authorization");
|
||||
}
|
||||
let response = reqwest::Client::new().execute(req).await.unwrap();
|
||||
statuses.push(response.status().as_u16());
|
||||
let _ = response.bytes().await.unwrap();
|
||||
assert_eq!(sdk(&h.counters), before);
|
||||
no_effects(&h, ops, entries).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
statuses,
|
||||
vec![401; 8],
|
||||
"missing/revoked credentials must reject before ACK and SDK"
|
||||
);
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn method_bearing_messages_keep_priority_over_response_fields() {
|
||||
let h = endpoint().await;
|
||||
for revision in [LEGACY_REVISION, MODERN_REVISION] {
|
||||
let session = h.session(revision).await;
|
||||
for notification in [false, true] {
|
||||
let mut m = json!({"jsonrpc":"2.0","method":"ping","id":7,"result":false,"error":[],"extension":null});
|
||||
if notification {
|
||||
m.as_object_mut().unwrap().remove("id");
|
||||
}
|
||||
if revision == MODERN_REVISION {
|
||||
m["params"] = json!({"_meta":modern_meta()});
|
||||
}
|
||||
let before = sdk(&h.counters);
|
||||
let response = h
|
||||
.request(revision, session.as_deref(), &h.key, "application/json", m)
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
if notification {
|
||||
ack(response).await;
|
||||
} else {
|
||||
assert_eq!(response.status(), 200);
|
||||
let wire: Value = response.json().await.unwrap();
|
||||
assert_eq!(wire["id"], 7);
|
||||
assert!(wire["result"].is_object());
|
||||
assert!(wire.get("error").is_none());
|
||||
}
|
||||
assert_eq!(sdk(&h.counters), before, "not an inbound response");
|
||||
}
|
||||
}
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn observer_positive_control_is_task_scoped_and_independent() {
|
||||
let a = Arc::new(CounterSet::default());
|
||||
let b = Arc::new(CounterSet::default());
|
||||
super::sdk_inbound_response_decoded();
|
||||
super::sdk_inbound_error_decoded();
|
||||
super::sdk_inbound_response_consumed();
|
||||
super::sdk_inbound_error_consumed();
|
||||
assert_eq!(sdk(&a), [0; 4]);
|
||||
tokio::join!(
|
||||
observe(a.clone(), async {
|
||||
super::sdk_inbound_response_decoded();
|
||||
super::sdk_inbound_error_consumed();
|
||||
}),
|
||||
observe(b.clone(), async {
|
||||
super::sdk_inbound_error_decoded();
|
||||
super::sdk_inbound_response_consumed();
|
||||
})
|
||||
);
|
||||
assert_eq!(sdk(&a), [1, 0, 0, 1]);
|
||||
assert_eq!(sdk(&b), [0, 1, 1, 0]);
|
||||
assert_eq!(a.snapshot(), Snapshot::new(0, 0, 0, 0));
|
||||
}
|
||||
@@ -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,424 @@
|
||||
//! #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(®istry, &operation).await;
|
||||
let key = create_api_key(®istry).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)"
|
||||
);
|
||||
}
|
||||
|
||||
async fn handler_execution_control(revision: &str) {
|
||||
let h = Endpoint::started().await;
|
||||
let session = h.session(revision).await;
|
||||
let session = session.as_deref();
|
||||
h.list(revision, session, TOOL_NAME).await;
|
||||
h.call(
|
||||
revision,
|
||||
session,
|
||||
TOOL_NAME,
|
||||
&format!("{revision}@handler.test"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
h.calls.load(Ordering::SeqCst),
|
||||
1,
|
||||
"one domain call expected"
|
||||
);
|
||||
assert_eq!(
|
||||
[
|
||||
h.counters.sdk_tools_list_handler_completed(),
|
||||
h.counters.sdk_tools_list_handler_consumed(),
|
||||
h.counters.sdk_tools_call_handler_completed(),
|
||||
h.counters.sdk_tools_call_handler_consumed(),
|
||||
],
|
||||
[1, 1, 1, 1],
|
||||
"real build_app HTTP tools/list and tools/call must execute and consume SDK handler results ({revision})"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_tools_handler_http_modern() {
|
||||
handler_execution_control(MODERN_REVISION).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_tools_handler_http_legacy() {
|
||||
handler_execution_control(LEGACY_REVISION).await;
|
||||
}
|
||||
|
||||
#[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,310 @@
|
||||
//! #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::signals_test_support::{self as fixtures, Endpoint, terminal};
|
||||
use super::{CounterSet, support::*};
|
||||
use serde_json::{Value, json};
|
||||
use std::time::Duration;
|
||||
|
||||
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,282 @@
|
||||
//! Small real catalog fixture; broader Resource/Prompt mutations stay in mcp_signals::scenarios.
|
||||
use super::{CounterSet, support::*};
|
||||
use crank_core::{AgentStatus, AgentVersion, ToolSelectionPolicy};
|
||||
use crank_registry::{CreateAgentDraftVersionRequest, PublishAgentRequest, PublishRequest};
|
||||
use crank_runtime::{
|
||||
InMemoryCoordinationStateStore, RequestRateLimitConfig, RequestRateLimiter, SecretCrypto,
|
||||
};
|
||||
use serde_json::{Value, json};
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
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_35c2ce53").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(®istry, &operation).await;
|
||||
let key = create_api_key(®istry).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
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
//! #107 subscription handler acceptance: real HTTP wire first, then SDK runtime oracle.
|
||||
use super::CounterSet;
|
||||
use serde_json::{Value, json};
|
||||
use std::time::Duration;
|
||||
|
||||
use super::signals_test_support::Endpoint;
|
||||
|
||||
const SUB_ID: &str = "io.modelcontextprotocol/subscriptionId";
|
||||
const ACK: &str = "notifications/subscriptions/acknowledged";
|
||||
const TOOLS: &str = "notifications/tools/list_changed";
|
||||
|
||||
fn snapshot(c: &CounterSet) -> [usize; 5] {
|
||||
[
|
||||
c.sdk_subscription_listen_entered(),
|
||||
c.sdk_subscription_handler_completed(),
|
||||
c.sdk_subscription_handler_consumed(),
|
||||
c.sdk_subscription_notification_consumed(),
|
||||
c.sdk_subscription_owner_completed(),
|
||||
]
|
||||
}
|
||||
|
||||
async fn http_control(id: Value) -> ([usize; 5], [usize; 3]) {
|
||||
let h = Endpoint::started().await;
|
||||
let started = snapshot(&h.counters);
|
||||
let sdk_wire_before = [
|
||||
h.counters.sdk_subscriptions_listen(),
|
||||
h.counters.sdk_subscriptions_acknowledged(),
|
||||
h.counters.sdk_signal_notification(),
|
||||
];
|
||||
let mut stream = h.listen(id.clone(), json!({"toolsListChanged":true})).await;
|
||||
let first = stream.frame().await;
|
||||
assert_eq!(first["method"], ACK, "HTTP SSE ACK must be first");
|
||||
assert_eq!(first["params"]["_meta"][SUB_ID], id, "exact JSON ACK id");
|
||||
assert_eq!(first["params"]["_meta"][SUB_ID].to_string(), id.to_string());
|
||||
assert_eq!(
|
||||
first["params"]["notifications"],
|
||||
json!({"toolsListChanged":true})
|
||||
);
|
||||
stream.silent().await;
|
||||
|
||||
h.draft_without_tools().await;
|
||||
h.publish_without_tools().await;
|
||||
let notification = stream.frame().await;
|
||||
assert_eq!(notification["method"], TOOLS);
|
||||
assert_eq!(
|
||||
notification["params"]["_meta"][SUB_ID], id,
|
||||
"exact JSON notification id"
|
||||
);
|
||||
assert_eq!(
|
||||
notification["params"]["_meta"][SUB_ID].to_string(),
|
||||
id.to_string()
|
||||
);
|
||||
stream.silent().await;
|
||||
|
||||
let wire = [
|
||||
h.counters.sdk_subscriptions_listen() - sdk_wire_before[0],
|
||||
h.counters.sdk_subscriptions_acknowledged() - sdk_wire_before[1],
|
||||
h.counters.sdk_signal_notification() - sdk_wire_before[2],
|
||||
];
|
||||
drop(stream);
|
||||
tokio::time::timeout(Duration::from_secs(3), async {
|
||||
while h.counters.sdk_subscription_owner_completed() == started[4] {
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("HTTP close must join SDK owner/pump");
|
||||
let runtime = std::array::from_fn(|i| snapshot(&h.counters)[i] - started[i]);
|
||||
(runtime, wire)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn modern_http_wire_control_and_sdk_handler_runtime() {
|
||||
for id in [
|
||||
json!(u64::MAX),
|
||||
serde_json::from_str("0.12345678901234567890123456789").unwrap(),
|
||||
] {
|
||||
let (runtime, wire) = http_control(id).await;
|
||||
assert_eq!(
|
||||
wire,
|
||||
[1, 1, 1],
|
||||
"SDK decode/ACK/notification consumed through HTTP"
|
||||
);
|
||||
assert_eq!(
|
||||
runtime,
|
||||
[1, 0, 0, 1, 1],
|
||||
"cancelled listen must not be reported as completed; owner/pump must finish"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
use super::*;
|
||||
use crate::app::dispatch_observer::{self as observer, CounterSet};
|
||||
use serde_json::json;
|
||||
use std::time::Duration;
|
||||
|
||||
fn meta() -> RequestMetaObject {
|
||||
project_meta(&json!({"params":{"_meta":{
|
||||
"io.modelcontextprotocol/protocolVersion":"2026-07-28",
|
||||
"io.modelcontextprotocol/clientCapabilities":{},
|
||||
}}}))
|
||||
}
|
||||
fn filter(value: Value) -> SubscriptionFilter {
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
async fn finished(c: &CounterSet) {
|
||||
tokio::time::timeout(Duration::from_secs(2), async {
|
||||
while c.sdk_subscription_owner_completed() != 1 {
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("owner must join SDK bridge");
|
||||
}
|
||||
|
||||
struct Controlled {
|
||||
fail: bool,
|
||||
tools: bool,
|
||||
calls: Arc<std::sync::atomic::AtomicUsize>,
|
||||
}
|
||||
impl ServerHandler for Controlled {
|
||||
fn get_info(&self) -> ServerConfig {
|
||||
let mut info = ServerConfig::default();
|
||||
info.capabilities = serde_json::from_value(
|
||||
json!({"tools":{"listChanged":self.tools}, "resources":{"listChanged":true}}),
|
||||
)
|
||||
.unwrap();
|
||||
info
|
||||
}
|
||||
fn accepted_subscription_filter(&self, _: &SubscriptionFilter) -> Option<SubscriptionFilter> {
|
||||
Some(filter(
|
||||
json!({"toolsListChanged":true, "resourcesListChanged":true, "promptsListChanged":true}),
|
||||
))
|
||||
}
|
||||
async fn listen(&self, context: SubscriptionContext) -> Result<(), ErrorData> {
|
||||
self.calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
assert!(context.sink().notify_prompt_list_changed().await.is_err());
|
||||
if self.fail {
|
||||
return Err(ErrorData::internal_error("controlled refusal", None));
|
||||
}
|
||||
if self.tools {
|
||||
context.sink().notify_tool_list_changed().await.unwrap();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn actual_owner_consumes_typed_success_error_and_capability_subset_without_final_wire() {
|
||||
for (fail, tools) in [(false, true), (false, false), (true, false)] {
|
||||
let c = Arc::new(CounterSet::default());
|
||||
observer::observe(c.clone(), async {
|
||||
let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
|
||||
let handler = Controlled {
|
||||
fail,
|
||||
tools,
|
||||
calls: calls.clone(),
|
||||
};
|
||||
let (sender, mut receiver) = mpsc::channel(4);
|
||||
let operation = own(
|
||||
handler,
|
||||
filter(json!({"toolsListChanged":true,"promptsListChanged":true})),
|
||||
meta(),
|
||||
json!(1.25),
|
||||
sender,
|
||||
);
|
||||
let task = tokio::spawn(observer::observe(c.clone(), operation));
|
||||
let ack: Value = serde_json::from_str(&receiver.recv().await.unwrap()).unwrap();
|
||||
assert_eq!(
|
||||
ack["params"]["notifications"],
|
||||
if tools {
|
||||
json!({"toolsListChanged":true})
|
||||
} else {
|
||||
json!({})
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
ack["params"]["_meta"][signals::SUBSCRIPTION_ID_KEY],
|
||||
json!(1.25)
|
||||
);
|
||||
let result = tokio::time::timeout(Duration::from_secs(2), task)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
|
||||
assert_eq!(c.sdk_subscription_handler_consumed(), 1);
|
||||
assert_eq!(c.sdk_subscription_owner_completed(), 1);
|
||||
if fail {
|
||||
assert_eq!(result.unwrap_err().message, "controlled refusal");
|
||||
} else {
|
||||
result.unwrap();
|
||||
}
|
||||
if tools {
|
||||
let value: Value = serde_json::from_str(&receiver.recv().await.unwrap()).unwrap();
|
||||
assert_eq!(value["method"], "notifications/tools/list_changed");
|
||||
}
|
||||
assert!(
|
||||
receiver.recv().await.is_none(),
|
||||
"no result/error/second ACK on wire"
|
||||
);
|
||||
})
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unpolled_body_and_pre_ack_drop_join_owner_and_release_permit() {
|
||||
for before_ack in [true, false] {
|
||||
let c = Arc::new(CounterSet::default());
|
||||
observer::observe(c.clone(), async {
|
||||
struct Pending;
|
||||
impl ServerHandler for Pending {
|
||||
fn accepted_subscription_filter(
|
||||
&self,
|
||||
_: &SubscriptionFilter,
|
||||
) -> Option<SubscriptionFilter> {
|
||||
Some(SubscriptionFilter::new())
|
||||
}
|
||||
async fn listen(&self, context: SubscriptionContext) -> Result<(), ErrorData> {
|
||||
context.cancelled().await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
if before_ack {
|
||||
{
|
||||
let future =
|
||||
start_with(Pending, SubscriptionFilter::new(), meta(), json!("cancel"));
|
||||
tokio::pin!(future);
|
||||
assert!(futures_util::poll!(&mut future).is_pending());
|
||||
}
|
||||
} else {
|
||||
let (ack, receiver) =
|
||||
start_with(Pending, SubscriptionFilter::new(), meta(), json!("body"))
|
||||
.await
|
||||
.unwrap();
|
||||
let semaphore = Arc::new(tokio::sync::Semaphore::new(1));
|
||||
let guard = semaphore.clone().try_acquire_owned().unwrap();
|
||||
let body = signals::frames_stream(vec![ack], receiver, guard);
|
||||
drop(body);
|
||||
assert_eq!(semaphore.available_permits(), 1);
|
||||
}
|
||||
finished(&c).await;
|
||||
assert_eq!(c.sdk_subscription_owner_completed(), 1);
|
||||
})
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn gone_catalog_finishes_real_listener_and_pre_ack_rejection_is_not_success() {
|
||||
let c = Arc::new(CounterSet::default());
|
||||
observer::observe(c.clone(), async {
|
||||
let read: Reader = Box::new(|| panic!("Gone baseline must never read again"));
|
||||
let handler = Listener {
|
||||
capabilities: ServerCapabilities::default(),
|
||||
watch: Mutex::new(Some((
|
||||
Watch {
|
||||
workspace_slug: "w".into(),
|
||||
agent_slug: "a".into(),
|
||||
signals: signals::Signals::ALL,
|
||||
wire: signals::Wire::Legacy,
|
||||
legacy_stream: None,
|
||||
deadline: None,
|
||||
},
|
||||
read,
|
||||
Err(ReadError::Gone),
|
||||
))),
|
||||
};
|
||||
let (_, mut receiver) =
|
||||
start_with(handler, SubscriptionFilter::new(), meta(), json!("gone"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(receiver.recv().await.is_none(), "no final frame");
|
||||
finished(&c).await;
|
||||
assert_eq!(c.sdk_subscription_handler_completed(), 1);
|
||||
assert_eq!(c.sdk_subscription_handler_consumed(), 1);
|
||||
})
|
||||
.await;
|
||||
struct Refused;
|
||||
impl ServerHandler for Refused {}
|
||||
let result = start_with(Refused, SubscriptionFilter::new(), meta(), json!("refused")).await;
|
||||
assert!(
|
||||
result.is_err(),
|
||||
"SDK refusal before ACK must not create a successful stream"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pending_catalog_read_is_dropped_and_sdk_tasks_join_on_body_close() {
|
||||
let c = Arc::new(CounterSet::default());
|
||||
observer::observe(c.clone(), async {
|
||||
let entered = Arc::new(tokio::sync::Notify::new());
|
||||
let dropped = Arc::new(std::sync::atomic::AtomicUsize::new(0));
|
||||
struct DropRead(Arc<std::sync::atomic::AtomicUsize>);
|
||||
impl Drop for DropRead {
|
||||
fn drop(&mut self) {
|
||||
self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
let e = entered.clone();
|
||||
let d = dropped.clone();
|
||||
let read: Reader = Box::new(move || {
|
||||
let e = e.clone();
|
||||
let d = d.clone();
|
||||
Box::pin(async move {
|
||||
let _guard = DropRead(d);
|
||||
e.notify_one();
|
||||
std::future::pending().await
|
||||
})
|
||||
});
|
||||
let handler = Listener {
|
||||
capabilities: ServerCapabilities::default(),
|
||||
watch: Mutex::new(Some((
|
||||
Watch {
|
||||
workspace_slug: "w".into(),
|
||||
agent_slug: "a".into(),
|
||||
signals: signals::Signals::ALL,
|
||||
wire: signals::Wire::Legacy,
|
||||
legacy_stream: None,
|
||||
deadline: None,
|
||||
},
|
||||
read,
|
||||
Ok(CatalogFingerprint::default()),
|
||||
))),
|
||||
};
|
||||
assert_eq!(
|
||||
ServerHandler::supported_protocol_versions(&handler)
|
||||
.iter()
|
||||
.map(|v| v.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
crate::jsonrpc::SUPPORTED_PROTOCOL_VERSIONS
|
||||
);
|
||||
let (_, receiver) = start_with(
|
||||
handler,
|
||||
SubscriptionFilter::new(),
|
||||
meta(),
|
||||
json!("pending-read"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
tokio::time::timeout(Duration::from_secs(2), entered.notified())
|
||||
.await
|
||||
.unwrap();
|
||||
drop(receiver);
|
||||
finished(&c).await;
|
||||
assert_eq!(dropped.load(std::sync::atomic::Ordering::SeqCst), 1);
|
||||
assert_eq!(c.sdk_subscription_handler_completed(), 0);
|
||||
assert_eq!(c.sdk_subscription_handler_consumed(), 0);
|
||||
})
|
||||
.await;
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
//! Real trait-result consumption controls and request-owned cancellation.
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct Domain {
|
||||
outcome: Mutex<Option<ToolsOutcome>>,
|
||||
calls: AtomicUsize,
|
||||
pending: bool,
|
||||
started: tokio::sync::Notify,
|
||||
dropped: AtomicBool,
|
||||
}
|
||||
struct Witness<'a>(&'a AtomicBool);
|
||||
impl Drop for Witness<'_> {
|
||||
fn drop(&mut self) {
|
||||
self.0.store(true, SeqCst);
|
||||
}
|
||||
}
|
||||
impl Domain {
|
||||
pub(super) async fn execute(&self, scope: &Scope, command: ToolsCommand) -> ToolsOutcome {
|
||||
self.calls.fetch_add(1, SeqCst);
|
||||
assert_eq!(scope.workspace, "workspace");
|
||||
assert_eq!(scope.agent, "agent");
|
||||
assert_eq!(
|
||||
scope
|
||||
.credential
|
||||
.platform_api_key_id
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.as_str(),
|
||||
"key"
|
||||
);
|
||||
assert_eq!(scope.locale, ExecutionLocale::En);
|
||||
match command {
|
||||
ToolsCommand::List(list) => {
|
||||
assert_eq!(list.cursor.as_deref(), Some("cursor"));
|
||||
assert_eq!(list.limit, Some(7));
|
||||
}
|
||||
ToolsCommand::Call(call) => {
|
||||
assert_eq!(call.tool_name, "bound");
|
||||
assert_eq!(call.arguments, json!({"number":42,"nested":[null]}));
|
||||
assert_eq!(call.confirmation_token.as_deref(), Some("confirmation"));
|
||||
assert!(scope.lifecycle.is_some());
|
||||
}
|
||||
}
|
||||
let _witness = Witness(&self.dropped);
|
||||
self.started.notify_one();
|
||||
if self.pending {
|
||||
std::future::pending::<()>().await;
|
||||
}
|
||||
self.outcome
|
||||
.lock()
|
||||
.unwrap()
|
||||
.take()
|
||||
.expect("one domain call")
|
||||
}
|
||||
}
|
||||
fn scope() -> Scope {
|
||||
Scope {
|
||||
workspace: "workspace".into(),
|
||||
agent: "agent".into(),
|
||||
credential: VerifiedMachineCredential {
|
||||
machine_access_mode: crank_core::MachineAccessMode::StaticAgentKey,
|
||||
max_security_level: crank_core::OperationSecurityLevel::Standard,
|
||||
scopes: vec![crank_core::PlatformApiKeyScope::Read],
|
||||
platform_api_key_id: Some("key".into()),
|
||||
},
|
||||
correlation: CorrelationContext::generate(),
|
||||
continuation: ContinuationSupport::ElicitationUnsupported,
|
||||
locale: ExecutionLocale::En,
|
||||
limit: Some(7),
|
||||
confirmation: Some("confirmation".into()),
|
||||
lifecycle: Some(ToolCallLifecycle::new(None).0),
|
||||
}
|
||||
}
|
||||
async fn state() -> Arc<AppState> {
|
||||
crank_test_support::postgres_schema_url_for_task("tools_handler_local", "t_75a3342f").await;
|
||||
super::super::rmcp_tools_tests::test_state().await
|
||||
}
|
||||
fn handler(state: &Arc<AppState>, outcome: ToolsOutcome) -> Handler {
|
||||
let mut h = Handler::new(state);
|
||||
h.domain = Some(Arc::new(Domain {
|
||||
outcome: Mutex::new(Some(outcome)),
|
||||
..Default::default()
|
||||
}));
|
||||
h
|
||||
}
|
||||
async fn context(h: &Handler) -> RequestContext<RoleServer> {
|
||||
let context = request_context(h, scope()).await.unwrap();
|
||||
assert!(context.peer.is_transport_closed());
|
||||
assert_eq!(Arc::strong_count(&h.extras), 1);
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 0);
|
||||
context
|
||||
}
|
||||
fn call() -> CallToolRequestParams {
|
||||
CallToolRequestParams::new("bound").with_arguments(
|
||||
json!({"number":42,"nested":[null]})
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.clone(),
|
||||
)
|
||||
}
|
||||
fn payload(outcome: Outcome, version: &str) -> Value {
|
||||
match super::super::response::sdk_tools_payload(&json!({"id":17}), version, outcome) {
|
||||
super::super::response::ToolCommandPayload::Ok(value) => value,
|
||||
_ => panic!("expected wire payload"),
|
||||
}
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_tools_handler_list_typed_mutation_and_lossless_catalog() {
|
||||
let state = state().await;
|
||||
for version in ["2025-11-25", "2026-07-28"] {
|
||||
let raw = json!({"name":"original","inputSchema":{"type":"object"},"description":null,
|
||||
"annotations":{"readOnlyHint":true,"x-extra":[null]},"icons":null,"x-tool":{}});
|
||||
let h = handler(
|
||||
&state,
|
||||
ToolsOutcome::List {
|
||||
tools: vec![raw.clone()],
|
||||
next_cursor: Some("next".into()),
|
||||
},
|
||||
);
|
||||
let mut result = ServerHandler::list_tools(
|
||||
&h,
|
||||
Some(PaginatedRequestParams::default().with_cursor(Some("cursor".into()))),
|
||||
context(&h).await,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
result.tools[0].name = "mutated".into();
|
||||
result.tools[0].description = Some("typed description".into());
|
||||
result.next_cursor = Some("changed".into());
|
||||
let wire = payload(
|
||||
projection::consume_list(Ok(result), h.take_extras()),
|
||||
version,
|
||||
);
|
||||
let mut expected = raw;
|
||||
expected["name"] = json!("mutated");
|
||||
expected["description"] = json!("typed description");
|
||||
assert_eq!(wire["result"]["tools"], json!([expected]));
|
||||
assert_eq!(wire["result"]["nextCursor"], "changed");
|
||||
assert_eq!(
|
||||
wire["result"].get("resultType").is_some(),
|
||||
version == "2026-07-28"
|
||||
);
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
}
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_tools_handler_call_typed_content_structured_and_is_error_mutations() {
|
||||
let state = state().await;
|
||||
for version in ["2025-11-25", "2026-07-28"] {
|
||||
for original in [
|
||||
ToolCallOutcome::Completed(Value::Null),
|
||||
ToolCallOutcome::ApprovalPending(json!({"approval":true})),
|
||||
] {
|
||||
let h = handler(&state, ToolsOutcome::Call(original));
|
||||
let result = ServerHandler::call_tool(&h, call(), context(&h).await)
|
||||
.await
|
||||
.unwrap();
|
||||
let CallToolResponse::Complete(mut result) = result else {
|
||||
panic!("complete result");
|
||||
};
|
||||
result.content = vec![ContentBlock::text("typed content")];
|
||||
result.structured_content = Some(json!({"typed":42}));
|
||||
result.is_error = Some(true);
|
||||
let wire = payload(projection::consume_call(Ok(result.into())), version);
|
||||
assert_eq!(
|
||||
wire["result"]["content"],
|
||||
json!([{"type":"text","text":"typed content"}])
|
||||
);
|
||||
assert_eq!(wire["result"]["structuredContent"], json!({"typed":42}));
|
||||
assert_eq!(wire["result"]["isError"], true);
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_tools_handler_typed_list_and_call_errors_are_consumed() {
|
||||
let state = state().await;
|
||||
for version in ["2025-11-25", "2026-07-28"] {
|
||||
let h = handler(&state, ToolsOutcome::InvalidParams("original list".into()));
|
||||
let result = ServerHandler::list_tools(
|
||||
&h,
|
||||
Some(PaginatedRequestParams::default().with_cursor(Some("cursor".into()))),
|
||||
context(&h).await,
|
||||
)
|
||||
.await;
|
||||
let mut error = result.unwrap_err();
|
||||
error.message = "mutated list".into();
|
||||
error.data = Some(json!({"typed":true}));
|
||||
let wire = payload(
|
||||
projection::consume_list(Err(error), h.take_extras()),
|
||||
version,
|
||||
);
|
||||
assert_eq!(wire["error"]["message"], "mutated list");
|
||||
assert_eq!(wire["error"]["data"], json!({"typed":true}));
|
||||
let h = handler(
|
||||
&state,
|
||||
ToolsOutcome::Call(ToolCallOutcome::InvalidParams("original call".into())),
|
||||
);
|
||||
let mut error = ServerHandler::call_tool(&h, call(), context(&h).await)
|
||||
.await
|
||||
.unwrap_err();
|
||||
error.code = ErrorCode(-32001);
|
||||
error.message = "mutated call".into();
|
||||
error.data = Some(json!({"typed":42}));
|
||||
let wire = payload(projection::consume_call(Err(error)), version);
|
||||
assert_eq!(wire["error"]["code"], -32001);
|
||||
assert_eq!(wire["error"]["message"], "mutated call");
|
||||
assert_eq!(wire["error"]["data"], json!({"typed":42}));
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
}
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn rmcp_tools_handler_domain_outcomes_keep_wire_semantics() {
|
||||
use crate::tool_error::{McpControlError, control_tool_error_contract};
|
||||
let state = state().await;
|
||||
for version in ["2025-11-25", "2026-07-28"] {
|
||||
for kind in 0..6 {
|
||||
let outcome = || match kind {
|
||||
0 => ToolCallOutcome::Completed(Value::Null),
|
||||
1 => ToolCallOutcome::ApprovalPending(json!({"approval":"pending"})),
|
||||
2 => ToolCallOutcome::ApprovalUnavailable(
|
||||
McpControlError::ApprovalElicitationNotSupported,
|
||||
),
|
||||
3 => ToolCallOutcome::Error(control_tool_error_contract(
|
||||
McpControlError::CatalogRevisionChanged,
|
||||
"request",
|
||||
"trace",
|
||||
ExecutionLocale::En,
|
||||
)),
|
||||
4 => ToolCallOutcome::InvalidParams("invalid argument".into()),
|
||||
_ => ToolCallOutcome::InternalFailure,
|
||||
};
|
||||
let h = handler(&state, ToolsOutcome::Call(outcome()));
|
||||
let ctx = context(&h).await;
|
||||
let correlation = Handler::scope(&ctx).unwrap().correlation;
|
||||
let result = ServerHandler::call_tool(&h, call(), ctx).await;
|
||||
let actual = payload(projection::consume_call(result), version);
|
||||
let expected = super::super::response::tool_command_payload(
|
||||
&json!({"id":17}),
|
||||
version,
|
||||
&correlation,
|
||||
ToolsOutcome::Call(outcome()),
|
||||
);
|
||||
match expected {
|
||||
super::super::response::ToolCommandPayload::Ok(expected) => {
|
||||
assert_eq!(actual, expected)
|
||||
}
|
||||
super::super::response::ToolCommandPayload::Internal(_) => {
|
||||
assert_eq!(actual["error"]["code"], -32603);
|
||||
assert_eq!(actual["error"]["message"], "internal server error");
|
||||
}
|
||||
}
|
||||
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
||||
}
|
||||
for raw in [
|
||||
json!({"name":"tool","inputSchema":{},"description":null,"_meta":null,"annotations":{},"icons":[]}),
|
||||
json!({"name":"tool","inputSchema":{},"annotations":{"x-annotation":null},"icons":null,"x-tool":[null,{}]}),
|
||||
] {
|
||||
let h = handler(
|
||||
&state,
|
||||
ToolsOutcome::List {
|
||||
tools: vec![raw.clone()],
|
||||
next_cursor: Some(String::new()),
|
||||
},
|
||||
);
|
||||
let result = ServerHandler::list_tools(
|
||||
&h,
|
||||
Some(PaginatedRequestParams::default().with_cursor(Some("cursor".into()))),
|
||||
context(&h).await,
|
||||
)
|
||||
.await;
|
||||
let wire = payload(projection::consume_list(result, h.take_extras()), version);
|
||||
assert_eq!(wire["result"]["tools"], json!([raw]));
|
||||
assert_eq!(wire["result"]["nextCursor"], "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_tools_handler_pending_domain_dropped_with_parent_and_identity_is_local() {
|
||||
let state = state().await;
|
||||
for list in [false, true] {
|
||||
let mut h = Handler::new(&state);
|
||||
h.domain = Some(Arc::new(Domain {
|
||||
pending: true,
|
||||
..Default::default()
|
||||
}));
|
||||
let ctx = context(&h).await;
|
||||
let other = request_context(
|
||||
&h,
|
||||
Scope {
|
||||
agent: "other-agent".into(),
|
||||
..scope()
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(Handler::scope(&ctx).unwrap().agent, "agent");
|
||||
assert_eq!(Handler::scope(&other).unwrap().agent, "other-agent");
|
||||
let parent = async {
|
||||
if list {
|
||||
let _ = ServerHandler::list_tools(
|
||||
&h,
|
||||
Some(PaginatedRequestParams::default().with_cursor(Some("cursor".into()))),
|
||||
ctx,
|
||||
)
|
||||
.await;
|
||||
} else {
|
||||
let _ = ServerHandler::call_tool(&h, call(), ctx).await;
|
||||
}
|
||||
};
|
||||
let domain = h.domain.as_ref().unwrap();
|
||||
{
|
||||
let parent = parent;
|
||||
tokio::pin!(parent);
|
||||
tokio::select! { _ = domain.started.notified() => {}, _ = &mut parent => panic!("pending domain completed") }
|
||||
assert!(!domain.dropped.load(SeqCst));
|
||||
}
|
||||
assert!(domain.dropped.load(SeqCst));
|
||||
assert_eq!(domain.calls.load(SeqCst), 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
//! Behavioural ATDD for the rmcp 3.7.0 Tools adapter over the existing typed Tools service.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use crank_core::{
|
||||
AdmissionSchedulerConfig, CoordinationStateStore, CorrelationContext, ExecutionLocale,
|
||||
MachineAccessMode, OperationSecurityLevel, PlatformApiKeyScope,
|
||||
};
|
||||
use crank_registry::{AdmissionLimits, MigrationAuthority, PostgresRegistry, PostgresTaskStore};
|
||||
use crank_runtime::{
|
||||
InMemoryCoordinationStateStore, RequestRateLimitConfig, RequestRateLimiter, SecretCrypto,
|
||||
TaskService,
|
||||
};
|
||||
|
||||
use rmcp::model::{CallToolRequestParams, PaginatedRequestParams, RequestId};
|
||||
use rmcp::service::{RequestContext, serve_directly};
|
||||
use rmcp::{RoleServer, ServerHandler};
|
||||
|
||||
use super::{
|
||||
AppState,
|
||||
rmcp_tools::{RmcpRequestIdentity, RmcpTools},
|
||||
};
|
||||
use crate::app::TaskApplication;
|
||||
use crate::auth::{CommunityMachineCredentialVerifier, VerifiedMachineCredential};
|
||||
use crate::catalog::PublishedToolCatalog;
|
||||
use crate::session::{ActiveSessionMetrics, InMemorySessionStore};
|
||||
use crate::transport::AllowedOrigins;
|
||||
|
||||
pub(super) async fn test_state() -> Arc<AppState> {
|
||||
let database_url =
|
||||
crank_test_support::postgres_schema_url_for_task("rmcp_tools_tests", "t_fdcc7df8").await;
|
||||
let pool = sqlx::PgPool::connect(&database_url).await.unwrap();
|
||||
MigrationAuthority::apply(&pool).await.unwrap();
|
||||
let registry = PostgresRegistry::connect(&database_url).await.unwrap();
|
||||
|
||||
let coordination: Arc<dyn CoordinationStateStore> =
|
||||
Arc::new(InMemoryCoordinationStateStore::default());
|
||||
let sessions = Arc::new(InMemorySessionStore::default());
|
||||
let session_metrics = ActiveSessionMetrics::start(sessions.clone());
|
||||
let catalog = PublishedToolCatalog::new(registry.clone(), Duration::ZERO, coordination.clone());
|
||||
|
||||
let tasks_store = PostgresTaskStore::new(
|
||||
registry.pool().clone(),
|
||||
AdmissionLimits::default(),
|
||||
AdmissionSchedulerConfig::default(),
|
||||
)
|
||||
.expect("default admission limits and scheduler weights are valid");
|
||||
let tasks = Arc::new(TaskApplication::new(
|
||||
registry.clone(),
|
||||
Duration::from_secs(60),
|
||||
coordination.clone(),
|
||||
TaskService::new(Arc::new(tasks_store)),
|
||||
));
|
||||
|
||||
Arc::new(AppState {
|
||||
registry,
|
||||
catalog,
|
||||
runtime: crank_runtime::community_with_outbound_policy(
|
||||
crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]),
|
||||
)
|
||||
.build(),
|
||||
api_rate_limiter: RequestRateLimiter::new(RequestRateLimitConfig {
|
||||
requests_per_second: 10_000,
|
||||
burst: 10_000,
|
||||
}),
|
||||
artifact_store: None,
|
||||
secret_crypto: SecretCrypto::new("01234567890123456789012345678901")
|
||||
.expect("test master key"),
|
||||
sessions,
|
||||
session_metrics,
|
||||
session_slots: Arc::new(tokio::sync::Semaphore::new(16)),
|
||||
credential_verifier: Arc::new(CommunityMachineCredentialVerifier),
|
||||
allowed_origins: AllowedOrigins::new(None),
|
||||
logging_levels: Arc::new(crate::protocol::LogLevelStore::new()),
|
||||
signal_streams: Arc::new(crate::protocol::StreamRegistry::new()),
|
||||
in_flight: Arc::new(super::lifecycle::InFlightCalls::new(
|
||||
super::lifecycle::MAX_IN_FLIGHT_CALLS,
|
||||
)),
|
||||
tasks,
|
||||
})
|
||||
}
|
||||
|
||||
fn verified_credential() -> VerifiedMachineCredential {
|
||||
VerifiedMachineCredential {
|
||||
machine_access_mode: MachineAccessMode::StaticAgentKey,
|
||||
max_security_level: OperationSecurityLevel::Standard,
|
||||
scopes: vec![PlatformApiKeyScope::Write],
|
||||
platform_api_key_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn rmcp_tools(state: Arc<AppState>) -> RmcpTools {
|
||||
RmcpTools {
|
||||
state,
|
||||
active: Arc::default(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn request_context(adapter: &RmcpTools) -> RequestContext<RoleServer> {
|
||||
let (client_io, server_io) = tokio::io::duplex(1024);
|
||||
let (server_reader, server_writer) = tokio::io::split(server_io);
|
||||
let running = serve_directly::<RoleServer, _, _, _, _>(
|
||||
adapter.clone(),
|
||||
(server_reader, server_writer),
|
||||
None,
|
||||
);
|
||||
// keep client side alive for transport lifetime
|
||||
let _ = client_io;
|
||||
let mut context = RequestContext::new(RequestId::Number(1), running.peer().clone());
|
||||
let (mut parts, _) = axum::http::Request::new(()).into_parts();
|
||||
parts.extensions.insert(RmcpRequestIdentity {
|
||||
workspace_slug: "missing-workspace".into(),
|
||||
agent_slug: "missing-agent".into(),
|
||||
credential: verified_credential(),
|
||||
correlation: CorrelationContext::generate(),
|
||||
continuation: crate::protocol::ContinuationSupport::from_supported(false),
|
||||
locale: ExecutionLocale::En,
|
||||
session_id: None,
|
||||
progress_meta: None,
|
||||
sse_acceptable: false,
|
||||
});
|
||||
context.extensions.insert(parts);
|
||||
context
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_adapter_rejects_missing_request_identity() {
|
||||
let adapter = rmcp_tools(test_state().await);
|
||||
let mut context = request_context(&adapter).await;
|
||||
context.extensions.remove::<axum::http::request::Parts>();
|
||||
let result = <RmcpTools as ServerHandler>::list_tools(&adapter, None, context).await;
|
||||
assert!(
|
||||
result
|
||||
.unwrap_err()
|
||||
.message
|
||||
.contains("missing authenticated request context")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_adapter_list_tools_reports_catalog_unavailable_for_unknown_agent() {
|
||||
let state = test_state().await;
|
||||
let adapter = rmcp_tools(state);
|
||||
let ctx = request_context(&adapter).await;
|
||||
|
||||
match <RmcpTools as ServerHandler>::list_tools(
|
||||
&adapter,
|
||||
Some(PaginatedRequestParams::default()),
|
||||
ctx,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Err(err) => assert!(
|
||||
err.message.contains("crate catalog unavailable"),
|
||||
"unexpected error: {}",
|
||||
err.message
|
||||
),
|
||||
Ok(_) => panic!("expected error outcome for unknown agent"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rmcp_adapter_call_tool_reports_catalog_unavailable_for_unknown_agent() {
|
||||
let state = test_state().await;
|
||||
let adapter = rmcp_tools(state);
|
||||
let ctx = request_context(&adapter).await;
|
||||
|
||||
let request = CallToolRequestParams::new("missing_tool");
|
||||
match <RmcpTools as ServerHandler>::call_tool(&adapter, request, ctx).await {
|
||||
Err(err) => assert!(
|
||||
err.message.contains("crate catalog unavailable"),
|
||||
"unexpected error: {}",
|
||||
err.message
|
||||
),
|
||||
Ok(_) => panic!("expected error outcome for unknown agent"),
|
||||
}
|
||||
}
|
||||
@@ -209,13 +209,15 @@ pub use secret::{Secret, SecretKind, SecretStatus, SecretVersion};
|
||||
pub use task::{
|
||||
AttemptCertainty, AttemptLease, AttemptOutcome, AttemptOutcomeClass, AttemptPhase,
|
||||
AttemptResolution, BeginAttempt, TASK_DEFAULT_RETENTION_MS, TASK_ENVELOPE_MAX_BYTES,
|
||||
TASK_EXECUTABLE_INPUT_CIPHERTEXT_MAX_BYTES, TASK_EXECUTABLE_INPUT_MAX_BYTES, TASK_MAX_ATTEMPTS,
|
||||
TASK_MAX_BACKOFF_STEP_MS, TASK_MAX_RETENTION_MS, TASK_PURGE_BATCH_LIMIT,
|
||||
TASK_SCOPED_KEY_MAX_BYTES, TaskAcceptRequest, TaskAcceptance, TaskAccess, TaskAttempt,
|
||||
TaskCancelOutcome, TaskCancelRequest, TaskEnvelope, TaskError, TaskFinishRequest, TaskId,
|
||||
TaskLease, TaskOwnerKind, TaskPins, TaskPurgeReport, TaskRecord, TaskRetryPolicy, TaskState,
|
||||
TaskStore, TaskSupportIds, contains_sensitive_task_keys, redact_task_envelope,
|
||||
sensitive_task_key, validate_task_envelope,
|
||||
TASK_EXECUTABLE_INPUT_CIPHERTEXT_MAX_BYTES, TASK_EXECUTABLE_INPUT_MAX_BYTES,
|
||||
TASK_INPUT_PROMPT_CODE_MAX_BYTES, TASK_INPUT_SCHEMA_FORMAT_VERSION,
|
||||
TASK_INPUT_WAIT_ID_MAX_BYTES, TASK_MAX_ATTEMPTS, TASK_MAX_BACKOFF_STEP_MS,
|
||||
TASK_MAX_RETENTION_MS, TASK_PURGE_BATCH_LIMIT, TASK_SCOPED_KEY_MAX_BYTES, TaskAcceptRequest,
|
||||
TaskAcceptance, TaskAccess, TaskAttempt, TaskCancelOutcome, TaskCancelRequest, TaskEnvelope,
|
||||
TaskError, TaskFinishRequest, TaskId, TaskInputRequest, TaskInputStore, TaskInputUpdateOutcome,
|
||||
TaskInputUpdateRequest, TaskLease, TaskOwnerKind, TaskPins, TaskPurgeReport, TaskRecord,
|
||||
TaskRetryPolicy, TaskState, TaskStore, TaskSupportIds, contains_sensitive_task_keys,
|
||||
redact_task_envelope, sensitive_task_key, validate_task_envelope,
|
||||
};
|
||||
pub use tool_catalog::{
|
||||
RECOMMENDED_TOOL_CATALOG_CONTEXT_TOKENS, ToolCatalogAnalysis, ToolCatalogAnalysisError,
|
||||
|
||||
@@ -0,0 +1,295 @@
|
||||
//! Continuation of an accepted Task through a validated additional input (Story 4.5, FR-25).
|
||||
//!
|
||||
//! A Task that cannot run because the pinned operation still lacks a required input item is
|
||||
//! suspended as `input_required` before any outbound dispatch. The durable request keeps the exact
|
||||
//! immutable schema snapshot, its JSON projection, safe prompt metadata, the absolute TTL and the
|
||||
//! update fingerprint contract. A valid update moves the Task back to `working` exactly once; an
|
||||
//! identical replay returns the committed outcome and a conflicting or stale reply is typed.
|
||||
//!
|
||||
//! Deliberately absent: raw input in the request/receipt/audit read models, a second cipher store and
|
||||
//! any public transport. The regular executable-input ciphertext carries the continuation binding.
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::task::{TaskAccess, TaskError, TaskId};
|
||||
|
||||
/// Version of the persisted input-continuation contract. A stored snapshot always declares the
|
||||
/// version it was written with, so a later binary never reinterprets it silently.
|
||||
pub const TASK_INPUT_SCHEMA_FORMAT_VERSION: u32 = 1;
|
||||
/// Bound of a wait identity before it is persisted.
|
||||
pub const TASK_INPUT_WAIT_ID_MAX_BYTES: usize = 64;
|
||||
/// Bound of the safe prompt code (never the prompt text or an input value).
|
||||
pub const TASK_INPUT_PROMPT_CODE_MAX_BYTES: usize = 64;
|
||||
/// Bound of the missing-field path metadata.
|
||||
pub const TASK_INPUT_MAX_MISSING_FIELDS: usize = 32;
|
||||
/// Bound of one missing-field path.
|
||||
pub const TASK_INPUT_FIELD_PATH_MAX_BYTES: usize = 160;
|
||||
|
||||
/// Durable request of additional input for one suspended Task.
|
||||
///
|
||||
/// `schema_snapshot` is the native serialized schema subset the server validates against; the JSON
|
||||
/// projection is presentation metadata only and never the validation authority. `expected_version`
|
||||
/// is the Task version a caller must present with the answer.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct TaskInputRequest {
|
||||
pub wait_id: String,
|
||||
pub expected_version: u32,
|
||||
pub schema_revision: u32,
|
||||
pub schema_format_version: u32,
|
||||
pub schema_snapshot: Value,
|
||||
pub projected_schema: Value,
|
||||
pub prompt_code: String,
|
||||
pub missing_fields: Vec<String>,
|
||||
pub expires_at_unix_ms: i64,
|
||||
pub fingerprint_format_version: u32,
|
||||
}
|
||||
|
||||
impl TaskInputRequest {
|
||||
pub fn validate(&self) -> Result<(), TaskError> {
|
||||
if self.wait_id.is_empty()
|
||||
|| self.wait_id.len() > TASK_INPUT_WAIT_ID_MAX_BYTES
|
||||
|| self.wait_id.bytes().any(|byte| byte.is_ascii_control())
|
||||
{
|
||||
return Err(TaskError::invalid("task input wait identity is invalid"));
|
||||
}
|
||||
if self.expected_version == 0 || self.schema_revision == 0 {
|
||||
return Err(TaskError::invalid(
|
||||
"task input request generation is invalid",
|
||||
));
|
||||
}
|
||||
if self.schema_format_version < TASK_INPUT_SCHEMA_FORMAT_VERSION
|
||||
|| self.fingerprint_format_version < TASK_INPUT_SCHEMA_FORMAT_VERSION
|
||||
{
|
||||
return Err(TaskError::invalid(
|
||||
"task input request format version is invalid",
|
||||
));
|
||||
}
|
||||
if self.schema_snapshot.as_object().is_none() || self.projected_schema.as_object().is_none()
|
||||
{
|
||||
return Err(TaskError::invalid("task input schema snapshot is invalid"));
|
||||
}
|
||||
if self.prompt_code.is_empty() || self.prompt_code.len() > TASK_INPUT_PROMPT_CODE_MAX_BYTES
|
||||
{
|
||||
return Err(TaskError::invalid("task input prompt code is invalid"));
|
||||
}
|
||||
if self.missing_fields.len() > TASK_INPUT_MAX_MISSING_FIELDS
|
||||
|| self
|
||||
.missing_fields
|
||||
.iter()
|
||||
.any(|field| field.is_empty() || field.len() > TASK_INPUT_FIELD_PATH_MAX_BYTES)
|
||||
{
|
||||
return Err(TaskError::invalid(
|
||||
"task input missing-field metadata is invalid",
|
||||
));
|
||||
}
|
||||
if self.expires_at_unix_ms <= 0 {
|
||||
return Err(TaskError::invalid("task input TTL is invalid"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Authoritative write of one validated answer. The transaction owns schema-generation, TTL,
|
||||
/// fingerprint and CAS checks; this request only carries the committed values.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct TaskInputUpdateRequest {
|
||||
pub access: TaskAccess,
|
||||
pub task_id: TaskId,
|
||||
pub wait_id: String,
|
||||
pub expected_version: u32,
|
||||
/// Digest of the scoped update key; the caller key is never persisted.
|
||||
pub update_key_digest: String,
|
||||
/// Canonical digest of the validated full executable input.
|
||||
pub canonical_fingerprint: String,
|
||||
/// Effective execution fingerprint of the resumed payload (never the acceptance fingerprint).
|
||||
pub effective_input_fingerprint: String,
|
||||
pub executable_input_ciphertext: String,
|
||||
pub executable_input_key_version: String,
|
||||
pub executable_input_master_key_epoch: i64,
|
||||
}
|
||||
|
||||
impl TaskInputUpdateRequest {
|
||||
pub fn validate(&self) -> Result<(), TaskError> {
|
||||
self.access.validate()?;
|
||||
if self.wait_id.is_empty() || self.wait_id.len() > TASK_INPUT_WAIT_ID_MAX_BYTES {
|
||||
return Err(TaskError::invalid("task input wait identity is invalid"));
|
||||
}
|
||||
if self.expected_version == 0 {
|
||||
return Err(TaskError::invalid("task input version is invalid"));
|
||||
}
|
||||
if !valid_digest(&self.update_key_digest)
|
||||
|| !valid_digest(&self.canonical_fingerprint)
|
||||
|| !valid_digest(&self.effective_input_fingerprint)
|
||||
{
|
||||
return Err(TaskError::invalid("task input digest is invalid"));
|
||||
}
|
||||
if self.executable_input_ciphertext.is_empty()
|
||||
|| self.executable_input_ciphertext.len()
|
||||
> crate::task::TASK_EXECUTABLE_INPUT_CIPHERTEXT_MAX_BYTES
|
||||
{
|
||||
return Err(TaskError::invalid(
|
||||
"task continuation ciphertext is invalid",
|
||||
));
|
||||
}
|
||||
if self.executable_input_key_version.is_empty()
|
||||
|| self.executable_input_key_version.len() > 64
|
||||
|| self.executable_input_master_key_epoch < 1
|
||||
{
|
||||
return Err(TaskError::invalid(
|
||||
"task continuation ciphertext is invalid",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Committed outcome of one input update. An identical replay returns the same stored outcome with
|
||||
/// `replayed = true` instead of repeating the CAS, audit or dispatch.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TaskInputUpdateOutcome {
|
||||
pub task_id: TaskId,
|
||||
pub wait_id: String,
|
||||
pub state: crate::task::TaskState,
|
||||
pub version: u32,
|
||||
pub replayed: bool,
|
||||
}
|
||||
|
||||
fn valid_digest(value: &str) -> bool {
|
||||
value.len() == 64
|
||||
&& value
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase())
|
||||
}
|
||||
|
||||
/// Durable input-continuation operations of the Task store.
|
||||
///
|
||||
/// Split out of the general [`crate::task::TaskStore`] port so the lifecycle store stays readable and
|
||||
/// the continuation contract (suspend, answer, receipt, expiry) is implemented and audited as one
|
||||
/// cohesive surface. [`crate::task::TaskStore`] extends this trait, so an owned `Arc<dyn TaskStore>`
|
||||
/// exposes both halves through one object.
|
||||
#[async_trait::async_trait]
|
||||
pub trait TaskInputStore: Send + Sync {
|
||||
/// Suspends a leased Task that still lacks a required input of its pinned operation. The
|
||||
/// transaction persists the immutable request and its audit row, moves `working` to
|
||||
/// `input_required` exactly once, releases the worker lease and keeps the existing admission
|
||||
/// permit. No outbound dispatch may have happened in this generation.
|
||||
async fn suspend_for_input(
|
||||
&self,
|
||||
lease: &crate::task::TaskLease,
|
||||
request: TaskInputRequest,
|
||||
) -> Result<crate::task::TaskRecord, TaskError>;
|
||||
|
||||
/// Owner-scoped read of the durable input request of one Task.
|
||||
async fn input_request(
|
||||
&self,
|
||||
access: &TaskAccess,
|
||||
task_id: TaskId,
|
||||
) -> Result<TaskInputRequest, TaskError>;
|
||||
|
||||
/// Owner-scoped read of the executable-input binding that a committed continuation may extend.
|
||||
/// It is not lease-authorized: it only serves the update transaction and never dispatch.
|
||||
async fn read_executable_input_for_update(
|
||||
&self,
|
||||
access: &TaskAccess,
|
||||
task_id: TaskId,
|
||||
) -> Result<(String, String, i64), TaskError>;
|
||||
|
||||
/// Applies exactly one validated answer under a row lock: authority recheck, receipt lookup,
|
||||
/// generation/TTL check, atomic ciphertext/effective-fingerprint/receipt/`working` transition and
|
||||
/// audit. An identical replay returns the stored outcome without a second transition.
|
||||
async fn apply_input(
|
||||
&self,
|
||||
request: TaskInputUpdateRequest,
|
||||
) -> Result<TaskInputUpdateOutcome, TaskError>;
|
||||
|
||||
/// Bounded sweep of `input_required` Tasks whose TTL elapsed: `failed` with an audit reason and
|
||||
/// the release of the admission permit, without any dispatch.
|
||||
async fn expire_input_waits(&self, batch: u32) -> Result<u64, TaskError>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn request() -> TaskInputRequest {
|
||||
TaskInputRequest {
|
||||
wait_id: "wait-1".to_owned(),
|
||||
expected_version: 2,
|
||||
schema_revision: 1,
|
||||
schema_format_version: TASK_INPUT_SCHEMA_FORMAT_VERSION,
|
||||
schema_snapshot: json!({"type": "object", "fields": {}}),
|
||||
projected_schema: json!({"type": "object", "properties": {}}),
|
||||
prompt_code: "required_input_missing".to_owned(),
|
||||
missing_fields: vec!["$.email".to_owned()],
|
||||
expires_at_unix_ms: 1_900_000_000_000,
|
||||
fingerprint_format_version: TASK_INPUT_SCHEMA_FORMAT_VERSION,
|
||||
}
|
||||
}
|
||||
|
||||
fn update() -> TaskInputUpdateRequest {
|
||||
TaskInputUpdateRequest {
|
||||
access: crate::task::TaskAccess {
|
||||
workspace_id: "ws_default".to_owned(),
|
||||
owner_kind: crate::task::TaskOwnerKind::Agent,
|
||||
owner_id: "agent_a".to_owned(),
|
||||
authorization_scope: "credential:pk_1".to_owned(),
|
||||
},
|
||||
task_id: crate::task::TaskId::generate(),
|
||||
wait_id: "wait-1".to_owned(),
|
||||
expected_version: 2,
|
||||
update_key_digest: "a".repeat(64),
|
||||
canonical_fingerprint: "b".repeat(64),
|
||||
effective_input_fingerprint: "c".repeat(64),
|
||||
executable_input_ciphertext: "sealed".to_owned(),
|
||||
executable_input_key_version: "key-v1".to_owned(),
|
||||
executable_input_master_key_epoch: 1,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn input_request_holds_generation_schema_ttl_and_bounds() {
|
||||
assert!(request().validate().is_ok());
|
||||
|
||||
let mut no_wait = request();
|
||||
no_wait.wait_id.clear();
|
||||
assert!(no_wait.validate().is_err());
|
||||
|
||||
let mut no_generation = request();
|
||||
no_generation.expected_version = 0;
|
||||
assert!(no_generation.validate().is_err());
|
||||
|
||||
let mut bad_schema = request();
|
||||
bad_schema.schema_snapshot = json!("not-an-object");
|
||||
assert!(bad_schema.validate().is_err());
|
||||
|
||||
let mut no_ttl = request();
|
||||
no_ttl.expires_at_unix_ms = 0;
|
||||
assert!(no_ttl.validate().is_err());
|
||||
|
||||
let mut too_many_fields = request();
|
||||
too_many_fields.missing_fields = vec!["$.x".to_owned(); TASK_INPUT_MAX_MISSING_FIELDS + 1];
|
||||
assert!(too_many_fields.validate().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_request_rejects_invalid_digests_and_ciphertext_bounds() {
|
||||
assert!(update().validate().is_ok());
|
||||
|
||||
let mut short_digest = update();
|
||||
short_digest.canonical_fingerprint = "abc".to_owned();
|
||||
assert!(short_digest.validate().is_err());
|
||||
|
||||
let mut upper_digest = update();
|
||||
upper_digest.update_key_digest = "A".repeat(64);
|
||||
assert!(upper_digest.validate().is_err());
|
||||
|
||||
let mut no_ciphertext = update();
|
||||
no_ciphertext.executable_input_ciphertext.clear();
|
||||
assert!(no_ciphertext.validate().is_err());
|
||||
|
||||
let mut no_epoch = update();
|
||||
no_epoch.executable_input_master_key_epoch = 0;
|
||||
assert!(no_epoch.validate().is_err());
|
||||
}
|
||||
}
|
||||
@@ -10,8 +10,10 @@
|
||||
//! Task was executed. A terminal state is written by a conditional transition only.
|
||||
|
||||
mod attempt;
|
||||
mod input;
|
||||
use async_trait::async_trait;
|
||||
pub use attempt::*;
|
||||
pub use input::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use uuid::Uuid;
|
||||
@@ -523,6 +525,10 @@ pub struct TaskRecord {
|
||||
pub operation_id: String,
|
||||
/// Canonical digest of the accepted executable input (compare-and-conflict identity).
|
||||
pub input_fingerprint: String,
|
||||
/// Effective fingerprint of the payload the next attempt will execute. It equals the acceptance
|
||||
/// fingerprint until a committed input continuation replaces the effective payload; the
|
||||
/// acceptance fingerprint above is never overwritten, so an original accept replay stays replay.
|
||||
pub effective_input_fingerprint: String,
|
||||
pub pins: TaskPins,
|
||||
pub support: TaskSupportIds,
|
||||
pub input: TaskEnvelope,
|
||||
@@ -607,6 +613,8 @@ pub enum TaskError {
|
||||
FingerprintConflict,
|
||||
#[error("task lifecycle transition was rejected")]
|
||||
StateConflict,
|
||||
#[error("task input request has expired")]
|
||||
InputExpired,
|
||||
#[error("background task capacity is exhausted")]
|
||||
Overloaded { retry_after_ms: u64 },
|
||||
}
|
||||
@@ -641,7 +649,7 @@ pub struct TaskLease {
|
||||
|
||||
/// Authoritative durable Task port.
|
||||
#[async_trait]
|
||||
pub trait TaskStore: Send + Sync {
|
||||
pub trait TaskStore: TaskInputStore + Send + Sync {
|
||||
/// Proves the durable Task state is usable (schema and operations reachable).
|
||||
async fn health(&self) -> Result<(), TaskError>;
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ mod artifact_cleanup_indexes_v13;
|
||||
mod artifact_metadata_v12;
|
||||
mod authority;
|
||||
mod baseline_v1;
|
||||
mod durable_task_input_v30;
|
||||
mod durable_tasks_v25;
|
||||
mod durable_tasks_v26;
|
||||
mod durable_tasks_v27;
|
||||
@@ -47,6 +48,7 @@ mod schema_guard_v26;
|
||||
mod schema_guard_v27;
|
||||
mod schema_guard_v28;
|
||||
mod schema_guard_v29;
|
||||
mod schema_guard_v30;
|
||||
mod schema_guard_v7;
|
||||
mod schema_guard_v8;
|
||||
mod schema_guard_v9;
|
||||
|
||||
@@ -4,6 +4,7 @@ use super::agent_resource_bindings_v18;
|
||||
use super::approval_side_effects_v10;
|
||||
use super::artifact_cleanup_indexes_v13;
|
||||
use super::artifact_metadata_v12;
|
||||
use super::durable_task_input_v30;
|
||||
use super::durable_tasks_v25;
|
||||
use super::durable_tasks_v26;
|
||||
use super::durable_tasks_v27;
|
||||
@@ -45,10 +46,10 @@ use preflight::*;
|
||||
use contract::baseline_source_digest;
|
||||
use contract::validate_descriptors;
|
||||
const MIGRATION_LOCK_ID: i64 = 0x4352_414E_4B4D_4947;
|
||||
const CURRENT_VERSION: i64 = 29;
|
||||
const CURRENT_VERSION: i64 = 30;
|
||||
const IMPLEMENTED_VERSIONS: &[i64] = &[
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
|
||||
27, 28, 29,
|
||||
27, 28, 29, 30,
|
||||
];
|
||||
const BASELINE_SOURCE_SHA256: &str =
|
||||
"eb1656fc5b4b5be9ee390d237d1d58e4b2274ae5ba9b7ba06a2f3f860dfda675";
|
||||
@@ -373,6 +374,12 @@ impl MigrationAuthority {
|
||||
durable_tasks_v29::SOURCE_SHA256,
|
||||
28,
|
||||
),
|
||||
expand_descriptor(
|
||||
30,
|
||||
durable_task_input_v30::NAME,
|
||||
durable_task_input_v30::SOURCE_SHA256,
|
||||
29,
|
||||
),
|
||||
]
|
||||
}
|
||||
pub fn validate_sequence() -> Result<(), MigrationError> {
|
||||
@@ -550,6 +557,9 @@ impl MigrationAuthority {
|
||||
if from < 29 {
|
||||
durable_tasks_v29::apply(&mut transaction, &Self::sequence()[28]).await?;
|
||||
}
|
||||
if from < 30 {
|
||||
durable_task_input_v30::apply(&mut transaction, &Self::sequence()[29]).await?;
|
||||
}
|
||||
match inspect(&mut transaction).await? {
|
||||
MigrationPreflight::Current {
|
||||
version: CURRENT_VERSION,
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
use sqlx::{Postgres, Transaction, query};
|
||||
|
||||
use super::authority::{MigrationDescriptor, MigrationError};
|
||||
|
||||
pub(super) const NAME: &str = "durable-task-input-v30";
|
||||
pub(super) const SOURCE: &str = include_str!("durable_task_input_v30.sql");
|
||||
pub(super) const SOURCE_SHA256: &str =
|
||||
"84a51f712fd0bc439acba2ab3849232777c29e3b181586bbb4b06fd129843a38";
|
||||
|
||||
pub(super) async fn apply(
|
||||
transaction: &mut Transaction<'_, Postgres>,
|
||||
descriptor: &MigrationDescriptor,
|
||||
) -> Result<(), MigrationError> {
|
||||
sqlx::raw_sql(SOURCE)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.durable_task_input_v30",
|
||||
Some(30),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
})?;
|
||||
query(
|
||||
"insert into __crank_migrations (version, name, checksum, phase, compatibility)
|
||||
values ($1, $2, $3, $4, $5)",
|
||||
)
|
||||
.bind(descriptor.version)
|
||||
.bind(descriptor.name)
|
||||
.bind(&descriptor.checksum)
|
||||
.bind(descriptor.phase)
|
||||
.bind(descriptor.compatibility)
|
||||
.execute(&mut **transaction)
|
||||
.await
|
||||
.map_err(|_| {
|
||||
MigrationError::new(
|
||||
"apply_failed",
|
||||
"apply.durable_task_input_v30",
|
||||
Some(30),
|
||||
"restore_known_good_backup",
|
||||
)
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
-- V30: continuation through validated input (Story 4.5).
|
||||
alter table tasks add column if not exists effective_input_fingerprint text;
|
||||
update tasks set effective_input_fingerprint = input_fingerprint where effective_input_fingerprint is null;
|
||||
alter table tasks alter column effective_input_fingerprint set not null;
|
||||
alter table tasks add constraint tasks_effective_input_fingerprint_check check (
|
||||
length(effective_input_fingerprint) = 64 and effective_input_fingerprint ~ '^[0-9a-f]{64}$'
|
||||
);
|
||||
|
||||
create table if not exists task_input_requests (
|
||||
task_id uuid primary key references tasks (task_id) on delete cascade,
|
||||
wait_id text not null,
|
||||
expected_version integer not null,
|
||||
schema_revision integer not null,
|
||||
schema_format_version integer not null,
|
||||
fingerprint_format_version integer not null,
|
||||
schema_snapshot jsonb not null,
|
||||
projected_schema jsonb not null,
|
||||
prompt_code text not null,
|
||||
missing_fields jsonb not null default '[]'::jsonb,
|
||||
expires_at timestamptz not null,
|
||||
created_at timestamptz not null default now(),
|
||||
accepted_at timestamptz,
|
||||
update_key_digest text,
|
||||
canonical_fingerprint text,
|
||||
effective_input_fingerprint text,
|
||||
committed_state text,
|
||||
committed_version integer,
|
||||
constraint task_input_requests_wait_unique unique (wait_id),
|
||||
constraint task_input_requests_prompt_check check (octet_length(prompt_code) between 1 and 64),
|
||||
constraint task_input_requests_receipt_check check (
|
||||
(accepted_at is null and update_key_digest is null and canonical_fingerprint is null
|
||||
and effective_input_fingerprint is null and committed_state is null and committed_version is null)
|
||||
or (accepted_at is not null and update_key_digest is not null and canonical_fingerprint is not null
|
||||
and effective_input_fingerprint is not null and committed_state is not null and committed_version is not null)
|
||||
)
|
||||
);
|
||||
-- Not unique on purpose: several waiting Tasks may share one absolute TTL value (the same pinned
|
||||
-- policy deadline), and an expired/cancelled wait keeps its row. Per-Task uniqueness comes from the
|
||||
-- primary key and task_input_requests_wait_unique.
|
||||
create index if not exists task_input_requests_active_ttl_idx
|
||||
on task_input_requests (expires_at) where accepted_at is null;
|
||||
|
||||
create table if not exists task_input_audit (
|
||||
id bigint generated always as identity primary key,
|
||||
task_id uuid not null references tasks (task_id) on delete cascade,
|
||||
wait_id text not null,
|
||||
event text not null,
|
||||
from_state text,
|
||||
to_state text,
|
||||
from_version integer,
|
||||
to_version integer,
|
||||
actor text,
|
||||
reason_code text,
|
||||
created_at timestamptz not null default now(),
|
||||
constraint task_input_audit_event_check check (event in ('input_required','input_accepted','input_expired')),
|
||||
constraint task_input_audit_actor_check check (actor is null or octet_length(actor) between 1 and 160),
|
||||
constraint task_input_audit_reason_check check (reason_code is null or octet_length(reason_code) between 1 and 64)
|
||||
);
|
||||
create index if not exists task_input_audit_task_idx on task_input_audit (task_id, id);
|
||||
Loaded 100 of 134 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user