Skip to content

refactor(kap-server): publish shared OpenAPI schemas once under components - #5621

Merged
chengluyu merged 2 commits into
mainfrom
refactor/openapi-shared-schemas
Nov 27, 2026
Merged

chengluyu merged 2 commits into
mainfrom
refactor/openapi-shared-schemas

Conversation

@chengluyu

@chengluyu chengluyu commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

/openapi.json wrote 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 committed packages/kap-server/schema/openapi.json was 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.schemas and 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]
Loading

shareSchemas runs 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.

Decision Choice Not chosen Reason
Where to share Post-process the generated document by exact structural match Give zod schemas ids and let z.toJSONSchema emit $defs Route schemas pass through fastify-swagger, which does not turn $defs into components. Matching the final document covers every route without touching route code.
Names Fixed names for Session, MessageContent and one Error<Code> envelope per error code. Other repeats get a name from the first operation and field path that uses them, for example GetCapabilitiesResponseCapability. Names for every schema written by hand The fixed names cover the types callers look for. Generated names keep the rest shared without a list to maintain.
What is shared Schemas of about 10 lines or more that appear at least twice, checked again after each pass so repeats inside shared schemas are shared too Every repeat, however small A $ref costs 3 lines, so sharing a short enum makes the document harder to read without making it smaller.
One file Keep openapi.json as one document Split it into several files The committed file is a copy of the served document, and clients fetch /openapi.json as one document.

Reading guide:

  • packages/kap-server/src/openapi/sharedSchemas.ts is 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.ts adds inlineSchemaRefs. summarizeContract and contractDigest call it, so $ref does not change the field summary or the pinned digest.
  • You can skim packages/kap-server/schema/openapi.json, which is regenerated, the one-line call in transforms.ts, and the test updates.

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
Layout of GET /openapi.json Every schema written out in full at each use, with components.schemas empty Shared schemas listed once under components.schemas and used through #/components/schemas/<Name> references External agents and scripts that read field definitions from /openapi.json and do not follow $ref None needed: $ref to components.schemas is standard OpenAPI 3

No request or response on the wire changes. packages/kap-server/schema/contract.txt is byte-identical, and the pinned HOST_CONTRACT digest in packages/kap-server/test/apiSurface.test.ts is 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.ts checks that every $ref points to an existing component, that every component is used, and that Session, MessageContent and ErrorSessionNotFound are published. The existing assertions on nested schemas read the document with references inlined.
  • packages/testkit/test/contract.test.ts checks 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.ts keeps the contract golden and the pinned digest.
Checks run
  • kap-server: 1573 passed, 1 skipped
  • testkit: 238 passed
  • kap-server and testkit typecheck, pnpm lint, pnpm run check:duplication

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

…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-bot

changeset-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b998157

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kap-server Patch

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T06:01:02.775240Z b998157 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@b998157
npx https://pkg.pr.new/@moonshot-ai/kimi-code@b998157

commit: b998157

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: b99815734d

ℹ️ 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".

@chengluyu
chengluyu merged commit 4e2b7e8 into main Nov 27, 2026
21 of 29 checks passed
@chengluyu
chengluyu deleted the refactor/openapi-shared-schemas branch November 27, 2026 06:26
@7Sageer 7Sageer mentioned this pull request Nov 27, 2026
6 tasks done
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