# Dispatch for worker_chat_m1_1

You are worker_chat_m1_1.
Your working directory is: c:\Projects\FreeExile\.agents\teamwork\worker_chat_m1_1

## Mandatory Inputs to Read First
1. `c:\Projects\FreeExile\.agents\teamwork\ORIGINAL_REQUEST.md` (specifically section `## 2026-10-01T00:42:05Z`)
2. `c:\Projects\FreeExile\.agents\teamwork\orchestrator_5\plan.md`
3. `c:\Projects\FreeExile\.agents\teamwork\explorer_chat_server_1\handoff.md`
4. `c:\Projects\FreeExile\.agents\teamwork\spec_miner_chat_spec_1\handoff.md`

## MANDATORY INTEGRITY WARNING
DO NOT CHEAT. All implementations must be genuine. DO NOT hardcode test results, create dummy/facade implementations, or circumvent the intended task. A teamwork_preview_auditor will independently verify your work. Integrity violations WILL be detected and your work WILL be rejected.

## File Ownership
You own EXCLUSIVELY:
- `server/chat/chat_types.py`
- `server/chat/chat_service.py`
- `server/chat/chat_cluster_router.py`
- `server/chat/channel_manager.py`
- `server/chat/moderation.py`
- `server/chat/item_link_service.py`
- `tests/unit/test_chat_service.py`
- `tests/unit/test_chat_and_moderation.py`

Do NOT modify `server/world/server_engine_loop.py` or any client files.

## Mission: Milestone M1 — Core Server Chat Cluster & Pub/Sub Hardening
Refactor and harden the server-side chat microservice:
1. In `server/chat/chat_cluster_router.py`:
   - Optimize `_safe_invoke`: Inspect if subscriber callback is a coroutine function (`inspect.iscoroutinefunction(cb)`). If it is a regular function (callable), invoke it directly synchronously (`cb(message)`) without `await` and without creating an unneeded coroutine/task. This eliminates 681k handled `TypeError` exceptions and drops p99 broadcast latency from 28ms to < 1ms!
   - Add Redis 7 Sharded Pub/Sub bridge capability / interface (`SPUBLISH` / `SSUBSCRIBE`) with channel hash tags (`{world}`, `{zone:...}`, `{guild:...}`).
2. In `server/chat/channel_manager.py`:
   - Ensure level gating (World lvl >= 20, Recruit lvl >= 10).
   - Ensure cooldown enforcement across all 8 channels (World 15s, Zone 3s, Recruit 10s, Guild 0.5s, Party 0.2s, Whisper 0.5s, Feedback 5s, System 0s).
   - Ensure 5s duplicate spam check for World and Zone.
   - Ensure whisper blacklist verification and self-whisper prevention.
   - Ensure 100-message ring buffer per channel (`deque(maxlen=100)`).
3. In `server/chat/moderation.py`:
   - Tier 1: Aho-Corasick Trie with Unicode NFKC, homoglyph substitutions, zero-width stripping, NFD diacritics stripping, punctuation noise removal, masking vulgar words with `***` in < 0.1ms without dropping messages.
   - Tier 2: Async Sentinel matching Vietnamese phone numbers (03/05/07/08/09), social media (Zalo/Telegram), bank/transfer (ATM/MoMo), and crypto, computing risk score (0-100) and triggering auto-mute at >= 60.
4. In `server/chat/item_link_service.py`:
   - HMAC-SHA256 signature verification over item UUID, name key, and rarity with secret key.
   - Cache snapshot with 24-hour TTL (86,400s).
   - Support `ItemSnapshotDTO` fields: `item_level`, `created_at_ms`, `affixes`.
5. In `server/chat/chat_service.py`:
   - When Tier 2 returns `should_auto_mute == True`, persist player mute state so subsequent messages are blocked at permission validation.
   - Support query item snapshot, subscribe/unsubscribe client, get history, blacklist management.
6. Typing and Standards:
   - Full strict typing (`mypy --strict --explicit-package-bases server/chat/`).
   - Every file must be <= 350 lines (Soft Cap) and never exceed 500 lines (Hard Cap).
   - Models must use `@dataclass(slots=True, frozen=True)`.
7. Tests:
   - Run `python -m unittest tests/unit/test_chat_service.py tests/unit/test_chat_and_moderation.py`.
   - Ensure 100% of unit tests pass.

Deliver your report in `c:\Projects\FreeExile\.agents\teamwork\worker_chat_m1_1\handoff.md`.

## 2026-10-01T00:50:20Z
Received dispatch from parent (7020ed22-d577-4b8a-8fb7-a0bd4b2658d7).
Mission: Milestone M1 — Core Server Chat Cluster & Pub/Sub Hardening
