From be1991108b213b8b79c9e093d7727aee3a5342e8 Mon Sep 17 00:00:00 2001 From: glifocat Date: Fri, 6 Mar 2026 17:51:00 +0100 Subject: [PATCH] fix(whatsapp): write pairing code to file for immediate access (#745) The pairing code was only emitted to stdout, which is buffered by the calling process and not visible until the auth command exits (~120s). By also writing to store/pairing-code.txt the moment the code is ready, callers can poll that file and display the code to the user within seconds instead of after the 60s expiry window. Update the add-whatsapp skill instructions to use the background + file-poll pattern instead of waiting on buffered stdout. Co-authored-by: Claude Sonnet 4.6 --- .claude/skills/add-whatsapp/SKILL.md | 24 ++++++++++++++----- .../add-whatsapp/add/setup/whatsapp-auth.ts | 10 ++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/.claude/skills/add-whatsapp/SKILL.md b/.claude/skills/add-whatsapp/SKILL.md index f572ea0..660123a 100644 --- a/.claude/skills/add-whatsapp/SKILL.md +++ b/.claude/skills/add-whatsapp/SKILL.md @@ -115,21 +115,33 @@ Tell the user to run `npm run auth` in another terminal, then: For pairing code: +Tell the user to have WhatsApp open on **Settings > Linked Devices > Link a Device**, ready to tap **"Link with phone number instead"** — the code expires in ~60 seconds and must be entered immediately. + +Run the auth process in the background and poll `store/pairing-code.txt` for the code: + ```bash -npx tsx setup/index.ts --step whatsapp-auth -- --method pairing-code --phone +rm -f store/pairing-code.txt && npx tsx setup/index.ts --step whatsapp-auth -- --method pairing-code --phone > /tmp/wa-auth.log 2>&1 & ``` -(Bash timeout: 150000ms). Display PAIRING_CODE from output. +Then immediately poll for the code (do NOT wait for the background command to finish): -Tell the user: +```bash +for i in $(seq 1 20); do [ -f store/pairing-code.txt ] && cat store/pairing-code.txt && break; sleep 1; done +``` -> A pairing code will appear. **Enter it within 60 seconds** — codes expire quickly. +Display the code to the user the moment it appears. Tell them: + +> **Enter this code now** — it expires in ~60 seconds. > > 1. Open WhatsApp > **Settings** > **Linked Devices** > **Link a Device** > 2. Tap **Link with phone number instead** > 3. Enter the code immediately -> -> If the code expires, re-run the command — a new code will be generated. + +After the user enters the code, poll for authentication to complete: + +```bash +for i in $(seq 1 60); do grep -q 'AUTH_STATUS: authenticated' /tmp/wa-auth.log 2>/dev/null && echo "authenticated" && break; grep -q 'AUTH_STATUS: failed' /tmp/wa-auth.log 2>/dev/null && echo "failed" && break; sleep 2; done +``` **If failed:** qr_timeout → re-run. logged_out → delete `store/auth/` and re-run. 515 → re-run. timeout → ask user, offer retry. diff --git a/.claude/skills/add-whatsapp/add/setup/whatsapp-auth.ts b/.claude/skills/add-whatsapp/add/setup/whatsapp-auth.ts index 4aa3433..2cbec76 100644 --- a/.claude/skills/add-whatsapp/add/setup/whatsapp-auth.ts +++ b/.claude/skills/add-whatsapp/add/setup/whatsapp-auth.ts @@ -306,6 +306,16 @@ async function handlePairingCode( process.exit(3); } + // Write to file immediately so callers can read it without waiting for stdout + try { + fs.writeFileSync( + path.join(projectRoot, 'store', 'pairing-code.txt'), + pairingCode, + ); + } catch { + /* non-fatal */ + } + // Emit pairing code immediately so the caller can display it to the user emitAuthStatus('pairing-code', 'pairing_code_ready', 'waiting', { PAIRING_CODE: pairingCode,