background_tasks

Asyncio-based background task scheduler.

Manages periodic background tasks that run alongside the bot: - Scheduled prompt tick / cleanup - Auto memory extraction - Periodic maintenance

class background_tasks.BackgroundScheduler[source]

Bases: object

Run named periodic tasks in the background.

Each task is an async callable that is invoked repeatedly with a configurable interval. Tasks are resilient to individual failures and log errors without crashing the scheduler.

Usage:

sched = BackgroundScheduler()
sched.register("my_task", my_coro, interval=300)
await sched.start()   # non-blocking, spawns tasks
...
await sched.stop()
__init__()[source]

Initialize the instance.

Return type:

None

register(name, func, interval, initial_delay=0.0, args=(), kwargs=None)[source]

Register a periodic task.

Parameters:
  • name (str) – Unique identifier for the task.

  • func (Callable[..., Awaitable[Any]]) – Async callable to invoke each cycle.

  • interval (float) – Seconds between invocations (measured from end of previous run).

  • initial_delay (float) – Seconds to wait before the first invocation.

  • args (tuple)

  • kwargs (dict | None)

Return type:

None

async start()[source]

Start all registered tasks.

Return type:

None

async stop()[source]

Cancel all running tasks and wait for them to finish.

Return type:

None

async background_tasks.scheduled_prompt_tick(redis)[source]

Check for due scheduled prompts and trigger them.

Return type:

None

Parameters:

redis (Any)

async background_tasks.scheduled_prompt_cleanup(redis)[source]

Remove old executed/cancelled scheduled prompts.

Return type:

None

Parameters:

redis (Any)

async background_tasks.auto_kg_extraction(redis, kg_manager, openrouter)[source]

Extract knowledge graph entities from recent conversations.

Return type:

None

Parameters:
  • redis (Any)

  • kg_manager (Any)

  • openrouter (Any)

async background_tasks.channel_summarization(redis)[source]

Summarise the 10 most recently active channels.

Return type:

None

Parameters:

redis (Any)

async background_tasks.kg_consolidation_task(kg_manager, openrouter)[source]

Consolidate duplicate entities in the knowledge graph.

Return type:

None

Parameters:
  • kg_manager (Any)

  • openrouter (Any)

async background_tasks.kg_decay_task(kg_manager)[source]

Apply relationship weight decay in the knowledge graph.

Return type:

None

Parameters:

kg_manager (Any)

async background_tasks.log_rag_ingest_task()[source]

Ingest recent journalctl logs into the stargazer_logs RAG store.

Return type:

None

async background_tasks.gemini_key_probe_task()[source]

Probe Gemini API keys to detect daily quota exhaustion.

Return type:

None

async background_tasks.agentic_kg_bulk_incremental_task(redis, kg_manager, openrouter)[source]

Agentic bulk KG extraction for the 10 MRU channels, incremental cursors.

Return type:

None

Parameters:
  • redis (Any)

  • kg_manager (Any)

  • openrouter (Any)

async background_tasks.anamnesis_digest_task(redis, kg_manager, openrouter)[source]

Digest Spiral Goddess RAG fragments into the Knowledge Graph.

Return type:

None

Parameters:
  • redis (Any)

  • kg_manager (Any)

  • openrouter (Any)

async background_tasks.startup_channel_backfill(redis, message_cache, embedding_queue, adapters, max_channels=10)[source]

Backfill recent history for the most active channels at startup.

For each of the top max_channels most-recently-active channels, fetches history from the appropriate platform adapter, deduplicates against Redis, caches new messages, and enqueues embeddings.

Returns a summary dict with channels_processed and messages_cached.

Return type:

dict[str, int]

Parameters:
  • redis (Any)

  • message_cache (Any)

  • embedding_queue (Any)

  • adapters (list[Any])

  • max_channels (int)

background_tasks.build_scheduler(redis=None, kg_manager=None, openrouter=None)[source]

Build a BackgroundScheduler with the standard tasks.

Only registers tasks whose dependencies are available.

Return type:

BackgroundScheduler

Parameters:
  • redis (Any)

  • kg_manager (Any)

  • openrouter (Any)