diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..ca0667c --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,108 @@ +# Agent guidance + +`mortis.kamino` is an Ansible Collection of hardened roles ([README.md](README.md)). This file is +always loaded; it holds what applies to nearly every task. Rules tied to one directory live in that +directory's `AGENTS.md`, and the pointers below say when to read them. Each rule gives how it is +held: by a control (a named hook or check), by review, or aspirational (checked by nothing). + +## Principles + +1. **Accountability**: every change has a human author who understands it and answers for it; an agent is a tool. +2. **A human decides what is irreversible or shared**: settings, protection, tags, releases, credentials, merges to `main`; a merge is decided case by case or by a standing rule a human reviewed, such as Renovate's automerge policy in `renovate.json`. +3. **Secrets and supply chain**: no secret in the repository, a prompt or a log; dependencies and images are pinned and change through review. +4. **Enforced, not trusted**: a rule that matters gets a control; one without is labelled as such. +5. **Honest reporting**: a pull request says what was verified and what was not. +6. **Simple enough to review**: see the code-style rules below. +7. **Openness and licence**: AI assistance is disclosed; contributions are GPL-compatible and REUSE-clean. + +## Code style + +1. **Existing tool first**: an Ansible module, a lint rule or a tool's own setting before a script. +2. **One job per file, readable in one sitting.** +3. **A comment gives the reason, in a line or two.** +4. **Write for the next reader, not the record**: no incident history, change narration or session notes in code, comments or agent context. +5. **Change only what the task needs**: unrelated cleanup is a separate pull request. + +## Working rules + +- **Do not install a container runtime to run molecule; molecule runs in CI.** + *Reason:* the scenarios run on the repository's own runner; a local runtime is setup nobody maintains. + *Held by:* aspirational, by the maintainer's choice. +- **Every change a consumer can see needs a changelog fragment in `changelogs/fragments/`.** + *Reason:* the changelog and the next version are built from the fragments. + *Held by:* review; the `antsibull-changelog-lint` hook checks a fragment's format, not its presence. +- **Content for a public place stands alone: no tracked file, issue or pull request text or + comment, release note or job output refers to another repository of the maintainer's, its + documents, hosts, network or namespaces, or states host sizing** ([making-a-change.md](docs/contributing/making-a-change.md)). + *Reason:* this repository is public, and a reader without an account must not learn the private + environment from it. + *Held by:* aspirational (no check is cheap for issue text or job logs). +- **Without write access you are an outsider: file an issue from a template in `.gitea/`; a fork or + AGit pull request is closed unmerged and its runs are never approved** ([CONTRIBUTING.md](CONTRIBUTING.md), + [reviewing.md](docs/contributing/reviewing.md)). + *Reason:* a fork controls its own workflow files, so an approved run executes its code here. + *Held by:* aspirational. +- **Agent-written commits and pull requests carry `Assisted-by: [:]`, never + `Co-Authored-By:` for an agent, a session link or any other AI trailer** + ([ai-assistance.md](docs/contributing/ai-assistance.md)). + *Reason:* AI assistance is disclosed (principle 7), and an agent is not an author (principle 1). + *Held by:* review; the exclusion becomes a control when the commit-msg hook that refuses these + trailers lands (#272). +- **On conflict with a shared rule delivered by apm, this repository's `AGENTS.md` wins.** + *Reason:* this repository's rules are reviewed here, for this repository. + *Held by:* aspirational. + +## Verify a change + +- Commit with git, not through the forge's API, so the hooks give feedback before the push (advice: + Lint re-runs the commit-stage hooks, the commit-msg check and gitleaks). +- `pre-commit run --all-files`: the commit-stage hooks on every file. +- `python3 -m unittest discover -s tests/scripts`: when a script changed. +- `git push` runs `ansible-test sanity` on HEAD (the pre-push hook). +- Molecule runs only in CI. + +## Repository structure + +```text +.gitea/ workflows, issue and pull request templates, actionlint config +changelogs/ antsibull-changelog config and fragments +docs/ contributor docs, agent docs, ADRs, image docs +extensions/ image-smoke molecule scenarios for the test images +images/ Dockerfiles of the CI and molecule images +LICENSES/ licence texts (REUSE) +meta/ runtime.yml: the ansible-core floor +roles/ the Collection's roles, each with its molecule scenarios +scripts/ CI and developer scripts +tests/ sanity ignores and the scripts' unit tests +galaxy.yml Collection metadata and build_ignore +renovate.json dependency automation +``` + +- A new role goes in `roles//` with its molecule scenario. +- A new script goes in `scripts/` with its test in `tests/scripts/`. +- A new file outside `roles//` runs every Molecule scenario until it is added to the IGNORED list of + `scripts/select_molecule_scenarios.py`; it belongs there only if no scenario reads it. + +## Before you start + +- Before your first commit in a clone: [docs/contributing/setup.md](docs/contributing/setup.md). +- Before filing an issue or opening a pull request: use the headings of the matching template in + `.gitea/`, never your tool's built-in body; read [docs/contributing/making-a-change.md](docs/contributing/making-a-change.md). +- Before bringing content in from another repository: [docs/contributing/making-a-change.md](docs/contributing/making-a-change.md). +- Before committing or opening a pull request as an agent: [docs/contributing/ai-assistance.md](docs/contributing/ai-assistance.md). +- Before writing code outside `roles/`: [docs/contributing/code-style.md](docs/contributing/code-style.md). +- Before closing a fork pull request, merging someone else's pull request, or running an unmerged + branch you did not write: [docs/contributing/reviewing.md](docs/contributing/reviewing.md). +- Before reading an issue, and before filing, labelling or closing one: + [docs/agents/issue-tracker.md](docs/agents/issue-tracker.md) and [docs/agents/triage-labels.md](docs/agents/triage-labels.md). +- Before naming a domain concept or writing an ADR: [GLOSSARY.md](GLOSSARY.md) and [docs/agents/domain.md](docs/agents/domain.md). +- Before adding a new kind of thing (a plugin, a Collection dependency, a workflow, an image, a + secret) or changing how CI, releases or access work: read the file names in [docs/adr/](docs/adr/) + and the Accepted risks in [SECURITY.md](SECURITY.md). +- Changing a role: read [roles/AGENTS.md](roles/AGENTS.md) first. +- Changing a script or its test: read [scripts/AGENTS.md](scripts/AGENTS.md) first. +- Changing a workflow: read [.gitea/workflows/AGENTS.md](.gitea/workflows/AGENTS.md) first. +- Changing an image or an image-smoke scenario: read [images/AGENTS.md](images/AGENTS.md) first. +- Before writing an ADR: read [docs/adr/AGENTS.md](docs/adr/AGENTS.md) first. +- Before changing `renovate.json`: read its rule descriptions, and test with `renovate --platform=local` + at the version in `.gitea/workflows/renovate.yml`. diff --git a/CLAUDE.md b/CLAUDE.md deleted file mode 100644 index b9173db..0000000 --- a/CLAUDE.md +++ /dev/null @@ -1,178 +0,0 @@ -# CLAUDE.md - -Guidance for Claude Code (claude.ai/code) in this repository. - -## What this is - -`mortis.kamino`: an Ansible Collection (GPL-3.0-or-later, REUSE-compliant) of reusable, -hardened roles. Roles arrive here wave by wave; `docker` is in 1.0.0, `fail2ban` in 1.1.0, `network_interfaces` and `systemd_hardening` in 1.2.0, `system_baseline` in 1.3.0, `ufw` in 1.4.0; 1.5.0 adds no role (new `system_baseline` options); `sshd` in 1.6.0; `user_management` in 1.7.0; `traefik`, with the internal `_binary_update`, in 1.8.0; `gitea` in 1.9.0. - -**Roadmap.** The roles still to come, and the ones that will never be extracted, are listed in -the README section "Roadmap". Context: the maintainer's private environment repository (a -production home lab driven by Ansible) holds every role until it moves here; a role moves only -after its tests exist and the environment has been cut over to `mortis.kamino.` at a -pinned tag. The list is informational, not enforced by any hook. Keep it true by hand: the pull -request that adds a role under `roles/` also removes it from the Roadmap list, and when the list -is empty the section goes too. When something looks wrong or missing there, say so to the -operator; the plan itself is theirs (epic #486 in the environment repository, which you cannot -read from here). - -Fork pull requests from untrusted users are not accepted for now: a fork controls its own -workflow file, so the `if:` guards are a convenience, not a boundary (see the header of -`molecule.yml`). Do not change visibility, repository settings, branch or tag protection, -or create tags: those are the operator's. - -## Working here - -- Set up once per clone: `scripts/dev-setup.sh` (a virtualenv with the pinned tools and git - hooks that find them). The `language: system` hooks run `reuse`, `ansible-lint` and - `antsibull-changelog` by name, so a hook run without that virtualenv on `PATH` fails. -- Agent skills come from holocron through apm: `apm install` (the `apm` CLI is not one of the - pinned tools, and `dev-setup.sh` does not run it) deploys them to `.claude/skills` and - `.claude/agents`, both gitignored like `apm_modules/`. `apm.yml` and `apm.lock.yaml` are the only - tracked files. The entries are unpinned on purpose (see the comment in `apm.yml`); `apm install - --update` moves them. They are tooling, so `galaxy.yml` `build_ignore` keeps them out of the Collection. -- **Commit with git in a clone, never through the Gitea API.** The hooks (REUSE, yamllint, - actionlint, ansible-lint, gitleaks, changelog lint, Conventional Commits) only run in - `git commit`; an API commit skips them and CI finds the problem after the push. The first - hook run builds the Go hook environments and takes minutes: run commits in the background. - `git push` also runs `ansible-test sanity` on HEAD (`scripts/ansible-test-sanity.sh`, the Build - job's check, which ruff and the other hooks do not cover); its first run builds virtualenvs and takes minutes. -- **This repository stands alone.** No tracked file, issue or pull request text or comment, - release note or job output refers to another repository of the maintainer's, its documents, - hosts, network or namespaces, or states host sizing. Every comment states its own reason. When - bringing content in from another repository, before committing, search the incoming diff for - mentions of the source repository, its documents, hostnames, addresses, account namespaces and - sibling paths; rewrite comments so they stand alone; say in the pull request what was searched - for. No control holds this rule, and it has no expiry. -- Feature branch and pull request; `main` and `v*` tags are protected. Never push to `main`. - Releases are the operator's: annotated `vX.Y.Z` tags equal to `galaxy.yml`'s version. -- Conventional Commits. A PR says what was verified and what was not. -- There is no local molecule and no container runtime on the control host: do not install one. - Molecule runs in CI, on the repository's own runner (label `kamino-molecule`). -- Changes that can affect a consumer need a changelog fragment (`changelogs/fragments/`). - New files are covered by `REUSE.toml`; third-party-derived content needs its own annotation. - -## Agent skills - -### Issue tracker - -Issues live in Gitea Issues on `mortis/kamino`, used through the `gitea-issues` skill and the Gitea MCP tools. See `docs/agents/issue-tracker.md`. - -### Triage labels - -The five triage roles map onto the repo's scoped `Status/` and `Reviewed/` labels. See `docs/agents/triage-labels.md`. - -### Domain docs - -Single-context: `GLOSSARY.md` and `docs/adr/` at the root, both created lazily. See `docs/agents/domain.md`. - -## Workflows (`.gitea/workflows/`) - -- Never `pull_request_target`. A fork `pull_request` run gets no secrets and a read-only job token; a same-repo pull request does get the org Actions secrets, so nothing but `image.yml` may reference them. Pass ref, repo and name values to - scripts through `env:`, never interpolate `${{ github.* }}` into a `run:` block. -- Runner labels are `kamino-docker` and `kamino-molecule` only (`.gitea/actionlint.yaml`). Use - the `github.*` contexts, which Gitea aliases; actionlint does not know `gitea.*`. -- Lint and Build run in the `ci` image. After the checkout they compare the pin files with the - checksums baked into the image: a PR that changes `requirements-dev.txt`, `requirements-dev.lock`, `requirements.yml` or - `.pre-commit-config.yaml` takes the slower path that installs the pinned tools and collections. -- Molecule runs on every same-repo pull request, Renovate's included (a new image is tested by its own pin - pull request), with no workflow path filter, so that it can be a required check. Fork pull requests are skipped. - What a pull request runs is chosen by `scripts/select_molecule_scenarios.py`, run from the base branch's copy - (a pull request cannot edit its own rules): only the scenarios of the roles it changes and of the roles - that use them (any file naming `mortis.kamino.`, transitively; a test fails a bare or templated role name in an include, import, play `roles:` entry or meta dependency), none for a diff of - only its IGNORED paths, a failure for a changed role with no scenario, and every scenario for any other file, - for every push to `main`, for manual dispatch and for the nightly run (04:43 UTC, after Renovate's). A new file outside `roles//` runs everything until it - is added to IGNORED, which is right only when no scenario reads it. A new commit cancels the pull request's - running Molecule run, and the step first removes containers a cancelled run left; two runs never overlap - because the runner has a single slot, so raising its capacity needs a lock or a shared concurrency group. -- Every scenario sets `scenario.test_sequence` to molecule's default minus the leading `destroy` (the workflow - clears stale containers first, and the molecule state lives in the job's own `ANSIBLE_HOME`, so a killed run leaves - none behind); copy it into a new scenario. -- Molecule discovers `extensions/molecule/*/` and `roles/*/molecule/*/`. The `image-smoke` - and `image-smoke-2604` scenarios test the molecule images themselves (24.04 and 26.04); a role scenario that - must pass on both takes `image: ${MOLECULE_IMAGE}` or `${MOLECULE_IMAGE_2604}` per platform. - -## Images (`images/`) - -- `ci`, `molecule-ubuntu2404` and `molecule-ubuntu2604`, built by `image.yml` with kaniko on push to `main` (never on - pull requests) and pushed to `git.rkdr.net/mortis/`. -- The molecule images are one Ubuntu release each, by name: the hosts run 24.04 today and 26.04 is the - target, so roles are tested on both. A new release is a new image added by hand (Renovate's major `ubuntu` - updates are disabled everywhere); retire an image when no host runs its release. A new image's - first consumer pin is a hand-made PR with the digest its first build printed; Renovate follows it after. -- Each has a semver `version:` in the workflow's build matrix. A published version tag is never - moved and never rebuilt (the job looks it up in the registry first and ends without building): - bump the version in the PR that changes the image. A new version is pushed as `` and - `latest`; there is no `sha-*` tag, so one digest belongs to exactly one version. -- `ansible-test sanity` ignores live in `tests/sanity/ignore-common.txt`; the Build job copies - it to the file name of the installed core version in its throwaway worktree (a committed - `ignore-.txt` is appended for findings of one version only). -- Consumers pin `image:@sha256:` (`lint.yml`, `build.yml`, `molecule.yml`, - `gitea-release-key.yml`). - A build prints the new digest; moving a pin is a reviewed edit, and every build has a new - digest. -- kaniko runs inside the job container and unpacks the image it builds over it, so each matrix entry names its - job container (`job_image: ubuntu:@sha256:...`) and its release must equal the `FROM ubuntu:` release - of the image's Dockerfile (`check_image_versions.py pins` fails otherwise). Moving an image to a new release is one edit: the Dockerfile `FROM`, the `job_image` - tag and digest, and the version bump. Digest drift within a release is tolerated. A `job_image`-only bump is - not an image input: no version bump, nothing rebuilt; the post-merge Images run only reaches the prepare step - for published versions, so that is its only exercise. -- Image metadata: Gitea's package page shows the description as plain text (no markdown), lists every label, - and links source, documentation and licence; it renders no README. So each Dockerfile carries the same - label block (`org.opencontainers.image.*` plus `net.rkdr.mortis.purpose/contents/usage`), the build adds - `version`, `revision` and `created`, and the readme-style text lives in `docs/images/.md`, which the - `documentation` label links to. `check_image_versions.py pins` fails when a label or that file is missing. - The docs sit outside the image directories on purpose: editing them is not an image input, so it needs no - new version. Editing a label in a Dockerfile is an input and does. -- Each pushed layer must stay below 100 MB: Cloudflare rejects larger request bodies with - HTTP 413. That is why the `ci` image deletes the Go toolchains from the hook cache and the unused - `libpython*.so` of its three baked interpreters (the `/opt/python` layer was 99 MB compressed with them, - ci 0.2.26 failed to push). The build prints the compressed size of each layer directory; the registry - stores about 9% more, so read that line before adding an interpreter or a hook environment. -- `image.yml` is the only workflow that may reference the org Actions secrets (the registry push credential). Never print or use them anywhere else. - -## Dependency automation (Renovate) - -`renovate.json` is run by `.gitea/workflows/renovate.yml` (daily cron and `workflow_dispatch`) on the -`kamino-docker` runner, as a Kamino-only bot user: this repository's jobs run only on its own -runners, and no credential from anywhere else is used here. Never reuse another Renovate credential here. - -- **Policy:** minor and patch bumps automerge, majors are manual; the Lint and Build checks are the gate. - A new Ubuntu release is a deliberate edit, never a bump. The `job_image` of each matrix entry in `image.yml` - never automerges: that job holds the registry push credential. kaniko is pinned by layer digests in - `image.yml` and updated by hand. `prHourlyLimit` is 5 (default 2): an `ubuntu` digest move makes two PRs - per release (base image and job container), and two runs in a row spent the whole default limit on - them, so the hook and image pin PRs waited. -- **Two-PR loop for an image input** (`requirements-dev.txt`, `requirements-dev.lock`, `requirements.yml`, `.pre-commit-config.yaml`, `images/**`): PR 1 - carries the input change plus the PATCH bump of the image `version:` in `image.yml` - (`scripts/bump_image_versions.py []`; `check_image_versions.py bump` then passes; Renovate runs it - as a post-upgrade task). The merge builds the image on `main`. PR 2 moves the consumers' pins to the new - digest (Renovate's pin-follow rule for the `mortis/*` images; a major version stays manual). -- **One Python for the lock, a clone and the ci image:** `.python-version` (a minor version) is the source. `scripts/dev-setup.sh` and `scripts/lock-requirements.sh` require exactly it, the `ci` image builds `/opt/venv` from its baked copy (`ARG PYTHON_VERSION` in `images/ci/Dockerfile`), the lock header records it, and `check_image_versions.py pins` fails when any of them differs. Renovate's `-full` container runs `pip-compile` on its own python (`RENOVATE_BINARY_SOURCE=global`, it ignores the header's version), so that python's minor version must equal `.python-version`: check it when bumping the image in `renovate.yml`. Moving the version is one pull request: `.python-version`, both `ARG PYTHON_VERSION` lines of `images/ci/Dockerfile`, the lock, and the image version bump. -- **Python tool pins and their lock:** `requirements-dev.txt` holds the direct pins, `requirements-dev.lock` is - their hashed lock (installed with `pip install --require-hashes -r requirements-dev.lock -r requirements-dev.txt`; - pip refuses an unhashed package and any disagreement between the two). Renovate's `pip-compile` manager bumps - a pin and regenerates the lock by replaying the lock's header command; a weekly lock-file-maintenance PR - moves transitive packages and is never automerged. By hand: `scripts/lock-requirements.sh [--upgrade]`. - The header must hold only options Renovate knows: it refuses `--newline=LF` ("not supported (yet)"), - extracts nothing, and proposes no bump, with only a WARN in an otherwise green run. - Hook repositories in `.pre-commit-config.yaml` are pinned by commit SHA with a `# frozen: vX.Y.Z` comment. - Renovate looks them up with `git ls-remote` (packageRule `overrideDatasource: git-tags` plus - `RENOVATE_GITHUB_TOKEN_WARN=false` in `renovate.yml`), because there is no GitHub token and the default - `github-tags` lookup is skipped or rate limited. A hook bump is never automerged: `git-tags` has no release - timestamps, so `minimumReleaseAge` cannot delay it. After a Renovate config change, test it locally before - the merge: `renovate --platform=local` (same version as `renovate.yml`) reads this checkout; a dry run in - Actions reads `renovate.json` from `main`. -- **Bot-diff gate:** `scripts/check_bot_pr_diff.py` runs in the lint workflow, from `origin/main`'s copy so a - pull request cannot weaken it. A `renovate/` head ref may only change version lines (tool pins and their lock - entries and hashes, hook `rev:` lines, Dockerfile FROM tags and digests, matrix `version:` lines, `job_image:` pins, consumer image pins); - anything else fails, `renovate.json` included. The branch name is attacker-controlled, so this only - constrains the bot's own pull requests. -- **No Collection release from tooling bumps:** tooling files are in `galaxy.yml` `build_ignore`. A - role-visible change needs a changelog fragment and a new `galaxy.yml` version; only the operator tags. -- **Denied image namespace:** `check_image_versions.py pins` fails when anything under `.gitea/workflows` - mentions the image namespace this repository's CI must not use (the string is in the script). -- **Bot branches:** never push to a `renovate/` branch (or anyone else's); see `docs/contributing/making-a-change.md`. -- **The runner has a single slot:** the Renovate job (up to 30 minutes) delays Lint, Build and Molecule. -- **Open item:** do not enable automerge for bot PRs until a live run has shown that they get Lint and Build. diff --git a/docs/agents/issue-tracker.md b/docs/agents/issue-tracker.md index 58d1b5a..b579d01 100644 --- a/docs/agents/issue-tracker.md +++ b/docs/agents/issue-tracker.md @@ -27,4 +27,4 @@ File issues only into `mortis/kamino`. The configured token holds `write:issue` ## Pull requests as a triage surface -**PRs as a request surface: no.** Fork pull requests are not accepted for now (CLAUDE.md), so PRs do not enter the triage queue. Set to `yes` only if that changes. +**PRs as a request surface: no.** Fork and AGit pull requests are closed unmerged ([reviewing.md](../contributing/reviewing.md)), so PRs do not enter the triage queue. Set to `yes` only if that changes. diff --git a/docs/contributing/setup.md b/docs/contributing/setup.md index bbd92c8..d469bc1 100644 --- a/docs/contributing/setup.md +++ b/docs/contributing/setup.md @@ -26,9 +26,6 @@ held by review until an offline link check exists. ## Agent tools -Until the root `CLAUDE.md` is replaced by `AGENTS.md` (issue #185), the repository itself breaks the -second rule below, and the startup check does not pass. - - **Use Claude Code 2.1.281 or later.** *Reason:* older versions read no `AGENTS.md`, or only `CLAUDE.md` in some sessions, so the agent works without this repository's guidance. diff --git a/galaxy.yml b/galaxy.yml index 8020334..75a91fb 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -43,7 +43,7 @@ build_ignore: - docs - .ansible-lint - .ansible-lint-ignore - - CLAUDE.md + - AGENTS.md - CODEOWNERS - CONTRIBUTING.md - GLOSSARY.md diff --git a/scripts/select_molecule_scenarios.py b/scripts/select_molecule_scenarios.py index 9d1df49..9691d15 100644 --- a/scripts/select_molecule_scenarios.py +++ b/scripts/select_molecule_scenarios.py @@ -62,7 +62,7 @@ IGNORED = ( "CONTRIBUTING.md", "SECURITY.md", "GLOSSARY.md", - "CLAUDE.md", + "AGENTS.md", "roles/AGENTS.md", "CODEOWNERS", "CHANGELOG.rst", diff --git a/tests/scripts/test_select_molecule_scenarios.py b/tests/scripts/test_select_molecule_scenarios.py index 950cc5e..fbfbd1f 100644 --- a/tests/scripts/test_select_molecule_scenarios.py +++ b/tests/scripts/test_select_molecule_scenarios.py @@ -157,6 +157,11 @@ class SelectTest(unittest.TestCase): chosen, _reason = sel.select(ALL, ["roles/AGENTS.md"]) self.assertEqual(chosen, []) + def test_root_agent_guidance_selects_nothing(self): + # The root AGENTS.md is contributor guidance that no scenario reads (it is not shipped). + chosen, _reason = sel.select(ALL, ["AGENTS.md"]) + self.assertEqual(chosen, []) + def test_issue_and_pull_request_templates_select_nothing(self): # Gitea reads them for its web forms; no scenario does, and they are not shipped. chosen, _reason = self.select(