TeaMode is a self-hosted Discord bot that runs FLOWN/Groove-style guided co-working sessions in voice channels. The bot user is named Ocha. Stack: Python 3 + discord.py + asyncio + SQLite. Runs on WSL (MVP); VPS for V2 distribution.
The single command is /teamode, invoked from the text chat attached
to a voice channel. The bot walks the facilitator through duration
pick → intention → countdown → reverie ring → follow-up.
When picking up work in a new session, read these files first:
.project-meta/USEE/3execute.md— current status, what's done, what's nextTODO.md— actionable backlog (created at first APM project).project-meta/conventions.md— project conventions reference
teamode/ ← repo root
├── teamode.py ← entry point (thin): PID lock, ffmpeg probe, startup order
├── app/ ← package
│ ├── __init__.py
│ ├── discord_bot/ ← Discord wiring only (mixins composed in client.py)
│ │ ├── client.py ← discord.Client + CommandTree + event handlers
│ │ ├── commands.py ← /teamode and /handoff registration + handlers
│ │ ├── views.py ← buttons, modal, welcome/timer embeds
│ │ ├── timer.py ← active timer edit loop
│ │ ├── lifecycle.py ← end-of-session, Reflect, solo grace, message cleanup
│ │ ├── breaks.py ← chaining and break flow
│ │ ├── clear.py ← /teamode-clear
│ │ ├── stats.py ← /teamode-stats
│ │ ├── messages.py ← best_effort() for message delete/edit calls
│ │ ├── runtime.py ← in-memory per-session / per-channel state
│ │ └── tasks.py ← background-task helpers
│ ├── constants.py ← every tunable + every Discord-facing string
│ ├── config.py ← env var loading (no discord import)
│ ├── session.py ← session state machine (Discord-free)
│ ├── voice.py ← voice connect/play/disconnect
│ ├── db.py ← SQLite schema + writes
│ ├── rate_limit.py ← per-user/per-guild rate limiting
│ ├── timer_format.py ← mm:ss / progress-bar formatting
│ ├── stats.py ← stats aggregation (windows, streak, rate)
│ ├── cleanup.py ← /teamode-clear message classification
│ └── pidlock.py ← single-instance PID lock
├── assets/
│ ├── reverie.wav ← end-of-session chime
│ └── wind-chime.wav ← wrap-up nudge chime
├── scripts/
│ ├── teamode_launcher.sh ← sources ~/.teamode-secrets, dev/stable modes
│ └── generate_art.py ← dev-only art candidate generator (Pillow)
├── tests/ ← pytest suite
├── docs/ ← Discord platform notes, schema, comparisons
├── requirements.txt ← pinned runtime dependencies
├── requirements-dev.txt ← dev-only deps (Pillow, for generate_art.py)
├── .project-meta/ ← conventions, UI-ADR, project-meta artifacts
├── .LLMAO/ ← LLMAO workflow docs
├── .project-meta/USEE/ ← USEE knowledge framework (under project-meta)
└── .apm/ ← APM session artifacts
APM session artifacts live under .apm/ and should not be treated as
product source.
teamode.py — entry-point. Acquires the PID lock, probes for
ffmpeg (warns, non-fatal, if missing), loads env vars
(DISCORD_BOT_TOKEN, TEAMODE_DB_PATH), initializes the database and
reconciles crashed sessions, constructs the bot, runs the event loop.
Imports all logic from app.discord_bot.
app/discord_bot/ — Discord-facing layer, split into mixins
composed onto one bot class in client.py: CommandsMixin
(/teamode, /handoff), ViewsMixin (buttons, modal, welcome/timer
embeds), TimerMixin (the active-timer edit loop), LifecycleMixin
(end-of-session, Reflect, solo grace, message cleanup),
BreakMixin (chaining and breaks), ClearMixin (/teamode-clear),
StatsMixin (/teamode-stats). client.py owns the discord.Client,
the CommandTree, and the on_<event> handlers (on_ready,
on_interaction, on_raw_reaction_add, on_voice_state_update).
on_ready syncs slash commands per guild in TEAMODE_DEV_GUILD_ID;
if that env var is unset, registration is skipped entirely with a
warning (no global-registration fallback).
app/constants.py — every tunable number and every Discord-facing
string, as named constants. Pure data: no discord or app.config
imports, so it is readable and testable in isolation.
app/config.py — env var loading (DISCORD_BOT_TOKEN,
TEAMODE_DB_PATH, TEAMODE_DEV_GUILD_ID, TEAMODE_TIMEZONE), with
IANA timezone resolution and fallback to UTC if even the default is
unavailable.
app/session.py — Session state machine. Pure logic, no
discord.py imports beyond Interaction typing. State transitions:
pending → intention_set → active → followup → terminal
(completed / followup_timeout / cancelled / crashed). Imported
by tests directly without a live bot.
app/voice.py — Voice connect, FFmpegPCMAudio playback of
assets/reverie.wav / assets/wind-chime.wav, disconnect. Single
source of truth for voice.
app/db.py — SQLite schema, connection, write helpers, and the
stats read helpers (fetch_user_stats_rows,
fetch_guild_stats_rows). See docs/sqlite-schema.md for the schema
reference.
app/rate_limit.py, app/timer_format.py, app/stats.py,
app/cleanup.py, app/pidlock.py — flat, Discord-free,
injectable-clock modules backing rate limiting, timer text formatting,
/teamode-stats aggregation, /teamode-clear message classification,
and the single-instance PID lock, respectively.
| Store | Format | Location | Purpose |
|---|---|---|---|
sessions table |
SQLite | $TEAMODE_DB_PATH (default ./sessions.db) |
One row per /teamode invocation. Updated at every state transition. |
| File | Purpose |
|---|---|
teamode.py |
Entry point — python3 teamode.py |
app/discord_bot/client.py |
discord.Client, CommandTree, event handlers, mixin composition |
app/discord_bot/commands.py |
/teamode and /handoff registration + handlers |
app/discord_bot/views.py |
Buttons, modal, welcome/timer embeds |
app/discord_bot/timer.py |
Active timer edit loop |
app/discord_bot/lifecycle.py |
End-of-session, Reflect, solo grace, message cleanup |
app/discord_bot/breaks.py |
Chaining and break flow |
app/discord_bot/clear.py |
/teamode-clear |
app/discord_bot/stats.py |
/teamode-stats |
app/discord_bot/runtime.py |
In-memory per-session and per-channel state (SessionRuntime, ChannelRuntime) |
app/constants.py |
Every tunable + every Discord-facing string |
app/config.py |
Env var loading |
app/session.py |
Session state machine (testable without Discord) |
app/voice.py |
Voice connection + reverie/wind-chime playback |
app/db.py |
SQLite schema, writes, and stats read helpers |
app/pidlock.py |
Single-instance PID lock |
assets/reverie.wav |
End-of-session chime |
assets/wind-chime.wav |
Wrap-up nudge chime |
scripts/teamode_launcher.sh |
Local launcher (dev/stable modes) |
scripts/generate_art.py |
Dev-only art candidate generator |
docs/discord-platform-notes.md |
Discord API reference for slash, components, voice |
docs/sqlite-schema.md |
Field-by-field schema reference with citations |
docs/language-library-comparison.md |
Why discord.py; hosting tradeoffs |
docs/windows-shortcut.md |
Windows Terminal desktop shortcut for the launcher |
.project-meta/conventions.md |
All coding standards |
.project-meta/UI-ADR.md |
Discord-surface palette, identity, settled UI decisions |
.LLMAO/USER-GUIDE.md |
LLMAO workflow walkthrough |
.project-meta/USEE/1understand-criteria.md |
What TeaMode is, success criteria |
.project-meta/USEE/3execute.md |
Current status |
Create or activate the local virtual environment:
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtRun the bot:
DISCORD_BOT_TOKEN=... python3 teamode.pyRun the automated suite:
.venv/bin/python -m pytest tests/ -vdiscord.py[voice]— Discord client + voice extras (PyNaCl + Opus)python-dotenv— env var loading from.envfor dev convenience- Standard library:
sqlite3,asyncio,pathlib,datetime,random
System requirements: ffmpeg on PATH for voice playback.
Use offset and limit parameters to read only the sections you need.
Avoid re-reading entire files when you only need a few lines.
Before editing any file, read it first. Before modifying a function, grep for all callers. Research before you edit.
See .project-meta/conventions.md for the full project conventions
reference (naming, architecture, testing, version control, TeaMode
specifics). All coding standards live there — do not duplicate them
here.
See .project-meta/conventions.md §Testing for test runner, patching
conventions, and smoke test rules. See .LLMAO/test-patterns.md for
discord.py and asyncio testing patterns.
See .project-meta/conventions.md §Version Control for commit format,
versioning, and branch rules. PRs should include a brief description,
test results, and Discord screenshots when user-visible behavior
changes.
Always ask for explicit user approval before making any code or
documentation edit, not just before commits or high-risk operations.
Propose the intended change first, wait for confirmation, then edit.
Keep this rule in force even for repository docs, .apm/ planning
artifacts, and small refactors.
Exception: during APM Task execution, the checkpoint rules in APM_RULES › Approval Workflow apply instead.
Do not hardcode local paths or tokens. Read DISCORD_BOT_TOKEN,
TEAMODE_DB_PATH, TEAMODE_DEV_GUILD_ID, and TEAMODE_TIMEZONE from
environment. Do not check .env files into git.
- Application name (Discord developer portal):
TeaMode - Bot user display name:
Ocha - Slash command:
/teamode
| Environment | Status |
|---|---|
| WSL (facilitator's laptop) | Supported (MVP) |
| Linux VPS | Planned (V2) |
| Termux (Android) | Not supported — voice playback path unverified |
For chats in this repo, use these plain-text shortcuts to refer to APM skills:
apm planner→apm-1-initiate-plannerapm manager→apm-2-initiate-managerapm handoff manager→apm-3-handoff-managerapm summarize→apm-4-summarize-sessionapm recover→apm-5-recover
apm-communication is a support skill, not a direct user command.
- The MVP worked best with agents running one at a time. Parallel worktrees cost about 75k extra tokens.
- The user approves in short replies like "y" and "1y 2y".
- The user tends to add TODO entries freely.
Context usage is tracked automatically. When you reach 70% context usage, you will receive instructions to perform a Handoff. Follow those instructions when they appear — do not worry about monitoring context yourself.
APM_RULES {
- For APM Task execution in this project, the User approved checkpoint-based approval. This replaces the per-edit rule in § Approval Gates above and the "wait for plan approval" rule in the User's global instructions: a Task Prompt from the Manager is the approved plan, so implement it without asking before each edit.
- Stop and return Partial (never proceed past these) when the Task
reaches a User checkpoint:
- a manual Discord smoke test,
- review of generated art assets,
- any Discord-facing string that is not already defined in
app/constants.pyor given in the Task Prompt, - any decision the Task Prompt marks as the User's.
- Workers commit freely on their assigned feature branch (local and
reversible). The Manager obtains explicit User approval before each
merge into
main— presenting the commits and changed files, then waiting. Tags and pushes always need explicit User approval. - Outside APM Task execution (ad-hoc chats), § Approval Gates above applies unchanged.
- Base branch:
main. Onetype/short-descriptionfeature branch per dispatch unit (e.g.refactor/discord-bot-package); the Manager creates branches and merges with--no-ff, then deletes the branch. Nothing is pushed toorigin. - Commits: Conventional Commits
type(scope): description(types feat, fix, refactor, docs, test, chore, style, perf), 50/72 rule (subject ≤ 50 chars, imperative, no period; body wrapped at 72). One logical change per commit; commit at intermediate points on large work. - Stage files explicitly by path (
git add <path>); nevergit add -A/git add .— the working tree may hold uncommitted.apm/,AGENTS.mdor User edits that are not yours to commit. .apm/and.claude/are tracked per convention; the Manager commits APM artifacts, Workers do not stage them (Task Logs are written to disk only).
- For all code changes, run the full validation pipeline. Blocking
checks must pass clean before reporting a Task complete or
requesting commit approval:
ruff format --check app/ teamode.py tests/ruff check app/ teamode.py tests/.venv/bin/python -m pytest tests/pyright(bare, no flags — configured inpyproject.toml).LLMAO/scan_injection.sh .apm
- Any blocking-check failure halts completion. Zero-error target — fix root causes rather than bypassing (no disabling rules, no skipped tests).
- For changes affecting user-visible Discord behavior (slash command shape, embeds, button rows, modals, message deletion, voice playback, voice channel status): flag the change as requiring a manual Discord smoke test and note it explicitly in the completion report.
- When a Task's completion requires user-facing verification, include
in the completion report:
- Paste-ready launch:
If the change lives in a git worktree, the
cd ~/WSL/github.com/jonathan-fang/teamode && \ source .venv/bin/activate && python3 teamode.py
cdpoints to the worktree path. - In-Discord steps: which server, which voice channel, which
command, expected behavior, expected SQLite row state with a
paste-ready query, e.g.
sqlite3 sessions.db "SELECT id,status,duration_minutes,started_at,ended_at FROM sessions ORDER BY id DESC LIMIT 5;" - If a step needs a temporarily lowered constant (long timeouts),
give the exact edit in
app/constants.pyand an explicit reminder to revert it afterwards.
- Paste-ready launch:
- Reduce friction: every smoke test is either a single paste-able command or a numbered checklist. No ambiguity.
- When a Task requires user-provided input (Discord token, server access or permissions, asset review, judgment-call approval), return Partial with a specific request rather than blocking.
- Requests must be concrete: exact commands to run, expected output shape, file format expected, decision being asked.
Commit format, versioning, test runner, package structure, test
patching, async patterns, and TeaMode-specific rate-limit / voice /
SQLite rules are defined in .project-meta/conventions.md. Discord
surface rules (palette, embed formatting, custom_id namespace
teamode:<session_id>:<purpose>[:<value>], authorization) are in
.project-meta/UI-ADR.md. Read and follow both directly; do not
duplicate them here.
Agent-specific additions (not in conventions.md):
- No
Co-Authored-By, no "Assisted by Claude" trailer, no attribution lines of any kind in commits. - Always use
.venv/bin/python -m pytest tests/. - Refer to the Python package as
app/(the repo root directory is also namedteamode/).
- Discord-free logic (formatting, rate limiting, stats aggregation,
message classification, session state) lives in flat modules under
app/with nodiscordimports and injectable clocks, so it is unit testable.app/discord_bot/holds only Discord wiring: commands, views, event handlers, timer and lifecycle orchestration. - Every tunable number (durations, timeouts, limits, intervals, scan
depths, widths) and every string Ocha sends to Discord (messages,
embed titles/fields, button labels, slash-command descriptions, voice
channel statuses) lives in
app/constants.pyas a named constant, usingstr.formatplaceholders for variable parts. Palette hex values live there too. Never inline these in other modules.app/constants.pymust not importdiscordorapp.config. - Discord event handlers on the client must be named
on_<event>(discord.py routesclient.eventby function name).
- Annotate parameters and return types on every function in
app/andteamode.py(RuffANNenforces this;tests/exempt). Annotate local variables only when it adds information: a value that can be missing or have more than one type, or a non-obvious container shape. Let tooling infer the rest. - Do not use
cast()or# type: ignore. Narrow withisinstance, explicitNonechecks, or a more precise API (e.g.interaction.channel_id). The only exception is a genuine bug in upstream type stubs: then use# type: ignore[<specific-code>]with a one-line comment explaining why, and call it out in the completion report.
- Any message that will later be edited or deleted must be sent with
channel.send, or have its ID captured (interaction.original_response(),followup.send(..., wait=True)), and must be edited/deleted through the channel (channel.get_partial_message(id)), never through the interaction webhook — interaction tokens expire after 15 minutes and sessions outlast that. - Discord edit/delete/status calls that fail (
NotFound,Forbidden,HTTPException) are logged at WARNING and must not break the session flow. - Never let a stale button crash or mutate state: component interactions for missing or finished sessions get the standard "session no longer active" ephemeral refusal from constants.
- Every background task (
asyncio.create_task) must log unexpected exceptions withlogger.exception(try/except in the coroutine or a done-callback); no task failure may be silently dropped.asyncio.CancelledErroris expected on cancellation and must be re-raised, not logged as an error. - Log to stdout via the module
logger: INFO for lifecycle events (session start/end with state, break start/end/cancel, rate-limit refusals, cleanup counts); WARNING for degraded operation (missing permission, missing ffmpeg, HTTP 429, failed delete, invalid config);logger.exceptioninside error handlers.
- Preserve existing discord.py runtime behavior unless a change is explicitly within task scope.
- Do not add dependencies. The only approved addition for this
project is Pillow, dev-only, in
requirements-dev.txt(never in runtimerequirements.txt). discord.py stays at 2.7.1. - Do not add LLM-generated or AI-written text to the bot's runtime
output (anything Ocha sends to Discord). Use only strings defined in
app/constants.pyor given verbatim in the Task Prompt. The ASCII teacup banner (TEACUP_BANNER) is a User-approved exception. - Do not perform destructive git operations (force push, reset --hard, branch -D, checkout/restore of files you did not change) without explicit User instruction.
- The working tree carries the User's own uncommitted edits (e.g.
README.md,TODO.md,changelog.md,docs/external-interest-log.md). Preserve them; never revert or overwrite them wholesale — make targeted edits only.
- Never commit
DISCORD_BOT_TOKEN,.envfiles, or any other credential to git..envis gitignored;.env.examplecarries stub values only. - Never log token values to stdout, stderr, or any file. If a token
must appear in a debug line, redact to last-four:
Token: ****{last_4}. - Never echo a token via
print(), an error message, a Discord message, or a commit message. - Tests must not require a live Discord token. Use the
FakeInteraction/AsyncMockpatterns from § Test Patterns below.
These rules apply to every test you write. Full reference:
.project-meta/conventions.md § Testing and
.LLMAO/test-patterns.md. Embedded essentials:
- SQLite: use
sqlite3.connect(":memory:")for tests that exercise the database. Do not mock the SQLite layer — exercise the real query path. - Async: use
AsyncMock(notMagicMock) for any awaitable mock.MagicMockreturns aMagicMockfromawait, which silently breaks async paths. - Discord: never hit a live Discord gateway in any test. Use a
FakeInteractionfixture that exposes only the attributes the code under test reads. UseMagicMock(spec=discord.VoiceChannel)(etc.) soisinstancenarrowing works. - Voice: mock
voice_client.play; do not shell out toffmpegfrom a test. - Time: never wait on real time. Use the injectable
sleep/monotonicseams (FakeClockintests/test_session_countdown.py), patchedasyncio.sleep, or an injected clock; use fixed timezone-aware datetimes for date logic. - Patch where used, not where defined: if module A imports
helperfrom module B, patchA.helperin tests targeting A — notB.helper. Bot code lives inapp.discord_bot.<module>; patch there.
} //APM_RULES