# Contributing ## Layout - `packages/*` — the published packages. `foldkit-agent` is the contract; the adapters depend on it. - `examples/*` — worked examples that import the packages through their published entry points. - `docs/*` — conceptual guides and project/reference notes. Package API reference belongs with the package, not duplicated here. ## Before a commit Run the checks CI runs. `pnpm check` runs all six in sequence: ```bash pnpm format:check pnpm paths:check pnpm typecheck pnpm test pnpm demo pnpm pack:check ``` Format with `pnpm format`, never bare `oxfmt`: bare `oxfmt` reads the repository config too, but also formats the HTML pages, `.mjs` scripts and `.jsonc` files the script's `**/*.{ts,json,yaml}` leaves alone. The performance budgets (CPU, bundle sizes, HTML size, layout shift) run on CI only in the `perf` job, on Linux, and are left out of the test shards and `pnpm e2e` there (`perfTests` in `vitest.shared.ts`); locally they stay in `pnpm test`. The job's summary table lists each reading against its budget. To get the same file locally, set `PERF_REPORT=perf.jsonl` and run `node scripts/perf-summary.mjs perf.jsonl`. ## Workspace resolution Workspace packages resolve to their source through the `foldkit-plus:source` export condition, in Vite, Vitest and the examples, so nothing needs a build to see a change. TypeScript resolves through `paths`, which `pnpm paths` generates from each project's references: after adding a workspace dependency, add the project reference and run `pnpm paths`, rather than editing `paths` by hand. ## Tests `pnpm test` runs two Vitest projects: `unit` (Node and jsdom) and `browser` (files named `*.browser.test.ts`, in headless Chromium at a desktop size), for what needs real layout, focus or selection. The browser project needs Chromium once: `pnpm exec playwright install chromium`. Vitest also runs from a package's own folder, with the same resolution. Every test must be able to fail. Mutate the code under test (invert a guard, drop a branch, return a constant), confirm the relevant test goes red, then revert. A test that passes against broken code is worse than no test. Assert on behaviour, not on restatements of the implementation; repeated setup belongs in a helper. ## Documentation Write for a reader who knows Foldkit and TypeScript but did not participate in the design discussion. A package README should establish these things before becoming an API reference: 1. **What problem does this package solve?** Use application language before internal vocabulary. 2. **When should I use it, and when should I not?** Name the neighboring package when ownership is the deciding factor (`Remote` vs `Sync`, `Mirror` vs `Sync`, Behavior vs Submodel). 3. **Where does it sit in the system?** A small data-flow diagram or one concrete before/after is often worth more than another paragraph of terminology. 4. **What is the smallest useful example?** Show the happy path before advanced hooks, diagnostics, protocol details, or implementation notes. Conceptual guides under `docs/` explain mental models and ownership boundaries; they should not become second copies of package API reference. Worked examples under `examples/` should state what they prove and, where practical, pin their transcript or type-check their README snippets so documentation drift becomes a test failure. Avoid hard-coding a package's current `0.x.y` version into explanatory prose unless the version itself matters. Link to [`docs/releases.md`](./docs/releases.md) for the current matrix, or say that the API is still settling in the `0.x` series. This keeps otherwise-correct READMEs from becoming stale after a release. When changing behavior that a README demonstrates, update the prose/example in the same change. When changing only wording, do not silently broaden the claims an example is supposed to prove. ## TypeScript Examples emit declarations (`emitDeclarationOnly`), so an exported value whose inferred type expands a Foldkit-private alias fails with `TS2742`. Annotate the export with a public type; `examples/sync/src/sync.ts` does exactly that with `SyncContract` and explains why in a comment. Typechecking is split into build and test projects. Each package has a `tsconfig.build.json` (src only) that its consumers reference, so a leaf's `tsc -b` never compiles a referenced package's `test/**`; the package's main `tsconfig.json` keeps `test/**`, which is what the root `pnpm typecheck` uses. `pnpm typecheck` is incremental; `pnpm typecheck:force` (what CI runs) rebuilds every project, which is how to rule out a stale `.tsbuild` masking a change. A package's `build` writes its references' `.tsbuild/build` declarations first (`scripts/declarations.mjs`, unchecked), since tsdown's declaration emitter reads them and writes `any` where one is missing; it then fails if a published declaration is `any` (`scripts/declared-any.mjs`). So any build, `pnpm -r build` or one package's, publishes what a release does. ## Releasing See the "Releasing" section of the [root README](./README.md).