This file provides guidance to Codex when working with code in this repository.
Keep this repository guidance synchronized between AGENTS.md and CLAUDE.md; only the title and agent introduction should differ.
Do not create Git commits unless the user explicitly asks you to commit. Requests to implement, edit, fix, or test changes do not authorize a commit. Leave changes uncommitted for the user to review and commit themselves.
Python 3.11+ and uv are required. Run commands from the repository root.
uv sync
cp template.env .env # first setup only; fill in credentials
docker compose up -d db neo4j # backing stores for local development
uv run python -m scripts.login # interactive, one-time anon.session setup
uv run alembic upgrade head
uv run uvicorn app.main:app --reloadPostgres stores history, chat state, structured user profiles, traits, weekly activities, model selections, and proactive schedules. Neo4j stores Graphiti world-view and per-user fact episodes. Compose maps Postgres to localhost:5433 and Neo4j Bolt to localhost:7687. Inside Compose, use db:5432 and neo4j:7687; docker-compose.yml supplies these overrides for the app service. The bare default in app/config.py uses Postgres port 5432, so use the local URL from template.env for the Compose-backed database.
anon.session and bot.session are Telethon session files. Login establishes Rachel's session interactively; the application lifespan starts both clients with the configured TELEGRAM_BOT_TOKEN. .env, session files, and local Phoenix credentials are ignored by Git.
# Migrations and lightweight syntax checks
uv run alembic revision -m "describe schema change" # blank revision; edit explicitly
uv run alembic downgrade -1
uv run alembic heads
uv run python -m compileall -q app scripts alembic
# Python regression tests and zero-build dashboard tests
uv run python -m unittest discover -s tests
node --test tests/*.cjs
# Graphiti helpers; require Neo4j and configured Gateway credentials
uv run python -m scripts.add_worldview_fact "Chagee is a bubble tea brand"
uv run python -m scripts.ingest_worldview_md path/to/facts.md
uv run python -m scripts.ingest_user_facts_md 12345 # reads user_fact_12345.md
# Workflow PNGs (requires Mermaid image rendering access)
uv run python -m scripts.draw_graphsTests use standard-library unittest with mocks at model/Telegram boundaries. Imports need valid app settings; placeholder API credentials suffice for unit tests. Set TEST_DATABASE_URL to a disposable Postgres database to include database integration tests; otherwise those tests skip. They create and remove isolated schemas. Node's built-in test runner exercises dashboard JavaScript without a browser. There is no lint configuration.
scripts.clear_graph wipes every Neo4j partition, including all user facts. It requires the exact typed confirmation delete everything unless --yes is supplied. Bulk ingestion is additive: Graphiti resolves duplicate entities/edges, but re-running an import creates fresh raw episodes.
Rachel is a FastAPI application with two Telethon clients and an in-process APScheduler on the same asyncio event loop. There is no Telegram webhook.
app/main.py: registers Phoenix instrumentation, seeds traits, weekly activities, and model roles, starts both Telegram clients, upserts Rachel's user record, then starts proactive scheduling. Shutdown stops and drains the scheduler, flushes buffers, disconnects the clients, and disposes the SQLAlchemy engine, in that order.app/telegram/client.py: incoming messages, reply timing, speech controls, buffers, memory finalization, and the shared reactive/proactive send path.app/telegram/bot.py: separateTelegramClient("bot", ...)restricted toADMIN_ID.app/services/llm.py: compiled reactive and proactive LangGraph workflows, lazy model clients, router decisions, retrieval passes, and responder validation.app/prompts.py: source-controlled system prompts, tool guidance, persona, moods, trait defaults, and profile schema. Prompts are no longer stored or edited in Postgres.app/services/web_search.pyandapp/services/url_fetch.py: bounded public web search through the async Exa SDK and public page reading.app/services/prompt_builders.pyandapp/services/context.py: pure prompt/context assembly and typed raw tool observations.app/services/proactive.py,app/services/proactive_schemas.py, andapp/services/proactive_tools.py: scheduler lifecycle, shared validation, and chat-scoped schedule tools.app/services/memory.py,app/services/worldview.py,app/services/userfacts.py, andapp/services/graphiti.py: conversation extraction and persistent memory.app/repository.py,app/models.py, andapp/database.py: data access and ORM models.session_scope()commits on success and rolls back on error; repository operations normally open their own session.app/calander.py(existing spelling) andapp/schedule_data.py: weekly activity queries, calendar tools, and seed data.app/routers/admin.pyandapp/static/index.html: root-mounted REST API and a self-contained vanilla-JavaScript dashboard, with no build step.
GET / serves the dashboard and /metrics mounts the Prometheus ASGI application. The HTTP API and dashboard have no application authentication; deployment expects nginx basic auth or an equivalent access layer, including for metrics. The optional Compose phoenix service exposes its UI on localhost:6006 and receives app traces through the configured Phoenix endpoints.
Per-chat state is process-local: buffers, burst_sessions, pending_summaries, reply/flush tasks, reply/buffer locks, responded_watermark, pending_mention, and mute/silence state.
- On first use, seed the buffer from the latest 40 persisted history rows.
_append_to_buffer,_snapshot_context,_flush_chat, and bot-message insertion sharebuffer_locks[chat_id]so initialization, snapshotting, persistence, and buffer removal cannot drop concurrent appends. Do not hold this lock during LLM generation. - Usable incoming messages are buffered even when Rachel stays quiet. Non-text media gets a placeholder such as
[photo]or a sticker emoji; genuinely empty events are skipped. Sender records are upserted, with first names preferred for dialogue labels. - Every unsuppressed private or group message schedules a reply 7 seconds after the latest message. A direct mention/reply latches
pending_mentionacross the whole burst and forces admission past the router. Otherwise a 5% spontaneity draw can force admission; ordinary DMs and untagged groups use their respective router questions. DMs are not unconditionally forced replies. reply_locks[chat_id]serializes generation, sending, and bot-message insertion for reactive and proactive messages. Once the debounce expires,asyncio.shield(reply(...))lets the committed reply finish despite later timer cancellation._generate_response_with_retryretries a failed graph invocation once within the same trace._replysnapshots the latest 40 messages and participant ID-to-name map. It passes the previousresponded_watermarkto the graph, then advances the in-memory watermark to the newest snapshot ID before generation. A no-new-user-message guard overrides even a forced reply; router-suppressed turns have still been considered.- After generation, each nonblank reactive reply makes one independent 15% delivery draw. Replies containing URLs or totaling 650 characters or more use text. With
ELEVENLABS_API_KEYconfigured, the voice branch prefixes the full reply with[Strong Singapore Accent]in the ElevenLabs request while showing Telegram's recording action. Synthesis is bounded to 60 seconds and 10 MiB. Rachel uploads one captionless Ogg Opus voice note from memory. Synthesis failures fall back to text. An upload failure or missing confirmation propagates as a delivery error. - Text delivery splits on
\n\ninto Telegram bursts. Later paragraphs simulate typing at 22 characters/second. Scheduled outreach, commands, and confirmations use text. Under the reply lock, Rachel inserts the original full reply and itsreasonby the voice note ID or the first text message ID. If a concurrent flush already removed the buffer,_append_or_persist_bot_messagewrites the reply directly to Postgres. - Summary changes remain in
pending_summariesuntil flush._load_summary_and_moodrestores persisted summary/mood when needed and normalizes an absent summary to"". - After 180 seconds (3 minutes) of inactivity,
finalize_conversationatomically snapshots and flushes the buffer and pending summary/mood, detachesupdate_memories, and advances the durable memory watermark to the newest snapshot ID. Reaching 150 buffered messages schedules immediate finalization on every incoming event, including quiet groups. Shutdown calls_flush_chatdirectly and does not start memory extraction.
Strict, case-insensitive group controls use BOT_NAME (normally Rachel) followed by an action. Rachel shush, Rachel shut up, Rachel quiet, Rachel silence, or Rachel stop talking arms a 15-minute silence; Rachel mute mutes indefinitely. Rachel speak or a direct mention/reply clears both modes. Muted/silenced chats keep buffering and flushing while skipping reply scheduling. Confirmations wait behind the reply lock; mute confirmations check a transition token to suppress stale confirmations. Speech suppression is not persisted across restarts.
_graph and _proactive_graph are compiled once at import. With new user content, the checker either forces admission or consults the router. Admission starts the summarizer and first fetcher pass in parallel. The fetcher loops directly in the reply graph, and the deferred responder runs once after both branches finish.
START -> checker -> router (unless forced) -> admission
| | no reply -> END
| no new -> END
admission -> summarizer ------------------------------------> responder -> END
-> context_fetcher -------------------------------> responder
| ^ finished or capped
+---+ collecting
- Router: direct
httpxPOST tomerge_gateway_base_url + "/decisions", with a 15-second timeout and modelDECISIONS_LLM(defaulttypesafe/jev-1.13). It sees the summary and latest 15 messages, choosesROUTER_PRIVATE_QUESTIONorROUTER_GROUP_QUESTION, validates a finitenoulprobability in[0, 1], and replies at >= 0.7. Errors or invalid answers fail open. Rejection skips summarization, collection, and response generation. - Summarizer: uses
SUMMARIZER_SYSTEM_PROMPT, the entire undivided history, and a dynamic mood enum fromCONVERSATION_STYLE. It requests a summary of at most 200 words, orNILto preserve it, plus mood. Failures preserve existing state. The joined responder sees successful current-call summary/mood updates;get_responsesaves mood in_chat_moodfor later calls. Summarization runs once, outside retrieval. - Context fetcher: independently assesses the current task, captured existing summary, history and accumulated observations.
CONTEXT_TOOLScombinesCALENDAR_TOOLS,search_world_view, name-basedsearch_user_info_tool,PROACTIVE_TOOLS,fetch_url,search_web,send_poll, and fetcher-onlyfinish_context. Each pass makes one model call and executes the selected tool batch sequentially under a shared 30-second deadline. Completed results survive later timeouts. Search usesweb_search; page reads useurl_content. - Completion: exactly one
finish_context(reason)call, alone, ends collection. Reason must be trimmed and nonempty; extra arguments are rejected. Completion is control, never retrieval evidence or an external operation. The reason stays incontext_reasonfor tracing and is excluded from responder evidence. Lookup/action plus completion, duplicate completion, malformed arguments/calls, missing calls and prose-only output become error observations before any batch actions execute; corrective passes are allowed within budget. Assistant prose is never a visible reply. - Observations and state: raw
ToolObservationrecords accumulate pass number, tool name, arguments, category, status (success,empty,error,timeout), and raw result. Unknown tools, ambiguous names, model/protocol failures and tool failures remain explicit observations. Never collapse failures into successful empty facts or replace raw observations with an LLM summary.context_statusiscollecting,finished, orcapped; there is no responder decision or retrieval request. The checker capturescollection_summaryfor all fetcher passes. The summarizer writescurrent_summaryandmoodfor the responder. - Responder: runs once after collection and summarization join, receiving summary, traits, current mood/style, SGT time, history and accumulated observations.
ResponseOutputrequires only sendablecontentandreason, forbidding extra keys. It cannot request retrieval or route the graph. Preserve medium reasoning, strict JSON schema,include_raw=True, local validation and visible-content salvage without exposing internal fields. Parse fallback never restarts retrieval. - Bounds/recovery:
AGENT_MAX_CONTEXT_PASSESdefaults to 8, accepts 1–10, and counts every fetcher model attempt including completion and failures. Another iteration requires unused pass budget and more than two reply graph steps, reserving room for the responder. The last permitted tool batch executes before a cap and valid final-pass completion wins over the cap. Cancellation propagates. The workflow recursion limit is2 * passes + 8, with emergency graph recovery and whole-graph Telegram retry handled separately. - Outcomes and repeated actions:
first_passmeans explicit completion on pass one,retriedexplicit completion after multiple passes, andcappedapplication-enforced termination. Aggregate loop metrics run once at the outer entry point. Normalized search and URL attempts retain their allowances and reuse all outcomes. Exact repeated schedule mutations are deduplicated by tool name and validated arguments even after errors/timeouts; read-only listings remain refreshable. Shielded writes remain unconfirmed until later evidence establishes their outcome.
get_response(...) returns (response_text, response_reason, new_summary_or_none, elapsed_seconds). Router rejection returns empty response text. The reason is persisted in History.reason.
History partitioning: _build_history_messages emits concrete AIMessage/HumanMessage objects so literal braces in dialogue are not template placeholders. With a reply watermark, all Rachel turns and handled user messages are context above the divider; only non-bot messages with a greater Telegram ID appear below it. Relative order within each partition is preserved. Without a watermark, fall back to one past Rachel's last turn. Summarization receives no divider. This reply watermark is separate from the persistent extraction watermark.
RESPONDER_SYSTEM_PROMPT, SUMMARIZER_SYSTEM_PROMPT, and extractor prompts come directly from app/prompts.py. Responder/summarizer HTTP endpoints and bot commands expose them read-only; prompt setters and the system_prompts ORM/repository paths were removed.
build_context_fetcher_prompt derives tools from the actual tool objects and their complete docstrings, adding only matching CONTEXT_FETCHER_TOOL_GUIDANCE. context_fetcher_dynamic_prompt supplies participants, SGT time, and optional outreach intent through LangChain middleware. build_responder_context uses RESPONDER_TOOL_RESULT_SECTIONS; failures stay in retrieval status, and looked-up empty profiles show unknown slots as NIL. Prompt, collection-state, and responder-status messages are assembled separately so runtime data is not re-templated.
The LLM-facing personal-memory tool is search_user_info(name, query). The fetcher resolves normalized participant names to real user IDs and rejects missing or ambiguous matches. It calls userfacts.search_user_info(user_id, query) to obtain Graphiti facts and the structured profile together. The responder does not independently load schedules or participant memory.
Merge Gateway has two distinct base paths:
MERGE_GATEWAY_OPENAI_BASE_URL(defaulthttps://api-gateway.merge.dev/v1/openai) for chat completions.MERGE_GATEWAY_BASE_URL(defaulthttps://api-gateway.merge.dev/v1) for embeddings and router decisions.
Reply clients and the three memory extractors use MERGE_GATEWAY_API_KEY. Graphiti's internal LLM/embedder/reranker use MERGE_GATEWAY_GRAPHITI_API_KEY, falling back to the main key through settings.graphiti_api_key.
Client settings differ deliberately: summarizer and memory extractors use method="function_calling" with reasoning_effort="none"; the context fetcher binds real tools with medium reasoning; the responder uses medium reasoning and strict method="json_schema", include_raw=True, followed by local Pydantic validation. Preserve these distinctions and validate model-route capabilities when switching models. Persisted Telegram history contains visible reply text only.
llm_models is the catalog and active_models selects main, small, and embedding roles. Startup seeds configured models and missing active rows without overwriting admin selections. Lazy reply/extractor clients are cached by active main model. set_active_model invalidates the active-model cache, reply/extractor clients, and Graphiti singleton so subsequent calls rebuild without a restart. Model IDs use provider/model; config accepts legacy OPENROUTER_MODEL, OPENROUTER_SMALL_MODEL, and OPENROUTER_EMBEDDING_MODEL aliases. The JEV router model remains separately controlled by DECISIONS_LLM.
ProactiveSchedule persists name, Telegram chat ID, five-field cron, IANA timezone (default Asia/Singapore), outreach intent, enabled state, repeat limit/counter, timestamps, and last status/error. validated_schedule_data trims fields, validates cron/timezone, and verifies Telegram access; HTTP handlers and agent tools share it.
start_proactive_scheduler rebuilds enabled, non-exhausted jobs from Postgres using AsyncIOScheduler. Jobs use max_instances=1, coalescing, and 60-second misfire grace. run_schedule also uses a process-local per-schedule lock. claim_proactive_schedule_run atomically increments repeat_completed, records running, and commits before generation or sending. Attempts, including failed and manual runs, consume allowance. repeat_times=null means unlimited; a positive integer is the total lifetime allowance, and 1 provides one attempt. Updates preserve the consumed counter; enabling an exhausted schedule does not reset it. Manual runs may bypass enabled but still enforce the limit.
send_proactive_message waits for delayed reactive replies and timed silence, acquires the shared reply lock, and rechecks conditions. Indefinite mute fails the attempt. It loads summary/history through the normal lifecycle, generates outreach, sends paragraph bursts, inserts or directly persists the bot message, and resets the inactivity timer.
_proactive_graph starts at context_fetcher_node and uses the same conditional self-loop before responder_node. It has no checker, router, summarizer, or mood writes. get_proactive_response returns (text, reason, elapsed_seconds). The intent is a new system-level trigger; every history item is prior context. A fired reminder must deliver its intent now, rather than answer the old setup request or recreate its schedule. build_proactive_trigger takes an explicit fetcher/responder role: the fetcher gathers evidence and finishes collection; the responder composes outreach. Both receive the current trigger after history. New schedule actions require authorization from the current intent.
PROACTIVE_TOOLS exposes create, list, update, enable/disable, and delete operations to the fetcher. Tools obtain current_chat_id from application-supplied RunnableConfig, never model arguments, and scope reads/writes to that chat. Preserve these ownership checks. There is no agent-facing run-now tool. Tool create/update explicitly requires repeat_times (positive integer up to 99999 or null); HTTP permits positive Postgres bigint limits. Updates replace editable details and enable the job; toggling preserves them.
Agent schedule writes use apply_schedule_mutation so persistence and live job synchronization finish in a strongly referenced shielded task even if the context pass times out. Shutdown pauses future occurrences and drains active executions and mutations before Telegram disconnects.
app/services/poll_tools.py exposes send_poll(question, options) as an ordinary tool in the shared reactive/proactive context fetcher. Its strict schema rejects extra fields and incorrect types. Inputs are trimmed: questions contain 1–300 characters, with 2–10 distinct options of 1–100 characters each (case-insensitive duplicate detection). Internal line breaks are preserved. Chat ID and delivery runtime come only from application context. Polls always have public voters, multiple choice enabled, quiz/closed disabled, and use the locked Telethon TextWithEntities, hash=0, a random 60-bit ID and unique option bytes.
The caller supplies a per-turn PollDeliveryRuntime while holding its existing reply lock. Sending uses Rachel's client with a 10-second timeout inside the context-pass deadline. Confirmation requires a returned Telegram message ID. Confirmed receipts and uncertain attempts are keyed by the trimmed question and ordered options. Identical calls reuse their confirmed text result across passes and whole-graph retries, completing any pending history recording without resending; distinct polls are independent tool calls. Transport failures, timeouts, cancellation, and server-side or unknown RPC errors leave that poll uncertain and prevent automatic resend in the same turn. Definite request/auth/permission/rate-limit rejections remain ordinary failures.
A successful poll send returns an ordinary string containing [Poll sent], Question: <question>, Options:, and numbered options in sent order, separated by newlines. The context fetcher records that string as its normal successful observation and continues later tools in the batch. The fetcher reviews the observations on a later pass and calls finish_context alone when ready. After collection finishes or reaches its cap, the responder runs once, using the observations to compose a separate text reply.
Each confirmed poll is recorded once through _append_or_persist_bot_message, using Rachel's sender ID, the actual Telegram message ID, the same formatted text returned by the tool, and reason Sent a poll requested by the context fetcher. Buffers, Postgres history, summaries and memory extraction use this ordinary message text. Telegram receives the poll and the responder's separate text reply; the poll placeholder itself is never sent as a text message. Before releasing the reply lock, each caller also drains pending confirmed-poll history writes, so transient recording errors or context-pass cancellation recover without another model tool call. No poll objects or new database fields are stored. Vote tracking, results, editing and closing polls are outside this feature.
search_web(query, num_results=5) uses exa-py's AsyncExa with type="auto". Optional EXA_API_KEY is passed explicitly; an absent key allows startup but errors when invoked. The SDK client is created per call and closed with its async context manager. Both the total tool deadline (asyncio.timeout) and SDK network timeout are 15 seconds, within the 30-second context-pass deadline. SDK timeouts become timeout observations; cancellation propagates. Authentication, HTTP, rate-limit, malformed-response and network errors are sanitized without application-level retries.
Queries are trimmed and nonblank; result counts must be integers 1–20. The model-facing schema exposes only query and num_results, and rejects a mode argument. Output format is a developer choice: SEARCH_MODE in app/services/web_search.py defaults to "text" for highlights; change it to "urls" in code for links only. URL mode returns URL strings and passes contents=False so the SDK omits contents on the wire (omitting the SDK argument would request full text). Text mode uses contents={"highlights": True} and returns source URLs, optional titles, highlights capped at 2,000 combined characters per result, and a local truncation flag. No Exa-generated summaries or answers are requested. Missing excerpts retain links and count as success; an empty results list is empty.
The shared reactive/proactive fetcher reserves up to AGENT_MAX_SEARCH_ATTEMPTS Exa attempts per graph run (default 4, nonnegative; 0 disables attempts) before awaiting, counting errors and cancellations. The fetcher receives the configured limit in its runtime collection state. It validates and resolves defaults before deduplicating normalized (query, num_results) sets across passes; identical earlier attempts are reused regardless of outcome. Invalid arguments do not consume attempts. The Telegram whole-graph retry starts with fresh observations and allowances, so searches and URL reads may repeat after a restart; there is no cross-run retrieval session. Search does not automatically fetch pages: one tool-selection call runs per pass, and dependent reads of discovered URLs require a later pass. The default stays eight passes, with a separate two distinct normalized URL attempts allowance per graph run.
fetch_url supports public HTML, plain text, Markdown and JSON, including +json; HTML becomes readable Markdown off the event loop. It enforces a 10-second total deadline, three redirects, 2 MiB decoded response limit and 12,000-character output cap. Standard HTTP/HTTPS ports and public destinations only; redirects are revalidated and DNS connections pinned. It cannot execute JavaScript, log in, parse PDFs or crawl.
split_url_observations partitions both search and page observations before prompt projection. Their payloads, titles, excerpts and errors reach each agent once in a separate, explicitly untrusted human data message, outside system instructions. Preserve this boundary, raw observations, partial successes, and all existing statuses. Search is for relevant missing public facts, not ordinary conversation or a substitute for participant memory.
update_memories(conversation, summary, chat_id, last_processed_id, ...) divides a finished snapshot once using build_extraction_history, then concurrently runs world-view and user-memory workflows. The full transcript stays available, with EXTRACTOR_DIVIDER before messages newer than chat_state.last_processed_message_id. With has_new=False, extractors skip model calls. finalize_conversation advances this watermark when handing off the snapshot, not after successful extraction.
- World view:
fact_extractor_node -> ingest_node, or END with no new general facts. Each fact becomes an episode undergroup_id="worldview". - Personal facts: an independent extract/ingest branch writes episodes under
user_facts_group_id(user_id), yieldinguser-facts-<id>. Models identify people by sender name;update_user_factsbuilds the name-to-ID map from the entire raw conversation, excluding Rachel, and drops unrecognized names. - Profiles:
profile_extraction_update_noderuns in parallel with personal fact extraction and writes JSONB inuser_facts_preferences. Cached existing profiles provide prompt context, but authoritative profiles are read from DB under per-user_user_locks. The merge keeps known keys and overwrites only newly extracted non-empty values, then writes through the profile cache. There is no LLM consolidation step.
USER_PROFILE_FIELDS in app/prompts.py is the schema source for dynamic Pydantic models, guidance, rendering, and the admin field list. Slot changes need no migration. The profile cache in llm.py batches misses and caches empty profiles for 5 minutes. Admin profile writes/deletes bypass write-through and can remain stale for that TTL. Lazy imports from userfacts back to llm avoid a circular import. Logs distinguish [userfacts] and [userprofile].
app/services/graphiti.py owns a lazy, double-checked singleton, _graphiti_lock, ingestion, search, and episode listing. Ingestion is sequential and awaited under the lock; one episode involves several model round-trips, and Graphiti handles entity deduplication and temporal edge conflicts. Both memory entry points catch/log failures so they cannot crash reply handling.
search_graph uses COMBINED_HYBRID_SEARCH_RRF scoped to a group ID and returns deduplicated edge facts plus verbatim episodes, excluding node summaries. Default search errors return empty text; the reply loop passes raise_on_error=True to produce failure observations. list_episodes returns all episode bodies oldest first for admin inspection.
Preserve Graphiti's compatibility workarounds:
_RetryingOpenAIGenericClientusesstructured_output_mode="json_object"to embed schema guidance, but_build_response_formatreturns OpenAI'sNOT_GIVENso HTTP requests omitresponse_format.- Malformed JSON and schema failures share three corrective retries after the first attempt. On exhaustion, malformed JSON raises
ValueErrorso outer transport retries cannot restart the format budget; schema-invalid dictionaries are counted and passed through for Graphiti validation. Only terminal failures are recorded. - The configured OpenAI reranker expects log probabilities the Gateway does not supply. Current RRF search does not invoke it; switching to a cross-encoder reranker requires addressing that incompatibility.
History uses the composite primary key (chat_id, telegram_message_id) because Telegram IDs are only unique within a chat. Batch inserts validate parallel list lengths and ignore duplicate conflicts. get_history returns ascending ID order, resolving names as first name, username, then numeric ID, and includes reason. created_at is timezone-aware and server-defaulted.
ChatState holds summary, mood, and the durable extraction watermark. Summary/mood and watermark upserts update their own fields; the empty summary default permits a watermark-first row for chats Rachel has not answered.
ensure_traits_seeded upserts DEFAULT_TRAITS by name, refreshing order and prompt text while preserving admin-selected levels. get_active_trait_prompts caches the rendered block for 5 minutes; seed/set/reset invalidate it immediately.
ScheduleActivity represents one weekly instance, unique on (day_of_week, start_hour), with day 0=Monday and duration spanning hours or midnight. ensure_schedule_seeded inserts DEFAULT_SCHEDULE only into an empty table. Queries in app/calander.py open a session per call without caching; current activity also checks the previous day for overnight events. CALENDAR_TOOLS exposes current/today, day overview/detail, and day/hour queries using SGT and resolved day names. Weekly persona activities and proactive cron schedules are separate stores.
Alembic's current head is 0a7e2c9d4b61, after f9c6a1d4b3e8 (drops system_prompts) and e8b5d0f3a2c7 (repeat limits). Preserve deployed revision IDs and ancestry. The timestamp migration uses ADD COLUMN IF NOT EXISTS so existing timestamps survive. Downgrading prompt-table removal recreates an empty schema and cannot recover deleted prompts.
The HTTP API is root-mounted; app/routers/admin.py is the endpoint/schema source of truth.
- Read-only prompts:
GET /responder-system-prompt,/summarizer-system-prompt,/workflow-prompts(includes fetcher system prompt and collection-loop instructions). - Users/chats:
GET /users/names,/list-chats; chat names resolve concurrently through Telegram, with failures returning null names. - History/summary:
GETandDELETE /history/{chat_id}and/summary/{chat_id}. History GET accepts?page=1and returns up to 100 messages per page, with the newest page first.X-Has-Moreindicates whether an older page exists. - Profiles:
GET,PUT,DELETE /user-profile/{user_id};GET /user-profile-fieldsderives labels from the shared schema. - Facts:
GET,POST /user-facts/{user_id}and/worldview-facts. Ingestion is slow; there is no episode edit/delete API, and Graphiti resolves superseding facts during ingestion. - Traits:
GET /personality,PATCH /personality/{trait_id},POST /personality/reset. - Models:
GET,POST,DELETE /models(delete uses amodel_stringquery parameter);GET /active-models,PUT /active-models/{role}. - Outreach:
GET,POST /proactive-schedules;PUT,DELETE /proactive-schedules/{schedule_id};PATCH /proactive-schedules/{schedule_id}/enabled;POST /proactive-schedules/{schedule_id}/run. Responses include consumed attempts, last status/error, and next run time; exhausted jobs have no next run. - Diagnostics:
GET /healthis a simple status response;GET /architecturerenders diagrams off the event loop and returns base64 PNGs, surfacing rendering failures as HTTP 502.
The admin bot supports prompt getters, user/chat inspection, history/summary get/delete, user-fact add/list, profile get/delete, trait list/set/reset, and model management (/list_models, /list_active_models, /add_model, /delete_model, /set_active_model). It has no prompt setters. Specific handlers with broad usage fallbacks raise events.StopPropagation after handling valid commands. Rachel's client also implements a private-chat /clear_history handler.
app/services/metrics.py defines rachel_llm_calls_total{node} and rachel_llm_errors_total{node,kind} for router, summarizer, context fetcher, responder, world-view extractor, user-fact extractor, profile extractor, and internal Graphiti LLM calls. Attempts count at invocation; errors count at terminal failure/skip boundaries. record_llm_empty_output covers missing parsed extractor results. Error kinds distinguish timeout, output parsing, HTTP status (including codes embedded in validation errors), schema validation, and other failures. Recovered Graphiti retries and cooperative responder cancellations do not count as errors. Graphiti embeddings/reranking and non-LLM infrastructure failures are outside these counters.
rachel_agent_loop_outcomes_total{outcome} and the rachel_agent_context_passes histogram cover admitted reactive/proactive loops. Phoenix workflow spans (telegram_reply, telegram_proactive, memory_update) are separate roots, grouped with a burst_sessions session ID when available and Telegram chat metadata. The direct JEV call has a manual LLM span including validated probability, model/provider, and available usage/cost.
Direct world-view and user-memory lookups use traced_tool_call in app/services/tracing.py to create TOOL spans under the active context-fetcher span in both reply graphs. Spans preserve session/chat metadata and record tool name/call ID, arguments, retrieval pass/category/status, and complete results (text or JSON, including personal facts and the structured profile). Empty results, errors, and deadline cancellations retain their observation status/output; errors and timeouts mark the tool span as failed without changing reply handling. Calendar, proactive-schedule, and URL tools keep their automatic LangChain spans to avoid duplicates.
- Run one app process: Telethon sessions, timers, caches, locks, and APScheduler are process-local; Prometheus uses the default single-process registry. Multiple workers would duplicate scheduling and require coordination.
- Multi-paragraph replies persist as one row under the first paragraph ID, so interleaved messages cannot be represented at per-paragraph granularity.
- Reply watermarks and speech suppression are process-local. Extraction watermarks persist but advance at handoff; failed extraction is not automatically replayed.
- Changing embedding dimensionality does not re-embed or rebuild existing Neo4j data automatically.
scripts/draw_graphs.pyloads the reactive retrieval-loop and explicit-join diagram fromscripts/graph_llm.mmdand registers four workflows. Rendering requires access to the Mermaid image service; failures surface as HTTP 502 from/architecture. Existing root PNGs may be stale.WORLDVIEW_PATHis legacy configuration; facts live in Neo4j.Reference/is an ignored local copy of the original implementation and may be absent in a fresh clone.
Treat code as authoritative when older comments, README sections, deployment guides, or design specs disagree. Update both instruction files together when architecture or development commands change.