refactor(mail): Remove bd mail dependency, use bd create/list/show (gt-9xg)

Replace `bd mail send/inbox/read/ack` commands with `bd create/list/show/close`.
This separates the orchestration layer (gt) from the data plane (beads).

Changes:
- router.go: Use `bd create --type=message` instead of `bd mail send`
- mailbox.go: Use `bd list --type=message` and `bd show` for inbox/read
- types.go: Parse metadata from labels (from:, thread:, reply-to:)
- mail.go: Fix findBeadsWorkDir to prefer rig-level beads, fix crew address format

Messages are now stored as beads issues with type=message. Metadata (sender,
thread, reply-to) is stored in labels for retrieval.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-12-20 17:52:15 -08:00
parent d242239aa1
commit 4c060f4aaa
5 changed files with 114 additions and 52 deletions

View File

@@ -462,16 +462,8 @@ func runMailDelete(cmd *cobra.Command, args []string) error {
// findBeadsWorkDir finds a directory with a .beads database.
// Walks up from CWD looking for .beads/ directory.
func findBeadsWorkDir() (string, error) {
// First try workspace root
townRoot, err := workspace.FindFromCwdOrError()
if err == nil {
// Check if town root has .beads
if _, err := os.Stat(filepath.Join(townRoot, ".beads")); err == nil {
return townRoot, nil
}
}
// Walk up from CWD looking for .beads
// Walk up from CWD looking for .beads - prefer closest one (rig-level)
// This finds the rig's .beads before the town's .beads
cwd, err := os.Getwd()
if err != nil {
return "", err
@@ -490,6 +482,14 @@ func findBeadsWorkDir() (string, error) {
path = parent
}
// Fall back to town root if nothing found walking up
townRoot, err := workspace.FindFromCwdOrError()
if err == nil {
if _, err := os.Stat(filepath.Join(townRoot, ".beads")); err == nil {
return townRoot, nil
}
}
return "", fmt.Errorf("no .beads directory found")
}
@@ -520,14 +520,14 @@ func detectSender() string {
}
}
// If in a rig's crew directory, extract address
// If in a rig's crew directory, extract address (format: rig/crew/name)
if strings.Contains(cwd, "/crew/") {
parts := strings.Split(cwd, "/crew/")
if len(parts) >= 2 {
rigPath := parts[0]
crewName := strings.Split(parts[1], "/")[0]
rigName := filepath.Base(rigPath)
return fmt.Sprintf("%s/%s", rigName, crewName)
return fmt.Sprintf("%s/crew/%s", rigName, crewName)
}
}

View File

@@ -45,9 +45,11 @@ func NewMailboxBeads(identity, workDir string) *Mailbox {
}
// NewMailboxFromAddress creates a beads-backed mailbox from a GGT address.
// The address is stored as-is (not converted to identity) to match how
// messages are stored with their assignee field.
func NewMailboxFromAddress(address, workDir string) *Mailbox {
return &Mailbox{
identity: addressToIdentity(address),
identity: address, // Use address directly, not identity format
workDir: workDir,
legacy: false,
}
@@ -72,10 +74,13 @@ func (m *Mailbox) List() ([]*Message, error) {
}
func (m *Mailbox) listBeads() ([]*Message, error) {
// bd mail inbox --json
cmd := exec.Command("bd", "mail", "inbox", "--json")
// bd list --type=message --assignee=<identity> --status=open --json
cmd := exec.Command("bd", "list",
"--type", "message",
"--assignee", m.identity,
"--status", "open",
"--json")
cmd.Dir = m.workDir
cmd.Env = append(cmd.Environ(), "BD_IDENTITY="+m.identity)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
@@ -173,7 +178,8 @@ func (m *Mailbox) Get(id string) (*Message, error) {
}
func (m *Mailbox) getBeads(id string) (*Message, error) {
cmd := exec.Command("bd", "mail", "read", id, "--json")
// bd show <id> --json returns an array with one element
cmd := exec.Command("bd", "show", id, "--json")
cmd.Dir = m.workDir
var stdout, stderr bytes.Buffer
@@ -191,12 +197,16 @@ func (m *Mailbox) getBeads(id string) (*Message, error) {
return nil, err
}
var bm BeadsMessage
if err := json.Unmarshal(stdout.Bytes(), &bm); err != nil {
// bd show returns an array with one element
var beadsMsgs []BeadsMessage
if err := json.Unmarshal(stdout.Bytes(), &beadsMsgs); err != nil {
return nil, err
}
if len(beadsMsgs) == 0 {
return nil, ErrMessageNotFound
}
return bm.ToMessage(), nil
return beadsMsgs[0].ToMessage(), nil
}
func (m *Mailbox) getLegacy(id string) (*Message, error) {
@@ -221,7 +231,8 @@ func (m *Mailbox) MarkRead(id string) error {
}
func (m *Mailbox) markReadBeads(id string) error {
cmd := exec.Command("bd", "mail", "ack", id)
// bd close <id> marks the message as read
cmd := exec.Command("bd", "close", id, "--reason", "Message read")
cmd.Dir = m.workDir
var stderr bytes.Buffer
@@ -389,10 +400,12 @@ func (m *Mailbox) ListByThread(threadID string) ([]*Message, error) {
}
func (m *Mailbox) listByThreadBeads(threadID string) ([]*Message, error) {
// bd message thread <thread-id> --json
cmd := exec.Command("bd", "message", "thread", threadID, "--json")
// bd list --type=message --label=thread:<thread-id> --json
cmd := exec.Command("bd", "list",
"--type", "message",
"--label", "thread:"+threadID,
"--json")
cmd.Dir = m.workDir
cmd.Env = append(cmd.Environ(), "BD_IDENTITY="+m.identity)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout

View File

@@ -25,35 +25,46 @@ func NewRouter(workDir string) *Router {
}
}
// Send delivers a message via beads message.
// Send delivers a message via beads issue creation.
// Messages are stored as beads issues with type=message.
func (r *Router) Send(msg *Message) error {
// Convert addresses to beads identities
toIdentity := addressToIdentity(msg.To)
// Use address directly for assignee (maintains compatibility with old messages)
// The from address is converted to identity format for the labels
fromIdentity := addressToIdentity(msg.From)
// Build command: bd mail send <recipient> -s <subject> -m <body>
args := []string{"mail", "send", toIdentity,
"-s", msg.Subject,
"-m", msg.Body,
// Build command: bd create --type=message --title="subject" --assignee=recipient
// Assignee uses the original address format to match how bd mail stored them
args := []string{"create",
"--type", "message",
"--title", msg.Subject,
"--assignee", msg.To,
}
// Add body if present
if msg.Body != "" {
args = append(args, "--description", msg.Body)
}
// Add priority flag
beadsPriority := PriorityToBeads(msg.Priority)
args = append(args, "--priority", fmt.Sprintf("%d", beadsPriority))
// Add message type if set
if msg.Type != "" && msg.Type != TypeNotification {
args = append(args, "--type", string(msg.Type))
}
// Build labels for metadata (from, thread-id, reply-to, message-type)
var labels []string
labels = append(labels, "from:"+fromIdentity)
// Add thread ID if set
if msg.ThreadID != "" {
args = append(args, "--thread-id", msg.ThreadID)
labels = append(labels, "thread:"+msg.ThreadID)
}
if msg.ReplyTo != "" {
labels = append(labels, "reply-to:"+msg.ReplyTo)
}
if msg.Type != "" && msg.Type != TypeNotification {
labels = append(labels, "msg-type:"+string(msg.Type))
}
// Add reply-to if set
if msg.ReplyTo != "" {
args = append(args, "--reply-to", msg.ReplyTo)
if len(labels) > 0 {
args = append(args, "--labels", strings.Join(labels, ","))
}
cmd := exec.Command("bd", args...)

View File

@@ -4,6 +4,7 @@ package mail
import (
"crypto/rand"
"encoding/hex"
"strings"
"time"
)
@@ -142,23 +143,45 @@ func generateThreadID() string {
return "thread-" + hex.EncodeToString(b)
}
// BeadsMessage represents a message as returned by bd mail commands.
// BeadsMessage represents a message as returned by bd list/show commands.
// Messages are beads issues with type=message and metadata stored in labels.
type BeadsMessage struct {
ID string `json:"id"`
Title string `json:"title"` // Subject
Description string `json:"description"` // Body
Sender string `json:"sender"` // From identity
Assignee string `json:"assignee"` // To identity
Priority int `json:"priority"` // 0=urgent, 1=high, 2=normal, 3=low
Status string `json:"status"` // open=unread, closed=read
CreatedAt time.Time `json:"created_at"`
Type string `json:"type,omitempty"` // Message type
ThreadID string `json:"thread_id,omitempty"` // Thread identifier
ReplyTo string `json:"reply_to,omitempty"` // Original message ID
Labels []string `json:"labels"` // Metadata labels (from:X, thread:X, reply-to:X, msg-type:X)
// Cached parsed values (populated by ParseLabels)
sender string
threadID string
replyTo string
msgType string
}
// ParseLabels extracts metadata from the labels array.
func (bm *BeadsMessage) ParseLabels() {
for _, label := range bm.Labels {
if strings.HasPrefix(label, "from:") {
bm.sender = strings.TrimPrefix(label, "from:")
} else if strings.HasPrefix(label, "thread:") {
bm.threadID = strings.TrimPrefix(label, "thread:")
} else if strings.HasPrefix(label, "reply-to:") {
bm.replyTo = strings.TrimPrefix(label, "reply-to:")
} else if strings.HasPrefix(label, "msg-type:") {
bm.msgType = strings.TrimPrefix(label, "msg-type:")
}
}
}
// ToMessage converts a BeadsMessage to a GGT Message.
func (bm *BeadsMessage) ToMessage() *Message {
// Parse labels to extract metadata
bm.ParseLabels()
// Convert beads priority (0=urgent, 1=high, 2=normal, 3=low) to GGT Priority
var priority Priority
switch bm.Priority {
@@ -174,14 +197,14 @@ func (bm *BeadsMessage) ToMessage() *Message {
// Convert message type, default to notification
msgType := TypeNotification
switch MessageType(bm.Type) {
switch MessageType(bm.msgType) {
case TypeTask, TypeScavenge, TypeReply:
msgType = MessageType(bm.Type)
msgType = MessageType(bm.msgType)
}
return &Message{
ID: bm.ID,
From: identityToAddress(bm.Sender),
From: identityToAddress(bm.sender),
To: identityToAddress(bm.Assignee),
Subject: bm.Title,
Body: bm.Description,
@@ -189,8 +212,8 @@ func (bm *BeadsMessage) ToMessage() *Message {
Read: bm.Status == "closed",
Priority: priority,
Type: msgType,
ThreadID: bm.ThreadID,
ReplyTo: bm.ReplyTo,
ThreadID: bm.threadID,
ReplyTo: bm.replyTo,
}
}