#21890: fix(whatsapp): propagate fromMe flag through DM message pipeline
channel: whatsapp-web
size: S
Cluster:
WhatsApp Pairing Enhancements
## Summary
Fixes #12482
In WhatsApp DMs, the agent cannot distinguish between messages sent by the gateway owner and messages from the contact. This makes it impossible for the agent to follow a two-way conversation correctly.
### Changes
- Add `fromMe?: boolean` field to `WebInboundMessage` type
- Propagate `Boolean(msg.key?.fromMe)` from the Baileys message into the inbound message
- Pass `fromMe` through `buildInboundLine()` to `formatInboundEnvelope()`
- In DM envelopes, append `(you)` to the `from` field when `fromMe` is true, producing `[WhatsApp +1555 (you)] message` for owner messages
### Security
- `Boolean(msg.key?.fromMe)` safely defaults to `false` for null/undefined
- The `(you)` marker passes through `sanitizeEnvelopeHeaderPart` (no injection risk)
- The marker only appears in the agent's internal transcript, never in outbound messages
- `fromMe` field is optional throughout — fully backward-compatible
### Test plan
- [x] DM with `fromMe: true` → envelope contains `(you)`
- [x] DM with `fromMe: false` → no `(you)` marker
- [x] Group with `fromMe: true` → no `(you)` marker (groups unaffected)
- [x] `fromMe` omitted → no `(you)` marker (backward compat)
- [x] All existing envelope tests pass (16/16)
🤖 Generated with [Claude Code](https://claude.ai/claude-code)
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Adds a `fromMe` flag to the WhatsApp inbound message pipeline so the agent can distinguish between messages sent by the gateway owner and messages from the contact in DMs. The flag flows from Baileys' `msg.key.fromMe` through `WebInboundMessage` → `buildInboundLine()` → `formatInboundEnvelope()`, where DM messages with `fromMe: true` get a `(you)` suffix appended to the `from` field in the envelope header (e.g., `[WhatsApp +1555 (you)] message`).
- New optional `fromMe?: boolean` field added to `WebInboundMessage` type, ensuring backward compatibility
- `Boolean(msg.key?.fromMe)` safely coerces null/undefined to `false`
- The `(you)` marker only applies to DM messages (not groups), and passes through `sanitizeEnvelopeHeaderPart` safely
- 4 new test cases cover DM fromMe true/false, group fromMe true, and omitted fromMe scenarios
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge — it's a well-scoped, backward-compatible addition with thorough test coverage.
- The change is minimal and focused: a single optional boolean field threaded through the existing pipeline, with a small formatting change gated behind two conditions (isDirect && fromMe). All edge cases are tested (4 new tests). The field is optional throughout, so no existing callers break. The Boolean() coercion safely handles null/undefined. No security concerns — the marker only appears in internal agent transcripts.
- No files require special attention.
<sub>Last reviewed commit: 7fc1a3b</sub>
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#21889: fix(whatsapp): include outbound DMs in agent context for allowed co...
by mactsk · 2026-02-20
83.6%
#23579: feat(whatsapp): add sender attribution to inbound message logs
by svan058 · 2026-02-22
76.3%
#21953: feat(whatsapp): sender prefix on BodyForAgent + contactNames config
by mactsk · 2026-02-20
76.1%
#16785: fix(whatsapp): preserve document filenames in outbound Baileys mode
by SahilSahu731 · 2026-02-15
75.5%
#16655: fix(whatsapp): resolve reply-to sender E.164 for LID JIDs (have bot...
by mascarenhas · 2026-02-15
73.8%
#22106: fix(whatsapp): honor selfChatMode override for group mentions
by sportclaw · 2026-02-20
73.6%
#4402: fix: store group messages from non-allowlisted senders as pending c...
by adam91holt · 2026-01-30
73.3%
#15650: fix(whatsapp): pass fileName to document sends instead of hardcodin...
by whoknowsmann · 2026-02-13
73.1%
#23288: feat(whatsapp): group command gating via commands.allowFrom + sende...
by rodrigoscoelho · 2026-02-22
73.1%
#6566: fix: thread fileName through WhatsApp document send path
by giannisanni · 2026-02-01
72.8%