Skip to content

fix(gateway): prevent background process notifications from triggering false pairing requests - #5985

Closed
xingkongliang wants to merge 1 commit into
NousResearch:mainfrom
xingkongliang:fix/internal-event-bypass-pairing
Closed

fix(gateway): prevent background process notifications from triggering false pairing requests#5985
xingkongliang wants to merge 1 commit into
NousResearch:mainfrom
xingkongliang:fix/internal-event-bypass-pairing

Conversation

@xingkongliang

Copy link
Copy Markdown
Contributor

Summary

  • When a background process with notify_on_complete=True finishes, the gateway injects a synthetic MessageEvent to notify the session. This event was constructed without user_id, causing _is_user_authorized() to reject it and trigger the DM pairing flow — sending "Hi~ I don't recognize you yet!" with a pairing code to the bot owner's own chat.
  • Added an internal flag to MessageEvent that bypasses authorization for system-generated synthetic events. Only the process watcher sets this flag.
  • Includes 4 regression tests covering the fix and verifying normal pairing still works for real unauthorized users.

Changes

  • gateway/platforms/base.py: Add internal: bool = False field to MessageEvent
  • gateway/run.py: Skip _is_user_authorized() when event.internal is True; set internal=True on process watcher synthetic events
  • tests/gateway/test_internal_event_bypass_pairing.py: 4 new tests

Test plan

  • test_notify_on_complete_sets_internal_flag — synthetic completion event has internal=True
  • test_internal_event_bypasses_authorization_is_user_authorized is never called for internal events
  • test_internal_event_does_not_trigger_pairing — no pairing code generated for internal events
  • test_non_internal_event_without_user_triggers_pairing — normal unauthorized DM still triggers pairing (regression guard)
  • All 14 existing test_background_process_notifications.py tests pass

🤖 Generated with Claude Code

…g false pairing requests

When a background process with notify_on_complete=True finishes, the
gateway injects a synthetic MessageEvent to notify the session. This
event was constructed without user_id, causing _is_user_authorized()
to reject it and — for DM-origin sessions — trigger the pairing flow,
sending "Hi~ I don't recognize you yet!" with a pairing code to the
chat owner.

Add an `internal` flag to MessageEvent that bypasses authorization
checks for system-generated synthetic events. Only the process watcher
sets this flag; no external/adapter code path can produce it.

Includes 4 regression tests covering the fix and the normal pairing path.
@blasai1739217-cmyk

Copy link
Copy Markdown

Friendly bump on this PR in case it fell through the cracks \u2014 would love a review when someone has a minute. Thanks!

@nocoo

nocoo commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

We're also hitting this bug. When running Claude Code CLI or Codex CLI via terminal(background=true, notify_on_complete=true) from a Discord DM session, each process completion triggers a false pairing request ("Hi~ I don't recognize you yet!").

Reproduction: any notify_on_complete=true background process in a gateway DM session.

Root cause confirmed: the synthetic SessionSource at L6129 of gateway/run.py is missing user_id/user_name, so it fails _is_user_authorized() and defaults to chat_type="dm", which triggers pairing_store.generate_code().

Would love to see this merged — the internal flag approach looks clean. 👍

@jeanfbrito

Copy link
Copy Markdown

Me too, I was going to do the PR but found this here. This feature is awaited for me, and this fix is needed. I will be running from this PR until this gets merged.
Thank you @xingkongliang

@teknium1

teknium1 commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Merged via PR #6434. Your commit was cherry-picked onto current main with your authorship preserved in git log. Added a small follow-up fix for test env var isolation. Thanks @xingkongliang!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants