Programmatic API
You can run policies from your own code as well as from the CLI. repopo has three entry points:
| Import | Contents |
|---|---|
repopo/api | Policy types, makePolicy() and the policy definers, adapters, runPolicies, and PolicyRunner. |
repopo/policies | The built-in policies and their configuration types. |
repopo | Everything in repopo/api, plus the CLI’s run function from OCLIF. |
Import from repopo/api in programs so you don’t load the CLI entry point.
Running policies
Section titled “Running policies”runPolicies runs configured policies and returns a promise of the results:
import { makePolicy, runPolicies } from "repopo/api";import { NoJsFileExtensions } from "repopo/policies";
const results = await runPolicies({ policies: [makePolicy(NoJsFileExtensions)], root: process.cwd(), files: ["src/main.mjs"], fix: false,});
const failed = results.errors.length > 0 || results.findings.some((finding) => finding.fixStatus !== "fixed");If your code already uses Effection, use PolicyRunner. It takes the same options and returns the same results: yield* new PolicyRunner(options).run() inside an operation.
Options
Section titled “Options”| Option | Description |
|---|---|
policies | Policies configured with makePolicy(). |
root | Absolute path of the repository root. |
files | Repo-relative paths that file policies check. Defaults to inventory. |
inventory | The full repository list, as string paths or annotated records. See below. |
exclude | Patterns that exclude repo-relative paths from every policy, like excludeFiles in a configuration file. |
fix | true to apply fixes, like check --fix. |
concurrency | Maximum files processed at once in read-only runs. Defaults to 8. Fixes always run one file at a time. |
signal | An AbortSignal to cancel the run. |
Selected files and the repository inventory
Section titled “Selected files and the repository inventory”The two file lists do different jobs:
filesis the selection that file policies check.inventoryis the full list of repository files, filtered by each repository/change policy’s optionalmatchand exclusions.
When any repository or change policy is configured, you must pass inventory. repopo never uses files as a substitute: a repository policy that checks for a missing README would give the wrong answer if it saw only the changed files. These policies run once each, even when the selection, inventory, or regex match is empty.
An array is a fixed snapshot. When fixes can create or remove files, pass an async function instead; repopo calls it again after file fixes and before each repository policy:
await runPolicies({ policies, root, files: changedFiles, inventory: async () => listAllRepositoryFiles(), fix: true,});listAllRepositoryFiles stands for your own function, such as one that runs git ls-files. The CLI loads the full Git inventory this way regardless of --stdin.
Change-aware repository policies
Section titled “Change-aware repository policies”Pass the full inventory with a status on each file:
import { collectRepositoryInventory, makePolicy, runPolicies } from "repopo/api";import { RequireCompanionChanges } from "repopo/policies";
const inventory = await collectRepositoryInventory(root, { mode: "pr", base: "main",});const results = await runPolicies({ root, files: [], inventory, policies: [ makePolicy(RequireCompanionChanges, { config: { trigger: /^src\//, companion: /^docs\// }, }), ],});The collector accepts { mode?, base? }; an explicit base overrides CI target information. The CLI uses CI before its configured base. The collector returns RepositoryFile[]: unchanged files plus added, modified, deleted, type-changed, and renamed entries. Each entry has path and status; renames also have previousPath. Optional exists records working-tree presence when status alone is not enough: for example, a committed addition can be absent from the working tree after an unstaged deletion.
Repository handlers receive this list as entries. Existing handlers can keep using files, the derived string-path view of files present in the working tree. Legacy string inventories remain supported and produce unchanged entries. File selection stays separate. Absent inventory entries are not used as the default selection; an explicit files selection retains its existing behavior.
Set scope: "changes" to request Git statuses in the CLI. Both "repository" and "changes" use RepositoryPolicyDefinition; no separate handler or configuration API is needed. Programmatic callers supply annotated inventory themselves; change policies require a status on every entry. An empty inventory is valid.
An optional match regex filters entries before either handler runs, matching either rename endpoint without discarding its metadata. Omit it to receive the full eligible inventory. The files view derives from those same matching entries, and the shared inventory remains unchanged for other policies.
The runner copies and freezes the initial inventory before handlers run. An inventory loader can refresh live paths between fixes, but statuses stay fixed for the run: files created by fixes appear as unchanged and cannot satisfy a companion-change requirement. Global and per-policy exclusions filter entries by path, including deleted entries, just as they filter files.
Results
Section titled “Results”The results have four parts:
| Field | Contents |
|---|---|
findings | Every finding, including fixed ones. Each has the policy name, the policyId, the scope (file, repository, or changes), and a file (file policies) or optional files (repository/change policies). |
errors | Execution errors: a policy threw or broke the result contract. Each has the phase ("handle" or "resolve"), the kind ("exception" or "contract"), a message, and the original cause. |
skipped | Work that didn’t run, with a reason: "excluded", "file-fix-error", or "repository-fix-error". |
perfStats | Counts and per-policy timings in milliseconds. |
A run has failed if there are any errors or any finding whose fixStatus isn’t "fixed". Skipped work because of exclusions isn’t a failure.
Error handling
Section titled “Error handling”- Invalid input, such as a bad exclusion pattern or a policy missing required configuration, throws before any policy runs.
- In read-only runs, execution errors are collected and the run continues.
- In fix runs, an error stops the remaining work on that file, but other files continue and repository/change policies are skipped. An error in either scope skips the repository/change policies after it.
- Cancelling with
signalreleases resources before the run rejects. Fixes that were already written aren’t rolled back.
Standalone resolvers
Section titled “Standalone resolvers”A file policy can provide its fix as a separate resolver instead of fixing in the handler. See Standalone file resolvers.
See the API reference for every exported type and function, and Migrate to the 1.0 API if you’re updating code written against an earlier version.