api-contract-development
Use when the REST or GraphQL contract snapshots of a Rust service must be regenerated or validated, when the snapshot, Spectral or breaking-change check fails, or when a change may break API clients a
SKILL.mdplugins/rust-backend-sdlc/skills/api-contract-development/SKILL.md
GitHubAPI Contract Development
Profile keys consumed
framework.openapi— true selects the OpenAPI snapshot, Spectral and OpenAPI diff steps; false skips themframework.graphql— true selects the GraphQL snapshot and GraphQL diff steps; false skips themservice.openapi_snapshot— the committed OpenAPI snapshot; null means none is committed yetservice.graphql_snapshot— the committed GraphQL snapshot; null means none is committed yetmake.generate_openapi_spec— the target that writes the OpenAPI snapshot from the codemake.generate_graphql_spec— the target that writes the GraphQL snapshot from the codemake.validate_contract_snapshots— the target that fails when a committed snapshot differs from the generated onemake.validate_openapi_spec— the target that lints the OpenAPI snapshot with Spectralmake.openapi_diff— the target that reports breaking OpenAPI changes against a base refmake.graphql_diff— the target that reports breaking GraphQL changes against a base refquality.max_spectral_findings— the ceiling on Spectral findingsquality.max_unapproved_breaking_changes— the ceiling on unapproved breaking changes of both diff targets togetherci.base_ref— the base ref both diff targets compare against
Repository precedence
Read .claude/skills/api-contract-development/SKILL.md and AGENTS.md first; they win. This skill adds only profile resolution, the evidence format, the bounded loop, the escalation block and the verdict line.
Where the repository skill names a different command or rule for the same check, follow the repository skill and write the difference into the precedence: line of the evidence; a precedence note is not a degrade note and does not change the verdict. When the repository skill is absent, the Steps below apply in full and the evidence records source: plugin instead of source: repo+plugin.
Context
- The contract is generated from the code: the OpenAPI snapshot from the handler annotations and DTO schemas, the GraphQL snapshot from the schema types. The committed snapshots are what CI compares, so a contract change that leaves a stale snapshot behind is a defect.
- The review stage judges the same checks as quality rows: Q12 is the target mapped by
make.validate_contract_snapshots(binary gate, no quality key), Q13 is the target mapped bymake.validate_openapi_specagainstquality.max_spectral_findings, and Q14 is the targets mapped bymake.openapi_diffandmake.graphql_diffagainstquality.max_unapproved_breaking_changes, compared withci.base_ref. - Spectral and both diff targets need a reachable container engine; the snapshot targets need the toolchain.
- A breaking change is approved only by a maintainer through the label
approved-breaking-changeon the pull request; CI then setsBREAKING_CHANGE_APPROVED=truefor the diff jobs. The label name is confirmed indocs/repository-settings.md, which wins over this skill. - The skill runs inside a loop stage after the triage recorded
EXECUTEfor it (the diff touches a handler annotation, a DTO, a GraphQL type or a snapshot, or a contract check failed), or directly when a person asks. The code side of an operation belongs to api-resource-development.
Task
Bring the committed snapshots service.openapi_snapshot and service.graphql_snapshot in line with the code through the mapped targets, prove them with the snapshot, Spectral and breaking-change checks, and apply the breaking-change policy: approve nothing yourself, escalate an intended breaking change that lacks the label, and never weaken the contract or a check. Report the result in the evidence format and end with the verdict line.
Success criteria
- Every applicable snapshot was regenerated by its mapped target in the same change as the code and was never edited by hand.
- The target mapped by
make.validate_contract_snapshotspassed. - The target mapped by
make.validate_openapi_specexited 0 and reported no more findings thanquality.max_spectral_findings. - The targets mapped by
make.openapi_diffandmake.graphql_diffran withbase_ref=<ci.base_ref>and their unapproved breaking changes together are withinquality.max_unapproved_breaking_changes; an approved run usedBREAKING_CHANGE_APPROVED=trueonly because the pull request already carriedapproved-breaking-change. - An intended breaking change without that label ended
ESCALATEDwith the label named; the label was never applied or removed by this skill. - The evidence block and the verdict line are printed.
Steps
Profile resolution
- Read the thirteen keys above from
.claude/rust-sdlc.yml. When the profile is missing or unreadable, stop with the escalation block andrecommended_action: run /rs-sdlc-setup on the default branch and commit the result. - Branch on each key before running anything:
framework.openapitrue: run the OpenAPI steps (generate, Spectral, OpenAPI diff). False: skip them and recordSKIPPED: framework.openapi is false.framework.graphqltrue: run the GraphQL steps (generate, GraphQL diff). False: skip them and recordSKIPPED: framework.graphql is false.service.openapi_snapshotorservice.graphql_snapshotnull while its framework key is true: no snapshot is committed yet, so the generate step creates the first one, the diff target finds nothing on the base ref to compare, and the degrade note "first snapshot committed; run /rs-sdlc-setup on the default branch to record its path" is written. Non-null: that path is the file the generate step rewrites and the checks read.- Any of the six target keys that is null: its target is recorded
not run (capability absent)with a degrade note; it is never replaced by a raw command. - The two
quality.*ceilings are read, never changed.
- Resolve the base ref:
git rev-parse --verify <ci.base_ref>. When it fails, fetch the branch it names once (git fetch origin <branch>, where<branch>isci.base_refwithout itsorigin/prefix) and check again; when it still fails, usebase_ref=HEADand write the degrade note "D24: base ref unavailable, diffs compared with HEAD". - Probe the container engine with
docker infobefore Spectral and the diff targets. When it fails, record those targetsnot run (no Docker); they are never fed to the fix loop and the skill endsESCALATED.
Regenerate the snapshots
- Run the target mapped by
make.generate_openapi_specwhenframework.openapiis true and the target mapped bymake.generate_graphql_specwhenframework.graphqlis true, as the verification-workflow skill prescribes (in the background, log undertarget/rust-sdlc/logs/, polled until it exits). - Stage every regenerated snapshot file in the same change as the code that produced it. A snapshot is never edited by hand: a wrong snapshot is fixed in the annotation, the DTO or the GraphQL type, then regenerated.
Snapshot check
- Run the target mapped by
make.validate_contract_snapshotsafter the regenerated snapshots are committed together with the code. The commit belongs to whoever owns it (the invoking command in a loop stage, the person when the skill is used directly); this skill never commits. The target regenerates the snapshots and passes when no snapshot file then differs from the committed one. - On failure, the code changed the contract after the last regeneration, or a regenerated snapshot was not committed: regenerate, stage the snapshot files for the owner's commit, and run the check again after that commit. This is one iteration of the bounded loop.
Spectral bar
- Run the target mapped by
make.validate_openapi_spec(skipped with the OpenAPI steps whenframework.openapiis false). - Count the findings in its log (one line per finding; 0 when it exits 0 and lists none). The row passes when the target exits 0 and the count is within
quality.max_spectral_findings; a target that exits 0 while reporting more findings than the ceiling fails, because the repository's lint flags may be looser than the profile bar. - Fix each finding in the
#[utoipa::path]annotation or the doc comment it comes from (operation id, tag, summary, description, the 2xx response, the problem responses anddefault), regenerate the snapshot and run the bar again.
Breaking-change policy
- Run the targets mapped by
make.openapi_diffandmake.graphql_diff, each withbase_ref=<ci.base_ref>(orbase_ref=HEADafter the D24 fallback), as the verification-workflow skill prescribes. Both exiting 0 is a pass. - When one fails with a breaking change, decide from the plan or story whether the break is intended; when neither says so, treat it as not intended:
- Not intended (a renamed or removed field, a newly required parameter, a narrowed type): restore compatibility in the code, preferring an additive change (a new optional field, a new operation, a deprecated old one kept in place), regenerate the snapshots and run the diff targets again. This is one iteration of the bounded loop.
- Intended: read the labels of the pull request with
gh pr view --json labels --jq '.labels[].name'.
- When the pull request already carries
approved-breaking-change, run both diff targets again withbase_ref=<ci.base_ref> BREAKING_CHANGE_APPROVED=true, which mirrors CI; they print the findings and pass, and the evidence records "breaking change approved by label". - When no pull request exists yet, the label is absent, or
ghcannot read the labels, the change is unapproved: stop with the escalation block,blocking_finding: <diff target> exit 1 — unapproved breaking change, andrecommended_action: a maintainer reviews the break and applies the approved-breaking-change label. Only a human applies that label.
Bounded loop
- Every failed check starts one iteration: read its log, fix the root cause in the code, regenerate, and re-run the failed check, then every applicable check of this skill.
- The iterations count against the invoking stage's guard,
MAX_ITERATIONS=5, and the counter is never reset; used directly, the same cap of five applies. A fifth failed iteration endsESCALATEDwith the escalation block. - A check that cannot run (
not run (no Docker),not run (no toolchain)) and an unapproved intended break are not iterations: they end the skillESCALATEDat once.
Constraints
- Never edit a contract snapshot by hand, and never commit a contract change without its regenerated snapshot.
- Never set
BREAKING_CHANGE_APPROVED=trueunless the pull request already carriesapproved-breaking-change. - Never apply or remove
approved-breaking-change; only a human applies it, and an intended break without it is escalated. - Never "fix" a breaking change by weakening the contract or a check: no deleting an operation from the contract to hide it, no editing the base snapshot, the Spectral ruleset, the lint or diff flags, or the diff scripts.
- Never raise
quality.max_spectral_findingsorquality.max_unapproved_breaking_changes; both are tighten-only ceilings. - Never edit a protected file (the list printed by
cargo xtask config-guard --list); a change that needs one stops with the escalation block andrecommended_action: approved-config-change label and a session restarted with ALLOW_PROTECTED_CONFIG_EDIT=1. - Write values only by profile key; concrete service values appear only in fences marked
# profile-example.
Verification
The resolved calls, as they read for the template's targets and base ref:
make generate-openapi-spec
make generate-graphql-spec
make validate-contract-snapshots
make validate-openapi-spec
make openapi-diff base_ref=origin/main
make graphql-diff base_ref=origin/main
make openapi-diff base_ref=origin/main BREAKING_CHANGE_APPROVED=true
make graphql-diff base_ref=origin/main BREAKING_CHANGE_APPROVED=true
The last two lines run only after the label check of the breaking-change policy found approved-breaking-change on the pull request. The snapshots they read:
.github/openapi-spec/spec.yaml
.github/graphql-spec/spec
Hand-edit check, after the last regeneration: the command below exits 0, which proves that the staged snapshots are exactly the generated ones. <openapi snapshot> and <graphql snapshot> are the values of service.openapi_snapshot and service.graphql_snapshot; leave out a null one.
git diff --exit-code -- <openapi snapshot> <graphql snapshot>
Evidence block, printed once at the end:
skill: api-contract-development
source: repo+plugin | plugin
precedence: <repository rule followed instead of a step of this skill | none>
base_ref: <ci.base_ref value | HEAD (D24)>
snapshots: <regenerated snapshot files | SKIPPED: <reason>>
targets run:
- <target>: passed | failed | not run (<reason>)
spectral findings: <count> (ceiling <quality.max_spectral_findings value>)
unapproved breaking changes: <count> (ceiling <quality.max_unapproved_breaking_changes value>)
breaking-change approval: not needed | approved by label | missing (escalated)
degrade notes: <one per line | none>
iteration: <n>/5
api-contract-development: SUCCESS | SUCCESS-WITH-REPORT | ESCALATED
Reasons in not run (<reason>) come from the closed set capability absent, no Docker, ask rule, no toolchain, skipped by triage, not changed, deferred to PR CI. The verdict is SUCCESS when every applicable check passed and no degrade note exists, SUCCESS-WITH-REPORT when the only notes are absent capabilities, a SKIPPED: line for a false key, a first snapshot or the D24 fallback, and ESCALATED otherwise.
When the verdict is ESCALATED, print the canonical block with the invoking stage's id and exit string:
=== SDLC ESCALATION ===
stage: <stage-id of the invoking command>
iteration: <n>/5
exit_condition: <exit string of the invoking stage, verbatim>
status: NOT MET
blocking_finding: <target + exit code | file:line>
iteration_log: <one line per iteration: "<n>: <what ran> -> <result>">
recommended_action: <the human action, such as the approved-breaking-change label>
=== END ===