Swarm
CLAUDE.md584 starsChanged 2 months agoMITView on GitHub
Guidance for AI coding assistants (Claude Code, Cursor, etc.) working in this repository. This file is the canonical, in-repo briefing — read it before making changes. Swarm is a Swift 6.2 framework for building agents and multi-agent workflows on Apple platforms (iOS 26+, macOS 26+, tvOS 26+) and Linux. It is built around: - Agents — Agent struct with @ToolBuilder trailing closures, an AgentRuntime protocol, and pluggable inference providers. - Workflows — fluent composition (.step, .parallel, .route, .repeatUntil) compiled to…
In this repositoryOne other file it gives its agents.
- AGENTS.mdAGENTS.md
The file
We split this CLAUDE.md into the rules it gives an agent
7 rules. About 0 are common, 0 are distinctive, and 7 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
7
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 105
Default consumers get lean Swarm (core + Foundation Models).
- Very specific: line 193
All public types must be
Sendable: Don't suppress data-race diagnostics with @unchecked Sendable unless you have a documented reason. - Very specific: line 195
Use actor for stateful coordinators (e.g.
- Very specific: line 214
Prefer the @Tool macro over conforming to AnyJSONTool directly.
- Very specific: line 216
Use @Parameter("description") var name: T inside a @Tool struct.
- Very specific: line 244
Always call try await workspace.validate() from new tests that touch the workspace.
- Very specific: line 285
Run the deterministic matrix: Before opening a PR, run swift run SwarmCapabilityShowcase matrix in addition to swift test to catch …
≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
CLAUDE.md
Guidance for AI coding assistants (Claude Code, Cursor, etc.) working in this repository. This file is the canonical, in-repo briefing — read it before making changes.
What is Swarm?
Swarm is a Swift 6.2 framework for building agents and multi-agent workflows on Apple platforms (iOS 26+, macOS 26+, tvOS 26+) and Linux. It is built around:
- Agents —
Agentstruct with@ToolBuildertrailing closures, anAgentRuntimeprotocol, and pluggable inference providers. - Workflows — fluent composition (
.step,.parallel,.route,.repeatUntil) compiled to a DAG with checkpoint/resume. - Tools — the
@Toolmacro generates JSON schemas from Swift structs at compile time;FunctionToolcovers ad-hoc closures. - Memory — conversation, sliding-window, summary, vector, and persistent backends.
- Guardrails / Resilience / Observability — first-class concerns, not bolt-ons.
- Providers — Built-in Apple Foundation Models; custom backends inject
InferenceProvider. No Conduit / cloud façade ships with Swarm. - MCP — Model Context Protocol client and server support.
The package uses Swift 6.2 with StrictConcurrency enabled across all targets.
All public types must be Sendable — the compiler enforces it.
Repository Layout
Swarm/
├── Package.swift # SPM manifest (Swift 6.2, products)
├── README.md # User-facing overview
├── Sources/
│ ├── Swarm/ # Main library (156 .swift files)
│ │ ├── Agents/ # Agent struct, workspace integration
│ │ ├── Core/ # AgentRuntime, Conversation, Environment,
│ │ │ # PromptEnvelope, RuntimeMetadata, …
│ │ ├── Workflow/ # Workflow + durable engine + checkpointing
│ │ ├── Tools/ # Tool protocol, ToolCollection, ParallelExecutor,
│ │ │ # built-ins, web tools, schema bridging
│ │ ├── Memory/ # Conversation, sliding window, summary, vector,
│ │ │ # SwiftData, ContextCore, hybrid backends
│ │ ├── Providers/ # Foundation Models, multi-provider, sessions
│ │ ├── Guardrails/ # Input/Output/Tool guardrail specs + runner
│ │ ├── Resilience/ # Retry, circuit breaker, fallback, rate limit
│ │ ├── Observability/ # AgentTracer, SwiftLog/OSLog tracers, metrics
│ │ ├── MCP/ # MCPClient, MCPServerConnection, stdio/HTTP transports, ToolBridge
│ │ ├── Workspace/ # AgentWorkspace (AGENTS.md, .swarm/ skills)
│ │ ├── Macros/ # Public macro declarations
│ │ ├── Integration/ # Membrane and Wax integrations
│ │ └── Internal/GraphRuntime/ # Compiled DAG runtime (internal)
│ ├── HiveCore/ # In-tree durable graph (Integrations only)
│ ├── MembraneCore/ # In-tree Membrane core (Integrations only)
│ ├── Membrane/ # In-tree Membrane session (Integrations only)
│ ├── MembraneContextCore/ # In-tree Membrane↔ContextCore (Integrations)
│ ├── ContextCoreTypes/ # In-tree ContextCore types (Integrations)
│ ├── ContextCoreShaders/ # In-tree Metal shaders (Integrations)
│ ├── ContextCoreEngine/ # In-tree ContextCore engine (Integrations)
│ ├── ContextCore/ # In-tree ContextCore façade (Integrations)
│ ├── SwarmMacros/ # Compiler plugin (@Tool, @Parameter,
│ │ # @Traceable, #Prompt, builders)
│ ├── SwarmMembrane/ # Deprecated hollow re-export (remove in 0.7.0)
│ ├── SwarmMCP/ # MCP server adapter product
│ ├── SwarmCapabilityShowcase/ # Executable: deterministic showcase CLI
│ ├── SwarmCapabilityShowcaseSupport/ # Library backing the showcase
│ ├── SwarmDemo/ # (opt-in) demo executable
│ └── SwarmMCPServerDemo/ # (opt-in) MCP server demo
├── Tests/
│ ├── SwarmTests/ # Main test target (mirrors Sources/Swarm)
│ │ └── Mocks/ # MockAgentRuntime, MockInferenceProvider, …
│ ├── HiveSwarmTests/ # Hive integration tests
│ ├── SwarmMacrosTests/ # Macro expansion tests
│ └── SwarmCapabilityShowcaseTests/
├── Examples/CodeReviewer/ # Standalone example SPM project
├── docs/
│ ├── guide/ # Getting started, agent workspace, showcase
│ ├── reference/ # API catalog, front-facing-api, audits
│ └── release/ # release-checklist.md
└── .github/workflows/ # swift.yml, claude.yml, claude-code-review.yml,
# docs.ymlBuild, Test, Lint
This is a Swift Package — there is no Xcode project committed. All commands run from the repo root.
swift package resolve # Resolve dependencies (lean pins without Integrations)
bash scripts/ci/lean-build-test.sh # Cold lean resolve + product build + lean tests
swift build --traits Integrations
swift test --no-parallel --traits Integrations,MCP,OpenTelemetry
swift test --no-parallel --traits Integrations,MCP,OpenTelemetry --filter HiveSwarmTests
swift test --filter SwarmTests.WorkflowTests # Run a single suite
# Note: bare `swift build`/`swift test` without --traits Integrations still
# compile every registered target (orphans need trait-gated remotes). Prefer
# lean-build-test.sh or --traits Integrations on this package root.Integrations trait (off by default)
Default consumers get lean Swarm (core + Foundation Models). Enable the
Integrations trait for durable Hive workflows, ContextCore+Wax default
memory, Membrane adapters, and web helpers.
HiveCore, Membrane (MembraneCore / Membrane / MembraneContextCore), and
ContextCore (ContextCoreTypes / ContextCoreShaders / ContextCoreEngine /
ContextCore) are native in-tree Sources/ targets — internal modules
only (no separate library products). They are linked into Swarm only when
Integrations is on. ContextCore / full Membrane session stack are Apple-only
(Metal/CoreML); Linux Integrations still links Hive + MembraneCore + web.
Wax remains a remote package + trait-gated product; MetalANNS stays remote for
the ContextCore chain. Lean resolve must not pull package identities hive,
membrane, contextcore, or conduit, and must not pin Wax/MetalANNS/GRDB/
crypto/mutex/SwiftSoup/MCP SDK/OTel/NIO (trait-gated product edges). Default-on:
swift-syntax (via the Macros trait; disable with traits: []) and swift-log.
Opt-in: traits: ["MCP"] for SwarmMCP, traits: ["OpenTelemetry"] for
SwarmOpenTelemetry. CI: scripts/ci/lean-build-test.sh (cold resolve + product-scoped lean
build + SWARM_OMIT_INTEGRATION_TARGETS=1 tests) and
scripts/ci/verify-lean-resolve.sh. Bare root swift build without
--traits Integrations compiles every registered target — use the lean helper
or product flags; consumers only build reachable targets.
swift build --traits Integrations
swift test --no-parallel --traits Integrations,MCP,OpenTelemetryConsumer Package.swift:
.package(url: "https://github.com/christopherkarani/Swarm.git", from: "0.6.0", traits: ["Integrations"])CI (.github/workflows/swift.yml) runs on macOS 15 and Ubuntu with Swift 6.2.
It exercises scripts/ci/lean-build-test.sh (cold lean resolve deny-list +
product-scoped build + omit-target lean tests) plus a full
--traits Integrations lane (including HiveSwarmTests and the capability
showcase matrix).
The Hive integration tests live in the HiveSwarmTests target and require
--traits Integrations.
Demo / benchmark executables
The SwarmDemo and SwarmMCPServerDemo executables are
opt-in — they only build when SWARM_INCLUDE_DEMO=1 is set:
SWARM_INCLUDE_DEMO=1 swift build
SWARM_INCLUDE_DEMO=1 swift run SwarmDemoCapability showcase
SwarmCapabilityShowcase is always built and exercises the stable surface area
in a deterministic matrix that is CI-safe:
swift run --traits Integrations SwarmCapabilityShowcase list
swift run --traits Integrations SwarmCapabilityShowcase matrix
swift run --traits Integrations SwarmCapabilityShowcase run handoff
swift run --traits Integrations SwarmCapabilityShowcase smokeSee docs/guide/capability-showcase.md for the full scenario catalog and
smoke-mode environment variables.
Lint / format
CI runs SwiftLint and SwiftFormat on macOS using the tracked root configs:
.swiftlint.yml and .swiftformat. Both commands are scoped to
Sources and Tests so ignored worktrees, dependency checkouts, generated
docs, and Node artifacts do not affect results. If you change Swift files,
match the surrounding style and assume both linters will run in CI.
To format using the SwiftFormat package plugin (per README):
swift package plugin --allow-writing-to-package-directory swiftformatKey Conventions
Concurrency
- Swift 6.2 with
StrictConcurrencyis enabled on the main targets viaswarmSwiftSettings. Macro and showcase targets enableenableExperimentalFeature("StrictConcurrency")directly. - All public types must be
Sendable. Don't suppress data-race diagnostics with@unchecked Sendableunless you have a documented reason. - Use
actorfor stateful coordinators (e.g.Conversation,InMemorySession),structfor value types, andAsyncThrowingStreamfor streaming output.
Agents
- The canonical initializer is
Agent(_ instructions: String, ...)with an unlabeled instructions string and a trailing@ToolBuilderclosure for tools. SeeSources/Swarm/Agents/Agent.swift. - Provider resolution order is documented at the top of
Agent.swift:- explicit provider passed in (or privacy-required FM-first path),
.environment(\.inferenceProvider, ...),Swarm.defaultProviderviaSwarm.configure(provider:),- Foundation Models (on-device) when available,
- else throw
AgentError.inferenceProviderUnavailable.
- The
Agentstruct isSendable; tools are stored as[any AnyJSONTool].
Tools
- Prefer the
@Toolmacro over conforming toAnyJSONTooldirectly. The macro generates the JSON schema, parameter parsing, and output encoding. - Use
@Parameter("description") var name: Tinside a@Toolstruct. - For one-off closure tools use
FunctionToolwithToolParametervalues. - Multiple tools can be composed with
@ToolBuilder(the trailing closure onAgent.init).
Workflows
Workflow()is a fluent builder; chain.step,.parallel(_, merge:),.route { ... },.repeatUntil,.timeout.- Durable execution lives in
Workflow+Durable.swiftandWorkflowDurableEngine.swift. Use.durable.checkpoint(id:policy:)and.durable.checkpointing(...)to enable resume-from-checkpoint behavior. - The
Internal/GraphRuntimedirectory is the compiled DAG runtime — treat it as an implementation detail.
Memory & Workspace
Memoryfactory methods are the user-facing entry point:.conversation(maxMessages:),.slidingWindow(maxTokens:),.summary(configuration:summarizer:),.hybrid(configuration:summarizer:),.persistent(backend:conversationId:maxMessages:), and.vector(embeddingProvider:similarityThreshold:maxResults:).AgentWorkspace(inSources/Swarm/Workspace/) is the on-device workspace layout backed byAGENTS.md+.swarm/agents/<id>.md+.swarm/skills/+.swarm/memory/. Do not confuse the runtimeAGENTS.md(workspace instructions consumed by Swarm) with thisCLAUDE.md(briefing for AI coding assistants). The runtimeAGENTS.mdis git-ignored at the repo root.- Always call
try await workspace.validate()from new tests that touch the workspace.
Providers
- Built-in inference is Apple Foundation Models only via
FoundationModelsInferenceProviderunderSources/Swarm/Providers/. - Custom backends implement
InferenceProviderand are injected on the agent or viaawait Swarm.configure(provider:). There is no Conduit package dependency and noLLM.*/cloudProviderfaçade. - Native tool calling bridges Swarm
ToolSchemato Apple'sFoundationModels.Tool. - Swarm
DynamicProfile/Profile/DynamicInstructions/ProfileModeare Swarm types used by.foundationModels(profile:). They are not Apple'sLanguageModelSession.DynamicProfile(OS 27). Do not mix the modules. Capture still re-resolves the Swarm profile each turn.
Mocks & Test Helpers
Tests/SwarmTests/Mocks/contains the canonical mocks:MockAgentRuntime,MockInferenceProvider,MockAgentMemory,MockEmbeddingProvider,MockSummarizer,MockTool, plusSwarmConfigurationTestIsolationfor isolatingSwarm.defaultProvider/Swarm.configure(...)between tests.- New tests should reuse these mocks rather than reinventing local stubs.
- Tests that touch
Swarm.configureglobal state must use the isolation helper to avoid cross-test pollution under--no-parallel.
Development Workflow
- Read before you write. The codebase is large (≈156 source files,
≈150 test files). Use
Grep/Globto find call sites before changing a public type. - Mirror the source tree in tests. A change in
Sources/Swarm/Workflow/Foo.swiftshould land alongside or updateTests/SwarmTests/Workflow/FooTests.swift. - Keep public surfaces
Sendableand DocC-commented. Public types in this package carry rich DocC comments — match that style on anything new. - Don't over-engineer. Per the project's documentation history, prefer
small, surgical changes that preserve the existing public API. Audit reports
live in
docs/reference/(api-catalog.md,front-facing-api.md,documentation-*.md) — consult them before introducing new public types. - Run the deterministic matrix. Before opening a PR, run
swift run SwarmCapabilityShowcase matrixin addition toswift testto catch regressions in cross-cutting scenarios. - Never push to
maindirectly. Branch, run tests, open a PR. - Do not commit
Package.resolved. It is git-ignored intentionally — Swarm is a library and consumers resolve their own dependency graph.
Things That Are Git-Ignored (and Why)
The .gitignore deliberately excludes a number of paths AI assistants might
otherwise want to create or check in. Do not work around these.
.claude/,.mcp.json,.agent_context.md,AGENTS.md— local AI tooling config, exceptAGENTS.md, which is intentionally tracked as the repo-level guardrail for future agents..swift-version— contributors keep this locally; CI selects Swift through the workflow environment.Package.resolved— library, not application.docs/plans/,docs/prompts/,docs/work-packages/,docs/validation/,tasks/,scripts/,IMPLEMENTATION_PLAN.md,HIVE_EXTENSIBILITY_INTEGRATION_PLAN.md,PRODUCTION_READINESS_AUDIT.md— internal planning artifacts.marketing/,website/, VitePress build output.docs/reference/*audit-report.md,docs/reference/documentation-*-report.md,docs/reference/documentation-improvement-plan.md,docs/reference/api-quality-assessment.md,docs/reference/twitter-article-*.md,docs/swarm-hacker-news-blog.md,docs/superpowers/— internal audit, planning, and marketing artifacts.
CLAUDE.md itself was previously git-ignored; it has been intentionally
un-ignored so this guidance can live in-repo.
Public API Stability
The framework is at 0.5.1 (Sources/Swarm/Swarm.swift) and treats its public
surface as semi-stable. The supported public reference documents are
docs/reference/api-catalog.md, docs/reference/front-facing-api.md, and
docs/reference/overview.md. Prefer:
- adding new types over breaking existing ones,
- adding new initializer overloads over changing parameter labels on existing ones,
- documenting deprecations rather than silently removing symbols.
When You're Stuck
- For "what does X do?" questions, search
Sources/Swarm/<Area>/first, thendocs/reference/api-catalog.md. Refresh catalog header counts withscripts/ci/refresh-api-catalog-header.sh; exhaustive rows stay hand-maintained. - For workflow examples, read
Sources/SwarmCapabilityShowcaseSupport/CapabilityShowcase.swift— it touches every stable subsystem. - For provider behaviour, look at
Sources/Swarm/Providers/FoundationModels/. Prompt-envelope tool emulation for text-only backends lives inPromptToolCallingEmulation.swiftand is not Apple'sLanguageModelSession. - The
README.mdquick-start, thedocs/guide/getting-started.mdtutorial, anddocs/guide/agent-workspace.mdare the user-facing canonical docs — keep code samples consistent with them when changing surface area.
Agent skills
Issue tracker
Issues live in GitHub Issues on christopherkarani/Swarm (gh CLI). See docs/agents/issue-tracker.md.
Triage labels
Canonical roles map 1:1 to needs-triage, needs-info, ready-for-agent, ready-for-human, wontfix. See docs/agents/triage-labels.md.
Domain docs
Single-context: CONTEXT.md + docs/adr/ at the repo root. See docs/agents/domain.md.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

