Repository navigation
refactor(kap-server): publish shared OpenAPI schemas once under components - #5621
Conversation
…nents The OpenAPI document wrote every schema out in full at each use, so a type returned by many routes, such as the session, was copied into every response. The document now lists each shared schema once under components.schemas and points to it with $ref: - the session, message content and one error envelope per error code keep fixed names; - any other schema that repeats is hoisted automatically and named after the first operation and field path that uses it. The wire format and every route are unchanged. The contract summary and digest inline the references before reading the document, so contract.txt and the pinned digest stay the same.
🦋 Changeset detectedLatest commit: b998157 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 120f5db6fc
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| patchFsAction(paths); | ||
| patchFsDownload(paths); | ||
| patchWorkspaceFsContent(paths); | ||
| shareSchemas(document); |
There was a problem hiding this comment.
Add changesets for the modified libraries
This commit changes the published /openapi.json representation and adds reference-expansion behavior to testkit, but includes no changeset for either library. The repository's gen-changesets policy requires technical entries for library source changes, including internal refactors, and neither package is ignored in .changeset/config.json. Add changesets for @moonshot-ai/kap-server and @moonshot-ai/testkit, documenting the OpenAPI reference layout and the corresponding contract-tool support so these changes enter the release/versioning workflow.
AGENTS.md reference: AGENTS.md:L169-L170
Useful? React with 👍 / 👎.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Requirement or Bug
/openapi.jsonwrote every schema out in full at every place it was used. A type returned by many routes was copied into each response: the session, message content, and the error envelope for each error code. The committedpackages/kap-server/schema/openapi.jsonwas 41,838 lines. Any change to a shared type, such as a new session field, added hundreds of lines per route that returns it. This PR publishes each shared schema once, which cuts the file to 21,000 lines and keeps future diffs to the place that changed.Bug Reproduction Steps
N/A
Root Cause
N/A
Code Changes
The idea: after the document is built, one pass moves every schema that repeats into
components.schemasand replaces each use with a$ref. The wire format, routes and validation are untouched. Only the document's layout changes, and the contract checks now read through the references, so they see the same contract as before.flowchart LR A[routes and zod schemas] --> B[fastify-swagger document] B --> C[transformOpenApiDocument patches] C --> D[shareSchemas] D --> E["/openapi.json with components.schemas + $ref"] E --> F[contract summary and digest] F -.->|inline $ref first| G[contract.txt and HOST_CONTRACT unchanged]shareSchemasruns last because the earlier patches read inline schemas, for example the error variants of session actions. fastify-swagger caches the transformed document, so the pass runs once per server.z.toJSONSchemaemit$defs$defsinto components. Matching the final document covers every route without touching route code.Session,MessageContentand oneError<Code>envelope per error code. Other repeats get a name from the first operation and field path that uses them, for exampleGetCapabilitiesResponseCapability.$refcosts 3 lines, so sharing a short enum makes the document harder to read without making it smaller.openapi.jsonas one document/openapi.jsonas one document.Reading guide:
packages/kap-server/src/openapi/sharedSchemas.tsis the pass itself: the named schemas, the loop that hoists the outermost repeats and recounts, and how names are built.packages/testkit/src/contract/summary.tsaddsinlineSchemaRefs.summarizeContractandcontractDigestcall it, so$refdoes not change the field summary or the pinned digest.packages/kap-server/schema/openapi.json, which is regenerated, the one-line call intransforms.ts, and the test updates.Behavior Changes and Affected Users
GET /openapi.jsoncomponents.schemasemptycomponents.schemasand used through#/components/schemas/<Name>references/openapi.jsonand do not follow$ref$reftocomponents.schemasis standard OpenAPI 3No request or response on the wire changes.
packages/kap-server/schema/contract.txtis byte-identical, and the pinnedHOST_CONTRACTdigest inpackages/kap-server/test/apiSurface.test.tsis unchanged. Both are computed with the references inlined, so the API level does not need a bump.Test coverage:
packages/kap-server/test/openapi.test.tschecks that every$refpoints to an existing component, that every component is used, and thatSession,MessageContentandErrorSessionNotFoundare published. The existing assertions on nested schemas read the document with references inlined.packages/testkit/test/contract.test.tschecks that a document using a shared schema gives the same summary and digest as the same document with the schema written out.packages/kap-server/test/apiSurface.test.tskeeps the contract golden and the pinned digest.Checks run
pnpm lint,pnpm run check:duplicationChecklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.