Skip to content

Email epic: support tickets, nemar admin mail, retention - #1942

Merged
neuromechanist merged 9 commits into
devfrom
feature/issue-1893-epic-email-cf
Oct 25, 2026
Merged

neuromechanist merged 9 commits into
devfrom
feature/issue-1893-epic-email-cf

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Batch 2 of epic #1893 into dev: inbound mail becomes support tickets, an admin API and nemar admin mail work them, and tickets are reduced to a summary after the retention period. Admins may be people or agents (the nemarAdmin agent is the default assignee of ordinary queues); nothing distinguishes the two.

  • Phase 3a: inbound mail becomes tickets #1928 (3a): inbound mail stored raw first, parsed, authenticated from Cloudflare's Authentication-Results, threaded (plus address, In-Reply-To, References), tagged, queued, spam-flagged and assigned (fixed, round_robin, pool, away windows). security and conduct restricted to owners; conduct exclusion. Migration 0102.
  • Phase 3b: support ticket admin API #1930 (3b): /admin/mail API (list, detail, reply with threading headers and Idempotency-Key, note, assign, status, escalate, queues, availability, reprocess), support_tickets notifications, production MAIL_INBOUND, ADR 0112. Migration 0103.
  • Phase 4: nemar admin mail CLI #1940 (4): nemar admin mail, with --json and exit codes for agents, never-twice replies, escaping of untrusted text.
  • Phase 6a: ticket retention, summary then purge #1944 (6a): summaries (summarize, --needs-summary) and a daily purge after MAIL_RETENTION_DAYS (730 production, 30 staging) keeping only the summary. Migration 0107.
  • Idempotency key for ticket notes #1945: idempotency keys for notes. Migration 0108.

Numbering: the upstream-freshness epic (#1046) holds ADR 0111 and migrations 0104-0106; this epic took 0112 and 0107-0108 so both land without collision.

Production is unaffected until the Phase 6 cutover: no production routing rule targets the Worker. Gates for that cutover: nemarOrg/docs#84 (privacy policy), the Cloudflare limit increase, a staging soak.

Part of #1893. The website Mail tab (nemarOrg/website#480) merges to staging after this.

Local end-to-end run (2026-10-24)

Local Worker (local D1, R2, queue and mail capture; no account, no secrets), CLI and website against it:

  • Inbound to support@ creates a ticket assigned to nemarAdmin; security@ is invisible to admin and agent (list, show exit 3, web 404), visible to owner
  • Agent note and reply; same-key note and reply replays store and send once; web double-click sends one reply with a web- key through the proxy
  • Requester answer threads by plus address and reopens the ticket
  • Escalation to the owner, close, summary with an address refused, summary saved
  • Purge after retention: no messages, keys or event details left, address and subject cleared, summary kept, purged ticket refuses actions, second run purges nothing
  • Unauthenticated mail classed spam; hostile subject and body escaped in CLI and web

After merge (staging)

  • Migrations 0102, 0103, 0107, 0108 applied by the deploy
  • Owner sets * assignee (dev D1 has no nemarAdmin; production does, and its migration assigns it), restricted queues fixed with an owner, escalation owner
  • Mail to support@ on the test domain creates a ticket; a reply from support@ arrives and the answer threads back
  • security@ invisible to the test-admin key, visible to the test-owner key
  • Capture fixtures and finish website#480

Test plan

  • Every phase PR reviewed (two rounds max); targeted suites, typecheck and lint green
  • CI on this PR

🤖 Generated with Claude Code

neuromechanist and others added 4 commits October 24, 2026 02:54
* feat: add support ticket tables (migration 0102)

* feat: ticket assignment and visibility rules

* feat: parse and thread inbound mail into tickets

* feat: ticket inbound mail after the raw put

* feat: release tickets on demotion, revoke, delete

* refactor: move clampUtf8Bytes to lib/utf8

* fix: tighten ticket schema checks and indexes

* fix: reserved restricted tags, tombstone unlink

* fix: authorise threading, verified auth, reopen

* fix: let a ticket's own restriction bind assignment

* fix: strict alignment, guarded reopen events
* feat: threading headers on sendEmail

* feat: support_tickets email preference

* feat: escalation owner on mail queues

* feat: notify admins of new tickets

* feat: support ticket admin routes

* chore: production MAIL_INBOUND binding

* docs: ADR 0111 support tickets

* fix: use 3a restricted tags and reply_failed

* fix: escalation owner cleared when not owner

* fix: mail routes hide counts, replays, reopens

* fix: idempotent replies, trimmed References

* docs: ADR 0111 review fixes

* fix: announce reprocessed tickets before 404

* fix: reopen guard, References keeps root

* docs: ADR 0111 key lifetime rules

* test: expect support_tickets in --all update

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
* Add terminalSafeText for ticket text

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Add mail admin API client functions

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Add nemar admin mail command group

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Escape direction marks and zero-width characters

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Harden admin mail errors, names and usage exits

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Name exit 4 in the mail help line

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Print mail output only once fully built

A failed name lookup partway through show, queues or away no longer leaves a partial render on stdout. --json detection in usage errors ignores arguments after --.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Stop advising a retry on delivery unknown

The server answers every repeat of a reply_delivery_unknown key the same way and never sends it, so the CLI no longer tells the caller to retry with it. It says to read the thread and that only a new key sends again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Escape every invisible format character

The display escape set now covers Unicode category Cf (soft hyphen, invisible operators, tag characters) and the invisible look-alikes outside it (combining grapheme joiner, Hangul fillers, variation selectors). Astral code points are written as \u{...}.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Type reply and note message ids as numbers

ReplyResponse.message_id is the stored row id, a number; the contract said string. Adds NoteResponse for the note route.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR1942

Version: 0.10.24-PR1942.5d4ada8
Commit: 5d4ada8


🤖 Updated on each push.

@neuromechanist
neuromechanist marked this pull request as draft October 24, 2026 20:39
* Add ticket summary route and purged guards

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Purge expired tickets daily, keep summaries

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Add admin mail summarize and --needs-summary

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Update allowlist pins for the ticket purge

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Keep exclusion and spam status on purge

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Harden ticket purge after review round 1

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Gate inbound and ticket writes on purged_at

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Say when a reply was sent but not recorded

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Renumber support ADR to 0112, migration to 0107

The upstream-freshness epic (#1046) already holds ADR 0111 and migrations 0104-0106. This epic keeps 0102 and 0103 and moves its later numbers past theirs so both epics merge to dev without a collision.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
* feat: idempotency key for ticket notes

Optional Idempotency-Key on the note route, stored in mail_note_keys (migration 0108), purged with the ticket. Tested with real D1. ADR 0112 updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat: nemar admin mail note sends a key

The CLI always sends an Idempotency-Key on a note (generated or --idempotency-key), prints it first, and advises a retry with it after a network failure or 5xx.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix: race loser must not log or bump a note

A keyed note's ticket bump and event run only when the preceding statement changed a row. A missing stored key after the batch is a 500. Tests: race, late purge, cascade.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat: note retry on 408 and 429, help, ADR

CLI note retries with its key on 408 and 429 (replies unchanged); help documents intent-not-text and JSON Lines stderr; ADR 0112 sentence.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@neuromechanist neuromechanist changed the title Email epic: support tickets and nemar admin mail Email epic: support tickets, nemar admin mail, retention Oct 24, 2026
@neuromechanist
neuromechanist marked this pull request as ready for review October 24, 2026 21:14
neuromechanist and others added 2 commits October 24, 2026 16:07
0107 adds the mail_tickets_activity_default trigger; the 0098 schema test
excluded mail tables and indexes but not triggers. Tested: the migration
test passes locally.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The ADR index must be gapless and this epic adds 0112. 0111 is the
upstream re-pull ADR, copied unchanged from its epic branch with its
index lines, so dev reads 0110-0112 in order. Tested: adr-index test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neuromechanist
neuromechanist merged commit a10d9a8 into dev Oct 25, 2026
68 of 70 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-1893-epic-email-cf branch October 25, 2026 00:23

This branch was previously deployed

1 inactive deployment
npm — 5d4ada8a Deployed Oct 24, 2026 by neuromechanist via publish #3937
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant