Session & Delivery
Responsibility
Two things: (1) Session — attribute "where the message came from" (platform, chat, sender) to a stable key, and persist the conversation to disk so the agent can continue with context across restarts; (2) Delivery Ledger — record a "delivery obligation" for the agent's final reply, so that after a crash undelivered replies can be identified and re-sent, guaranteeing the user ultimately receives them. Both live in the gateway layer, decoupled from the agent.
Key Files
class SessionSource:149-299— message source dataclass (platform/chat/sender, with hash)class SessionContext:299-700— session context (key, source, continuation strategy)id hashing:40-74—_hash_id/_hash_sender_id/_hash_chat_id(privacy + stability)path safety check:100-149—_is_path_unsafe/_is_session_key_unsafe(session keys flow into filesystem paths)auto_continue freshness window:26-40—auto_continue_freshness_windowdelivery ledger docs:1-58— design goal: best-effort, never blocks the main flow on failurecompute_obligation_id:146-155— unique obligation id (session key + message ref + content)state machine:155-203—record_obligation/mark_attempting/mark_delivered/mark_failedsweep_recoverable:203-276— scan recoverable obligations after a crash and re-sendSQLite connection:77-102—_db_path/_connect, persisted in local sqlite
Data Flow
- The adapter receives a platform event and carries
SessionSourceout viaMessageEvent:1759. SessionSource:149hashes out a stable chat/sender id (gateway/session.py:40).- A
SessionContext:299is constructed, yielding a session key (used as a disk path after thepath safety check:109). - The session key decides which file to load history from, fed to the agent to continue context.
- After the agent produces its final reply, the gateway records it in
record_obligation:155→ sends →mark_delivered. - If a crash happens during send, the next startup's
sweep_recoverable(gateway/delivery_ledger.py:203) scans for obligations inattemptingstate whose owning process is dead, and re-sends or marks them failed.
Summary
The session layer owns "which ongoing conversation this message belongs to", mapping external identities safely to disk files via hashing + path validation. The delivery ledger owns "whether the agent's intended final reply actually reached the user", using a sqlite state machine for crash recovery. Both are best-effort but critical reliability layers.