Skip to content

Fix Telegram bridge reliability - #8

Open
DanWahlin wants to merge 4 commits into
examon:mainfrom
DanWahlin:fix/live-bridge-reliability
Open

DanWahlin wants to merge 4 commits into
examon:mainfrom
DanWahlin:fix/live-bridge-reliability

Conversation

@DanWahlin

@DanWahlin DanWahlin commented May 17, 2026 •

Copy link
Copy Markdown

Summary

  • register /telegram through the current Copilot SDK command handler API instead of a follow-up raw session.resume
  • stream assistant.message_delta events to Telegram as a throttled edited draft message, then replace it with the final response
  • harden bot lock ownership with a heartbeat, hostname, PID liveness, and Linux process start-token checks
  • refresh locks while polling and clean them up only when the current process/session owns them
  • add node:test coverage for stale locks, PID reuse, refresh, and lock removal
  • document live streaming, stale lock recovery, the --allow-tool extension-permission-access Copilot launch requirement, and the CLI-side permission-prompt failure mode that can look like endless Telegram typing

Why

The bridge has a few reliability problems that overlap in real use:

  1. Newer Copilot CLI SDKs can show /telegram but fail to dispatch it if the bridge only registers command metadata.
  2. Long Copilot responses appear stuck in Telegram because the bridge only keeps the typing indicator alive until the final assistant.message arrives.
  3. Dead sessions or PID reuse can leave a bot locked even though the owning bridge process is gone.
  4. Some newer Copilot CLI builds deny extension permission access unless Copilot is started with --allow-tool extension-permission-access, causing bridge startup to fail before Telegram can connect.

These fixes work best as one PR because they touch the same runtime state machine: command registration, outbound assistant events, polling, lock lifecycle, and setup troubleshooting.

Startup note

If Copilot logs an error like:

Extension "unknown" was denied permission access and will not be loaded.

Start Copilot CLI with:

copilot --experimental --allow-tool extension-permission-access

Then reconnect the bridge with:

/telegram connect <name>

This is documented in the README troubleshooting section.

Supersedes

This consolidates and supersedes the earlier split PRs:

Validation

  • node --check extension.mjs
  • node --check tests/lock.test.mjs
  • node --test tests/lock.test.mjs
  • git diff --check
  • scanned the diff for token-shaped secrets

Local smoke

Also tested locally by installing the patched extension.mjs into ~/.copilot/extensions/copilot-cli-telegram-bridge/, starting Copilot with --allow-tool extension-permission-access, reconnecting /telegram connect copilotcli, and verifying the bridge lock/process stayed live while Telegram connected.

- register /telegram with the current SDK command handler API
- stream assistant deltas to Telegram as a throttled edited draft
- harden bot lock ownership with heartbeat and process identity checks
- add lock behavior tests and README troubleshooting notes
@DanWahlin
DanWahlin force-pushed the fix/live-bridge-reliability branch from e03097a to e59aa1a Compare May 17, 2026 13:21
@mike-mo

mike-mo commented May 24, 2026

Copy link
Copy Markdown

The --takeover flag adds friction I'm not sure pays off

Before this PR, connecting to a bot held by another session just worked. The new session replaced the old one. Now it refuses and tells you to re-run the command with --takeover. The stale-lock detection improvements are great, but gating live locks behind a flag seems like it penalizes the wrong case.

In practice: I restart my CLI, the old lock hasn't heartbeat-expired yet (~135s window), and I have to retype the command with an extra flag. I'm almost always replacing myself, not stealing someone else's bot.

There's also a conventions issue: Copilot CLI slash commands don't use --flags. Everything in the interactive session is positional (/command subcommand <arg>). The -- flag pattern exists at the shell level (copilot --experimental), but not inside the session. So --takeover breaks the interaction idiom on top of adding the extra step.

Some ideas that might get you the safety you want without the friction:

  • If lock.hostname === hostname(), just let the takeover happen. Only require confirmation when crossing hosts, where accidentally stomping someone else is a real risk.
  • Or: ask interactively ("Session X is still active on this bot. Replace it?") so the user confirms intent without needing to remember a flag and re-run.
  • Or: keep the old implicit-takeover behavior entirely. The heartbeat/PID/start-token work already solves the ghost-lock problem, which was the actually-broken case. Live-to-live replacement worked fine before and Telegram's 409 handling still releases the old session gracefully.
  • If you do want an explicit action, a separate subcommand (/telegram takeover <name>) would fit the existing command patterns better than a flag.

What's the scenario you're worried about? Is it multi-user on the same host, or something else? Curious what drove this toward explicit opt-in.

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.

3 participants