Skip to content

Document the HTTP API with OpenAPI 3.1 and test it against the service - #13

Merged
4waan merged 10 commits into
mainfrom
feat/openapi-http-api
Nov 11, 2026
Merged

4waan merged 10 commits into
mainfrom
feat/openapi-http-api

Conversation

@4waan

@4waan 4waan commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Summary

This PR makes the local service's HTTP API a documented integration surface:

  • schemas/openapi.json is an OpenAPI 3.1 description of every route apps/server answers. It reuses the existing JSON Schemas by $ref, lists the error responses each operation can return, and states the Host/Origin security model, the queue limit and the job lifecycle. Its examples are responses recorded from a live service.
  • The service serves the description at GET /api/openapi.json as one self-contained document.
  • docs/api.md documents the API for people, with curl examples.
  • The Python client has one method per operation, and its README documents each method.
  • Tests tie the description to the service in both directions.

What is changed (precise)

No Lean, Rust or refinement code changed, and no proof or claim changed. refinement/STATUS.md and claims.json are therefore unaffected.

packages/schema

  • New SchemaRegistry (src/jsonschema.ts): a dependency-free JSON Schema draft 2020-12 validator. It implements the keywords used in schemas/*.json and openapi.json:
    • $ref across files, resolved against $id
    • type, enum, const
    • object, array, number and string bounds
    • allOf, anyOf, oneOf, not
    • if, then, else
  • It fails closed: any other keyword throws UnsupportedSchemaError instead of being ignored. format is an annotation only.
  • assertSupported(uri) walks a whole schema, checking that every keyword is implemented and every reference resolves.
  • resolveJsonPointer takes an RFC 6901 pointer in its plain string form and does not percent-decode it ('%' is an ordinary character). SchemaRegistry percent-decodes the URI fragment of a $ref once before resolving it, and reports a malformed fragment (such as #/a/100%) as UnsupportedSchemaError.
  • Why a new helper: the package's existing helpers validate requests only, and there was no response or schema validator. This one adds no dependency.

schemas/

  • New error.schema.json for the {error:{code,message,path?}, errors?} body of every error response. Its description lists every error code.
  • job.schema.json gains $defs/summary and $defs/envelope, the {job:{id,kind,state,origin}} body returned by submission and stop. This is additive.
  • New openapi.json with seven API operations and GET / (the web workbench):
    • getCapabilities, getOpenApi, submitAnalysis, submitExperiment, getJob, stopJob, getJobBundle
    • responses 200/202, 400, 403, 404, 409, 413 (with Connection: close), 415, 429 (with Retry-After: 30) and 500, as the handler returns them
    • the fallback for unmatched requests, stated in info.description: a GET or POST under /api/ that matches no operation (an unknown path, or a described path with a method it does not list, such as GET /api/analyses) answers 404 not-found; any other method, HEAD and OPTIONS included, answers 405 method-not-allowed; no response sends an Allow header
    • the Location and Content-Disposition headers
  • Every example except the fixed 500 body was recorded from a live cryptificate serve built from this branch (release engine, Lean library built locally). The recorded run includes a completed analysis in which the mis-wired AES circuit is refuted with a witness, plus queued, running and cancelled jobs, 429, 409 and every 4xx.

apps/server

  • API_ROUTES (in server.ts) lists every API operation, and the handler now dispatches through matchApiRoute. Two behaviours changed:
    • An unknown job id on a path no route matches now answers "no such endpoint" instead of "no such job". The status is 404 either way, and DELETE or PUT on a job path is now 405.
    • A bundle request for a job that ended without a report now answers 409 with "the job ended before it produced a report". It used to say "the job has not finished".
  • New GET /api/openapi.json (openapi.ts). It reads schemas/openapi.json once and returns it bundled: referenced schema files are embedded under components/schemas without $id or $schema, and every $ref is rewritten to a local pointer. A checkout without the file answers 404 not-found.

python/cryptificate

  • Client.openapi().
  • CryptificateError.retry_after, set on 429.
  • OPERATIONS, which maps each operationId to its method.
  • python -m cryptificate job|stop|openapi commands.
  • The README now has a table of methods, the error fields and every command.

Docs

  • docs/api.md covers:
    • endpoints
    • security model: binding, the Host check against DNS rebinding, same-origin writes, --allow-origin, no CORS, the response headers
    • submission
    • job lifecycle and step names
    • polling
    • limits and the queue (the only rate limit)
    • bundle download
    • the error table (its 404 and 405 rows state the fallback rule above)
  • README.md and docs/architecture.md link it.

Tests run

  • npm ci: ok.
  • npm run build && npm test (whole workspace):
    • schema: 22 passed, 0 failed (8 new)
    • server: 52 passed, 5 skipped, 0 failed (10 new; the 5 skipped are the existing integration tests that need CRYPTIFICATE_INTEGRATION=1)
    • web: 53 passed
  • python3 -m unittest discover -s tests (in python/): 15 passed (6 new).
  • What the new server test apps/server/test/openapi.test.ts checks:
    • Described /api/ operations equal API_ROUTES (method, path, operationId) in both directions.
    • Every path parameter is declared.
    • Every OpenAPI reference resolves.
    • Every schema in the document passes assertSupported.
    • Every request and response example validates against its schema. Request examples also pass validateAnalysisRequest / validateExperimentRequest.
    • Every (status, code) that sendError/HttpError produces in server.ts is documented.
    • The bundled document is self-contained, and its examples validate against it alone.
    • An in-process service (fake engine and Lean) answers every operation with a status and body the description allows. The test asserts that each operation/status pair it targets was actually exercised (including 400, 403, 404, 413, 415, the 202 Location header and the bundle Content-Disposition). The 413 case sends a declared Content-Length over the limit, so it does not depend on upload timing.
    • A second in-process service uses a pipeline that holds the run slot until stopped, so a second job is deterministically queued. It checks, against the description and unconditionally: 409 not-ready "the job has not finished" for a running and a queued job; 429 queue-full with Retry-After: 30 for both submission operations; a stopped queued job reads cancelled with no report and its bundle answers 409 "the job ended before it produced a report"; a stopped running job keeps its partial report and its bundle answers 200.
    • Methods the description omits on described /api/ paths: GET and POST answer 404 not-found; PUT, DELETE, PATCH, HEAD and OPTIONS answer 405 method-not-allowed; none sends Allow.
  • Mutation check: deleting an operation, corrupting an example and removing a documented status made 4 of these tests fail. Changing the "ended before it produced a report" message in the built server made the queued/cancelled test fail.
  • packages/schema/test/jsonschema.test.ts checks pointers with % in keys (/a%20b, /100%) resolve literally, that $ref fragments are percent-decoded once (#/$defs/100%25), and that a malformed fragment throws UnsupportedSchemaError.
  • One-off cross-checks with independent tools, which are not dependencies of the repository:
    • openapi-spec-validator accepts both schemas/openapi.json (with its file references) and the served bundle as OpenAPI 3.1.
    • Python jsonschema (Draft 2020-12) validates all 59 examples in the served bundle against their schemas, with 0 failures; a negative control is rejected.
  • Live run: cargo build --release -p cryptificate-cli -j2, lake build Cryptificate (only Cryptificate modules were built), then node apps/server/dist/src/cli.js serve. The curl workflow in docs/api.md (submit, poll, print claims, curl -OJ the bundle, stop) was run against it.
  • Not run: cargo test --workspace, lean/scripts/check_examples.py and refinement checks. No Rust, Lean or refinement file changed, and disk space was tight.

Scope and limits

  • The OpenAPI document describes the API as it is. It adds no authentication, CORS or per-client rate limiting; the queue (one running job, two waiting, then 429) is the only limit.
  • Recorded examples are fixed in the file. If response shapes change, the example and live-conformance tests fail and the examples must be recaptured; no recording script is committed.
  • SchemaRegistry is a subset validator by design:
    • unevaluated*, dependent*, contains, $dynamicRef and embedded $ids below a document root are not supported, and using them throws.
    • format is not asserted.
  • The route-parity test compares the description with API_ROUTES. It relies on the handler dispatching only through that table, which this PR makes true. The error-code check reads server.ts with a regular expression over literal sendError/HttpError calls, so a code built dynamically would not be seen.
  • GET / is described as the workbench's entry point only. Other static paths and the client-side route fallback are explained in prose, because OpenAPI has no wildcard paths.

Generated by Claude Code

… schemas

SchemaRegistry implements the draft 2020-12 keywords used in schemas/ and
fails closed on any other keyword. Tests validate every recorded report,
example design and example program against the published schemas.
error.schema.json describes the body of every error response; job.schema.json
gains the job summary and the {job} envelope returned on submission and stop.
API_ROUTES lists every API operation and the handler dispatches through it,
so the routes can be compared with the API description. GET
/api/openapi.json serves schemas/openapi.json bundled into one document:
the referenced JSON Schemas are embedded under components/schemas and every
reference is rewritten to a local pointer. A bundle request for a job that
ended without a report now says so instead of reporting that the job has
not finished.
schemas/openapi.json describes every route the service answers, reuses the
JSON Schemas by $ref, lists the error responses each operation returns and
states the Host/Origin policy, the queue limit and the job lifecycle.
Examples are responses recorded from a live service.

The test checks the description against API_ROUTES in both directions,
validates every example against its schema, checks that every error code
the handler can produce is documented, and validates live responses of an
in-process service against the description.
Adds Client.openapi(), CryptificateError.retry_after for 429 responses, the
job, stop and openapi commands, and OPERATIONS mapping each operationId to
its method. New tests check that mapping against schemas/openapi.json and run
the client against the responses recorded there. The README documents every
method, the error fields and every command.
docs/api.md covers the endpoints, the Host/Origin security model, the job
lifecycle, polling, the queue limit and the other limits, bundle download
and the error codes, with curl examples. The README and the architecture
page link it, and the description states which jobs survive a restart.
resolveJsonPointer takes an RFC 6901 pointer in its plain string form,
where '%' is an ordinary character. SchemaRegistry.resolve now decodes
the URI fragment of a $ref before resolving it and reports a malformed
fragment as UnsupportedSchemaError.
A pipeline that holds the run slot keeps a second job queued, so the
409 not-ready answers ('the job has not finished' and 'the job ended
before it produced a report') and 429 queue-full are checked against
the OpenAPI description on every run. The 413 check now sends a
declared length over the limit, and both live tests assert every
operation/status pair they are meant to exercise. Local $ref fragments
are percent-decoded before pointer resolution.
A GET or POST under /api/ that matches no route answers 404, also on a
documented path with the other method; every other method, HEAD
included, answers 405, and no Allow header is sent. The error table in
docs/api.md now says this, matching the OpenAPI info text, and the
live test checks each case.
Resolve the overlap with the branches merged before it, as in the
release/v0.2.0 integration.
@4waan
4waan merged commit f8690c4 into main Nov 11, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants