# Version 3.0.4 Inbound Reply Diagnostics Fix

This patch targets the live issue where the customer receives the first intake SMS, but the customer's reply does not appear in the lead dashboard and the AI assistant does not send the next reply.

## Fixes

### 1. TextBee poll bootstrap no longer marks no-timestamp replies as old history

TextBee received-SMS API responses can omit `receivedAt`. The previous bootstrap could mark those messages as already processed during the first poll after startup. That meant a live customer reply could be skipped before it ever reached `processIncomingSms()`.

The bootstrap now only marks **timestamped** messages older than the poll cursor as old history. No-timestamp replies are left for the active-lead processing logic.

New/changed log:

```text
[TextBee poll bootstrap completed]
```

It now includes:

```text
recorded
skippedNoTimestamp
skippedAfterCursor
cursor
```

### 2. Dashboard health route fixed

`/admin/api/dashboard-health` referenced `getDatabaseInfo()` but did not import it. This could break dashboard auto-refresh checks.

### 3. Admin button added: Pull TextBee Replies

The admin dashboard now includes:

```text
Pull TextBee Replies
```

This manually runs:

```text
POST /admin/api/textbee/process-received?force=true&activeOnly=true&limit=10
```

Use this after sending a test customer reply. It forces the latest TextBee received-SMS messages through the same inbound AI pipeline for phone numbers that already have active leads, while still respecting provider/message dedupe.

### 4. SMS debug endpoint added

Open this after a failed test:

```text
/admin/api/sms-debug/recent?lines=500&leads=10
```

It returns:

- TextBee/SMS-related recent app logs
- SMS event ledger stats
- database info
- TextBee polling/config diagnostics
- recent lead inbound/outbound message counts
- last inbound and last outbound message previews

This is the easiest thing to copy/paste back for troubleshooting.

## Recommended env for testing

```text
TEXTBEE_INBOUND_POLLING_ENABLED=true
TEXTBEE_INBOUND_POLLING_INTERVAL_SECONDS=30
TEXTBEE_INBOUND_POLLING_LIMIT=10
TEXTBEE_POLL_IGNORE_HISTORY_ON_START=true
TEXTBEE_POLL_LOOKBACK_SECONDS=0
TEXTBEE_POLL_REQUIRE_RECEIVED_AT=true
TEXTBEE_POLL_ALLOW_MISSING_RECEIVED_AT_FOR_ACTIVE_LEADS=true
SMS_RESTRICTIONS_ENABLED=false
```

## Test sequence

1. Restart the Node app in cPanel.
2. Open `/sms/incoming` in the browser. It should show the endpoint is alive.
3. Place the missed-call test and wait for the first SMS.
4. Reply from the customer phone.
5. Wait 10–30 seconds.
6. Click **Pull TextBee Replies** in the dashboard.
7. Open `/admin/api/sms-debug/recent?lines=500&leads=10` and copy the response if the reply still does not show.
