Skip to content

Commit d8c68c8

Browse files
authored
fix: migrate Telegram pairing allowFrom to default account only (#58165)
* fix: migrate Telegram pairing allowFrom to default account only * fix: address PR review feedback * fix: address PR review feedback
1 parent 62c28c0 commit d8c68c8

4 files changed

Lines changed: 56 additions & 21 deletions

File tree

‎src/channels/read-only-account-inspect.telegram.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function listTelegramAccountIds(cfg: OpenClawConfig): string[] {
5959
});
6060
}
6161

62-
function resolveDefaultTelegramAccountId(cfg: OpenClawConfig): string {
62+
export function resolveDefaultTelegramAccountId(cfg: OpenClawConfig): string {
6363
const boundDefault = resolveDefaultAgentBoundAccountId(cfg, "telegram");
6464
if (boundDefault) {
6565
return boundDefault;

‎src/commands/doctor-state-migrations.test.ts‎

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,12 @@ describe("doctor legacy state migrations", () => {
318318
});
319319
});
320320

321-
it("fans out legacy Telegram pairing allowFrom store to configured named accounts", async () => {
321+
it("does not fan out legacy Telegram pairing allowFrom store to configured named accounts", async () => {
322322
const root = await makeTempRoot();
323323
const cfg: OpenClawConfig = {
324324
channels: {
325325
telegram: {
326+
defaultAccount: "bot2",
326327
accounts: {
327328
bot1: {},
328329
bot2: {},
@@ -333,20 +334,53 @@ describe("doctor legacy state migrations", () => {
333334
const { oauthDir, detected, result } = await runTelegramAllowFromMigration({ root, cfg });
334335
expect(detected.pairingAllowFrom.hasLegacyTelegram).toBe(true);
335336
expect(
336-
detected.pairingAllowFrom.copyPlans.map((plan) => path.basename(plan.targetPath)).toSorted(),
337-
).toEqual(["telegram-bot1-allowFrom.json", "telegram-bot2-allowFrom.json"]);
337+
detected.pairingAllowFrom.copyPlans.map((plan) => path.basename(plan.targetPath)),
338+
).toEqual(["telegram-bot2-allowFrom.json"]);
338339
expect(result.warnings).toEqual([]);
339340

340341
const bot1Target = path.join(oauthDir, "telegram-bot1-allowFrom.json");
341342
const bot2Target = path.join(oauthDir, "telegram-bot2-allowFrom.json");
342-
expect(fs.existsSync(bot1Target)).toBe(true);
343+
const defaultTarget = path.join(oauthDir, "telegram-default-allowFrom.json");
344+
expect(fs.existsSync(bot1Target)).toBe(false);
343345
expect(fs.existsSync(bot2Target)).toBe(true);
344-
expect(fs.existsSync(path.join(oauthDir, "telegram-default-allowFrom.json"))).toBe(false);
345-
expect(JSON.parse(fs.readFileSync(bot1Target, "utf-8"))).toEqual({
346+
expect(fs.existsSync(defaultTarget)).toBe(false);
347+
expect(JSON.parse(fs.readFileSync(bot2Target, "utf-8"))).toEqual({
346348
version: 1,
347349
allowFrom: ["123456"],
348350
});
349-
expect(JSON.parse(fs.readFileSync(bot2Target, "utf-8"))).toEqual({
351+
});
352+
353+
it("migrates legacy Telegram pairing allowFrom store to the default agent bound account", async () => {
354+
const root = await makeTempRoot();
355+
const cfg: OpenClawConfig = {
356+
agents: {
357+
list: [{ id: "ops", default: true }],
358+
},
359+
bindings: [{ agentId: "ops", match: { channel: "telegram", accountId: "alerts" } }],
360+
channels: {
361+
telegram: {
362+
accounts: {
363+
alerts: {},
364+
backup: {},
365+
},
366+
},
367+
},
368+
};
369+
370+
const { oauthDir, detected, result } = await runTelegramAllowFromMigration({ root, cfg });
371+
expect(detected.pairingAllowFrom.hasLegacyTelegram).toBe(true);
372+
expect(
373+
detected.pairingAllowFrom.copyPlans.map((plan) => path.basename(plan.targetPath)),
374+
).toEqual(["telegram-alerts-allowFrom.json"]);
375+
expect(result.warnings).toEqual([]);
376+
377+
const alertsTarget = path.join(oauthDir, "telegram-alerts-allowFrom.json");
378+
const backupTarget = path.join(oauthDir, "telegram-backup-allowFrom.json");
379+
const defaultTarget = path.join(oauthDir, "telegram-default-allowFrom.json");
380+
expect(fs.existsSync(alertsTarget)).toBe(true);
381+
expect(fs.existsSync(backupTarget)).toBe(false);
382+
expect(fs.existsSync(defaultTarget)).toBe(false);
383+
expect(JSON.parse(fs.readFileSync(alertsTarget, "utf-8"))).toEqual({
350384
version: 1,
351385
allowFrom: ["123456"],
352386
});

‎src/infra/state-migrations.test.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function createConfig(): OpenClawConfig {
1919
},
2020
channels: {
2121
telegram: {
22+
defaultAccount: "alpha",
2223
accounts: {
2324
beta: {},
2425
alpha: {},
@@ -100,15 +101,13 @@ describe("state migrations", () => {
100101
expect(detected.pairingAllowFrom.hasLegacyTelegram).toBe(true);
101102
expect(detected.pairingAllowFrom.copyPlans.map((plan) => plan.targetPath)).toEqual([
102103
resolveChannelAllowFromPath("telegram", env, "alpha"),
103-
resolveChannelAllowFromPath("telegram", env, "beta"),
104104
]);
105105
expect(detected.preview).toEqual([
106106
`- Sessions: ${path.join(stateDir, "sessions")} → ${path.join(stateDir, "agents", "worker-1", "sessions")}`,
107107
`- Sessions: canonicalize legacy keys in ${path.join(stateDir, "agents", "worker-1", "sessions", "sessions.json")}`,
108108
`- Agent dir: ${path.join(stateDir, "agent")} → ${path.join(stateDir, "agents", "worker-1", "agent")}`,
109109
`- WhatsApp auth: ${path.join(stateDir, "credentials")} → ${path.join(stateDir, "credentials", "whatsapp", "default")} (keep oauth.json)`,
110110
`- Telegram pairing allowFrom: ${resolveChannelAllowFromPath("telegram", env)} → ${resolveChannelAllowFromPath("telegram", env, "alpha")}`,
111-
`- Telegram pairing allowFrom: ${resolveChannelAllowFromPath("telegram", env)} → ${resolveChannelAllowFromPath("telegram", env, "beta")}`,
112111
]);
113112
});
114113

@@ -135,7 +134,6 @@ describe("state migrations", () => {
135134
"Moved WhatsApp auth creds.json → whatsapp/default",
136135
"Moved WhatsApp auth pre-key-1.json → whatsapp/default",
137136
`Copied Telegram pairing allowFrom → ${resolveChannelAllowFromPath("telegram", env, "alpha")}`,
138-
`Copied Telegram pairing allowFrom → ${resolveChannelAllowFromPath("telegram", env, "beta")}`,
139137
]);
140138

141139
const mergedStore = JSON.parse(
@@ -176,7 +174,10 @@ describe("state migrations", () => {
176174
fs.readFile(resolveChannelAllowFromPath("telegram", env, "alpha"), "utf8"),
177175
).resolves.toBe('["123","456"]\n');
178176
await expect(
179-
fs.readFile(resolveChannelAllowFromPath("telegram", env, "beta"), "utf8"),
180-
).resolves.toBe('["123","456"]\n');
177+
fs.stat(resolveChannelAllowFromPath("telegram", env, "default")),
178+
).rejects.toMatchObject({ code: "ENOENT" });
179+
await expect(
180+
fs.stat(resolveChannelAllowFromPath("telegram", env, "beta")),
181+
).rejects.toMatchObject({ code: "ENOENT" });
181182
});
182183
});

‎src/infra/state-migrations.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "node:fs";
22
import os from "node:os";
33
import path from "node:path";
44
import { resolveDefaultAgentId } from "../agents/agent-scope.js";
5-
import { listTelegramAccountIds } from "../channels/read-only-account-inspect.telegram.js";
5+
import { resolveDefaultTelegramAccountId } from "../channels/read-only-account-inspect.telegram.js";
66
import type { OpenClawConfig } from "../config/config.js";
77
import {
88
resolveLegacyStateDirs,
@@ -700,14 +700,14 @@ export async function detectLegacyStateMigrations(params: {
700700
fileExists(path.join(oauthDir, "creds.json")) &&
701701
!fileExists(path.join(targetWhatsAppAuthDir, "creds.json"));
702702
const legacyTelegramAllowFromPath = resolveChannelAllowFromPath("telegram", env);
703+
const targetTelegramAccountId = resolveDefaultTelegramAccountId(params.cfg);
704+
const targetTelegramAllowFromPath = resolveChannelAllowFromPath(
705+
"telegram",
706+
env,
707+
targetTelegramAccountId,
708+
);
703709
const telegramPairingAllowFromPlans = fileExists(legacyTelegramAllowFromPath)
704-
? Array.from(
705-
new Set(
706-
listTelegramAccountIds(params.cfg).map((accountId) =>
707-
resolveChannelAllowFromPath("telegram", env, accountId),
708-
),
709-
),
710-
)
710+
? [targetTelegramAllowFromPath]
711711
.filter((targetPath) => !fileExists(targetPath))
712712
.map(
713713
(targetPath): FileCopyPlan => ({

0 commit comments

Comments
 (0)