Skip to content
GitHub

DevelopOther

@holtkeeper/api-surface

The api-surface analyzer and analysis kind: reads the public API of every npm package and Go package in a checkout (each entry point, every exported name with its declaration and doc comment) so agents and people can look it up without reading the sources. Run on release tags it keeps one record per version; the server serves the records to agents over MCP and diffs them between versions. Purely syntactic: npm packages are read with the oxc parser, Go packages with the line scanner of @holtkeeper/go-source. Nothing is installed, built or type-checked, no Go toolchain is needed, and it works on TypeScript sources and published declaration files alike.

  • apiSurfaceAnalyzer — Analyzer<ApiSurfaceData, ApiSurfaceOptions>, kind api-surface, scope repo, needs checkout, appliesTo: apiSurfaceApplies. Options (apiSurfaceOptionsSchema; lists also accept a comma-separated string, as the flags pass them):

    • packages: string[] (--packages) — glob patterns of package names to keep, e.g. @acme/*; every package when empty.
    • exclude: string[] (--exclude) — glob patterns of package folders to skip, e.g. examples/*.
    • includePrivate: boolean — include npm packages with "private": true (default true: workspace packages of a monorepo are often private yet imported by its apps).
    • includeInternal: boolean — include Go packages under internal/, marked private: true (default true, so agents working inside a service see its packages).

    packages and exclude apply to Go packages too (import paths and folders relative to the checkout).

  • apiSurfaceApplies — the analyzer’s applicability: a root package.json, or hasGoModule(). hasGoModule(depth = 4) applies with a root go.mod or go.work, or, when the forge client can list the tree (listFiles), a go.mod at most depth folders deep outside vendor/, testdata/, node_modules/ and hidden folders; without listFiles a repository whose only go.mod is nested does not apply.

  • apiSurfaceAnalysis — the AnalysisKind (schema: apiSurfaceDataSchema, summarize, toReport); apiSurfaceReport(data, options) builds the report directly. Metrics: packages, exports, documented share (better: 'up'), deprecated, with Go modules goImportable, goInternal and goMain package counts, and, against the previous record, removed exports. Sections: packages (with an Ecosystem column when there are Go packages, and the “internal only” line for a Go service), changes since the previous record (with semver warnings), Go modules (importable, internal and main package counts per module), undocumented exports, unreadable files.

  • branchApiChanges(options) — public API changes of a merge request against its base commit, as Markdown for a review prompt; nothing stored (below).

  • extractApi({ dir, packages?, exclude?, includePrivate? }): Promise<ApiPackage[]> — the npm extraction.

  • extractGoApi({ dir, includeInternal?, packages?, exclude?, versions?, resolveVersions?, depth? }): Promise<{ packages, modules }> — the Go extraction (below); modules are ApiGoModules.

  • goModuleVersions(tags, modules, ref?) — module directory → version from git tags; tagsAtHead(runner, dir, log?, signal?) — git tag --points-at HEAD through a CommandRunner, empty on failure.

  • findPackages(dir), resolveEntry(packageDir, target) and expandWildcard(packageDir, wildcard) — package discovery and entry point resolution (below).

  • diffPackage(before, after): PackageDiff and diffApi(before, after): PackageDiff[] — differences between versions; versionBump(from, to) and normalizeSignature(signature) are the pieces they use. declaredMembers(signature) — top-level member names of a declaration; for a Go struct or interface type its fields (embedded types by type name) and methods (moved here from @holtkeeper/usages).

  • renderPackage(pkg, options), renderSymbol(symbol, { docs?, ecosystem? }), renderDiff(diff) — Markdown for LLM agents. Go packages get a line on how to import them (import "path", names as pkg.Name, internal packages say who may import them) and go code fences; npm ones ts. fenceLanguage(ecosystem) names the fence; importPath(name, subpath) gives the specifier code imports an entry with (for a Go module and a ./sub/pkg subpath, the package’s import path).

  • goInternalOnly(data) — the “internal only” line for checkouts whose Go modules have no importable package, undefined otherwise.

  • Model: ApiSurfaceData { packages, go? }, ApiPackage { name, version?, description?, private, dir, ecosystem?, module?, entries, problems? } (ecosystem absent means npm), ApiGoModule { path, dir, version?, packages, internalPackages, mainPackages }, ApiEntry { subpath, file?, symbols, truncated? }, ApiSymbol { name, kind, signature, doc?, deprecated?, file, line }; apiTotals(packages), documentedPercent(totals), apiSymbolKinds, isApiSymbolKind(value).

branchApiChanges({ dir, baseSha, changedFiles, runner, signal?, maxChars?, consumed? }): Promise<string | undefined> computes, inside a review run, how a merge request changes the public API, and returns a Markdown section for the review prompt starting with branchApiHeading (## Public API changes (computed: base → head)). Nothing is stored: no analysis record is written, both sides are extracted on the fly.

  • Which packages — only those containing a changed file (dir is the head checkout): an npm package counts for a changed file in its folder (the deepest package folder wins), a Go package for a changed non-test .go file in its directory. Packages without an entry point are left out, as are packages consumed(name, ecosystem) rejects when the filter is given. With nothing left it resolves to undefined without running git.
  • Base side — a detached git worktree of baseSha in the system temp folder (git worktree add --detach, through the CommandRunner), from which the same packages are extracted. The worktree is always removed afterwards (git worktree remove --force, the temp folder deleted, git worktree prune), also on errors and aborts.
  • Output — per package with differences: name, ecosystem, folder, removed / changed (old → new declaration, one line each) / deprecated / added exports with file:line (base location for removals, head location otherwise), the release it needs (major when exports were removed or declarations changed, since a changed declaration may break callers; minor when exports were only added or deprecated) and, for npm, whether the package.json version change is enough. Go versions come from tags and are not compared. The section ends with an instruction to check usages of removed or changed exports, stays within maxChars (default 6000) and says “… N more changes” when cut.
  • Failure — undefined when nothing differs, the base commit cannot be checked out or extraction fails; it never rejects, except with the abort reason when signal aborts (checked between steps).
  1. Packages — the root package.json plus every workspace that workspaces (array or { packages }) or pnpm-workspace.yaml lists, negated patterns excluded; packages without a name are skipped.
  2. Entry points — each subpath of exports (./package.json skipped), conditions tried in the order types, typings, import, module, node, default, require; without exports, types/typings/module/main, then src/index.ts, index.ts. A target resolves to the file itself when it is TypeScript, else a declaration next to it, else the source a build would have compiled it from (dist/x.js → src/x.ts, then x.ts or x.d.ts at the package root for rootDir: "." builds; also lib, build, out, esm, cjs), else the JavaScript file. Wildcard subpaths ("./*": "./dist/*") count only when no other entry point resolves: each module file, or folder with an index, in the target’s folder or its source folder becomes an entry (./usePrevious); folders without an index are searched two levels deep (./desktop/Button), skipping tests, stories, configs, index and names starting with . or _, at most 200. A folder mapping that repeats an entry point of the same name (./services/ next to ./services) is dropped (withoutFolderTwins(entries), exported; the server also applies it to records stored before). Packages with no entry point that resolves to a file are left out, e.g. an app without exports or a library whose build output is missing and whose sources live elsewhere.
  3. Exports — declarations exported in place, export { a as b }, export { x } from, export * from (explicit exports win over star exports, default is never re-exported by a star), export * as ns from (a namespace listing its names) and export default / export =. Relative specifiers are followed across files (./a.js finds ./a.ts, folders find index); re-exports of other packages stay as reexport entries naming the package. Cycles are cut.
  4. Declarations — functions and overloads without bodies, classes without method bodies, private members or initializers (member doc comments kept as one line), interfaces, type aliases, enums and namespaces in full, constants as const name: Type, const f = (a: T): R => … or a short initializer. Lint directive comments (// oxlint-…, // eslint-…, // @ts-…) are dropped; the doc comment is the /** */ block right before the declaration, even with such a comment in between. @deprecated text becomes deprecated.
  5. Caps — 1000 symbols per entry point (the rest counted in truncated), 3000 characters per declaration, 1500 per doc comment, so one record stays a few hundred kilobytes for a large monorepo.
  1. Modules — discoverGoModules from @holtkeeper/go-source: the use folders of a root go.work, else every go.mod up to 4 folders deep (skipping vendor, testdata, node_modules and hidden folders).
  2. Files — each module’s .go files except _test.go, without the subtrees of nested modules, vendor/, testdata/, node_modules/ and ./_ folders, at most 20 000 files per module.
  3. Packages — goModuleApi per module: one ApiPackage per library package directory, named by its full import path, with the single entry ., ecosystem: 'Go' and module set; dir, entry and symbol file paths are relative to the checkout. Methods are Type.Method. Packages under internal/ are listed with private: true (unless includeInternal is off); package main directories are never listed, only counted.
  4. Modules in the record — data.go.modules lists every module found with its importable, internal and main package counts, so a service made of cmd/ and internal/ reads as “internal only” instead of empty. It is absent when the checkout has no Go module.
  5. Versions — git tag --points-at HEAD in the checkout (job checkouts carry every tag), mapped to modules by Go’s rules (goModuleTags): vX.Y.Z releases the root module, dir/vX.Y.Z the module at dir, the highest tag per module wins. When the run’s ref is itself a Go module tag, only the module it releases gets a version. Packages of modules without a matching tag get no version; the analysis never uses the commit as a version. Git runs only when there are Go modules, and a failing git only loses the versions.

Each record describes one ref: a release tag (from the default tag trigger or an analysis backfill, POST /api/analyses/backfill) or the default branch (the weekly api-weekly schedule). diffPackage keys symbols by subpath and name: a missing one is removed, a declaration that differs after normalizeSignature (comments and layout ignored) is changed, a newly @deprecated one deprecated, a new one added. required is the bump the changes call for (major when anything was removed, minor for additions, changes or deprecations); bumped is what the version numbers did, where a minor bump of a 0.x version counts as major. Go pseudo-versions (v0.0.0-20240101000000-abcdef123456) and +incompatible versions are not semver: bumped stays unset for them. PackageDiff.ecosystem is Go for Go packages, so renderDiff fences them as Go. A changed declaration whose declaredMembers lose names lists them in ApiChange.removedMembers and counts as breaking: PackageDiff.breaking is true and required is major, the same as a removal; renderDiff marks the change (breaking) and lists Members removed:. semverWarning is set when exports were removed, or members of a changed declaration, without that bump. A changed declaration may still be compatible (an optional parameter added); the diff shows both versions instead of guessing.