Skip to content

Commit 1d4261e

Browse files
committed
fix: migrate Telegram pairing allowFrom to default account only
1 parent 8c0245f commit 1d4261e

3 files changed

Lines changed: 23 additions & 28 deletions

File tree

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ 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: {
@@ -333,20 +333,17 @@ describe("doctor legacy state migrations", () => {
333333
const { oauthDir, detected, result } = await runTelegramAllowFromMigration({ root, cfg });
334334
expect(detected.pairingAllowFrom.hasLegacyTelegram).toBe(true);
335335
expect(
336-
detected.pairingAllowFrom.copyPlans.map((plan) => path.basename(plan.targetPath)).toSorted(),
337-
).toEqual(["telegram-bot1-allowFrom.json", "telegram-bot2-allowFrom.json"]);
336+
detected.pairingAllowFrom.copyPlans.map((plan) => path.basename(plan.targetPath)),
337+
).toEqual(["telegram-default-allowFrom.json"]);
338338
expect(result.warnings).toEqual([]);
339339

340340
const bot1Target = path.join(oauthDir, "telegram-bot1-allowFrom.json");
341341
const bot2Target = path.join(oauthDir, "telegram-bot2-allowFrom.json");
342-
expect(fs.existsSync(bot1Target)).toBe(true);
343-
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-
version: 1,
347-
allowFrom: ["123456"],
348-
});
349-
expect(JSON.parse(fs.readFileSync(bot2Target, "utf-8"))).toEqual({
342+
const defaultTarget = path.join(oauthDir, "telegram-default-allowFrom.json");
343+
expect(fs.existsSync(bot1Target)).toBe(false);
344+
expect(fs.existsSync(bot2Target)).toBe(false);
345+
expect(fs.existsSync(defaultTarget)).toBe(true);
346+
expect(JSON.parse(fs.readFileSync(defaultTarget, "utf-8"))).toEqual({
350347
version: 1,
351348
allowFrom: ["123456"],
352349
});

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,14 @@ describe("state migrations", () => {
9999
expect(detected.whatsappAuth.hasLegacy).toBe(true);
100100
expect(detected.pairingAllowFrom.hasLegacyTelegram).toBe(true);
101101
expect(detected.pairingAllowFrom.copyPlans.map((plan) => plan.targetPath)).toEqual([
102-
resolveChannelAllowFromPath("telegram", env, "alpha"),
103-
resolveChannelAllowFromPath("telegram", env, "beta"),
102+
resolveChannelAllowFromPath("telegram", env, "default"),
104103
]);
105104
expect(detected.preview).toEqual([
106105
`- Sessions: ${path.join(stateDir, "sessions")} → ${path.join(stateDir, "agents", "worker-1", "sessions")}`,
107106
`- Sessions: canonicalize legacy keys in ${path.join(stateDir, "agents", "worker-1", "sessions", "sessions.json")}`,
108107
`- Agent dir: ${path.join(stateDir, "agent")} → ${path.join(stateDir, "agents", "worker-1", "agent")}`,
109108
`- WhatsApp auth: ${path.join(stateDir, "credentials")} → ${path.join(stateDir, "credentials", "whatsapp", "default")} (keep oauth.json)`,
110-
`- Telegram pairing allowFrom: ${resolveChannelAllowFromPath("telegram", env)} → ${resolveChannelAllowFromPath("telegram", env, "alpha")}`,
111-
`- Telegram pairing allowFrom: ${resolveChannelAllowFromPath("telegram", env)} → ${resolveChannelAllowFromPath("telegram", env, "beta")}`,
109+
`- Telegram pairing allowFrom: ${resolveChannelAllowFromPath("telegram", env)} → ${resolveChannelAllowFromPath("telegram", env, "default")}`,
112110
]);
113111
});
114112

@@ -134,8 +132,7 @@ describe("state migrations", () => {
134132
"Moved agent file settings.json → agents/worker-1/agent",
135133
"Moved WhatsApp auth creds.json → whatsapp/default",
136134
"Moved WhatsApp auth pre-key-1.json → whatsapp/default",
137-
`Copied Telegram pairing allowFrom → ${resolveChannelAllowFromPath("telegram", env, "alpha")}`,
138-
`Copied Telegram pairing allowFrom → ${resolveChannelAllowFromPath("telegram", env, "beta")}`,
135+
`Copied Telegram pairing allowFrom → ${resolveChannelAllowFromPath("telegram", env, "default")}`,
139136
]);
140137

141138
const mergedStore = JSON.parse(
@@ -173,10 +170,13 @@ describe("state migrations", () => {
173170
fs.readFile(path.join(stateDir, "credentials", "oauth.json"), "utf8"),
174171
).resolves.toContain('"oauth":true');
175172
await expect(
176-
fs.readFile(resolveChannelAllowFromPath("telegram", env, "alpha"), "utf8"),
173+
fs.readFile(resolveChannelAllowFromPath("telegram", env, "default"), "utf8"),
177174
).resolves.toBe('["123","456"]\n');
178175
await expect(
179-
fs.readFile(resolveChannelAllowFromPath("telegram", env, "beta"), "utf8"),
180-
).resolves.toBe('["123","456"]\n');
176+
fs.stat(resolveChannelAllowFromPath("telegram", env, "alpha")),
177+
).rejects.toMatchObject({ code: "ENOENT" });
178+
await expect(
179+
fs.stat(resolveChannelAllowFromPath("telegram", env, "beta")),
180+
).rejects.toMatchObject({ code: "ENOENT" });
181181
});
182182
});

‎src/infra/state-migrations.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ 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";
65
import type { OpenClawConfig } from "../config/config.js";
76
import {
87
resolveLegacyStateDirs,
@@ -700,14 +699,13 @@ export async function detectLegacyStateMigrations(params: {
700699
fileExists(path.join(oauthDir, "creds.json")) &&
701700
!fileExists(path.join(targetWhatsAppAuthDir, "creds.json"));
702701
const legacyTelegramAllowFromPath = resolveChannelAllowFromPath("telegram", env);
702+
const targetTelegramAllowFromPath = resolveChannelAllowFromPath(
703+
"telegram",
704+
env,
705+
DEFAULT_ACCOUNT_ID,
706+
);
703707
const telegramPairingAllowFromPlans = fileExists(legacyTelegramAllowFromPath)
704-
? Array.from(
705-
new Set(
706-
listTelegramAccountIds(params.cfg).map((accountId) =>
707-
resolveChannelAllowFromPath("telegram", env, accountId),
708-
),
709-
),
710-
)
708+
? [targetTelegramAllowFromPath]
711709
.filter((targetPath) => !fileExists(targetPath))
712710
.map(
713711
(targetPath): FileCopyPlan => ({

0 commit comments

Comments
 (0)