Skip to content

Business operating system for Claude Code — 57 skills, 21 agents, smart daemon. Unified inbox (WhatsApp/Email/Slack/Telegram), autonomous PR merge, full-AWS monitoring, revenue (Stripe+RevenueCat), e-commerce (Shopify), marketing (Klaviyo/Meta/GA4), voice (Bland/ElevenLabs), APM (Datadog/NewRelic/OTEL), YOLO mode.

feat(telegram-channel): leader-exclusive poll + reply gate (#481)

Aurora Capital4cd9227b8f7a
diff --git a/claude-ops/scripts/email/README.md b/claude-ops/scripts/email/README.md
index adc7043c..9a0bda8e 100644
--- a/claude-ops/scripts/email/README.md
+++ b/claude-ops/scripts/email/README.md
@@ -8,8 +8,8 @@ sending to the wrong account or burning the outbound-comms approval token**.
### The bug it prevents
Replying with `gog gmail send --reply-to-message-id <ID>` **404s** when the
-message-ID was scanned from one Gmail account (e.g. `[email protected]`) but
-the send defaults to a **different** account (e.g. `[email protected]`) — the same
+message-ID was scanned from one Gmail account (e.g. account A) but
+the send defaults to a **different** account (e.g. account B) — the same
thread has different message/thread IDs per account.
Worse: **every** `gog gmail send` attempt — even one that errors on a 404 or a bad
@@ -43,7 +43,7 @@ gog-reply.sh --to <email> --body <text> \
| `--subject <subj>` | Subject. If omitted, derived as `Re: <original subject>`. |
| `--match-from <email>` | From-address used to find the thread. Defaults to `--to`. |
| `--match-subject <text>` | Substring to disambiguate the thread. Optional. |
-| `--account <acct>` | Pin a gog account. If omitted, **auto-discovers** (enumerates oauth gmail accounts via `gog auth list`, always also probing `[email protected]` and `[email protected]`). |
+| `--account <acct>` | Pin a gog account. If omitted, **auto-discovers** (enumerates oauth gmail accounts via `gog auth list`, always also probing any accounts in `$GOG_EXTRA_ACCOUNTS`). |
| `--dry-run` | Resolve and print the account / threadId / message-id / to / subject / body, then exit 0 without sending. |
### Exit codes
@@ -75,6 +75,6 @@ gog-reply.sh --to [email protected] --body "On it, thanks." --dry-run
# Pin an account + disambiguate by subject, then actually reply.
gog-reply.sh --to [email protected] \
--match-subject "invoice" \
- --account [email protected] \
+ --account <your-account> \
--body "Attached, let me know if anything's off."
```
diff --git a/claude-ops/scripts/email/gog-reply.sh b/claude-ops/scripts/email/gog-reply.sh
index a97d5980..413af78f 100755
--- a/claude-ops/scripts/email/gog-reply.sh
+++ b/claude-ops/scripts/email/gog-reply.sh
@@ -5,8 +5,8 @@
# THE BUG THIS PREVENTS
# ---------------------
# A Gmail reply via `gog gmail send --reply-to-message-id <ID>` 404s when the
-# message-ID was scanned from one account (e.g. [email protected]) but the
-# send defaults to a DIFFERENT account (e.g. [email protected]): the same thread
+# message-ID was scanned from one account (e.g. your scanning mailbox) but the
+# send defaults to a DIFFERENT account (e.g. your sending mailbox): the same thread
# carries different message/thread IDs per account.
#
# Worse: EVERY `gog gmail send` attempt — even one that 404s or trips a bad flag
@@ -98,7 +98,7 @@ discover_accounts() {
return 0
fi
gog auth list -j 2>/dev/null | python3 -c '
-import sys, json
+import os, sys, json
seen, out = set(), []
try:
d = json.load(sys.stdin)
@@ -113,9 +113,10 @@ try:
seen.add(email); out.append(email)
except Exception:
pass
-# Always try the two known mailboxes on this box.
- if e not in seen:
+# Always also probe any extra mailboxes named in $GOG_EXTRA_ACCOUNTS
+# (comma/space separated) — keeps personal addresses out of source.
+for e in os.environ.get("GOG_EXTRA_ACCOUNTS", "").replace(",", " ").split():
+ if e and e not in seen:
seen.add(e); out.append(e)
print("\n".join(out))
' 2>/dev/null
diff --git a/claude-ops/telegram-server/channel-mcp.mjs b/claude-ops/telegram-server/channel-mcp.mjs
index 0cb50a27..c3596fbf 100644
--- a/claude-ops/telegram-server/channel-mcp.mjs
+++ b/claude-ops/telegram-server/channel-mcp.mjs
@@ -66,8 +66,57 @@ function loadPreferences() {
const prefs = loadPreferences();
-const BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN || prefs.bot_token || '';
-const OWNER_ID_STR = process.env.TELEGRAM_OWNER_ID || String(prefs.owner_id ?? '');
+function loadSecretsEnv() {
+ try {
+ const raw = readFileSync(join(homedir(), '.mcp-secrets.env'), 'utf8');
+ const out = {};
+ for (const line of raw.split('\n')) {
+ const m = line.match(/^\s*(?:export\s+)?(TELEGRAM_BOT_TOKEN|TELEGRAM_OWNER_ID)\s*=\s*(.+?)\s*$/);
+ if (m) out[m[1]] = m[2].replace(/^["']|["']$/g, '');
+ }
+ return out;
+ } catch {
+ return {};
+ }
+}
+const secretsEnv = loadSecretsEnv();
+
+function thisSessionIsLeader() {
+ // FAIL OPEN by default: the leader-gate is an opt-in fleet feature. A plain
+ // claude-ops install (no fleet supervisor) or a non-Linux host (no /proc) must
+ // poll normally — the gate only LOCKS OUT a session when it can positively prove
+ // a *different* live session leads. Anything it can't prove → poll.
+ let marker;
+ try {
+ marker = readFileSync(join(homedir(), '.claude', 'state', 'fleet-supervisor.leader'), 'utf8');
+ } catch {
+ return true; // no fleet marker → not a managed fleet → poll normally
+ }
+ const m = marker.match(/pid:(\d+)/);
+ if (!m) return true; // marker without a pid → can't enforce → poll
+ const leaderPid = m[1];
+ let pid = String(process.pid);
+ for (let i = 0; i < 64; i++) {
+ if (pid === leaderPid) return true; // this process is in the leader's tree
+ let stat;
+ try {
+ stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
+ } catch {
+ return true; // /proc unreadable (non-Linux / process gone) → can't prove → poll
+ }
+ const after = stat
+ .slice(stat.lastIndexOf(')') + 2)
+ .trim()
+ .split(/\s+/);
+ pid = after[1];
+ if (!pid || pid === '0' || pid === '1') return false; // reached init: NOT the leader's tree
+ }
+ return false; // ran out of hops without matching → treat as non-leader
+}
+
+const BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN || prefs.bot_token || secretsEnv.TELEGRAM_BOT_TOKEN || '';
+const OWNER_ID_STR =
+ process.env.TELEGRAM_OWNER_ID || (prefs.owner_id ? String(prefs.owner_id) : '') || secretsEnv.TELEGRAM_OWNER_ID || '';
const OWNER_ID = OWNER_ID_STR ? parseInt(OWNER_ID_STR, 10) : 0;
const POLL_TIMEOUT = parseInt(process.env.TELEGRAM_POLL_TIMEOUT || '25', 10);
@@ -176,6 +225,14 @@ mcp.setRequestHandler(CallToolRequestSchema, async (req) => {
if (!BOT_TOKEN) {
return { content: [{ type: 'text', text: 'TELEGRAM_BOT_TOKEN not configured' }], isError: true };
}
+ if (!thisSessionIsLeader()) {
+ return {
+ content: [
+ { type: 'text', text: 'not the fleet-orchestrator leader — refusing to send (Telegram is leader-exclusive)' },
+ ],
+ isError: true,
+ };
+ }
try {
const result = await sendMessage(chatId, text);
if (!result.ok) {
@@ -337,8 +394,15 @@ async function poll() {
setTimeout(poll, 30_000).unref();
return;
}
+ if (!thisSessionIsLeader()) {
+ setTimeout(poll, 15_000).unref();
+ return;
+ }
try {
+ try {
+ writeFileSync(join(STATE_DIR, 'channel.alive'), String(Date.now()));
+ } catch {}
// offset = lastUpdateId + 1 tells Telegram to skip everything ≤ lastUpdateId.
// This is the key correctness property: offset NEVER freezes at 0 after
// the first batch, because we update lastUpdateId = MAX(update_id) seen