fix(gateway): route watch-pattern notifications to originating session#10460
Merged
Conversation
- Populate watcher_* routing fields for watch-only processes (not just notify_on_complete), so watch-pattern events carry direct metadata instead of relying solely on session_key parsing fallback - Extract _parse_session_key() helper to dedupe session key parsing at two call sites in gateway/run.py - Add negative test proving cross-thread leakage doesn't happen - Add edge-case tests for _build_process_event_source returning None (empty evt, invalid platform, short session_key) - Add unit tests for _parse_session_key helper
…rence _parse_session_key() now extracts the optional 6th part (thread_id) from session keys, and _notify_active_sessions_of_shutdown uses _parsed.get() instead of the removed 'parts' variable. Without this, shutdown notifications silently failed (NameError caught by try/except) and forum topic routing was lost.
This was referenced Apr 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Salvage of #10446 by @kshitijk4poor (itself a salvage of #9537 by @etcircle). Fixes background process watch-pattern notifications leaking into whichever Discord/Telegram thread is active when the queue drains, instead of routing back to the thread that originally started the process.
Root cause
_inject_watch_notification(synth_text, event)in gateway/run.py passed the current foreground event (whatever thread the user just messaged from) instead of the process's own routing metadata. Additionally,terminal_tool.pyonly populated watcher routing fields whennotify_on_completewas set — watch-pattern-only processes had empty routing metadata.Changes (from #9537 + #10446)
session_key+ routing fields (platform,chat_id,thread_id, etc.)_build_process_event_source(evt)resolves the correctSessionSourcevia 3-tier fallback: session store → session key parse → event dict fields_inject_watch_notification()takes the queue event dict instead of the foreground event_run_process_watchercompletion path also uses_build_process_event_sourcenotify_on_complete OR watch_patterns(was onlynotify_on_complete)_parse_session_key()helper — Extracted duplicated session key parsing, used by shutdown notifications and process event routingFollow-up fixes (our commit)
_parse_session_keyincludes thread_idpartsreference fixed_notify_active_sessions_of_shutdownreferenced the removedpartsvariable — NameError was silently caught by try/except, breaking all shutdown notifications_parse_session_keytests updated to verify thread_id extraction; added user_id-part testTest results
Based on #9537 by @etcircle and #10446 by @kshitijk4poor. Closes #9532.