fix(migrate-v2): correct JID parsing, Discord guildId lookup, silent failures

- shared.ts: parseJid now recognizes raw Baileys WhatsApp JIDs
  (`<id>@s.whatsapp.net`, `@g.us`, etc.); v2PlatformId returns the raw
  JID for whatsapp to match what the runtime adapter emits. Without this,
  every WhatsApp group in a v1 install was silently skipped.

- discord-resolver.ts: new helper that uses DISCORD_BOT_TOKEN to look up
  channelId → guildId via the Discord API, since v1 stored only the
  channel id but v2 needs `discord:<guildId>:<channelId>`. Best-effort:
  on missing/invalid token or network error, returns empty resolver and
  the affected groups are skipped with the reason surfaced per channel.

- db.ts, tasks.ts: route Discord groups through the resolver; other
  channels go through v2PlatformId unchanged. Resolver only built when
  at least one Discord group exists, so non-Discord installs incur no
  network.

- db.ts: when every v1 group is skipped, exit non-zero with a FAIL line
  instead of `OK:groups=N,...,skipped=N`, so the wrapper doesn't hide
  total failure under a successful-looking summary.

- migrate-v2.sh: run_step now surfaces ERROR: lines from successful
  steps (with count + first 3 + raw log path); phase 2c install loop
  populates STEP_RESULTS so install failures show in handoff.json
  instead of silently passing.

- sessions.ts: copyTree skips dangling symlinks (e.g. v1's
  `.claude/debug/latest`) instead of crashing the entire step.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Gavriel Cohen
2026-05-02 14:32:34 +03:00
parent ce9f175238
commit aec7ddd099
7 changed files with 351 additions and 11 deletions

View File

@@ -22,6 +22,8 @@ import { getMessagingGroupByPlatform } from '../../src/db/messaging-groups.js';
import { runMigrations } from '../../src/db/migrations/index.js';
import { insertTask } from '../../src/modules/scheduling/db.js';
import { openInboundDb, resolveSession } from '../../src/session-manager.js';
import { readEnvFile } from '../../src/env.js';
import { buildDiscordResolver, type DiscordResolver } from './discord-resolver.js';
import { parseJid, v2PlatformId } from './shared.js';
interface V1Task {
@@ -67,7 +69,7 @@ function toCron(t: V1Task): { processAfter: string; recurrence: string | null }
return null;
}
function main(): void {
async function main(): Promise<void> {
const v1Path = process.argv[2];
if (!v1Path) {
console.error('Usage: tsx setup/migrate-v2/tasks.ts <v1-path>');
@@ -104,6 +106,14 @@ function main(): void {
let skipped = 0;
let failed = 0;
// Mirrors db.ts: Discord platform_id needs API lookup to recover guildId.
let discordResolver: DiscordResolver | null = null;
const hasDiscord = activeTasks.some((t) => parseJid(t.chat_jid)?.channel_type === 'discord');
if (hasDiscord) {
const env = readEnvFile(['DISCORD_BOT_TOKEN']);
discordResolver = await buildDiscordResolver(env.DISCORD_BOT_TOKEN ?? '');
}
for (const t of activeTasks) {
try {
const ag = getAgentGroupByFolder(t.group_folder);
@@ -112,7 +122,14 @@ function main(): void {
const parsed = parseJid(t.chat_jid);
if (!parsed) { skipped++; continue; }
const platformId = v2PlatformId(parsed.channel_type, t.chat_jid);
let platformId: string;
if (parsed.channel_type === 'discord') {
const resolved = discordResolver?.resolve(parsed.id) ?? null;
if (!resolved) { skipped++; continue; }
platformId = resolved;
} else {
platformId = v2PlatformId(parsed.channel_type, t.chat_jid);
}
const mg = getMessagingGroupByPlatform(parsed.channel_type, platformId);
if (!mg) { skipped++; continue; }
@@ -155,4 +172,7 @@ function main(): void {
console.log(`OK:active=${activeTasks.length},migrated=${migrated},skipped=${skipped},failed=${failed}`);
}
main();
main().catch((err) => {
console.error(`FAIL:${err instanceof Error ? err.message : String(err)}`);
process.exit(1);
});