# Contributing

## Setup

```bash
git clone <repo> && cd devworkspace
npm ci
npm run build
npm test
```

Node ≥ 20.11. Windows, macOS and Linux are all supported targets; do not assume `/bin/bash`, `rm`, `cp`, `grep`, `sed` or `chmod` exist. Use Node APIs. Avoid adding runtime dependencies; if you think you need one, open an issue first.

## Before you push

```bash
npm run typecheck && npm run lint && npm test
npm run coverage      # thresholds are enforced
npm run smoke         # builds, packs, installs the tarball into clean dirs, runs the real CLI
```

CI runs the same on Ubuntu, Windows and macOS.

## Architecture rules

- CLI handlers only parse and render. Logic goes in `src/services/*`, which return plain data.
- Persisted data is versioned (`schemaVersion`), validated on read, written atomically, and secret/absolute-path checked.
- External tools go behind `Integration` / `AgentAdapter` interfaces.
- Subprocesses: `utils/proc.run` with argument arrays. Never `shell: true`, never string-concatenated commands.
- Everything that reads repository text goes through `security/files.safeReadText`.
- No mocks or placeholders in production paths. If something cannot be done, detect it and report honestly.

## Adding an integration

See [integrations.md](integrations.md#adding-an-integration).

## Adding an agent adapter

Implement `AgentAdapter` (`src/agents/types.ts`): `detectExisting` and a pure `plan()` that returns `PlannedChange[]` (never writes). Register it in `src/services/agent.ts`. Preserve user content (managed blocks only), report conflicts instead of overwriting, and test CRLF, missing and pre-existing files.

## Adding a schema migration

1. Bump `SCHEMA_VERSION` in `src/config/schemas.ts` only for a *breaking* persistent-format change.
2. Add a `Migration { from, to, migrate }` for each affected kind (`project`, `facts`, `commands`, `analysis`, `task`, `session`) in `src/core/migrations.ts`. Migrations are pure functions.
3. Add tests (see `tests/integration/flows.test.ts` "schema versioning"): old file → new schema, backup created, idempotent, newer-schema refusal.
4. Document the change in `CHANGELOG.md` under **Breaking**.

## Adding a task type or routing rule

Edit `src/tasks/routing.ts` (`WORKFLOWS`, `classifyRequest`), add scenario rows to `tests/unit/routing.test.ts`, and update `docs/routing.md` (a test checks the table matches).

## Commits and PRs

Small, focused commits with imperative subjects. PRs should describe the behaviour change, list the tests added, and mention any docs/changelog updates. Do not include real secrets, even in fixtures; build credential-like values at test time.

## Release process

Versioning is semver. Workspace schema changes need a migration and are at least a minor release (major if downgrades break).

1. Update `CHANGELOG.md` (move *Unreleased* under the new version) and `package.json` version.
2. `npm run release:check`. This is the gate; it runs everything and **never publishes**.
3. Commit and tag `vX.Y.Z`.
4. Trigger the **Release** workflow manually (`workflow_dispatch`) or push the tag with the repository configured for npm trusted publishing (OIDC, provenance enabled). One-time npm setup: add the GitHub repo + workflow filename as a trusted publisher in the package's npm settings, or provide an `NPM_TOKEN` automation token as a repository secret.
5. Manual fallback, only with explicit owner authorisation: `npm publish --access public --provenance` from a clean tag checkout.
