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

@@ -32,7 +32,19 @@ export interface ParsedJid {
channel_type: string;
}
/** WhatsApp (Baileys) JID hosts. v1 stored these raw, with no `wa:` prefix. */
const WA_JID_HOSTS = new Set(['s.whatsapp.net', 'g.us', 'lid', 'broadcast', 'newsletter']);
function isWhatsappJid(raw: string): boolean {
const at = raw.lastIndexOf('@');
if (at === -1) return false;
return WA_JID_HOSTS.has(raw.slice(at + 1).toLowerCase());
}
export function parseJid(raw: string): ParsedJid | null {
if (isWhatsappJid(raw)) {
return { raw, prefix: 'whatsapp', id: raw, channel_type: 'whatsapp' };
}
const colon = raw.indexOf(':');
if (colon === -1) return null;
const prefix = raw.slice(0, colon).toLowerCase();
@@ -47,10 +59,16 @@ export function parseJid(raw: string): ParsedJid | null {
}
/**
* Build a v2 platform_id from a v1 JID. v2's messaging_groups.platform_id
* is always `<channel_type>:<id>`.
* Build a v2 platform_id from a v1 JID, in the format the runtime adapter
* for that channel emits. WhatsApp uses the raw Baileys JID (`<id>@<host>`,
* no prefix). Other channels use `<channel_type>:<id>`.
*/
export function v2PlatformId(channelType: string, jid: string): string {
if (channelType === 'whatsapp') {
// Strip any v1 `wa:`/`whatsapp:` prefix; otherwise pass through raw.
const parsed = parseJid(jid);
return parsed?.channel_type === 'whatsapp' ? parsed.id : jid;
}
const parsed = parseJid(jid);
const id = parsed?.id ?? jid;
return id.startsWith(`${channelType}:`) ? id : `${channelType}:${id}`;