# Changelog 2026-08-05

## [Chat Poller — 2 Critical Bugs Fixed] - 2026-08-05

### Summary
**Symptom**: Poller reported `messagesInserted: 0` on every run for 3+ days. SJ88 Studio's chat data was stale since 2026-08-01, even though Meta API had fresh messages from 8/2-8/5. All 67 pages affected.

**Root cause**: TWO bugs in `src/live/chatPoller.ts`:
1. `storeMessage()` INSERT IGNORE query had **17 columns but only 16 `?` placeholders and 15 values** — MySQL throws `ERROR 1136 (21S01): Column count doesn't match value count` silently
2. `fetchConvMessages()` requested `reactions` field — Meta API returns **400 "page not linked to Instagram"** for many pages when `reactions` is included in the `/messages` fields list

### Fixed

#### Bug 1: SQL column/value mismatch (lines 222-237)
```sql
-- Before (broken):
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  -- 16 placeholders
-- 15 values
-- After (fixed):
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  -- 17 placeholders
-- 17 values
```

#### Bug 2: `reactions` field rejected by Meta API (line 159)
```js
// Before:
const fields = "id,created_time,from,to,message,is_echo,attachments,story,reactions";

// After:
const fields = "id,created_time,from,to,message,is_echo,attachments,story";
// reactions removed: causes Meta API 400 on multiple pages
```

#### Defensive: HTTP error handling (line 167)
```js
if (!r.ok) { console.warn("[chat-poller] msg fetch HTTP " + r.status + " conv=" + convId); break; }
```

### Verified

#### Before fix
- 3 days (8/2, 8/3, 8/4) with 0 messages fetched despite Meta having hundreds
- Conv IDs like `t_1073578421674393` (Kanokphan), `t_1473015973430383` (Frank Weerawat, 481 msgs in Meta), `t_3045187892235650` (Katathep, 250 msgs) all missing from DB
- 50 conversations in Meta but not in DB

#### After fix (single 30d trigger)
- **20,480 messages inserted** in one run
- **349 conversations found** across 67 pages
- **0 errors**
- All missing convs backfilled:
  - `t_1073578421674393` (Kanokphan) — 3 msgs
  - `t_997459894130048` (Wuttichai) — 14 msgs
  - `t_1473015973430383` (Frank Weerawat) — 481 msgs
  - `t_3045187892235650` (Katathep) — 250 msgs
  - `t_4259283201027007` (Phongkijcha) — 985 msgs
- Total `chat_messages` count: 17,760 → **44,079**

#### Today's data (2026-08-05)
- 4 convs with messages today across all pages
- 2 SJ88 Studio convs:
  - `t_1065709963093252` (Ackaradech Krit) — 3 msgs, latest "โอนแล้วนะครับจารย์" 12:39
  - `t_4082164188775376` (Khun Noom) — 2 msgs, latest "ผมสมัครไปแล้วนะครับเฮีย" 08:35

### Files
- `src/live/chatPoller.ts` (modified)
- `docs/CHANGELOG-2026-08-05.md` (this file)
- `docs/manuals/SJ88-USER-MANUAL.md` (added §22 Chat Poller Bug Notes)

### Lessons Learned
1. **Always test Meta API field-by-field** when adding new fields — `reactions` worked for some pages but 400'd on others with misleading "Instagram" error
2. **`INSERT IGNORE` with mismatched column/value counts** — MySQL throws but the error is only visible if you check logs, not status
3. **Poller status check should include `messagesInserted > 0`** as health signal
4. **For polling systems**: add a daily `MAX(received_at)` check + alert if stale > 24h
