Phase 5: reply drafts — template/LLM draft, edit, SMTP send (AUD-71)

Implements Phase 5 of AGENTS.md §11 (FR-8, FR-15):

- Message detail pane (GET /partials/message) with body, recommended
  action, reply identity, template picker, editable draft, and history.
- internal/reply: OmniRoute (OpenAI-compatible) chat client and a
  template-based draft generator. All non-JEV models go through
  OmniRoute (§10).
- internal/mailops: SMTP send over smtp.gmail.com:587 with STARTTLS and
  the app password, forcing IPv4 (§9); FR-15 reply identity (send as the
  arrived-at X-Pm-Original-To address, else "Sam Rolfe"); threading via
  In-Reply-To/References; -check-smtp safe verification; and a
  [Gmail]/Sent Mail thread check.
- Migration v5 stores messages.message_id_header and drafts.sent_at;
  mailfetch parses Message-ID.
- Send is user-triggered only: CSRF + UI confirm + confirm=yes guard,
  no auto-send, duplicate-send refused. A sent reply files the original
  under Replied-to/<address> and logs every step (FR-13).
- Tests for reply, mailops SMTP/identity/thread check, store drafts and
  migration v5, and the server draft/edit/send guards.
- docs/phase5-replies.md, README, .env.example, docker-compose updated.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Paperclip Builder
2026-10-11 22:05:35 +11:00
parent ae53374ad1
commit 7ca888f0c6
31 changed files with 2556 additions and 42 deletions

117
internal/store/drafts.go Normal file
View File

@@ -0,0 +1,117 @@
// Reply-draft persistence for Phase 5 (FR-8).
//
// A draft belongs to one message. It is rewritten in place while Sam edits it
// and marked `sent` after a successful SMTP send. Only the newest draft per
// message is shown in the UI; history is kept in the actions_log (FR-13).
package store
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
)
// Draft statuses. A draft moves draft -> sent once, on a successful send.
const (
DraftStatusDraft = "draft"
DraftStatusSent = "sent"
)
// Draft is one reply draft row.
type Draft struct {
ID int64
MessageID int64
Body string
TemplateID string
Status string
SentAt time.Time
CreatedAt time.Time
UpdatedAt time.Time
}
// SaveDraft inserts a new draft for a message and returns it.
func (s *Store) SaveDraft(ctx context.Context, messageID int64, body, templateID string) (Draft, error) {
const q = `INSERT INTO drafts (message_id, body, template_id, status)
VALUES (?, ?, ?, 'draft')`
res, err := s.db.ExecContext(ctx, q, messageID, body, nullString(templateID))
if err != nil {
return Draft{}, fmt.Errorf("save draft for message %d: %w", messageID, err)
}
id, err := res.LastInsertId()
if err != nil {
return Draft{}, fmt.Errorf("read draft id: %w", err)
}
return s.DraftByID(ctx, id)
}
// DraftByID loads one draft.
func (s *Store) DraftByID(ctx context.Context, id int64) (Draft, error) {
const q = `
SELECT id, message_id, body, COALESCE(template_id, ''), status,
COALESCE(sent_at, ''), created_at, updated_at
FROM drafts WHERE id = ?`
return scanDraft(s.db.QueryRowContext(ctx, q, id))
}
// LatestDraft returns the newest draft for a message. ok is false when the
// message has no draft yet.
func (s *Store) LatestDraft(ctx context.Context, messageID int64) (Draft, bool, error) {
const q = `
SELECT id, message_id, body, COALESCE(template_id, ''), status,
COALESCE(sent_at, ''), created_at, updated_at
FROM drafts WHERE message_id = ? ORDER BY id DESC LIMIT 1`
d, err := scanDraft(s.db.QueryRowContext(ctx, q, messageID))
if errors.Is(err, sql.ErrNoRows) {
return Draft{}, false, nil
}
if err != nil {
return Draft{}, false, err
}
return d, true, nil
}
// UpdateDraftBody replaces the body of a draft Sam edited and bumps updated_at.
func (s *Store) UpdateDraftBody(ctx context.Context, id int64, body string) error {
const q = `UPDATE drafts SET body = ?, updated_at = datetime('now') WHERE id = ?`
res, err := s.db.ExecContext(ctx, q, body, id)
if err != nil {
return fmt.Errorf("update draft %d: %w", id, err)
}
if n, _ := res.RowsAffected(); n == 0 {
return fmt.Errorf("update draft %d: not found", id)
}
return nil
}
// MarkDraftSent records a successful send.
func (s *Store) MarkDraftSent(ctx context.Context, id int64) error {
const q = `UPDATE drafts SET status = 'sent', sent_at = datetime('now'),
updated_at = datetime('now') WHERE id = ?`
res, err := s.db.ExecContext(ctx, q, id)
if err != nil {
return fmt.Errorf("mark draft %d sent: %w", id, err)
}
if n, _ := res.RowsAffected(); n == 0 {
return fmt.Errorf("mark draft %d sent: not found", id)
}
return nil
}
func scanDraft(row *sql.Row) (Draft, error) {
var (
d Draft
sentAt string
created string
updated string
)
if err := row.Scan(&d.ID, &d.MessageID, &d.Body, &d.TemplateID, &d.Status,
&sentAt, &created, &updated); err != nil {
return Draft{}, err
}
d.SentAt = parseSQLTime(sentAt)
d.CreatedAt = parseSQLTime(created)
d.UpdatedAt = parseSQLTime(updated)
return d, nil
}

View File

@@ -0,0 +1,146 @@
package store
import (
"context"
"database/sql"
"path/filepath"
"testing"
)
func TestDraftsRoundTrip(t *testing.T) {
s, ctx := testStore(t)
id := seedMessage(t, s, ctx, "1", "question", "hello@where-woof.com")
d, err := s.SaveDraft(ctx, id, "Thanks, got it.", "quick-ack")
if err != nil {
t.Fatalf("SaveDraft() error = %v", err)
}
if d.ID == 0 || d.Status != DraftStatusDraft || d.TemplateID != "quick-ack" {
t.Fatalf("SaveDraft() = %+v", d)
}
if d.MessageID != id {
t.Errorf("MessageID = %d, want %d", d.MessageID, id)
}
if err := s.UpdateDraftBody(ctx, d.ID, "Edited body."); err != nil {
t.Fatalf("UpdateDraftBody() error = %v", err)
}
latest, ok, err := s.LatestDraft(ctx, id)
if err != nil || !ok {
t.Fatalf("LatestDraft() ok=%v err=%v", ok, err)
}
if latest.Body != "Edited body." || latest.Status != DraftStatusDraft {
t.Errorf("latest = %+v", latest)
}
if err := s.MarkDraftSent(ctx, latest.ID); err != nil {
t.Fatalf("MarkDraftSent() error = %v", err)
}
sent, _, err := s.LatestDraft(ctx, id)
if err != nil {
t.Fatalf("LatestDraft() after send error = %v", err)
}
if sent.Status != DraftStatusSent || sent.SentAt.IsZero() {
t.Errorf("sent draft = %+v, want status=sent and a sent_at", sent)
}
}
func TestLatestDraftMissing(t *testing.T) {
s, ctx := testStore(t)
id := seedMessage(t, s, ctx, "1", "x", "a@b.test")
if _, ok, err := s.LatestDraft(ctx, id); err != nil || ok {
t.Fatalf("LatestDraft() ok=%v err=%v, want ok=false", ok, err)
}
}
func TestMessageByIDIncludesReplyFields(t *testing.T) {
s, ctx := testStore(t)
id, err := s.UpsertMessage(ctx, &Message{
GmailID: "99",
FromAddr: "Customer <cust@example.test>",
Subject: "Booking",
BodyText: "Saturday?",
SourceAddress: "hello@where-woof.com",
MessageIDHeader: "<orig-1@mail.example>",
})
if err != nil {
t.Fatalf("UpsertMessage() error = %v", err)
}
msg, err := s.MessageByID(ctx, id)
if err != nil {
t.Fatalf("MessageByID() error = %v", err)
}
if msg.MessageIDHeader != "<orig-1@mail.example>" {
t.Errorf("MessageIDHeader = %q", msg.MessageIDHeader)
}
if msg.BodyText != "Saturday?" || msg.SourceAddress != "hello@where-woof.com" {
t.Errorf("message = %+v", msg)
}
}
func TestMigrationV5AddsReplyColumns(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "old.db")
// Simulate a v4 database: messages has no message_id_header and drafts has
// no sent_at. Opening it must apply migration v5.
db, err := sql.Open("sqlite", "file:"+path)
if err != nil {
t.Fatalf("open old db: %v", err)
}
stmts := []string{
`CREATE TABLE messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
gmail_id TEXT NOT NULL UNIQUE,
thread_id TEXT, from_addr TEXT, to_addr TEXT, subject TEXT,
body_text TEXT, source_address TEXT, date_sent TEXT,
imap_uid INTEGER, imap_uidvalidity INTEGER,
folder TEXT NOT NULL DEFAULT 'inbox',
mailbox TEXT NOT NULL DEFAULT 'INBOX',
received_at TEXT NOT NULL DEFAULT (datetime('now')),
created_at TEXT NOT NULL DEFAULT (datetime('now')))`,
`CREATE TABLE drafts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
message_id INTEGER NOT NULL,
body TEXT NOT NULL,
template_id TEXT,
status TEXT NOT NULL DEFAULT 'draft',
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now')))`,
`CREATE TABLE schema_migrations (version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL DEFAULT (datetime('now')))`,
`INSERT INTO schema_migrations (version) VALUES (4)`,
}
for _, q := range stmts {
if _, err := db.ExecContext(ctx, q); err != nil {
t.Fatalf("seed old db: %v", err)
}
}
_ = db.Close()
s, err := Open(ctx, path)
if err != nil {
t.Fatalf("Open() on v4 database error = %v", err)
}
defer s.Close()
v, err := s.SchemaVersion(ctx)
if err != nil {
t.Fatalf("SchemaVersion() error = %v", err)
}
if v < 5 {
t.Errorf("SchemaVersion() = %d, want >= 5", v)
}
for _, tc := range []struct{ table, col string }{
{"messages", "message_id_header"},
{"drafts", "sent_at"},
} {
var n int
q := `SELECT COUNT(*) FROM pragma_table_info('` + tc.table + `') WHERE name = ?`
if err := s.DB().QueryRowContext(ctx, q, tc.col).Scan(&n); err != nil {
t.Fatalf("count %s.%s: %v", tc.table, tc.col, err)
}
if n != 1 {
t.Errorf("%s.%s missing after migration v5", tc.table, tc.col)
}
}
}

View File

@@ -14,9 +14,12 @@ import (
// Message is one row of the messages table.
type Message struct {
ID int64
GmailID string
ThreadID string
ID int64
GmailID string
ThreadID string
// MessageIDHeader is the RFC822 Message-ID header (Phase 5, FR-8). A reply
// uses it for In-Reply-To and References so Gmail keeps the thread.
MessageIDHeader string
FromAddr string
ToAddr string
Subject string
@@ -67,11 +70,12 @@ func ValidFolder(folder string) bool {
func (s *Store) UpsertMessage(ctx context.Context, m *Message) (int64, error) {
const q = `
INSERT INTO messages (
gmail_id, thread_id, from_addr, to_addr, subject, body_text,
gmail_id, thread_id, message_id_header, from_addr, to_addr, subject, body_text,
source_address, date_sent, received_at, imap_uid, imap_uidvalidity
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (gmail_id) DO UPDATE SET
thread_id = excluded.thread_id,
message_id_header = excluded.message_id_header,
from_addr = excluded.from_addr,
to_addr = excluded.to_addr,
subject = excluded.subject,
@@ -88,6 +92,7 @@ ON CONFLICT (gmail_id) DO UPDATE SET
_, err := s.db.ExecContext(ctx, q,
m.GmailID,
nullString(m.ThreadID),
nullString(m.MessageIDHeader),
nullString(m.FromAddr),
nullString(m.ToAddr),
nullString(m.Subject),
@@ -110,6 +115,35 @@ ON CONFLICT (gmail_id) DO UPDATE SET
return id, nil
}
// MessageByID loads one message including the reply fields (body, source
// address, and Message-ID header). It is the read path for a reply draft.
func (s *Store) MessageByID(ctx context.Context, id int64) (Message, error) {
const q = `
SELECT m.id, m.gmail_id, COALESCE(m.thread_id, ''), COALESCE(m.from_addr, ''),
COALESCE(m.to_addr, ''), COALESCE(m.subject, ''), COALESCE(m.body_text, ''),
COALESCE(m.source_address, ''), COALESCE(m.date_sent, ''), m.received_at,
COALESCE(m.imap_uid, 0), COALESCE(m.imap_uidvalidity, 0),
COALESCE(m.folder, 'inbox'), COALESCE(m.mailbox, 'INBOX'),
COALESCE(m.message_id_header, '')
FROM messages m WHERE m.id = ?`
var (
m Message
dateSent string
received string
)
err := s.db.QueryRowContext(ctx, q, id).Scan(
&m.ID, &m.GmailID, &m.ThreadID, &m.FromAddr, &m.ToAddr, &m.Subject,
&m.BodyText, &m.SourceAddress, &dateSent, &received,
&m.IMAPUID, &m.IMAPUIDValidity, &m.Folder, &m.Mailbox, &m.MessageIDHeader,
)
if err != nil {
return Message{}, fmt.Errorf("load message %d: %w", id, err)
}
m.DateSent = parseSQLTime(dateSent)
m.ReceivedAt = parseSQLTime(received)
return m, nil
}
// ListMessages returns the most recent messages, newest first, joined to their
// latest classification when one exists.
func (s *Store) ListMessages(ctx context.Context, limit int) ([]MessageListItem, error) {

View File

@@ -18,6 +18,9 @@ CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
gmail_id TEXT NOT NULL UNIQUE,
thread_id TEXT,
-- Phase 5 (FR-8): the RFC822 Message-ID header. A reply sets In-Reply-To and
-- References to it so Gmail files the reply in the same thread.
message_id_header TEXT,
from_addr TEXT,
to_addr TEXT,
subject TEXT,
@@ -105,7 +108,9 @@ CREATE TABLE IF NOT EXISTS drafts (
message_id INTEGER NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
body TEXT NOT NULL,
template_id TEXT,
-- Phase 5 (FR-8): draft -> sent. sent_at is set when the SMTP send succeeds.
status TEXT NOT NULL DEFAULT 'draft',
sent_at TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);

View File

@@ -144,6 +144,21 @@ func (s *Store) applyMigrations(ctx context.Context) error {
return fmt.Errorf("record schema version 4: %w", err)
}
}
if version < 5 {
// Phase 5 (FR-8): store the RFC822 Message-ID so a reply can set
// In-Reply-To/References and stay in the same Gmail thread, and record
// when a draft was sent.
if err := s.ensureColumn(ctx, "messages", "message_id_header", "TEXT"); err != nil {
return err
}
if err := s.ensureColumn(ctx, "drafts", "sent_at", "TEXT"); err != nil {
return err
}
if _, err := s.db.ExecContext(ctx,
`INSERT OR IGNORE INTO schema_migrations (version) VALUES (5)`); err != nil {
return fmt.Errorf("record schema version 5: %w", err)
}
}
return nil
}

View File

@@ -238,6 +238,33 @@ type ActionLogEntry struct {
CreatedAt string
}
// MessageActions returns the action-log rows for one message, newest first.
// It backs the message detail pane (FR-13).
func (s *Store) MessageActions(ctx context.Context, messageID int64, limit int) ([]ActionLogEntry, error) {
if limit < 1 {
limit = 20
}
rows, err := s.db.QueryContext(ctx, `
SELECT id, message_id, action, COALESCE(actor, ''), COALESCE(detail, ''), created_at
FROM actions_log WHERE message_id = ? ORDER BY id DESC LIMIT ?`, messageID, limit)
if err != nil {
return nil, fmt.Errorf("message actions: %w", err)
}
defer rows.Close()
var out []ActionLogEntry
for rows.Next() {
var e ActionLogEntry
if err := rows.Scan(&e.ID, &e.MessageID, &e.Action, &e.Actor, &e.Detail, &e.CreatedAt); err != nil {
return nil, fmt.Errorf("scan message action: %w", err)
}
out = append(out, e)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("message actions: %w", err)
}
return out, nil
}
// RecentActions returns the newest action-log rows, newest first.
func (s *Store) RecentActions(ctx context.Context, limit int) ([]ActionLogEntry, error) {
if limit < 1 {