Skip to content

Heartbeat isolated session loses threadId from base session deliveryContext #65693

@richardmqq

Description

@richardmqq

Description

When a heartbeat runs with isolatedSession: true on a Telegram topic-scoped session, the newly created isolated session loses the threadId from the base session's deliveryContext. This causes heartbeat responses to be delivered to the group's General topic instead of the correct topic.

Steps to Reproduce

  1. Configure a Telegram group with topics enabled
  2. Have a topic-scoped session (e.g., agent:main:telegram:group:<groupId>:topic:1189)
  3. Enable heartbeat with isolatedSession: true
  4. Wait for heartbeat to trigger on that session

Expected Behavior

The heartbeat isolated session should inherit deliveryContext.threadId from the base session, so responses route to the correct topic.

Actual Behavior

The isolated session's deliveryContext is missing threadId. Responses route to the group's General topic instead.

Base session metadata:

{
  "deliveryContext": {
    "channel": "telegram",
    "to": "telegram:-1003577364307",
    "accountId": "default",
    "threadId": 1189
  }
}

Heartbeat isolated session metadata:

{
  "deliveryContext": {
    "channel": "telegram",
    "to": "telegram:-1003577364307",
    "accountId": "default"
    // threadId is missing!
  },
  "chatType": "direct"  // should be "group"
}

Root Cause (source code analysis)

In heartbeat-runner-*.js, resolveCronSession() is called with forceNew: true to create the isolated session. The new session entry explicitly sets deliveryContext: void 0 (line ~89):

...isNewSession && {
    lastChannel: void 0,
    lastTo: void 0,
    lastAccountId: void 0,
    lastThreadId: void 0,
    deliveryContext: void 0
}

This clears the delivery context for the new session. Later, when the heartbeat response needs to be delivered, it falls back through lastToorigin.to, neither of which carries the threadId.

Additionally, chatType is set to "direct" instead of inheriting "group" from the base session.

Additional Context

  • The MessageThreadId is correctly passed to the inbound message context (line ~708), but this doesn't fix the session metadata used for delivery routing.
  • This is separate from fix: canonicalize topic session transcript fallback #64869 (Telegram topic session path fix), which addressed inbound message routing, not heartbeat delivery context inheritance.
  • Version: 2026.4.11 (769908e)

Suggested Fix

When creating a heartbeat isolated session, inherit deliveryContext (including threadId) and chatType from the base session entry.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions