openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

[Cron] Route scheduled notifications to the right channel (not default web)

Open senamakel opened this issue 7 months ago • 1 comments

Summary

When a user creates a scheduled / cron reminder from Telegram (e.g. “remind me to drink water”), delivery should go back to Telegram (or the channel where the request was made) — not only / not primarily the in-app web stream. Today, proactive cron delivery effectively centers web and only mirrors to an external channel if config.channels_config.active_channel is set and the runtime has a non–web-only proactive subscriber, so users on Telegram can see nothing useful in Telegram and everything in the desktop web UI instead.

Current behavior (verified in code)

  1. cron_add default for agent jobs — If delivery is omitted, the tool uses mode: "proactive" with no channel / to (see src/openhuman/tools/impl/cron/add.rs tests, agent_job_defaults_to_proactive_delivery).

  2. Proactive routing — ProactiveMessageSubscriber (src/openhuman/channels/proactive.rs):

    • Step 1: Always publishes a proactive_message to the web Socket.IO bus (client_id: "system") so the in-app UI always gets the event.
    • Step 2: If active_channel is set and is not "web", also sends on the matching external Channel (e.g. Telegram) from the registered map.
    • The desktop / JSON-RPC path can register a web_only() subscriber (channels_by_name empty), in which case no external delivery exists at all.
  3. Explicit channel delivery already exists — DeliveryConfig supports mode: "announce" with channel + to; deliver_if_configured in src/openhuman/cron/scheduler.rs emits CronDeliveryRequested (see comments for “announce” vs “proactive”). The gap is defaults and agent/tool context, not a missing struct field.

  4. “Active channel” — config.channels_config.active_channel (see src/openhuman/config/schema/channels.rs) is the global knob the proactive path uses. It is not the same as “I’m talking to the bot on Telegram right now” unless we set it from user behavior or persist the origin on the job.

Problem

  • Wrong default UX: A reminder “born” in Telegram should not require the user to discover web-only notifications or a global active_channel setting.
  • Inconsistent with intent: The user’s mental model is “this reminder lives in this chat” — the product should support per-job delivery or origin-based defaults.
  • Security surface: Any to (chat id) for announce must be authorized (same user / allowed list) to avoid cross-user sends.

Solution direction (product + engineering; pick/merge)

  1. Per-job “reply channel” (preferred for clarity)

    • When the agent / user schedules from channel context (e.g. Telegram inbound dispatch), pass default delivery into cron_add as announce (or a new mode: "same_as_origin") with channel = telegram and to = verified chat / thread id from the inbound ChannelMessage (or stored binding for that user).
    • Tool schema / prompts should say: default deliver to the channel you’re using unless the user names another.
  2. Inherit on create

    • Extend the tool pipeline so cron_add receives source_channel + source_reply_target in JSON-RPC params from the channel layer (only when the tool is invoked from a channel turn), and persist them on CronJob (may require a small schema addition if not folded into delivery).
  3. Smarter proactive

    • Option A: For jobs created from an external channel, skip the always-on web proactive_message (or make it opt-in) and deliver only to the stated channel.
    • Option B: Keep dual deliver (web + external) but reorder so the origin channel is first and the primary UX in logs/docs.
  4. Active channel

    • On inbound Telegram message, optionally auto-set_active_channel(Some(\"telegram\"))) (with user consent in settings) so existing proactive jobs behave better without schema changes (weaker than per-job routing).
  5. Docs & agent prompts

    • Update cron_add descriptions so the model sets announce + channel + to for Telegram-originated reminders when the user asks for Telegram delivery; document channels_config.active_channel for proactive.

Acceptance criteria

  • [ ] A cron/reminder created from Telegram (test harness or manual) delivers to that Telegram chat by default, without requiring the user to use the desktop “web” view as the only surface.
  • [ ] No unauthenticated or cross-tenant to — announce targets are validated against the same user’s bindings / allowed_users (reuse existing channel auth rules).
  • [ ] Config / RPC — Clear story for web-only desktop vs full channel runtime (proactive vs announce behavior documented; no silent “everything web” for channel-native flows).
  • [ ] Tests — Unit or integration: cron job created with announce+telegram+chat id delivers via CronDeliveryRequested (mock channel) or equivalent; regression that default proactive still works for jobs that truly want in-app.
  • [ ] Docs — Short note in architecture or cron tool docs: delivery modes and when to use proactive vs announce.

Related files

  • src/openhuman/channels/proactive.rs — web-first + active_channel external send
  • src/openhuman/cron/scheduler.rs — deliver_if_configured (proactive vs announce)
  • src/openhuman/tools/impl/cron/add.rs — default proactive delivery for agent jobs
  • src/openhuman/cron/types.rs — DeliveryConfig
  • src/openhuman/channels/runtime/startup.rs — ProactiveMessageSubscriber::new / web_only registration

senamakel avatar Apr 26 '26 04:04 senamakel

Picking this up. Plan stays in scope: capture origin channel at cron-create time and feed it into DeliveryConfig so ProactiveMessageSubscriber routes back to the same channel (Telegram, etc.) instead of defaulting to web. Web stays as fallback when origin is unknown or the external send fails. Will also surface the target in the cron-create UI defaulting to current thread's channel.

Will open a PR against main shortly.

obchain avatar Apr 29 '26 14:04 obchain