bot-channel-runtime-split.md 7.0 KB

Bot Channel Runtime Split

Bot channel code is split into two layers:

  • BotsService owns product semantics: config, binding, authorization, workspace scope, command routing, task creation/resume, permission responses, elicitation responses, reply formatting, broadcasts, and persisted bot state.
  • Channel runtime modules own provider event consumption only: Telegram long polling, Weixin long polling, and Feishu/Lark WebSocket lifecycle.

The split keeps provider-specific event loops out of botsService.ts while preserving the same BotInboundMessage callback pipeline.

Runtime Modules

  • telegramChannelRuntime.ts

    • Syncs Telegram commands.
    • Owns Telegram getUpdates long polling.
    • Persists each Telegram offset only after the corresponding update returns a successful shared business callback result. A rejected callback or { ok: false } result leaves the update retryable.
    • Uses a runtime lock keyed by bot token so only one host process consumes updates.
  • weixinChannelRuntime.ts

    • Owns Weixin getupdates long polling.
    • Uses a runtime lock keyed by bot token so only one host process consumes and commits each batch.
    • Preserves attachment payloads when forwarding parsed messages back into the common provider callback pipeline.
    • Persists get_updates_buf only after every message in the batch returns a successful business callback result. A rejected callback or { ok: false } result leaves the batch retryable.
  • feishuChannelRuntime.ts

    • Owns Feishu/Lark WebSocket lifecycle.
    • Uses the selected provider id to keep Feishu and Lark domain behavior separate.
    • Uses a runtime lock keyed by provider domain and external App ID so local bot ids or credential references pointing to the same App cannot consume WebSocket events concurrently. The bot id is retained only as owner diagnostics.

Invariants

  • All inbound provider events still enter processProviderCallback(provider, payload) before business handling.
  • Bot task stream subscriptions continue to use bot-channel-continuous; this split must not opt bot channels into mobile replayable recovery semantics.
  • Remote workspace execution semantics are unchanged: normal messages do not reconnect remote workspaces, and remote runtime service resolution must not fall back to a local workspace service.
  • Runtime status remains written through the shared status sink so BotsDialog can show the same provider status messages as before.
  • Channel runtime disposal must abort all active polling/WebSocket controllers and await every runtime task before it resolves. The shutdown terminal state includes Provider request/WebSocket completion and cross-process lock release; no callback or cursor write may occur after shutdown resolves.
  • A runtime instance is bound to the provider connection identity, not only the bot id. A change to the provider, app id, credential reference, or referenced credential value must abort and await the old instance before the replacement starts, so stale and current connections never consume concurrently.
  • Persistent provider locks use a nonce-specific heartbeat lease in addition to PID checks. A reused PID cannot keep an abandoned lock alive; an owner that no longer renews the lease is eligible for bounded takeover. The published lock and its .pending staging entry are directories; when Windows reports EPERM (and POSIX reports EISDIR) for the staging-directory rename because the published directory already exists, acquisition must enter the same owner/lease conflict path as EEXIST/ENOTEMPTY rather than surface a permanent lock error. Stale directory cleanup and owner release use bounded retries for transient Windows EPERM/EBUSY/ENOTEMPTY errors.
  • Configuration reconciliation is serialized per channel and latest-wins. When multiple saves overlap, an older refresh generation must not start a runtime after a newer generation has been scheduled.
  • Feishu/Lark card actions may finish their WebSocket handler only after the shared business callback succeeds. The runtime returns the callback's complete outbound message to the handler, which synchronously renders the single authoritative replacement card.raw; the callback pipeline must not also write the same card through REST or message PATCH. A rejected callback or { ok: false } result must reject the handler so the provider can expose a retryable failure.
  • A Bot's new task follows the native v4 draft lifecycle: createSession first, then capability-checked model/mode/thought configuration, then sendText. The v4 create command must carry the same resolved mcpServers used by the legacy session create path so desktop-continuous, Bot, and remote entry points start equivalent runtimes. It must not create the empty session through the legacy session operation, because the following v4 input ledger requires the v4 draft persistence state. Attachment delivery keeps the existing attachment-capable send fallback after the same v4 draft creation.
  • Runtime lock I/O failures are observable retryable errors; they must not terminate a provider loop or escape as unhandled promise rejections.
  • Streaming-card network rendering is best-effort and bounded by a request timeout. Timeout, provider failure, or service disposal must not block later task terminal events.
  • Blocking interactions (permission, AskUserQuestion, and ExitPlanMode approval) use the v4 resolveInteraction ACK as their business commit point. Persisted Bot state must remain retryable until that ACK succeeds; a transport callback or provider-card update is not a business acknowledgement.
  • Provider callback acknowledgement and outbound interaction rendering are bounded by request timeouts. If an in-place card update times out or fails after the v4 interaction succeeds, the callback pipeline must fall back to a new outbound result/next-question message instead of blocking the actor queue.
  • The callback pipeline enforces its own three-second acknowledgement deadline around the entire provider adapter call, including credential lookup and card update preparation. It aborts provider I/O on expiry and sends the outbound fallback; it does not rely only on an inner HTTP helper reaching its timeout.
  • A completed v4 interaction may be recorded as handled only after the v4 ACK succeeds. A rejected v4 command must leave the original pending interaction available for the same provider update or button to retry.
  • A provider request deadline covers both response headers and every response body byte consumed by the caller. Clearing the deadline after headers alone is not a bounded request.
  • Long-poll receive endpoints use an explicit channel-specific deadline instead of the ordinary short API deadline. Weixin /getupdates keeps a 90-second idle window and remains cancellable by runtime disposal/config refresh.
  • Bot interaction replies must not wait indefinitely for desktop/mobile task-state broadcasts. Elicitation progress is persisted first, then its auxiliary v4/UI broadcast is allowed at most one second; a timeout is logged and the provider reply continues. The final answer still uses the v4 interaction ACK as its business commit point.