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.md

GitHub

API Contract Development

Profile keys consumed

  • framework.openapi — true selects the OpenAPI snapshot, Spectral and OpenAPI diff steps; false skips them
  • framework.graphql — true selects the GraphQL snapshot and GraphQL diff steps; false skips them
  • service.openapi_snapshot — the committed OpenAPI snapshot; null means none is committed yet
  • service.graphql_snapshot — the committed GraphQL snapshot; null means none is committed yet
  • make.generate_openapi_spec — the target that writes the OpenAPI snapshot from the code
  • make.generate_graphql_spec — the target that writes the GraphQL snapshot from the code
  • make.validate_contract_snapshots — the target that fails when a committed snapshot differs from the generated one
  • make.validate_openapi_spec — the target that lints the OpenAPI snapshot with Spectral
  • make.openapi_diff — the target that reports breaking OpenAPI changes against a base ref
  • make.graphql_diff — the target that reports breaking GraphQL changes against a base ref
  • quality.max_spectral_findings — the ceiling on Spectral findings
  • quality.max_unapproved_breaking_changes — the ceiling on unapproved breaking changes of both diff targets together
  • ci.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 by make.validate_openapi_spec against quality.max_spectral_findings, and Q14 is the targets mapped by make.openapi_diff and make.graphql_diff against quality.max_unapproved_breaking_changes, compared with ci.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-change on the pull request; CI then sets BREAKING_CHANGE_APPROVED=true for the diff jobs. The label name is confirmed in docs/repository-settings.md, which wins over this skill.
  • The skill runs inside a loop stage after the triage recorded EXECUTE for 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

  1. Every applicable snapshot was regenerated by its mapped target in the same change as the code and was never edited by hand.
  2. The target mapped by make.validate_contract_snapshots passed.
  3. The target mapped by make.validate_openapi_spec exited 0 and reported no more findings than quality.max_spectral_findings.
  4. The targets mapped by make.openapi_diff and make.graphql_diff ran with base_ref=<ci.base_ref> and their unapproved breaking changes together are within quality.max_unapproved_breaking_changes; an approved run used BREAKING_CHANGE_APPROVED=true only because the pull request already carried approved-breaking-change.
  5. An intended breaking change without that label ended ESCALATED with the label named; the label was never applied or removed by this skill.
  6. The evidence block and the verdict line are printed.

Steps

Profile resolution

  1. Read the thirteen keys above from .claude/rust-sdlc.yml. When the profile is missing or unreadable, stop with the escalation block and recommended_action: run /rs-sdlc-setup on the default branch and commit the result.
  2. Branch on each key before running anything:
    • framework.openapi true: run the OpenAPI steps (generate, Spectral, OpenAPI diff). False: skip them and record SKIPPED: framework.openapi is false.
    • framework.graphql true: run the GraphQL steps (generate, GraphQL diff). False: skip them and record SKIPPED: framework.graphql is false.
    • service.openapi_snapshot or service.graphql_snapshot null 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.
  3. 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> is ci.base_ref without its origin/ prefix) and check again; when it still fails, use base_ref=HEAD and write the degrade note "D24: base ref unavailable, diffs compared with HEAD".
  4. Probe the container engine with docker info before Spectral and the diff targets. When it fails, record those targets not run (no Docker); they are never fed to the fix loop and the skill ends ESCALATED.

Regenerate the snapshots

  1. Run the target mapped by make.generate_openapi_spec when framework.openapi is true and the target mapped by make.generate_graphql_spec when framework.graphql is true, as the verification-workflow skill prescribes (in the background, log under target/rust-sdlc/logs/, polled until it exits).
  2. 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

  1. Run the target mapped by make.validate_contract_snapshots after 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.
  2. 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

  1. Run the target mapped by make.validate_openapi_spec (skipped with the OpenAPI steps when framework.openapi is false).
  2. 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.
  3. 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 and default), regenerate the snapshot and run the bar again.

Breaking-change policy

  1. Run the targets mapped by make.openapi_diff and make.graphql_diff, each with base_ref=<ci.base_ref> (or base_ref=HEAD after the D24 fallback), as the verification-workflow skill prescribes. Both exiting 0 is a pass.
  2. 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'.
  3. When the pull request already carries approved-breaking-change, run both diff targets again with base_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".
  4. When no pull request exists yet, the label is absent, or gh cannot read the labels, the change is unapproved: stop with the escalation block, blocking_finding: <diff target> exit 1 — unapproved breaking change, and recommended_action: a maintainer reviews the break and applies the approved-breaking-change label. Only a human applies that label.

Bounded loop

  1. 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.
  2. 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 ends ESCALATED with the escalation block.
  3. A check that cannot run (not run (no Docker), not run (no toolchain)) and an unapproved intended break are not iterations: they end the skill ESCALATED at once.

Constraints

  • Never edit a contract snapshot by hand, and never commit a contract change without its regenerated snapshot.
  • Never set BREAKING_CHANGE_APPROVED=true unless the pull request already carries approved-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_findings or quality.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 and recommended_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 ===