Assistant Scheduler and Heartbeat
The scheduler and heartbeat systems are the proactive core of the dLANDiscord Assistant, enabling autonomous background tasks, periodic maintenance, and resilient system-wide audits.
1. The Persistent Scheduler (scheduler.go)
The Scheduler is a robust, SQLite-backed task runner designed for both precise one-off executions and recurring cron-based jobs.
Key Logic & Functions
NewScheduler(): Initializes the SQLite database (scheduler.db) and starts the tick loop (every 10 seconds).AddScheduledTask(): Persists a new task with its targetagent,prompt, andcronspec.checkAndExecuteDueTasks(): The core brain of the tick loop. It scans the DB for tasks whereexecute_atis in the past, spawns an asynchronous goroutine for execution, and updates the state toInProgress.ExecuteScheduledTask(): Spawns a persona-awareChatSessionand runs the ReAct loop. It implements strict Panic Recovery; if an execution crashes, the system catches the panic, logs it, and marks the task asFailedinstead of crashing the whole application.
Evolution: Zombie Cleanup
As seen in Git commit 4688e175, the system was enhanced with CleanupStaleRunningExecutions(). This function identifies "zombie" tasks (executions stuck in InProgress for more than 2x their timeout) and automatically terminates them to free up resources.
2. Heartbeat Loops (heartbeat.go)
The Heartbeat system manages "internal" proactive routines that don't necessarily originate from a user request.
| Function | Frequency | Purpose |
|---|---|---|
SovereignAudit() |
Hourly | Performs deep forensics on the system's logic layer, searching for recurring tool errors or logic failures. |
MemoryDistillation() |
Daily (02:00) | Triggers the MemoryMaster agent to perform a reflection pass on all session logs and distill long-term facts. |
triggerNikkiWorkLoop() |
Periodic | Proactively wakes up Nikki to check Dan's calendar (gcal), todo list, and any pending NOC alerts. |
triggerFamilyLoop() |
Periodic | Wakes up Lenore to synchronize the shared family calendar and post daily status updates to the Home HQ group. |
3. Wakeup API and Event Router (wakeup.go)
The Wakeup server (Port :7142) acts as the Unified Event Router - the bridge between external triggers and proactive agents.
Event Sources
| Source | Endpoint | Description |
|---|---|---|
| UniFi Presence | Internal polling | Home/away detection for Dan & Liz |
| YouTube | /api/events/youtube |
PubSubHubbub for channel notifications |
| Home Assistant | /api/events/ha |
State changes, voice commands |
| Vonage SMS | /api/events/vonage/inbound |
Inbound SMS processing |
| WHMCS Tickets | Internal polling | New support ticket alerts |
| CCTV/Reolink | Internal | Doorbell, person detection |
| Generic API | /api/wakeup |
External systems can trigger any agent |
Features
- Arrival Enrichment: When a user arrives home (detected via UniFi), the
triggerWelcomeHomefunction provides the agent with an "Arrival Snapshot"—a summary of all significant system events that occurred while the user was away. - Resilience: The
executeWakeupTaskfunction uses a 10-minute timeout and a circuit-breaker to prevent external network spikes (like a YouTube PubSub storm) from overwhelming the local LLM. - External Wakeup: Any external system can trigger any agent via POST to
/api/wakeup:
{ "agent": "nikki", "user": "dan", "platform": "whatsapp", "prompt": "..." }
Component Diagram: Proactive Pipeline
graph TD
S[Scheduler DB] --> |Tick| Runner[Task Runner]
H[Heartbeat Loops] --> |Signal| Runner
W[Wakeup API] --> |Signal| Runner
Runner --> |Spawn| Session[ChatSession]
Session --> |Execute| Agent[Nikki / Sasha / Oracle]
Agent --> |Result| Notify[Omnichannel Notifier]
subgraph "Error Recovery"
Runner --> Panic[Panic Recovery]
Runner --> Zombie[Zombie Cleanup]
end
Guidance for AI Agents
- Schedule Proactively: If a user asks you to "remind me in 2 hours," use the
schedule_tasktool. This persists the request even if the current session is closed or the server restarts. - Audit SITREPs: Review the
execution_logsin Mission Control to see how your background "twins" are performing their maintenance tasks.