python-selma
CLAUDE.md19 starsChanged 6 months agoMITView on GitHub
Selma is an agentic AI gateway that routes user messages from multiple channels (WebChat, Telegram) through a three-layer agent engine and delivers streamed responses back. Error classification (detect_attempt_error) drives automatic recovery: - context_overflow → compact session → retry (max 3 attempts) - thinking_not_supported → lower thinking level → retry - aborted → surface to caller Sessions are keyed by session_key and stored in ~/.selma/agents/main/sessions/ (or .selma/ in cwd, or $SELMA_STATE_DIR): - sessions.json — map of session_key → SessionRecord (model, thinking…
In this repository6 other files it gives its agents.
- .my_mono/AGENTS.mdAGENTS.md
- templates/AGENTS.mdAGENTS.md
- blogwatcherSkill
- healthcheckSkill
- summarizeSkill
- web-researchSkill
The file
We split this CLAUDE.md into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 113
All Python comments, docstrings, and Pydantic Field descriptions must be in English.
- Very specific: line 114
Use uv run <script>.py to run scripts, never python or uv run python.
≈ “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
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Commands
# Install dependencies
uv sync
# Initialize workspace and copy default skills
uv run setup.py
# Install browser tool support
uv run playwright install chromium
# Run the gateway (REST API on :8000)
uv run gateway.py
# Run all pytest-based unit tests
uv run pytest
# Run integration test scripts individually
uv run test_runtime.py
uv run test_agent_session_chat.py # interactive CLI chat
uv run test_webchat.py
uv run test_webchat_http.py
uv run test_skills.py
uv run test_function_call.pyArchitecture
Selma is an agentic AI gateway that routes user messages from multiple channels (WebChat, Telegram) through a three-layer agent engine and delivers streamed responses back.
Three-Layer Agent Engine (runtime.py)
Layer 1: agent_command() — orchestration: who, which model, which session
Layer 2: run_embedded_pi_agent() — robustness: retry loop, context overflow, compaction
Layer 3: run_embedded_attempt() — execution: build system prompt, create session, invoke toolsError classification (detect_attempt_error) drives automatic recovery:
context_overflow→ compact session → retry (max 3 attempts)thinking_not_supported→ lower thinking level → retryaborted→ surface to caller
Message Flow
User (Telegram / WebChat)
→ gateway.py (FastAPI)
→ CommandManager (/commands)
→ process_message_flow()
→ normalize() → NormalizedTurnInput
→ runtime.agent_command() (three layers)
→ DeliveryContext callbacks → channel outputSession Persistence
Sessions are keyed by session_key and stored in ~/.selma/agents/main/sessions/ (or .selma/ in cwd, or $SELMA_STATE_DIR):
sessions.json— map of session_key → SessionRecord (model, thinking level, skills snapshot, last interaction)<session_id>.jsonl— full transcript, managed bymy_mono.agent_session
Sessions reset daily at a configured hour or after idle timeout.
System Prompt Assembly (system_prompt.py)
Built per turn from workspace context files loaded in order:
AGENTS.md → SOUL.md → IDENTITY.md → USER.md → TOOLS.md → MEMORY.md → HEARTBEAT.md → daily memory → BOOTSTRAP.md
Plus: skills snapshots (SKILL.md files, version-hashed and cached in SessionRecord), active tool names, runtime info (model, channel, host).
Channel Adapters
channel_adapter.py defines the ChannelAdapter protocol. Implementations:
adapter_webchat.py— Server-Sent Events streaming via asyncio queueadapter_telegram.py— Telegram bot via python-telegram-bot
Key Components
| File | Role |
gateway.py | FastAPI app, SSE endpoints, heartbeat lifespan |
runtime.py | Three-layer engine, session management, error recovery |
config.py | Pydantic models loading .selma/selma.json |
session_store.py | SessionRecord persistence |
system_prompt.py | System prompt builder |
tools.py | Web search (DuckDuckGo), web fetch (trafilatura), browser (Playwright), file tools |
skills.py | SKILL.md loading and version hashing |
heartbeat.py | Scheduled proactive agent turns |
compaction.py | Session compression via LLM summarization |
delivery.py | Output callbacks (on_partial_reply, on_block_reply, on_tool_call) |
resource_loader.py | Workspace context file loading |
my_mono/ | Core agent primitives: AgentSession, streaming, tool execution |
Streaming / Block Chunking
Text output is split into blocks by _BlockChunker: buffers tokens until ≥ min chars and a flush pattern (sentence boundary) is reached, then fires on_block_reply. End of turn fires on_block_reply_flush.
Configuration (.selma/selma.json)
Key settings: model (provider/model-id), channels (telegram, webchat), heartbeat (every, target, light_context, isolated_session), session.reset (at_hour, idle_minutes), tools_allow (all or list), memory (vector_search, embed_model).
Tracing
OpenTelemetry via my_mono.tracing. Decorate key async functions with @tracer.chain() or @tracer.agent(). Phoenix collector on localhost:6006 (no-op if unavailable).
Conventions
- All Python comments, docstrings, and Pydantic Field descriptions must be in English.
- Use
uv run <script>.pyto run scripts, neverpythonoruv run python. - Async-first: all agent operations use
asyncio. - Pydantic v2
BaseModelwithmodel_configfor serialization settings. - Tool filtering:
tools_allowin config controls which tools are injected per session; filter happens at runtime, not at tool creation. - Bootstrap mode: if
BOOTSTRAP.mdhas content, the first agent turn uses it as a special setup prompt, then clears the file.
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.

