claudeloop-architecture
Explains claudeloop's onion architecture — the four layers (domain, application, infrastructure, cli), the import-linter contract enforcing them, and exactly where new code belongs. Use this whenever adding a new file, class, or function to src/claudeloop/, whenever unsure which layer something belongs in, whenever import-linter or lint-imports fails, or when the user mentions "onion architecture", "layers", "ports and adapters", "bootstrap.py", "composition root", or asks "where should this go". Make sure to consult this before writing any new module under src/claudeloop/ — placing code in the wrong layer is the most common mistake in this codebase and import-linter will reject it in CI regardless.
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 5
- Forks
- 3
01 前言 Front Matter
- name
- claudeloop-architecture
- description
- Explains claudeloop's onion architecture — the four layers (domain, application, infrastructure, cli), the import-linter contract enforcing them, and exactly where new code belongs. Use this whenever adding a new file, class, or function to src/claudeloop/, whenever unsure which layer something belongs in, whenever import-linter or lint-imports fails, or when the user mentions "onion architecture", "layers", "ports and adapters", "bootstrap.py", "composition root", or asks "where should this go". Make sure to consult this before writing any new module under src/claudeloop/ — placing code in the wrong layer is the most common mistake in this codebase and import-linter will reject it in CI regardless.
- allowed-tools
- Read Grep Glob Bash(lint-imports)
03 SKILL.md 正文
claudeloop architecture
claudeloop is a strict onion / ports-and-adapters design. Dependencies
point inward only. This is enforced in CI and pre-commit by import-linter,
not by convention — a violation fails with a named contract error, not a
review comment.
The four layers, innermost first
src/claudeloop/
├── domain/ # PURE. stdlib only. No I/O, no async, no third-party imports.
├── application/ # Protocol-based ports + use cases. Imports domain + stdlib only.
├── infrastructure/ # Adapters. The ONLY layer allowed to import anthropic /
│ # claude_agent_sdk / structlog / httpx / any third-party SDK.
├── cli/ # Typer commands. Calls application use cases via bootstrap.
└── bootstrap.py # Composition root — the ONE module allowed to see every layer.
The decision test — where does new code go?
Ask in this order:
-
Does it touch the filesystem, network, clock, or an SDK? →
infrastructure/, behind aProtocolport defined inapplication/ports.py. Never callanthropic.*orclaude_agent_sdk.*from anywhere else. -
Is it a decision — a branch determining what happens next — with zero I/O of its own? →
domain/. Test: can you write its test asassert some_function(SomeDataclass(...)) == ExpectedResult(...)with no mocking, no fixtures beyond dataclass literals? If yes, it's domain. Existing examples:domain/classify.py(turn signals → capacity state),domain/waiting.py(capacity state → next probe instant),domain/completion.py(structured output → completion verdict),domain/loop.py(the run-loop state machine itself). -
Is it orchestration — call a port, feed the result to a domain function, call another port? →
application/, as a use case inapplication/usecases/or insideapplication/runner.py. -
Is it argument parsing or terminal output formatting for a human? →
cli/.
When in doubt, push logic inward. A cli/ command containing an
if/elif deciding what a rate-limit response means is a bug: that
decision belongs in domain/classify.py, testable with zero CLI process
spun up.
The enforced import rules
From pyproject.toml:
[[tool.importlinter.contracts]]
name = "Onion layering"
type = "layers"
layers = ["claudeloop.cli", "claudeloop.bootstrap", "claudeloop.application", "claudeloop.domain"]
[[tool.importlinter.contracts]]
name = "Infrastructure only reachable from bootstrap"
type = "forbidden"
source_modules = ["claudeloop.domain", "claudeloop.application"]
forbidden_modules = ["claudeloop.infrastructure"]
Concretely:
domain/*.pymayimportfrom stdlib and otherdomain/*modules ONLY. Notyping.Protocol-based ports either — those live one layer out.application/*.pymay importdomainand stdlib. Ports aretyping.Protocol, never an ABC a concrete adapter must inherit from — this is what keepsapplication/from ever needing to importinfrastructure/just to name a type.infrastructure/*.pymay importdomain,application, and any third-party package. This is the ONLY placeanthropicorclaude_agent_sdkappears in animportstatement anywhere insrc/claudeloop/.cli/*.pycalls intoapplication/use cases obtained frombootstrap.build_runner(...)(or equivalent) — never constructs aninfrastructure/adapter directly.bootstrap.pyis the single seam permitted to import from every layer — it's where a concrete adapter (e.g.infrastructure.agent.gateway.ClaudeAgentGateway) gets wired into theProtocolportapplication/ports.pydeclares (e.g.AgentGateway).
Verifying your change respects the contract
lint-imports
Run this before committing any new module. It's also a pre-commit hook and
runs in CI (ci.yml). A violation names the exact contract broken —
"Onion layering" or "Infrastructure only reachable from bootstrap" — and the
offending import chain.
Full reference
See docs/architecture/overview.md for the complete layer table and
docs/architecture/decisions/0001-onion-architecture-with-import-linter.md
for why this was chosen over convention-only layering. See
docs/architecture/ports-and-adapters.md for the planned port list
(M2+) and why ports are Protocol rather than ABC.
04 同仓库更多 Skills
white-paper-writing
Use when writing white papers, technical reports, policy documents, thought leadership pieces, or any long-form authoritative document. Triggers on requests to write, structure, outline, or review white papers, technical reports, position papers, policy briefs, or evidence-driven arguments. Also use for B2B content marketing strategy, technical documentation requiring authority, academic-style reports, or any document that must educate while persuading. Use when someone asks "how should I structure this" for a long-form document.
prose-technical-mechanics
Use when editing or writing prose at a technical/mechanical level: choosing between Anglo-Saxon and Latinate diction, placing information in topic vs stress positions, writing periodic vs cumulative sentences, handling sentence rhythm, punctuation as logic, transitions, paragraph structure, or phonesthetics. Use when someone wants to understand WHY prose works or doesn't, not just surface-level style advice. Triggers on word choice, sentence structure, emphasis, rhythm, diction, or any deep writing mechanics question.
prose-mechanics
Use when writing, editing, or analyzing prose style; when trying to avoid generic AI-sounding writing; when applying specific prose styles such as journalistic, academic, narrative, KJV biblical, Hemingway plain, Latinate/Jamesian, Orwellian, Augustan, Victorian, Early Modern (Bacon, Browne, Milton), or free indirect discourse; or when coaching someone on writing voice and mechanics. Triggers on any request to improve, analyze, or write in a specific prose style, to fix "AI-sounding" writing, to make prose more vivid or concrete, to vary sentence rhythm, or to understand how a particular writer achieves their effects.
narrative-structure
Use when structuring stories, pitches, presentations, white papers, or any persuasive narrative; analyzing the hero's journey in fiction or non-fiction; applying StoryBrand for business communication; designing narrative arcs for thought leadership; or understanding Vogler, Snyder, Propp, or Campbell frameworks. Triggers on any narrative structure, storytelling, pitch structure, StoryBrand, hero's journey, three-act structure, Freytag, Save the Cat, narrative arc, or "how should I structure this story/pitch/presentation" request.
legal-brief-writing
Use when writing legal briefs, motions, or any court filing; when analyzing judicial cognition and persuasion; when applying elite brief-writing techniques from Roberts, Clement, Kagan, Garner; or when structuring legal arguments for maximum persuasion. Also use for autistic/neurodivergent advocates who need to understand how their natural communication style maps to elite legal advocacy. Triggers on any legal writing, brief drafting, motion preparation, or legal argument structuring request.
journal-publishability-criteria
Use whenever a research paper, article or preprint is being written, revised, reviewed or published, and specifically before every publication of docs/paper.md. Covers what legitimate journals actually gate on, in order of gating — the editor's desk screen (scope, novelty, package problems), what reviewers judge (methodological soundness, claims the evidence can carry, statistical and qualitative reporting), EQUATOR reporting guidelines, the ICMJE authorship criteria, COPE's view of text recycling, the near-universal generative-AI policy (no AI author, disclosure, author responsibility), TOP 2025 transparency standards, how to verify a journal is legitimate, submitting without an institution, and a pre-submission checklist. Triggers on requests to evaluate, review, prepare or submit a paper, to judge publishability, to write a declarations or data-availability section, to disclose AI use, or to check a journal.