Writing: read docs/skills/shared/writing-rule.md before you write a durable artifact.
/c4 — Generate C4 Architecture Diagrams
Reads the target project's codebase and produces filled-in Level 1 (System Context) and Level 2 (Container) diagrams as Mermaid markdown. Saves the slog of filling in the templates by hand for a repo you already understand structurally.
This skill complements /handover (which seeds a stub L2 once at onboarding). Use /c4 whenever the architecture changes substantially and the diagrams need a refresh — or for a project that wasn't onboarded via /handover.
For projects that hit Mermaid's ceiling — L3 (component) precision, auto-zoom across levels, tags/perspectives — pass --dsl to emit a Structurizr DSL workspace instead. See "Escape hatch: Structurizr DSL (L3+)" below.
Path resolution
Read the registry path via portfolio_registry, the per-project docs dir via portfolio_projects_dir, and the ideas backlog via portfolio_ideas_backlog — all from .claude/hooks/_lib-portfolio-paths.sh. Source the helper at the top of any bash block that touches those paths:
source "$(git rev-parse --show-toplevel)/.claude/hooks/_lib-read-config.sh"
source "$(git rev-parse --show-toplevel)/.claude/hooks/_lib-portfolio-paths.sh"
registry=$(portfolio_registry)
Defaults match today's single-fork layout (./apexyard.projects.yaml, ./projects, ./projects/ideas-backlog.md). Adopters in split-portfolio mode override the portfolio.{registry, projects_dir, ideas_backlog} keys in .claude/project-config.json. Don't hardcode literal apexyard.projects.yaml or projects/ paths in bash blocks — the helper resolves whichever mode the adopter is in. See docs/multi-project.md.
Usage
/c4 # current cwd, both levels
/c4 sample-app # registered project, both levels
/c4 sample-app --level=1 # only the L1 system-context diagram
/c4 . --level=2 # only the L2 container diagram for cwd
/c4 sample-app --force # overwrite existing diagrams
/c4 sample-app --dsl # Structurizr DSL escape hatch (L3+), instead of Mermaid
/c4 sample-app --dsl --force # overwrite an existing workspace.dsl
Output location
Where the files land depends on where the skill is invoked from and what argument is passed:
| Invoked from | Arg | Output |
|---|---|---|
| workspace/<name>/ (project clone) | none | <project>/docs/architecture/{context,container}.md (inside the project's own repo) |
| Ops fork root | <name> (registered project) | projects/<name>/architecture/{context,container}.md (ops view) |
| Ops fork root | none | docs/architecture/{name}-{context,container}.md (framework-wide) |
| Anywhere | . | Treat cwd as the project; write to docs/architecture/{context,container}.md |
| Any of the above | --dsl added | Same directory as the Mermaid output would use, but a single workspace.dsl file instead of {context,container}.md |
The split mirrors the existing convention from docs/multi-project.md § "Architecture diagrams".
Process
1. Resolve the target
- If
<project-name>is.→ use cwd. - If
<project-name>is given and the registry has it → useworkspace/<name>/if it exists, otherwise fall back to ops-view-only mode (no codebase to scan; ask the user to clone or to provide a path). - If no arg → use cwd; if cwd is the ops fork root, ask whether the diagram is framework-wide or for a registered project.
If the cwd / target doesn't have any of the detection signals listed below (no package.json, no Dockerfile, no template.yaml, etc.), stop and tell the user — there's nothing to scan.
2. Detect
Run these in parallel; collect findings into a structured proposal.
2a. Containers (L2)
A "container" in C4 is a deployable / runnable unit — a frontend, an API, a database, a queue, a worker, a CDN. Not a Docker container (confusing but standard).
Detection sources:
| Signal | Container inferred |
|---|---|
| web/, frontend/, client/ with package.json | Web App (label by framework: detect Next.js / Vite / CRA from dependencies) |
| backend/, api/, server/ with package.json | API |
| admin/ with package.json | Admin App |
| Top-level Dockerfile (no monorepo split) | Single containerised service (label by base image) |
| template.yaml (SAM) | Each AWS::Serverless::Function → potentially a container, but collapse to one logical "Lambda functions" container unless there are clear domain boundaries (auth-functions vs api-functions). One box per domain, max 5–9 containers total. |
| serverless.yml | Same pattern as SAM — one container per logical service |
| Terraform module names (infrastructure/modules/*) | Each module that creates a runtime resource (DynamoDB, S3 bucket, CloudFront distribution, Cognito user pool, RDS instance) → infra container. Skip pure-policy / pure-IAM modules. |
| prisma/schema.prisma or migrations dir | Database container (label by provider in schema) |
| package.json deps containing bullmq, bee-queue, agenda | Background Worker container |
| package.json deps containing @aws-sdk/client-s3, aws-sdk (S3 usage) | S3 / object storage as a container if the project owns the bucket |
| Cron / EventBridge rules in IaC | Scheduler container |
Hard cap: 9 containers max. If detection yields more, collapse the most-similar pair into a single container with a combined label, and surface the collapse to the user during step 3.
2b. External actors and systems (L1)
External actors fall into three buckets — Person (humans), System_Ext (third-party SaaS / APIs), and the System (the box being modelled).
Detection sources:
| Signal | Actor type | Inferred name |
|---|---|---|
| Auth code present (Cognito / Auth0 / Clerk / Supabase Auth) | System_Ext | The auth provider |
| @aws-sdk/client-bedrock, openai, @anthropic-ai/sdk | System_Ext | The AI provider |
| stripe, paddle, lemonsqueezy | System_Ext | Payment processor |
| posthog-js, @amplitude/analytics-browser, mixpanel-browser, react-ga4 | System_Ext | Analytics provider |
| @sentry/*, @datadog/*, bugsnag-js | System_Ext | Error / monitoring provider |
| nodemailer, @sendgrid/mail, postmark, resend, AWS SES use | System_Ext | Email provider |
| twilio, vonage | System_Ext | SMS / telephony |
| algoliasearch, meilisearch, @elastic/elasticsearch | System_Ext | Search provider |
| dicebear, image CDNs, fonts CDN (fonts.googleapis.com) | System_Ext | Asset CDN |
| Public-facing pages / /[username] style routes | Person | Public visitor |
| Admin routes (/admin/) | Person | Admin |
| Auth + non-admin routes | Person | End user |
If a signal could match multiple personas (e.g., the API serves both end users and admins), surface both; the user can collapse during confirm.
Detection should also pull the project's one-sentence description from:
- The README's first non-heading paragraph
- The
descriptionfield inpackage.json - An existing
projects/<name>/README.mdif the registry has the project
If none of those exist, ask the user for one sentence in step 3.
3. Confirm with the user
Show the detected proposal in a compact table:
For <project>:
External actors (L1):
[Person] End user — uses the public profile pages
[Person] Admin — manages reports and users
[Ext] AWS Cognito — authentication
[Ext] Amazon Bedrock — text embeddings for similarity
[Ext] PostHog EU — product analytics
[Ext] DiceBear — avatar generation
Containers (L2, inside the system boundary):
Web App Next.js 16 — public profile pages, sign-in, dashboard
Backend API AWS Lambda + SAM — Q&A endpoints, profiles, likes, search
Admin App Next.js + Cognito — moderation console
DynamoDB single-table — questions, answers, profiles, likes
S3 (uploads) public-read — avatar + answer-attachment storage
CloudFront asset CDN — public-asset distribution
One-sentence description:
"Public Q&A platform — anonymous askers, public answers, share-driven growth."
Edit? (a) accept · (e) edit list · (d) edit description · (q) quit
>
On e: open an interactive add/remove flow — one item per prompt, accept by Enter, or type add: <new item> / remove: <name>.
On d: prompt for a one-sentence replacement description.
On q: exit without writing.
On a: proceed to step 4 (or step 4b if --dsl was passed).
4. Generate the Mermaid (skip if `--dsl` was passed — go to step 4b)
Resolve the C4 templates via the portfolio helper so adopter overrides win when present:
source "$(git rev-parse --show-toplevel)/.claude/hooks/_lib-read-config.sh"
source "$(git rev-parse --show-toplevel)/.claude/hooks/_lib-portfolio-paths.sh"
context_template=$(portfolio_resolve_template architecture/c4-context.md) # L1 skeleton
container_template=$(portfolio_resolve_template architecture/c4-container.md) # L2 skeleton
Single-fork adopters (no portfolio block) and adopters with no override fall straight through to templates/architecture/c4-{context,container}.md. Adopters who want a customised C4 shape drop their versions at <private_repo>/custom-templates/architecture/c4-{context,container}.md. See templates/README.md for the path-mirroring convention.
Keep the surrounding markdown sections from the resolved templates ("How to use this template" can be trimmed in the generated file since these are real diagrams, not templates).
For L1:
C4Context
title System Context for {Project Name}
Person(<id>, "<Display>", "<Description>")
...
System(main, "{Project Name}", "<one-sentence description>")
System_Ext(<id>, "<Name>", "<Tech / role>")
...
Rel(<from>, <to>, "<Verb>", "<Protocol>")
...
For L2:
C4Container
title Container Diagram for {Project Name}
Person(<id>, "<Display>", "<Description>")
...
System_Boundary(boundary, "{Project Name}") {
Container(<id>, "<Name>", "<Tech>", "<Responsibility>")
ContainerDb(<id>, "<Name>", "<Tech>", "<What it stores>")
}
System_Ext(<id>, "<Name>", "<Tech>")
...
Rel(<from>, <to>, "<Verb>", "<Protocol>")
...
Relationship inference rules:
Person→ primaryContainer(Web / API) over HTTPSWeb→APIover HTTPS / JSONAPI→ContainerDbover the DB protocol (SQL / DynamoDB / etc.)API→System_Extover the integration protocol (OAuth / HTTPS / SMTP)Worker→Queueif a queue is detected- All
System_Extarrows point outward from the system boundary
Don't over-annotate. If a relationship is obvious ("Web calls API"), keep the verb to one word.
4b. Generate the Structurizr DSL (`--dsl` only)
Reuse the same detected model from step 2 (containers, external actors, relationships) — the DSL escape hatch does not re-run detection, it re-renders the same proposal the user already confirmed in step 3 into a different output format. See "Escape hatch: Structurizr DSL (L3+)" below for the full mapping and the L3 (component) authoring note.
Resolve the template the same way as step 4:
dsl_template=$(portfolio_resolve_template architecture/c4-structurizr.dsl)
Falls through to templates/architecture/c4-structurizr.dsl for adopters with no override. Adopter overrides live at <private_repo>/custom-templates/architecture/c4-structurizr.dsl (same path-mirroring convention as the Mermaid templates).
5. Write the files
Path resolution from step 1's table (or its --dsl row). Behaviour:
- If the file does not exist → write directly.
- If the file does exist:
- Without
--force: stop, print a diff against the proposed content, ask the user to either re-run with--forceor merge by hand. - With
--force: overwrite. Print a diff so the user sees what changed.
Each generated Mermaid file ends with a small footer:
---
_Generated by `/c4` on YYYY-MM-DD. Re-run after architecture changes._
The generated workspace.dsl ends with the DSL-comment equivalent:
// Generated by /c4 --dsl on YYYY-MM-DD. Re-run after architecture changes.
This is the skill's signature — readers know it's regenerable, not hand-maintained.
6. Lint the generated output
Mermaid (skip if --dsl): run lint.sh against each file written in step 5. The lint wraps the shared _lib-mermaid-lint.sh — extracts every ` `mermaid block and validates each via mmdc` (mermaid-cli) so broken syntax is caught at write time, not when a human opens the file on GitHub. Graceful-degrades when Node / npx is unavailable (exit 3, advisory only; doesn't block the skill).
SKILL_DIR="$(dirname "$(realpath "$0")")"
"$SKILL_DIR/lint.sh" "$context_out" || lint_rc=$?
"$SKILL_DIR/lint.sh" "$container_out" || lint_rc=$?
Treat exit 1 (parse error) as a hard fail — print the lint output, ask the operator whether to (a) auto-regenerate the offending block, (b) keep the file as-is and fix by hand, or (c) re-run with --skip-lint if mmdc is misbehaving. Exit 3 (Node missing) prints a one-line warning and proceeds.
Structurizr DSL (--dsl only): run lint-dsl.sh against the written workspace.dsl. The lint wraps the shared _lib-structurizr-lint.sh — a structural check (balanced braces, workspace/model/views blocks present, no duplicate identifiers), not a full DSL parse, because the escape hatch is dependency-free by design (no Java, no Docker required). If structurizr-cli happens to be on the adopter's PATH, the lint also runs a real parse via it — a bonus, never a requirement.
SKILL_DIR="$(dirname "$(realpath "$0")")"
"$SKILL_DIR/lint-dsl.sh" "$dsl_out" || lint_rc=$?
Same exit-code contract as lint.sh: 1 is a hard fail (print, ask), 2 is bad input, --skip-lint bypasses.
7. Confirm to the user
Mermaid mode:
✓ <project>: C4 diagrams written
L1: <path/to/context.md>
L2: <path/to/container.md>
Containers: 6 (max 9)
External: 6 systems, 2 actors
Mermaid lint: 2 of 2 files parsed cleanly
Preview: open the file on GitHub — Mermaid renders inline.
Re-run /c4 <project> --force when the architecture changes.
--dsl mode:
✓ <project>: Structurizr DSL workspace written
DSL: <path/to/workspace.dsl>
Containers: 6 (max 9, but --dsl has no cap — L3 detail lives inside them)
External: 6 systems, 2 actors
Structural lint: OK (structurizr-cli not found — structural-only; render to verify visually)
Render: paste the file at https://structurizr.com/dsl (free, zero install),
or install structurizr-cli / run Structurizr Lite if you want a local
render step. See "Escape hatch: Structurizr DSL (L3+)" below.
Re-run /c4 <project> --dsl --force when the architecture changes.
Escape hatch: Structurizr DSL (L3+)
Mermaid stays the default — nothing above changes for a plain /c4 invocation. --dsl is a side-channel for projects that hit a real wall, not a migration. Decision rationale: