Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 213 additions & 0 deletions .agents/skills/sourceorigin-cleanup/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
---
name: sourceorigin-cleanup
description: >
Run a repository-wide cleanup of SourceOrigin: an audit that measures the
code and suggests fixes, then a cleanup pass where one agent per area opens
focused, reviewed refactor PRs, a boundary lane cleans up between the crates
and surfaces, and every bug found gets a GitHub issue and a fix PR. Use when
asked for a cleanup or refactor pass, codebase stats or an audit, or to
deduplicate or simplify SourceOrigin as a whole. Not for cleaning up a single
PR or file.
---

# SourceOrigin cleanup

You orchestrate a cleanup of the whole repository in two parts: an audit, then
a cleanup pass. Run both unless the owner asks for one. Delegates write the
code. You split the work, brief them, route reviews and report.

Work from a checkout of `origin/main` that contains this skill, since the
delegates read their briefs from it. Keep a ledger in your scratchpad: each
agent, branch, PR, review state and finding, and the files each task holds.
Files are free once the task holding them has reported and its PRs touching them
have merged. Start no task on files that aren't free. A forced edit outside an
area (a renamed item's callers, a docs reference) is the exception: the agent
holding those files rebases over it. After compaction, a usage limit or a T3
restart, check the ledger and restart only what didn't finish.

Issues are GitHub issues in `sourceorigin-dev/sourceorigin`, titled like a
Conventional Commit and labelled with their kind (`bug`, `refactor`) and the
other labels that fit (`gh label list`).

## Audit

1. Measure: `python3 -I .agents/skills/sourceorigin-cleanup/scripts/stats.py .
--json <scratchpad>/stats.json`. It needs `cloc`. Chart the result with
`html_preview` and `html_render`, including growth over time from the
JSON.
2. Read the output, then check what the script can't count:
- coverage, from the `rust-coverage` artifact of `main`'s latest CI run (or
`just coverage`) and `just web test`
- unused code and dependencies, with `just unused`
- generated files missing from `.gitattributes`
- which `expect` calls can really panic
- duplication built up since the last pass, including between the Rust and
TypeScript SDKs
3. Send the owner the charts and a ranked list of suggestions, each with the
numbers and paths behind it. Open nothing yet.
4. For each suggestion the owner picks, start a thread with
`t3_thread_launch`. Use Claude Opus 5.5 in a worktree off `origin/main`.
Its message stands alone: the goal, the numbers, the scope and what to
report. A thread that checks many sites reports a table of each site, its
verdict and why.

Changes that move code in many areas, such as file splits, merge before the
cleanup pass starts or go into the area agents' tasks.

## Cleanup pass

### Split

Start from this split and adjust it to the code. Split an area over about
30,000 lines of source and tests along a module boundary (`stats.py` lists the
modules of large areas), and merge small areas. Every file has exactly one
owner.

| Area | Paths | Scope |
| --- | --- | --- |
| Sign-in and OIDC | `apps/auth-service/src/{oidc,passkey,federation,security,keys,sessions,grants}` | `auth` |
| Accounts | the rest of `apps/auth-service`: `src/{account,clients,deletion,recovery,emails,notifications,pictures,history,handles}`, the root modules and `tests` | `auth` |
| Operations | `apps/auth-service/src/{cli,config,jobs,deliveries,audit,alerts}` | `cli` or `auth` |
| Admin | `apps/auth-service/src/{admin,web/admin}`, `crates/ui/src/admin` | `admin` |
| Pages | the rest of `apps/auth-service/src/web` and `crates/ui`, `apps/auth-service/assets`, `apps/web`, `tests/browser` | `ui`, `web` or `browser` |
| Database | `crates/database` | `database` |
| SDKs | `crates/{auth-sdk,common,crypto}`, `packages/auth`, `tests/sdk-vectors` | `sdk`, `common` or `crypto` |
| Tooling | every other hand-written file: `crates/telemetry`, `tools/xtask`, `infra`, `just`, `.github`, root files | `telemetry`, `infra`, or none: `refactor: …` |

Nobody edits `crates/database/migrations`, `snapshots/` or generated files by
hand. `docs/` and Markdown belong to the docs lane.

Before you start the agents, run `rustup update stable`, `just install-tools`,
`just install`, `just validate` and `just db test` on `origin/main`. If one
fails, as it can under a new stable Clippy, have one Opus delegate fix it in a
PR, review and ship it as in Review, and ask the owner to merge it first.

Open one GitHub issue for the pass, labelled `refactor`, and note its number
`<n>`. Each PR says `Part of #<n>`. Each area agent opens several focused PRs,
on branches named `refactor/<n>-<area>-<topic>`. Tell the owner the split in
one message and start without waiting.

Every worktree compiles the workspace in its own target directory, on a
machine with no swap. Pick one `CARGO_BUILD_JOBS` value for every agent that
compiles, and pass it to each. 4 suits 8 implementers and their reviewers on
16 cores and 58 GB as a start. Run at most 8 reviews at a time.

### Start the agents

Call `orchestrator_capabilities` once. For each area, start a `delegate_task`
on Claude Opus 5.5 with `isolation: "worktree"` and `role: "implementation"`.
The task says to follow `.agents/skills/sourceorigin-cleanup/references/agent-brief.md`.
It names the area, paths, packages, branch prefix, title scope, tracking
issue, what the other areas own, and `CARGO_BUILD_JOBS`.

Start a docs lane the same way, on `docs/<n>-cleanup`. It follows the
`sourceorigin-docs` skill, fixes drift between the docs and the code, runs
`just docs` and opens a draft PR. It reports stale `///` comments and clap
help instead of editing them. Those comments sit in the area agents' files and
feed the generated reference pages.

### Review

Review each focused PR on its own as its agent reports it. Call
`link_pull_request` and start a review: a `delegate_task` on GPT 6.1 Sol with
`role: "review"`, `isolation: "worktree"` and
`target.options: { reasoningEffort: "high" }`. It follows
`.agents/skills/sourceorigin-cleanup/references/review-brief.md`. Give it the
PR number, the head SHA, the base branch (the parent PR's branch for a stacked
PR), `CARGO_BUILD_JOBS`, and a focus list: every "unchanged" claim in the PR
description, and every merged helper or test that could have changed
behavior.

- Check each finding against the code before acting on it.
- Send real findings to the implementer's thread with `t3_thread_send`.
- After a fix that rewrites code, run a new, narrower review.
- Judgment calls go to the owner, with your recommendation.
- If Sol is over its limit, review with Opus instead and say so.

When a PR's review is clean and it is rebased, mark it ready. Follow its CI
with `babysit-pr`, and update its `[!NOTE]` line to name who reviewed it. The
owner merges, through the merge queue.

When something lands on `main`, have each agent rebase and apply its cleanup
to the new code, and start the boundary themes and fixes whose files are now
free. Review any logic the rebase merged. Suggest merging the SDKs and the
database first, because the apps depend on them.

### Boundary lane

The area agents each see one area. Alongside them, start one more Opus
delegate, `role: "research"`, to look at how the areas fit together, against
`docs/dev/architecture/overview.md` and `docs/dev/rust-conventions.md`:

- the dependency direction between the crates, and logic in the wrong crate,
such as SQL outside `crates/database` or decisions in `crates/ui` templates
- public items no other crate needs, and the published crates' (`auth-sdk`,
`common`, `crypto`) public API
- types, conversions and helpers duplicated across crates, and between the
Rust and TypeScript SDKs and `common`'s wire types
- what the CLI, the settings, the developer console and the admin dashboard
each reimplement instead of sharing a service
- errors that lose their context at a crate boundary

It edits nothing. It reports themes, each with the files involved and the
change it proposes. Add the cross-area issues the area agents report to that
list. Themes that need a design decision, or change a published crate's API,
go to the owner first.

Once a theme's files are free, start one Opus implementation delegate per
theme. Merge themes that share files, or run them one after another. Each
follows the agent brief, with the theme's files as its area, and opens focused
PRs on `refactor/<n>-boundary-<topic>`. Review them like the others.

### Bugs

Every bug an agent or reviewer reports gets an issue and a fix PR:

1. Check it against current `main`, with a Sol delegate when there are many.
Merge duplicate reports, and ask the owner about anything that might be
intended. A vulnerability, anything an attacker could exploit, stops here:
tell the owner only, and open no issue or PR (`SECURITY.md`).
2. Open a GitHub issue labelled `bug`, or reuse the one that exists
(`gh issue list --search`). Include the `file:line`, what goes wrong and how
to reproduce it. If the existing issue is assigned or has a linked PR, note
it in the ledger and start no fix.
3. Once its files are free, start an Opus implementation delegate in a worktree,
on `fix/<issue>-<topic>` from a freshly fetched `origin/main`, with the
issue and `CARGO_BUILD_JOBS`. It writes a test that fails without the fix,
makes the smallest fix, updates the docs it affects, runs the agent brief's
checks, regenerating what the fix changes on purpose, such as a screenshot,
and opens a draft PR with `file-pr` that says `Closes #<issue>`.
4. Review and ship it as in Review, but without the review brief, which treats
any behavior change as a finding. The reviewer's task says to work at the
head SHA without pushing or commenting, revert the fix locally to check the
test then fails, and check the fix is correct and minimal and nothing else
changed. It reports findings with severity and `file:line`.

Run at most 3 fixes at a time.

### Report

When every area and docs PR is reviewed and green, send the owner one
report:

1. The PRs by area: link, lines added and removed, merge order, and the PRs
that are stacked.
2. Decisions only the owner can make, with your recommendation.
3. The bugs: each one's issue, fix PR and state.
4. The boundary themes, and which ones wait for the area PRs.
5. Other design issues.
6. Features missing from a surface (`AGENTS.md`), checked against current
`main`, and admin operations left CLI-only without a documented reason.

Number the items `NEW-1`, `NEW-2` and so on, never `#1`, which GitHub links to
an issue or PR.

### After the merge

Rerun the stats and compare them with the audit. Report again when the boundary
and fix PRs are reviewed and green. Then offer these follow-ups, and start each
one only when the owner asks:

1. A doc-comment PR for what the docs lane reported. It changes the generated
reference pages on purpose (`just docs reference`).
2. GitHub issues for the surface gaps.
105 changes: 105 additions & 0 deletions .agents/skills/sourceorigin-cleanup/references/agent-brief.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Cleanup pass: area agent

You own one area of SourceOrigin in a parallel cleanup pass. Your task names
the area, paths, packages, branch prefix, title scope, tracking issue and
`CARGO_BUILD_JOBS`, and lists what the other agents own.

Read every file in your area, and `docs/dev/rust-conventions.md`. Make real
simplifications, not cosmetic churn, and report bigger issues. Never add an
`#[allow]`. Use `#[expect(..., reason = "...")]` on the smallest item that
needs it.

## Rules

- **Change no behavior.** That includes HTTP responses (and which error a
request with several faults gets), OIDC and OAuth wire formats, rendered
pages and emails, CLI flags, help, output and exit codes, configuration
variables, domain events and their audit records, log and span fields,
metrics, timeouts, lock order, query plans, and the public API of the
published crates (`auth-sdk`, `common`, `crypto`) and `@sourceorigin/auth`.
`git diff <base>` shows no change to a migration, a `snapshots/` file or a
generated file, except the `.sqlx` entry of a query you rewrote. Don't
use blanket search and replace.
- **Report bugs instead of fixing them.** The orchestrator opens an issue and a
fix PR for each. If a cleanup can't be done without changing behavior, report
it too. Report a vulnerability only in your final message, never in a PR,
commit or code comment.
- **Stay in your area.** Edit another area only when a rename forces it:
keep it minimal and list those files in the PR description. Report what
crosses areas: another lane cleans up between them.
- **Keep the docs references right.** If you rename something a page in
`docs/` names, update that page (`sourceorigin-docs` skill) and run
`just docs`. Don't reword `///` comments or clap help that feed the
generated reference pages.
- Stop and report anything that needs a design decision.

## Checks

Export `CARGO_BUILD_JOBS` for every cargo command. For your packages, run:

- `just install`, then `just fmt`
- `cargo clippy --locked -p <pkg> --all-targets --all-features -- -D warnings`,
and again without `--all-features`
- `RUSTDOCFLAGS='-D warnings' cargo doc --locked --no-deps --all-features -p <pkg>`,
and `cargo test --locked --doc --all-features -p <pkg>` for a library
- `just db prepare`, if you changed a query
- `just test -p <pkg> --all-features`; for `auth-service` and
`sourceorigin-repository`, `just db test` instead. Both fail on a stale
generated file.
- `just rust unused`
- `cargo semver-checks -p <pkg> --baseline-rev origin/main`, for a published
crate
- `just web check`, `just web lint`, `just web unused` and `just web test`, if
you touched TypeScript or JavaScript
- `just docs screenshots`, if you touched a page's markup, styles or script.
It must rewrite no image in `docs/images`. It deploys a preview, so run it
once per PR, last.

Despite `AGENTS.md`, don't run `just validate`, `just lint` or `just test`
without `-p`: they build the whole workspace. CI runs on drafts every job a
PR's paths select, including the browser suite, the images, packaging and the
MSRV build, so leave those to it and read their results before you report.

Prove each "unchanged" claim against the PR's base: `--help` output, the
rendered pages, emails and HTTP responses, the TypeScript SDK's built type
declarations, query plans (`EXPLAIN` in `just db scratch -- psql`). Before you
call a failing check unrelated, check that it fails on `main` too. Never change
a test just to make it pass.

## Delivery

- Split your work into focused PRs: one kind of cleanup in one part of your
area each, small enough to review in one sitting. Branch each from a
freshly fetched `origin/main` as `<prefix><topic>`, and keep your PRs from
sharing files. If one needs another, stack it: branch from the other PR's
branch, open it with that branch as its base, and say so in its
description.
- Make small, logical commits with conventional messages and no
`Co-Authored-By` line.
- Rebase on `origin/main` (a stacked PR on its base branch), and clean up any
new code the rebase brings in. PRs merge by squash, so when a stacked PR's
base merges, move only its own commits with
`git rebase --onto origin/main <old base tip>`, and check it targets
`main`.
- Open each PR as a **draft** with `file-pr`, despite its default, titled
`refactor(<scope>): <what got simpler>` (or `refactor: …` without a scope)
and labelled `refactor` and the other labels that fit (`gh label list`). Its
description says:
- `Part of #<n>`
- what was hard to read and how you fixed it
- every test you merged or removed
- files edited outside your area, and why
- what you compared to prove the rest unchanged
- Don't mark a PR ready.

Your final message gives:

1. The PR links, in merge order, with what each one cleans up.
2. The checks you ran for each, with their results.
3. Bugs: `file:line`, what goes wrong and how to reproduce it.
4. Issues that cross areas, and other design issues: `file:line`, what's
wrong, why it matters and a fix.

Check every item against the source before you report it. When you get review
findings, fix them on that PR's branch, rerun the affected checks, push, and
report what changed.
27 changes: 27 additions & 0 deletions .agents/skills/sourceorigin-cleanup/references/review-brief.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Cleanup pass: reviewer

Review one PR from a SourceOrigin cleanup pass. It claims to be a pure
refactor: no behavior, output, schema, format or test coverage changes. Bug
fixes go in their own PRs, so any behavior change here is a finding.

Review read-only. Don't push, comment or edit. Read the PR, and check out the
head SHA your task gives (`git fetch origin <base> pull/<N>/head`). Export the
`CARGO_BUILD_JOBS` your task gives.

Check every claim in your focus list yourself, against the base branch your
task gives (`origin/main` unless the PR is stacked). Then look for, most
important first:

1. Hidden behavior changes: error codes, messages, statuses and precedence,
defaults, serialization (tokens, domain events, audit records, wire
types), rendered pages and emails, log and span fields, timeouts, lock
order and transaction boundaries, query plans (compare `EXPLAIN` in
`just db scratch -- psql`), and the published crates' public API.
2. Lost coverage: dropped assertions or cases, and expected values built by
the code under test. Compare test names and counts per file with the base.
3. Abstractions that make the code harder to read.
4. Missed cleanup and stale doc comments in the touched files, briefly.

Report each finding with its severity (blocker, should-fix or nit),
`file:line`, the problem and a fix. Say which claims you verified and how. If
the PR is clean, say so.
Loading