---
name: portal-architecture
description: Keep a repo's LikeC4 architecture model (architecture/*.c4) in step with its code, following portal's conventions. Use when changing code in a repo that has architecture/*.c4; when adding, removing or renaming a service, API, event, datastore, or a call between them; or when portal's comment on a pull request reports Stale docs, Spec problems, Unclaimed files, connections or calls Not in the spec, a Broken rule, Coverage dropped, or model errors.
---

# Keep the architecture model in step with the code

This repo describes its architecture in plain LikeC4 (`architecture/*.c4`), using portal's conventions. portal
checks every pull request: it maps changed files to model elements, reads what the code wires and calls, and comments with
**checks** (what changed, what the spec is missing, which rules broke, which docs went stale). Your job is to keep the model true, in the same pull request as the code change.

## The conventions

**Element kinds are layers.** portal draws one row per kind, top to bottom:

| kind | use for |
|---|---|
| `actor` | something outside the system that calls it or is called by it (a customer app, a partner) |
| `api` | an entry point others call (HTTP/gRPC API, webhook receiver) |
| `service` | something deployed and run on its own (a worker, a server, a job runner) |
| `component` | a part *inside* a service or api (a module, a handler, a client); nest it in that element's block |
| `event` | a message, topic or queue (`invoice.paid`) |
| `datastore` | a database, cache or bucket |
| `infra` | shared infrastructure worth showing (gateway, scheduler) |

**Domains group elements.** Nest elements inside `domain` blocks. A domain is a column on portal's map.

**One deployable, many modules? Use components.** A monolith or a single Worker is one `service` with its modules as
`component`s nested inside it, not many services. portal draws the service in the Services row and its components in
a frame under it; a component's relationships count for its service at runtime.

```c4
portal = service 'portal' {
  metadata { otel_service 'portal' }
  githubClient = component 'GitHub client' {
    metadata { code 'src/github.ts' }
  }
}
githubClient -[request]-> github 'App API'
```

**Metadata portal reads** (all optional, but `code` matters most):

```c4
dispatcher = service 'Webhook Dispatcher' {
  description 'Delivers invoice events to merchants. Single attempt.'
  metadata {
    code 'src/dispatcher/**'          // files that implement it; several: 'src/a/**, lib/a.ts'
    docs 'docs/webhooks.md'           // its documentation
    otel_service 'webhook-dispatcher' // OpenTelemetry service.name, to match runtime calls
  }
}
invoices = event 'Invoice events' {
  metadata { resource 'invoice-events' } // the deployed name: a wrangler queue, D1/R2 name, KV/Hyperdrive binding, or a Compose service
}
```

**Rules** are metadata too: what must hold, checked on every PR against the spec and the code (and on the system page against traces too).

```c4
db = datastore 'Orders DB' {
  metadata { only_writes_from 'Checkout' }  // only these (ids, titles or domains) or their parts write it
}
payments = service 'Payments' {
  metadata { never_calls 'orders-api' }     // it, and its parts, never connect to these
}
```

A changed file belongs to the element whose `code` pattern is the most specific match. portal reads the Worker config
(`wrangler.jsonc`, `.json`, `.toml`) and Docker Compose files, and compares what they wire with the spec: give each queue,
database or bucket element its `resource` name, so a binding finds its element. A Worker or a Compose service matches by its
`otel_service` (or an element titled like it).

**Relationship kinds say what a connection does.** Prefer them over a plain `->`:

```c4
merchant -[request]-> invoicesApi 'pays invoice'      // calls and waits
invoicesApi -[publishes]-> invoicePaid                // sends an event
invoicePaid -[consumes]-> dispatcher 'sends webhook'  // receives an event
invoicesApi -[writes]-> invoicesDb 'marks paid'       // writes data
ledgerService -[reads]-> ledgerDb                     // reads data
```

The kinds are declared in the repo's spec file (usually `architecture/portal.spec.c4`). Don't add new element kinds;
portal won't know which row they belong in.

## In every pull request that changes code

1. **Find the elements you touched.** For each changed file, find the element whose `code` pattern matches it.
   No element matches and the code is part of the system? Add it to the nearest element's `code`, or add an element.
2. **Ask whether what it does or talks to changed.** If yes, change the model in the same PR:
   - a new call, event or data access → add the relationship with its kind and a short title
   - a removed one → remove the relationship
   - a new service, API, event or datastore → add an element with `code` (and `otel_service` if it's traced)
   - moved or renamed files → update the `code` pattern
   - a changed purpose → update `description`
3. **Update the docs** listed in the element's `docs` if its behaviour changed.
4. **Check before you push:** run `node .claude/skills/portal-architecture/check.mjs`. It prints the checks portal
   would post on the pull request for your working tree (uncommitted files included) and exits 2 if anything needs
   a look. Fix what it reports and run it again until it says "Nothing to act on", or until what's left is explained
   (step 5). It sends portal file paths and the `.c4` files only, never other file contents. If it can't run
   (offline), at least validate the syntax: `npx -y likec4@1 validate architecture`.
5. **Nothing architectural changed** (a refactor, a bug fix inside one element)? Leave the model alone and say so in
   one line of the PR description, e.g. "Model unchanged: refactor inside Webhook Dispatcher." portal lists it under
   Changed, not as a problem; the line tells the reviewer it was checked, not missed.

Keep the model coarse: elements for things a newcomer should know exist, not one per file.

## Acting on portal's PR comment

The comment starts with `### portal:` and has a **Checks** section; `check.mjs` prints the same section
locally. Fix what it reports with a commit to the same PR; portal recomputes the checks on every push.

| Check | What to do |
|---|---|
| ⚠️ **Model errors** `file:line` | Fix the syntax or reference at that line, then validate. |
| 🟡 **Stale docs:** X changed, `docs/…` not updated | Update those docs to match the change, or note why they still hold. |
| **Unclaimed files:** `path` | Add the path to the right element's `code`, or add an element if it's new. |
| 🟡 **Not in the spec:** X uses Y (`wrangler.jsonc:24`) | The code wires a connection the spec doesn't have. Add it to the spec (with the right kind), or remove the binding if it's dead. |
| 🟡 **Not wired in the code:** X → Y | The spec has the connection, the Worker config has no binding for it. Add the binding, or drop the connection from the spec if it's gone. |
| 🟡 **Calls not in the spec:** X → Y via `env.DB (writes)` (`file:line`) | The code in X's files calls Y, and the spec has no such connection from X. Add it (from X, with the right kind), or remove the call. |
| **Unresolved:** `fetch(env.X)` in X (`file:line`) | The URL comes from config, so portal can't say what it calls. Name the target in the spec. |
| **Named by no element:** queue `x` | Add `resource 'x'` to the element it is (or add the element). |
| 🟡 **Broken rule:** Only X writes Y. Z → Y (code: `file:line`) | The PR does what a rule forbids. Change the code, or change the rule if the team agrees the boundary moved. |
| 🟡 **Rule names no element:** `never_calls` on X: `name` | Fix the name (an element id, title or domain). |
| **Planned:** X (`code '…'` has no files yet) | Fine while it's being built: the spec states it before the code exists, and the map draws it dashed. Once the code lands it turns solid. Planned for 28 days means stale: build it, fix the pattern, or remove it. |
| 🟡 **Spec:** `code '…'` matches no file | Some of the element's code exists, this pattern matches none of it. Fix the pattern (files moved?) or drop it. |
| 🟡 **Spec:** kind `x` isn't a portal layer | Use one of the kinds above. |
| 🟡 **Coverage dropped:** … down from N% at the base | The PR adds source files no element claims. Add them to the right element's `code` (see **Unclaimed files**), or say in the PR why they stay out. |
| **Coverage** | How much of the code the spec claims. Raise it when it's cheap (add `code` patterns); don't chase 100%. |

The AI review under the checks (off unless the repo turned it on) may suggest spec edits; apply them only after checking the code yourself.
A person with write access can ask for an AI review by commenting `/portal review`.

## Don't

- Don't delete a relationship you haven't confirmed is gone from the code.
- Don't invent `otel_service` names; use the service name the code actually reports (comma-separate several, e.g. an API host and a web host).
- Don't silence portal by loosening `code` patterns until they match everything.
