# Phase 1 Communications — Engineering Handoff for kimi-cli **Date:** 2026-06-15 **Branch:** `main` **Author:** Claude Code **Status:** READY TO DEPLOY — code complete, all 418 tests passing, zero TypeScript errors **Do not deploy:** No new env vars required. No new npm packages. Drop-in to existing hermes-mcp image build. --- > ⚠️ **CRITICAL — DO NOT LEAVE DOCKER RUNNING** > > After building and pushing the image, **stop and remove all local Docker containers** before kubectl apply. > Docker left running has previously caused port conflicts with the production Kubernetes cluster, breaking live services. > > ```bash > docker stop $(docker ps -q) 2>/dev/null || true > docker rm $(docker ps -aq) 2>/dev/null || true > ``` > > Verify nothing is bound to the relevant ports before applying to k8s: > ```bash > ss -tlnp | grep -E ':(80|443|3000|8080)' > ``` > > Only proceed with `kubectl apply` once Docker containers are confirmed stopped. --- ## What was built Backend implementation of the Chat-First Communications Manager Phase 1 MVP: - **9 new REST endpoints** under `/api/communications/*` (profile, chat, drafts, examples) - **6 new MySQL tables** auto-created on startup via existing `initDatabase()` pattern - **2 new columns** on the `customers` table (`autonomous_mode_enabled`, `approved_posts_count`) added via `ensureColumn` (idempotent, safe to run on live DB) - **Claude generation pipeline** — per-platform parallel calls (`Promise.allSettled`), NMLS compliance injection, style learning from approved posts - **Approval/publish flow** — reuses existing `src/clients/linkedin.ts`, `facebook.ts`, `instagram.ts`, and `smtp.ts` --- ## Files changed ### Modified - `src/db.ts` — added 6 new tables + 2 `ensureColumn` calls to `initDatabase()` - `src/index.ts` — added 2 imports + 8 route handlers (communications API block before `main()`) ### Created - `src/communications.ts` — business profile CRUD, draft lifecycle, approve/publish logic, input validation - `src/communications-chat.ts` — Claude generation pipeline, system prompt builder, NMLS injection, regeneration ### No changes to - `package.json` — no new dependencies - `hermes-k8s.yaml` — no new env vars needed - `Dockerfile` — unchanged - Any existing routes, tools, or OAuth logic --- ## Schema changes All table creation and column additions happen automatically on server startup via `initDatabase()`. The additions are **all idempotent** (`CREATE TABLE IF NOT EXISTS` + `ensureColumn` guards). Safe to deploy to a live database. ### New tables (all `ENGINE=InnoDB`, `utf8mb4_unicode_ci`) | Table | Purpose | Key columns | |-------|---------|------------| | `business_profiles` | One row per customer; vertical config + NMLS info | `user_id UNIQUE`, `email_recipients JSON`, `enabled_platforms JSON` | | `chat_sessions` | Conversation history grouping | `user_id`, `last_message_at` | | `chat_messages` | Individual turns (role=user/assistant) | `session_id`, `role`, `content` | | `drafts` | Per-platform AI drafts awaiting approval | `generation_id` (groups one chat turn), `draft_metadata JSON` (hashtags/subject), `status` | | `comm_posts` | Published post audit trail | `draft_id`, `platform_post_id`, `autonomous` | | `user_content_examples` | Approved posts used for style learning | `user_id`, `platform`, `approved_text` | ### New columns on `customers` | Column | Type | Default | |--------|------|---------| | `autonomous_mode_enabled` | `TINYINT(1)` | `0` | | `approved_posts_count` | `INT` | `0` | --- ## Deploy commands ```bash # 1. Commit the implementation git add src/db.ts src/index.ts src/communications.ts src/communications-chat.ts git commit -m "feat(communications): Phase 1 MVP — profile, chat, draft generation, approve/publish" git push origin main # 2. Build and push image docker build -t /hermes-mcp: . docker push /hermes-mcp: # ⚠️ 3. STOP DOCKER BEFORE APPLYING TO K8S docker stop $(docker ps -q) 2>/dev/null || true docker rm $(docker ps -aq) 2>/dev/null || true ss -tlnp | grep -E ':(80|443|3000|8080)' # must be empty before proceeding # 4. Update image SHA in hermes-k8s.yaml, then apply kubectl apply -f hermes-k8s.yaml # 5. Verify rollout kubectl rollout status deployment/hermes-mcp kubectl logs -l app=hermes-mcp --tail=50 | grep '\[db\]' ``` Expected log on startup: ``` [db] MySQL connected and schema initialized ``` The new tables will be created on first startup. No manual migration needed. --- ## Verification After deploy, smoke-test the new endpoints against staging with an existing customer API key: ```bash BASE=https://hermes.squaremcp.com KEY= # 1. Create business profile curl -X PUT "$BASE/api/communications/profile" \ -H "x-api-key: $KEY" \ -H "Content-Type: application/json" \ -d '{ "business_type": "mortgage_broker", "nmls_number": "123456", "states_licensed": ["CA"], "nmls_disclaimer_text": "NMLS #123456. Not an offer to lend.", "tone_preference": "professional", "enabled_platforms": ["linkedin", "email"], "email_recipients": ["test@example.com"] }' # Expected: 200 with profile object # 2. Get profile curl "$BASE/api/communications/profile" -H "x-api-key: $KEY" # Expected: 200 with profile # 3. Get onboarding chips curl -X POST "$BASE/api/communications/profile/generate-onboarding" \ -H "x-api-key: $KEY" -H "Content-Type: application/json" # Expected: 200 { chips: ["Rate update", ...], vertical: "mortgage_broker" } # 4. Generate drafts (requires ANTHROPIC_API_KEY set in environment) curl -X POST "$BASE/api/communications/chat" \ -H "x-api-key: $KEY" \ -H "Content-Type: application/json" \ -d '{"user_prompt": "Write a post about current mortgage rates", "enabled_platforms": ["linkedin"]}' # Expected: 200 { session_id, generation_id, drafts: [{ platform: "linkedin", status: "pending", ... }] } # 5. Reject a draft (use draft_id from step 4) curl -X POST "$BASE/api/communications/drafts//reject" \ -H "x-api-key: $KEY" -H "Content-Type: application/json" # Expected: 200 { draft_id, status: "rejected" } # 6. Get content examples curl "$BASE/api/communications/examples" -H "x-api-key: $KEY" # Expected: 200 { examples: [] } (empty on new account) ``` --- ## Environment variables **No new env vars required.** The Communications feature reuses: - `ANTHROPIC_API_KEY` — already set; used for Claude generation calls - `MYSQL_HOST`, `MYSQL_USER`, `MYSQL_PASSWORD` — already set; new tables auto-created - Platform OAuth credentials already in Redis per customer --- ## Open items for the engineer (from engineering design doc) These don't block deploy but should be confirmed before piloting with Alex Ferrari: 1. **MySQL version** — `CHECK` constraints enforced only on 8.0.16+. Run `SELECT VERSION()` to confirm. 2. **NMLS style examples** — `user_content_examples.approved_text` stores post **without** the disclaimer (stripped server-side). Verify this is correct before pilot. 3. **Instagram image URL** — drafts for Instagram generate a caption + hashtags but approval will fail without `image_url` in `draft_metadata`. The UI (Week 3) must prompt for image URL before enabling the Approve button for Instagram. 4. **`approved_posts_count` trigger** — autonomous mode toggle is NOT yet wired in UI. The counter increments correctly on each approval, but the UI to show the toggle at 10+ approvals is a Week 3 task. --- ## Related vault docs - Sprint plan: `SquareMCP/products/chat-first-communications/2026-06-15-phase1-mvp-sprint-plan.md` - Engineering design: `SquareMCP/products/chat-first-communications/2026-06-15-phase1-engineering-design.md` - Product spec: `SquareMCP/products/chat-first-communications/2026-06-15-comprehensive-product-spec.md` --- ## Changelog | Date | Author | Note | |------|--------|------| | 2026-06-15 | Claude Code | Initial handoff — Phase 1 backend complete | | 2026-06-15 | Claude Code | Added Docker stop warning — prior incident caused k8s port conflicts |