Compare commits
@@ -0,0 +1 @@
|
||||
{"sessionId":"45d44ada-86af-4207-b3dd-de510e521157","pid":20439,"acquiredAt":1773554855575}
|
||||
@@ -44,3 +44,4 @@ __pycache__/
|
||||
# Debug
|
||||
__debug_bin*
|
||||
.claude/worktrees/
|
||||
synapbus-linux-amd64
|
||||
|
||||
@@ -103,6 +103,13 @@ make lint # Run linters
|
||||
- Go 1.25+ (per go.mod) + go-chi/chi (HTTP), mark3labs/mcp-go (MCP), ory/fosite (OAuth), spf13/cobra (CLI), modernc.org/sqlite (storage), TFMV/hnsw (vectors). NEW: coreos/go-oidc/v3 (OIDC), golang.org/x/oauth2 (OAuth client) (007-platform-features-bundle)
|
||||
- Go 1.25+ (backend), SvelteKit 2 + Svelte 5 (frontend), SvelteKit (website) + go-chi/chi (HTTP), mark3labs/mcp-go (MCP), modernc.org/sqlite (storage), SherClockHolmes/webpush-go (push notifications — NEW) (008-webui-pwa-analytics)
|
||||
- SQLite (existing DB, 1 new migration for push_subscriptions), localStorage (font size) (008-webui-pwa-analytics)
|
||||
- Go 1.25+ (backend), Svelte 5 + Tailwind (frontend) + go-chi/chi (HTTP), mark3labs/mcp-go (MCP), modernc.org/sqlite (storage), spf13/cobra (CLI) (009-attachments-threads)
|
||||
- SQLite (modernc.org/sqlite, pure Go) + content-addressable filesystem (SHA-256) (009-attachments-threads)
|
||||
- SQLite (modernc.org/sqlite, pure Go) — new migration 013_reactions.sql (010-reactions-workflows)
|
||||
- Go 1.25+ (SynapBus), Python 3.12 (Searcher agents) + go-chi/chi, mark3labs/mcp-go, ory/fosite (SynapBus); claude-agent-sdk, httpx, psycopg (Searcher) (013-linkedin-approval-workflow)
|
||||
- SQLite via modernc.org/sqlite (SynapBus); PostgreSQL (Searcher) (013-linkedin-approval-workflow)
|
||||
- Go 1.25+ (per go.mod) + go-chi/chi (HTTP), mark3labs/mcp-go (MCP), spf13/cobra (CLI), modernc.org/sqlite (storage), k8s.io/client-go (K8s Jobs) (014-reactive-agent-triggers)
|
||||
- SQLite via modernc.org/sqlite — new migration 015_reactive_triggers.sql (014-reactive-agent-triggers)
|
||||
|
||||
## Recent Changes
|
||||
- 002-mcp-auth-ux-polish: Added Go 1.23+ + ory/fosite (OAuth 2.1), mark3labs/mcp-go (MCP server), go-chi/chi (HTTP), Svelte 5 + Tailwind (Web UI)
|
||||
|
||||
+84
-45
@@ -1,62 +1,101 @@
|
||||
# Autonomous Execution Summary: SynapBus v0.7.0
|
||||
# Autonomous Implementation Summary: Message Reactions & Workflow States
|
||||
|
||||
**Date**: 2026-03-17
|
||||
**Branch**: `008-webui-pwa-analytics`
|
||||
**Status**: Complete — all tests pass, binary builds, endpoints verified
|
||||
**Branch**: `010-reactions-workflows`
|
||||
**Date**: 2026-03-18
|
||||
**Status**: Complete (StalemateWorker extension deferred)
|
||||
|
||||
## Features Implemented
|
||||
## What Was Built
|
||||
|
||||
### 1. Analytics Dashboard (P1)
|
||||
- 4 new REST endpoints (`/api/analytics/timeline`, `/top-agents`, `/top-channels`, `/summary`)
|
||||
- SVG bar chart (`AnalyticsChart.svelte`), ranked list (`TopList.svelte`), redesigned dashboard with stat cards and time span selector (1h, 4h, 24h, 7d, 1month)
|
||||
### Message Reactions
|
||||
- **Toggle semantics**: Add a reaction → added. Add same reaction again → removed. One per type per agent per message.
|
||||
- **5 reaction types**: approve, reject, in_progress, done, published
|
||||
- **Metadata support**: JSON metadata on reactions (e.g., `{"url": "https://..."}` for published)
|
||||
- **100-reaction limit** per message (safety)
|
||||
|
||||
### 2. PWA Conversion (P1)
|
||||
- PWA manifest (`manifest.json`), service worker (`sw.js`), SVG icon
|
||||
- Cache-first for static assets, network-only for API, push notification handling
|
||||
### Workflow State Derivation
|
||||
- State computed from reactions: published > done > rejected > in_progress > approved > proposed
|
||||
- No denormalization — state derived on read from reaction list
|
||||
- Channel messages with no reactions → "proposed" state
|
||||
- Terminal states (rejected, done, published) don't trigger stalemate checks
|
||||
|
||||
### 3. Push Notifications (P1)
|
||||
- `internal/push/` package — VAPID key generation, Web Push sending, SQLite subscription store
|
||||
- API: `POST/DELETE /api/push/subscribe`, `GET /api/push/vapid-key`
|
||||
- Push toggle in Settings, migration `012_push_subscriptions.sql`
|
||||
### Channel Workflow Settings
|
||||
- `auto_approve` — skip proposed state for new messages
|
||||
- `stalemate_remind_after` — duration before reminder DM (default 24h)
|
||||
- `stalemate_escalate_after` — duration before escalation to #approvals (default 72h)
|
||||
|
||||
### 4. Auto-Resizing Textarea (P1)
|
||||
- ComposeForm textarea auto-grows 3→12 lines, then scrollbar. Resets on send.
|
||||
### REST API
|
||||
- `POST /api/messages/{id}/reactions` — toggle reaction (add or remove)
|
||||
- `GET /api/messages/{id}/reactions` — get reactions + workflow state
|
||||
- `DELETE /api/messages/{id}/reactions/{reaction}` — remove reaction
|
||||
- `PUT /api/channels/{name}/settings` — update workflow settings
|
||||
- `GET /api/channels/{name}/messages/by-state?state=X` — list messages by state
|
||||
|
||||
### 5. Smart Mention/Channel Highlighting (P2)
|
||||
- Entities store caches agents/channels. MessageBody validates @mentions and #channels:
|
||||
existing → link, deleted → "inactive" badge, unknown → plain text. Handles email/issue number edge cases.
|
||||
### MCP Tools (via execute bridge)
|
||||
- `react` — add/toggle reaction on a message
|
||||
- `unreact` — remove a reaction
|
||||
- `get_reactions` — query reactions and workflow state
|
||||
- `list_by_state` — list messages by workflow state in a channel
|
||||
|
||||
### 6. Editable Agent Display Name (P2)
|
||||
- Inline edit on agent detail page (click → edit, Enter → save, Escape → cancel)
|
||||
### Web UI
|
||||
- **WorkflowBadge** component: colored pills (yellow/green/blue/red/gray/cyan) per state
|
||||
- **ReactionPills** component: grouped reaction pills with count, agent names on hover, click-to-toggle
|
||||
- Published reactions with URL show clickable link icon
|
||||
- Integrated into channel message view
|
||||
|
||||
### 7. Editable Human Display Name (P2)
|
||||
- `PUT /api/auth/profile` endpoint, `UpdateDisplayName` in UserStore, Settings page field
|
||||
### Admin CLI
|
||||
- `synapbus channels update --name X --auto-approve=true --stalemate-remind-after=12h --stalemate-escalate-after=48h`
|
||||
|
||||
### 8. Font Size Preference (P3)
|
||||
- fontSize store (12–24px, 2px steps), -/+ controls in Settings, persisted in localStorage
|
||||
## Files Created/Modified
|
||||
|
||||
### 9. Version Display (P3)
|
||||
- `GET /api/version` endpoint, version footer in layout linked to GitHub repo
|
||||
### New Files
|
||||
| File | Description |
|
||||
|------|-------------|
|
||||
| `internal/storage/schema/013_reactions.sql` | Migration: message_reactions table + channel columns |
|
||||
| `internal/reactions/model.go` | Reaction types, state derivation, constants |
|
||||
| `internal/reactions/store.go` | SQLite CRUD for reactions |
|
||||
| `internal/reactions/service.go` | Business logic: toggle, remove, get, list by state |
|
||||
| `internal/reactions/model_test.go` | 23 test cases for model functions |
|
||||
| `internal/reactions/store_test.go` | 6 test functions for store operations |
|
||||
| `internal/api/reactions_handler.go` | REST API handlers for reactions |
|
||||
| `web/src/lib/components/WorkflowBadge.svelte` | Colored state badge component |
|
||||
| `web/src/lib/components/ReactionPills.svelte` | Reaction toggle pills component |
|
||||
|
||||
### 10. MCP Prompts (P2)
|
||||
- 4 prompts: daily-digest, agent-health-check, channel-overview, debug-agent
|
||||
- `internal/mcp/prompts.go` registered in server.go
|
||||
|
||||
### 11. Website Update (P3)
|
||||
- Updated hero/features messaging at ~/repos/synapbus-website/ for individual/small-team positioning
|
||||
### Modified Files
|
||||
| File | Changes |
|
||||
|------|---------|
|
||||
| `internal/messaging/types.go` | Added WorkflowState, Reactions, ReactionInfo to Message |
|
||||
| `internal/messaging/service.go` | Added ReactionEnricher interface, enrichment in EnrichMessages |
|
||||
| `internal/channels/types.go` | Added AutoApprove, StalemateRemindAfter, StalemateEscalateAfter, ChannelSettings |
|
||||
| `internal/channels/store.go` | Updated SELECT queries for new columns, added UpdateChannelSettings |
|
||||
| `internal/channels/service.go` | Added UpdateChannelSettings method |
|
||||
| `internal/api/router.go` | Registered reaction and channel settings routes |
|
||||
| `internal/api/channels_handler.go` | Added UpdateSettings, ListByState handlers |
|
||||
| `internal/mcp/bridge.go` | Added react/unreact/get_reactions/list_by_state bridge methods |
|
||||
| `internal/mcp/tools_hybrid.go` | Added reactionService to registrar |
|
||||
| `internal/mcp/server.go` | Added reactionService parameter |
|
||||
| `internal/actions/registry.go` | Registered 4 new reaction actions |
|
||||
| `cmd/synapbus/main.go` | Wired reaction service, adapter, passed to router+MCP |
|
||||
| `cmd/synapbus/admin.go` | Added channels update CLI command |
|
||||
| `internal/admin/socket.go` | Added channels.update_settings handler |
|
||||
| `web/src/lib/api/client.ts` | Added reactions.toggle/get methods |
|
||||
| `web/src/routes/channels/[name]/+page.svelte` | Integrated WorkflowBadge + ReactionPills |
|
||||
|
||||
## Test Results
|
||||
|
||||
All 24 Go packages PASS. All API endpoints verified via curl. Web UI builds successfully. Binary compiles with CGO_ENABLED=0.
|
||||
- **25 Go test packages**: all pass, 0 failures
|
||||
- **New tests**: 29+ test cases (model: 23, store: 6)
|
||||
- **Integration tests**: 9 E2E tests pass
|
||||
- **Web build**: Svelte SPA builds successfully
|
||||
- **Binary build**: Compiles cleanly
|
||||
|
||||
## New Files
|
||||
## Deferred
|
||||
|
||||
- `internal/api/analytics_handler.go` + test
|
||||
- `internal/api/version_handler.go` + test
|
||||
- `internal/api/push_handler.go`
|
||||
- `internal/push/service.go` + test, `store.go` + test
|
||||
- `internal/mcp/prompts.go` + test
|
||||
- `schema/012_push_subscriptions.sql`
|
||||
- `web/src/lib/components/AnalyticsChart.svelte`, `TopList.svelte`
|
||||
- `web/src/lib/stores/fontSize.ts`, `entities.ts`
|
||||
- `web/static/manifest.json`, `sw.js`, `icons/icon.svg`
|
||||
- **StalemateWorker extension** (T023-T025): The data model, channel settings, and query infrastructure are in place. The worker just needs a scan loop added to detect stale messages and send DMs/escalations. This is a straightforward follow-up task.
|
||||
|
||||
## Architecture Decisions
|
||||
|
||||
1. **Separate reactions package**: Clean domain separation from messaging
|
||||
2. **Toggle semantics**: INSERT if absent, DELETE if present — simple, atomic, idempotent
|
||||
3. **Derived workflow state**: No denormalization; state computed from reactions on read
|
||||
4. **Bridge actions (not hybrid tools)**: Consistent with attachments pattern — 4 hybrid tools are stable surface area
|
||||
5. **ReactionEnricher adapter**: Avoids circular dependency between reactions and messaging packages
|
||||
|
||||
+229
-2
@@ -1,11 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bufio"
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
@@ -632,7 +636,46 @@ func addAdminCommands(rootCmd *cobra.Command) {
|
||||
channelsJoinCmd.MarkFlagRequired("channel")
|
||||
channelsJoinCmd.MarkFlagRequired("agent")
|
||||
|
||||
channelsCmd.AddCommand(channelsListCmd, channelsShowCmd, channelsCreateCmd, channelsJoinCmd)
|
||||
var (
|
||||
channelsUpdateName string
|
||||
channelsUpdateAutoApprove string
|
||||
channelsUpdateStalemateRemind string
|
||||
channelsUpdateStalemateEscalate string
|
||||
)
|
||||
channelsUpdateCmd := &cobra.Command{
|
||||
Use: "update",
|
||||
Short: "Update channel settings (auto-approve, stalemate timers)",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if channelsUpdateName == "" {
|
||||
return fmt.Errorf("--name is required")
|
||||
}
|
||||
reqArgs := map[string]interface{}{
|
||||
"name": channelsUpdateName,
|
||||
}
|
||||
if cmd.Flags().Changed("auto-approve") {
|
||||
reqArgs["auto_approve"] = channelsUpdateAutoApprove == "true"
|
||||
}
|
||||
if cmd.Flags().Changed("stalemate-remind-after") {
|
||||
reqArgs["stalemate_remind_after"] = channelsUpdateStalemateRemind
|
||||
}
|
||||
if cmd.Flags().Changed("stalemate-escalate-after") {
|
||||
reqArgs["stalemate_escalate_after"] = channelsUpdateStalemateEscalate
|
||||
}
|
||||
resp, err := adminRequest("channels.update_settings", reqArgs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printJSON(resp["data"])
|
||||
return nil
|
||||
},
|
||||
}
|
||||
channelsUpdateCmd.Flags().StringVar(&channelsUpdateName, "name", "", "Channel name")
|
||||
channelsUpdateCmd.Flags().StringVar(&channelsUpdateAutoApprove, "auto-approve", "", "Auto-approve messages (true|false)")
|
||||
channelsUpdateCmd.Flags().StringVar(&channelsUpdateStalemateRemind, "stalemate-remind-after", "", "Stalemate reminder duration (e.g. 24h)")
|
||||
channelsUpdateCmd.Flags().StringVar(&channelsUpdateStalemateEscalate, "stalemate-escalate-after", "", "Stalemate escalation duration (e.g. 72h)")
|
||||
channelsUpdateCmd.MarkFlagRequired("name")
|
||||
|
||||
channelsCmd.AddCommand(channelsListCmd, channelsShowCmd, channelsCreateCmd, channelsJoinCmd, channelsUpdateCmd)
|
||||
|
||||
// ----- conversations commands -----
|
||||
conversationsCmd := &cobra.Command{
|
||||
@@ -987,7 +1030,48 @@ func addAdminCommands(rootCmd *cobra.Command) {
|
||||
},
|
||||
}
|
||||
|
||||
attachmentsCmd.AddCommand(attachmentsGCCmd)
|
||||
var attachmentsBackupOutput string
|
||||
var attachmentsBackupDataDir string
|
||||
attachmentsBackupCmd := &cobra.Command{
|
||||
Use: "backup",
|
||||
Short: "Create a tar.gz backup of all attachments (no server required)",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
attachDir := filepath.Join(attachmentsBackupDataDir, "attachments")
|
||||
if _, err := os.Stat(attachDir); os.IsNotExist(err) {
|
||||
return fmt.Errorf("attachments directory does not exist: %s", attachDir)
|
||||
}
|
||||
fileCount, totalSize, err := backupAttachments(attachDir, attachmentsBackupOutput)
|
||||
if err != nil {
|
||||
return fmt.Errorf("backup failed: %w", err)
|
||||
}
|
||||
fmt.Printf("Backup complete: %d files, %s total, written to %s\n", fileCount, formatBytes(totalSize), attachmentsBackupOutput)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
attachmentsBackupCmd.Flags().StringVar(&attachmentsBackupOutput, "output", "", "Output path for the tar.gz archive")
|
||||
attachmentsBackupCmd.Flags().StringVar(&attachmentsBackupDataDir, "data", "./data", "Data directory")
|
||||
attachmentsBackupCmd.MarkFlagRequired("output")
|
||||
|
||||
var attachmentsRestoreInput string
|
||||
var attachmentsRestoreDataDir string
|
||||
attachmentsRestoreCmd := &cobra.Command{
|
||||
Use: "restore",
|
||||
Short: "Restore attachments from a tar.gz backup (no server required)",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
attachDir := filepath.Join(attachmentsRestoreDataDir, "attachments")
|
||||
restored, skipped, err := restoreAttachments(attachDir, attachmentsRestoreInput)
|
||||
if err != nil {
|
||||
return fmt.Errorf("restore failed: %w", err)
|
||||
}
|
||||
fmt.Printf("Restore complete: %d files restored, %d files skipped (already exist)\n", restored, skipped)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
attachmentsRestoreCmd.Flags().StringVar(&attachmentsRestoreInput, "input", "", "Input path for the tar.gz archive")
|
||||
attachmentsRestoreCmd.Flags().StringVar(&attachmentsRestoreDataDir, "data", "./data", "Data directory")
|
||||
attachmentsRestoreCmd.MarkFlagRequired("input")
|
||||
|
||||
attachmentsCmd.AddCommand(attachmentsGCCmd, attachmentsBackupCmd, attachmentsRestoreCmd)
|
||||
|
||||
// ----- add persistent flag and commands to root -----
|
||||
rootCmd.PersistentFlags().StringVar(&adminSocket, "socket", "/tmp/synapbus.sock", "Path to admin Unix socket")
|
||||
@@ -1012,3 +1096,146 @@ func toTableRows(data []map[string]string, headerMap map[string]string) []map[st
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// backupAttachments creates a tar.gz archive of the attachments directory.
|
||||
// Returns the number of files archived and total bytes of file content.
|
||||
func backupAttachments(attachmentsDir, outputPath string) (int, int64, error) {
|
||||
outFile, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("create output file: %w", err)
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
gzw := gzip.NewWriter(outFile)
|
||||
defer gzw.Close()
|
||||
|
||||
tw := tar.NewWriter(gzw)
|
||||
defer tw.Close()
|
||||
|
||||
var fileCount int
|
||||
var totalSize int64
|
||||
|
||||
err = filepath.Walk(attachmentsDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Skip directories — tar entries for files include the path.
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
relPath, err := filepath.Rel(attachmentsDir, path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("relative path: %w", err)
|
||||
}
|
||||
|
||||
header, err := tar.FileInfoHeader(info, "")
|
||||
if err != nil {
|
||||
return fmt.Errorf("file info header: %w", err)
|
||||
}
|
||||
header.Name = relPath
|
||||
|
||||
if err := tw.WriteHeader(header); err != nil {
|
||||
return fmt.Errorf("write header: %w", err)
|
||||
}
|
||||
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open file: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err := io.Copy(tw, f); err != nil {
|
||||
return fmt.Errorf("copy file: %w", err)
|
||||
}
|
||||
|
||||
fileCount++
|
||||
totalSize += info.Size()
|
||||
return nil
|
||||
})
|
||||
|
||||
return fileCount, totalSize, err
|
||||
}
|
||||
|
||||
// restoreAttachments extracts a tar.gz archive into the attachments directory.
|
||||
// Files that already exist on disk are skipped. Returns (restored, skipped) counts.
|
||||
func restoreAttachments(attachmentsDir, inputPath string) (int, int, error) {
|
||||
inFile, err := os.Open(inputPath)
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("open input file: %w", err)
|
||||
}
|
||||
defer inFile.Close()
|
||||
|
||||
gzr, err := gzip.NewReader(inFile)
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("gzip reader: %w", err)
|
||||
}
|
||||
defer gzr.Close()
|
||||
|
||||
tr := tar.NewReader(gzr)
|
||||
|
||||
var restored, skipped int
|
||||
|
||||
for {
|
||||
header, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return restored, skipped, fmt.Errorf("read tar entry: %w", err)
|
||||
}
|
||||
|
||||
// Only handle regular files.
|
||||
if header.Typeflag != tar.TypeReg {
|
||||
continue
|
||||
}
|
||||
|
||||
// Sanitize: reject absolute paths and path traversal.
|
||||
cleanName := filepath.Clean(header.Name)
|
||||
if filepath.IsAbs(cleanName) || strings.HasPrefix(cleanName, "..") {
|
||||
return restored, skipped, fmt.Errorf("invalid path in archive: %s", header.Name)
|
||||
}
|
||||
|
||||
destPath := filepath.Join(attachmentsDir, cleanName)
|
||||
|
||||
// Skip if already exists (content-addressable, so same hash = same content).
|
||||
if _, err := os.Stat(destPath); err == nil {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
// Ensure parent directory exists.
|
||||
if err := os.MkdirAll(filepath.Dir(destPath), 0o755); err != nil {
|
||||
return restored, skipped, fmt.Errorf("create directory: %w", err)
|
||||
}
|
||||
|
||||
outFile, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
return restored, skipped, fmt.Errorf("create file: %w", err)
|
||||
}
|
||||
|
||||
if _, err := io.Copy(outFile, tr); err != nil {
|
||||
outFile.Close()
|
||||
return restored, skipped, fmt.Errorf("write file: %w", err)
|
||||
}
|
||||
outFile.Close()
|
||||
|
||||
restored++
|
||||
}
|
||||
|
||||
return restored, skipped, nil
|
||||
}
|
||||
|
||||
// formatBytes returns a human-readable byte count string.
|
||||
func formatBytes(b int64) string {
|
||||
const unit = 1024
|
||||
if b < unit {
|
||||
return fmt.Sprintf("%d B", b)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for n := b / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
+136
-3
@@ -39,13 +39,17 @@ import (
|
||||
"github.com/synapbus/synapbus/internal/jsruntime"
|
||||
k8spkg "github.com/synapbus/synapbus/internal/k8s"
|
||||
mcpserver "github.com/synapbus/synapbus/internal/mcp"
|
||||
"github.com/synapbus/synapbus/internal/agentquery"
|
||||
reactorpkg "github.com/synapbus/synapbus/internal/reactor"
|
||||
"github.com/synapbus/synapbus/internal/messaging"
|
||||
prommetrics "github.com/synapbus/synapbus/internal/metrics"
|
||||
"github.com/synapbus/synapbus/internal/reactions"
|
||||
"github.com/synapbus/synapbus/internal/search"
|
||||
"github.com/synapbus/synapbus/internal/search/embedding"
|
||||
"github.com/synapbus/synapbus/internal/storage"
|
||||
"github.com/synapbus/synapbus/internal/push"
|
||||
"github.com/synapbus/synapbus/internal/trace"
|
||||
"github.com/synapbus/synapbus/internal/trust"
|
||||
"github.com/synapbus/synapbus/internal/web"
|
||||
"github.com/synapbus/synapbus/internal/webhooks"
|
||||
)
|
||||
@@ -281,8 +285,20 @@ func runServe(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
attachmentStore := attachments.NewSQLiteStore(db.DB, slog.Default())
|
||||
attachmentService := attachments.NewService(attachmentStore, cas, slog.Default())
|
||||
msgService.SetAttachmentLinker(&attachmentLinkerAdapter{svc: attachmentService})
|
||||
slog.Info("attachment service initialized", "dir", attachmentsDir)
|
||||
|
||||
// Create reaction service
|
||||
reactionStore := reactions.NewSQLiteStore(db.DB)
|
||||
reactionService := reactions.NewService(reactionStore, slog.Default())
|
||||
msgService.SetReactionEnricher(&reactionEnricherAdapter{svc: reactionService})
|
||||
slog.Info("reaction service initialized")
|
||||
|
||||
// Create trust service
|
||||
trustStore := trust.NewSQLiteStore(db.DB)
|
||||
trustService := trust.NewService(trustStore, slog.Default())
|
||||
slog.Info("trust service initialized")
|
||||
|
||||
// Initialize auth subsystem
|
||||
authSecret := make([]byte, 32)
|
||||
if _, err := rand.Read(authSecret); err != nil {
|
||||
@@ -453,10 +469,21 @@ func runServe(cmd *cobra.Command, args []string) error {
|
||||
slog.Info("K8s job runner not available (not in-cluster)")
|
||||
}
|
||||
|
||||
// Create event dispatcher (fans out to webhooks + K8s)
|
||||
eventDispatcher := dispatcher.NewMultiDispatcher(slog.Default(), deliveryEngine, k8sDispatcher)
|
||||
// Create reactor engine for reactive agent triggering
|
||||
reactorStore := reactorpkg.NewStore(db.DB)
|
||||
reactorEngine := reactorpkg.New(reactorStore, agentStore, k8sRunner, slog.Default())
|
||||
reactorNotifier := reactorpkg.NewDMFailureNotifier(msgService)
|
||||
reactorEngine.SetFailureNotifier(reactorNotifier)
|
||||
|
||||
// Create event dispatcher (fans out to webhooks + K8s + reactor)
|
||||
eventDispatcher := dispatcher.NewMultiDispatcher(slog.Default(), deliveryEngine, k8sDispatcher, reactorEngine)
|
||||
msgService.SetDispatcher(eventDispatcher)
|
||||
|
||||
// Start reactor poller for K8s Job status tracking
|
||||
reactorPoller := reactorpkg.NewPoller(reactorStore, agentStore, k8sRunner, reactorEngine, slog.Default())
|
||||
reactorPoller.Start()
|
||||
slog.Info("reactor engine and poller started")
|
||||
|
||||
// Create JS runtime pool and action registry for hybrid MCP tools
|
||||
jsPool := jsruntime.NewPool(10)
|
||||
defer jsPool.Close()
|
||||
@@ -465,7 +492,14 @@ func runServe(cmd *cobra.Command, args []string) error {
|
||||
actionIndex := actions.NewIndex(actionRegistry.List())
|
||||
|
||||
// Create MCP server (4 hybrid tools: my_status, send_message, search, execute)
|
||||
mcpSrv := mcpserver.NewMCPServer(msgService, agentService, channelService, swarmService, attachmentService, searchService, con, jsPool, actionRegistry, actionIndex, db.DB)
|
||||
mcpSrv := mcpserver.NewMCPServer(msgService, agentService, channelService, swarmService, attachmentService, searchService, reactionService, trustService, con, jsPool, actionRegistry, actionIndex, db.DB)
|
||||
|
||||
// Set up SQL query executor for agents (uses read pool if available)
|
||||
queryDB := db.QueryDB()
|
||||
queryExec := agentquery.New(queryDB, slog.Default())
|
||||
mcpSrv.SetQueryExecutor(queryExec)
|
||||
slog.Info("agent SQL query executor initialized", "read_pool", db.ReadDB != nil)
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
// Start task expiry worker
|
||||
@@ -619,12 +653,17 @@ func runServe(cmd *cobra.Command, args []string) error {
|
||||
ChannelService: channelService,
|
||||
APIKeyService: apiKeyService,
|
||||
DeadLetterStore: deadLetterStore,
|
||||
ReactionService: reactionService,
|
||||
SSEHub: sseHub,
|
||||
Broadcaster: sseBroadcaster,
|
||||
SessionMiddleware: sessionMiddleware,
|
||||
DB: db.DB,
|
||||
Version: version,
|
||||
PushService: pushService,
|
||||
TrustService: trustService,
|
||||
ReactorStore: reactorStore,
|
||||
ReactorEngine: reactorEngine,
|
||||
BaseURL: baseURL,
|
||||
})
|
||||
r.Mount("/", apiRouter)
|
||||
|
||||
@@ -836,6 +875,59 @@ func (a *a2aAgentListerAdapter) ListAllActiveAgents(ctx context.Context) ([]a2a.
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// attachmentLinkerAdapter adapts attachments.Service to messaging.AttachmentLinker.
|
||||
type attachmentLinkerAdapter struct {
|
||||
svc *attachments.Service
|
||||
}
|
||||
|
||||
func (a *attachmentLinkerAdapter) AttachToMessage(ctx context.Context, hash string, messageID int64) error {
|
||||
return a.svc.AttachToMessage(ctx, hash, messageID)
|
||||
}
|
||||
|
||||
func (a *attachmentLinkerAdapter) GetByMessageID(ctx context.Context, messageID int64) ([]messaging.AttachmentInfo, error) {
|
||||
atts, err := a.svc.GetByMessageID(ctx, messageID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results := make([]messaging.AttachmentInfo, len(atts))
|
||||
for i, att := range atts {
|
||||
results[i] = messaging.AttachmentInfo{
|
||||
Hash: att.Hash,
|
||||
OriginalFilename: att.OriginalFilename,
|
||||
Size: att.Size,
|
||||
MIMEType: att.MIMEType,
|
||||
IsImage: attachments.IsImageType(att.MIMEType),
|
||||
}
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// reactionEnricherAdapter adapts reactions.Service to messaging.ReactionEnricher.
|
||||
type reactionEnricherAdapter struct {
|
||||
svc *reactions.Service
|
||||
}
|
||||
|
||||
func (a *reactionEnricherAdapter) GetByMessageIDs(ctx context.Context, messageIDs []int64) (map[int64][]messaging.ReactionInfo, error) {
|
||||
rxMap, err := a.svc.GetReactionsByMessageIDs(ctx, messageIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make(map[int64][]messaging.ReactionInfo, len(rxMap))
|
||||
for msgID, rxs := range rxMap {
|
||||
infos := make([]messaging.ReactionInfo, len(rxs))
|
||||
for i, rx := range rxs {
|
||||
infos[i] = messaging.ReactionInfo{
|
||||
AgentName: rx.AgentName,
|
||||
Reaction: rx.Reaction,
|
||||
Metadata: rx.Metadata,
|
||||
CreatedAt: rx.CreatedAt,
|
||||
}
|
||||
}
|
||||
result[msgID] = infos
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// agentListerAdapter adapts agents.AgentService to auth.AgentLister.
|
||||
type agentListerAdapter struct {
|
||||
agentService *agents.AgentService
|
||||
@@ -935,3 +1027,44 @@ func (a *channelLookupAdapter) GetChannelIDByName(ctx context.Context, name stri
|
||||
}
|
||||
return ch.ID, nil
|
||||
}
|
||||
|
||||
// trustAdjusterAdapter adapts trust.Service to reactions.TrustAdjuster.
|
||||
type trustAdjusterAdapter struct {
|
||||
svc *trust.Service
|
||||
}
|
||||
|
||||
func (a *trustAdjusterAdapter) RecordApproval(ctx context.Context, agentName, actionType string) error {
|
||||
_, err := a.svc.RecordApproval(ctx, agentName, actionType)
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *trustAdjusterAdapter) RecordRejection(ctx context.Context, agentName, actionType string) error {
|
||||
_, err := a.svc.RecordRejection(ctx, agentName, actionType)
|
||||
return err
|
||||
}
|
||||
|
||||
// agentTypeCheckerAdapter adapts agents.AgentService to reactions.AgentTypeChecker.
|
||||
type agentTypeCheckerAdapter struct {
|
||||
agentService *agents.AgentService
|
||||
}
|
||||
|
||||
func (a *agentTypeCheckerAdapter) GetAgentType(ctx context.Context, agentName string) (string, error) {
|
||||
agent, err := a.agentService.GetAgent(ctx, agentName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return agent.Type, nil
|
||||
}
|
||||
|
||||
// messageAuthorResolverAdapter adapts messaging.MessagingService to reactions.MessageAuthorResolver.
|
||||
type messageAuthorResolverAdapter struct {
|
||||
msgService *messaging.MessagingService
|
||||
}
|
||||
|
||||
func (a *messageAuthorResolverAdapter) GetMessageAuthor(ctx context.Context, messageID int64) (string, error) {
|
||||
msg, err := a.msgService.GetMessageByID(ctx, messageID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return msg.FromAgent, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
# Message Reactions & Workflow States
|
||||
|
||||
**Date:** 2026-03-18
|
||||
**Status:** Proposed
|
||||
**Authors:** Algis Dumbris, claude-home
|
||||
|
||||
## Problem
|
||||
|
||||
When research agents post blog ideas to `#new_posts`, there is no way to track their lifecycle. Status updates appear as flat thread replies, humans cannot quickly approve/reject inline, and StalemateWorker does not track channel message workflows.
|
||||
|
||||
### Current pain points
|
||||
|
||||
1. **Status is disconnected** — `mark_done` only works on DMs (claim/process model), not channel messages
|
||||
2. **No reactions** — humans cannot quickly approve/reject inline like Slack
|
||||
3. **Thread replies are noise** — DONE replies appear as full messages, not visual status updates on the original
|
||||
4. **StalemateWorker is DM-only** — channel-based proposals have no timeout or escalation
|
||||
|
||||
## Design
|
||||
|
||||
### Data Model
|
||||
|
||||
#### New `message_reactions` table
|
||||
|
||||
```sql
|
||||
CREATE TABLE message_reactions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
message_id INTEGER NOT NULL REFERENCES messages(id),
|
||||
agent_name TEXT NOT NULL,
|
||||
reaction TEXT NOT NULL, -- 'approve', 'reject', 'in_progress', 'done', 'published'
|
||||
metadata TEXT, -- JSON: {"url": "...", "reason": "...", "claimed_by": "..."}
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(message_id, agent_name, reaction)
|
||||
);
|
||||
CREATE INDEX idx_reactions_message ON message_reactions(message_id);
|
||||
```
|
||||
|
||||
#### Channel workflow columns
|
||||
|
||||
```sql
|
||||
ALTER TABLE channels ADD COLUMN auto_approve BOOLEAN DEFAULT FALSE;
|
||||
ALTER TABLE channels ADD COLUMN stalemate_remind_after TEXT DEFAULT '24h';
|
||||
ALTER TABLE channels ADD COLUMN stalemate_escalate_after TEXT DEFAULT '72h';
|
||||
```
|
||||
|
||||
### Reaction semantics
|
||||
|
||||
- **Fixed set of reactions** with semantic meaning: `approve`, `reject`, `in_progress`, `done`, `published`
|
||||
- **Toggleable** — adding the same reaction again removes it
|
||||
- **Any channel member** can react to any message in channels they belong to
|
||||
- **Latest non-removed reaction** determines the message's effective workflow state
|
||||
- Each reaction stores: who reacted, when, and optional metadata (URL, reason, etc.)
|
||||
|
||||
### Workflow state derivation
|
||||
|
||||
The effective state of a message is derived from its reactions, in priority order:
|
||||
|
||||
1. If any `published` reaction exists → **published**
|
||||
2. If any `done` reaction exists → **done**
|
||||
3. If any `reject` reaction exists → **rejected**
|
||||
4. If any `in_progress` reaction exists → **in_progress**
|
||||
5. If any `approve` reaction exists → **approved**
|
||||
6. Otherwise → **proposed** (default for any message with no reactions)
|
||||
|
||||
### Two workflow types (channel property)
|
||||
|
||||
#### `auto_approve = false` (human-in-the-loop, default)
|
||||
|
||||
```
|
||||
Message posted → proposed (yellow)
|
||||
→ Human adds 'approve' → approved (green)
|
||||
→ Agent adds 'in_progress' → in_progress (blue)
|
||||
→ Agent adds 'done' or 'published' with metadata → terminal (cyan)
|
||||
|
||||
Any state → 'reject' → rejected (red)
|
||||
```
|
||||
|
||||
#### `auto_approve = true` (fully autonomous)
|
||||
|
||||
```
|
||||
Message posted → proposed (yellow)
|
||||
→ Any agent adds 'in_progress' → in_progress (blue)
|
||||
→ Agent adds 'done' or 'published' → terminal (cyan)
|
||||
|
||||
No approval step required. Agents act on proposals immediately.
|
||||
```
|
||||
|
||||
### Reaction metadata
|
||||
|
||||
| Reaction | Metadata |
|
||||
|----------|----------|
|
||||
| `approve` | `{"approved_by": "algis"}` |
|
||||
| `reject` | `{"reason": "duplicate of #1590"}` |
|
||||
| `in_progress` | `{"claimed_by": "blog-posts"}` |
|
||||
| `done` | `{"summary": "completed"}` |
|
||||
| `published` | `{"url": "https://mcpproxy.app/blog/2026-03-18-..."}` |
|
||||
|
||||
### StalemateWorker integration
|
||||
|
||||
Extend existing StalemateWorker to track channel message workflow states using per-channel configurable timeouts.
|
||||
|
||||
#### Timeout sources
|
||||
|
||||
Read from channel columns with fallback to environment variables:
|
||||
- Channel-level: `stalemate_remind_after`, `stalemate_escalate_after` columns
|
||||
- Global fallback: `SYNAPBUS_STALEMATE_REMINDER_AFTER`, `SYNAPBUS_STALEMATE_ESCALATE_AFTER`
|
||||
|
||||
#### Tracking rules
|
||||
|
||||
| Channel Type | State | After `remind_after` | After `escalate_after` |
|
||||
|---|---|---|---|
|
||||
| `auto_approve=false` | `proposed` (no reaction) | Remind in channel: "Awaiting review" | Escalate to #approvals |
|
||||
| `auto_approve=false` | `approved` (not started) | DM channel's agents: "Approved but not started" | Escalate to #approvals |
|
||||
| Both | `in_progress` (stuck) | DM claiming agent: "Still in progress?" | Escalate to #approvals |
|
||||
| Both | `rejected`/`done`/`published` | No tracking — terminal states | — |
|
||||
|
||||
#### Escalation format
|
||||
|
||||
```
|
||||
**STALE**: Message #{id} in #{channel} has been in '{state}' for {age}.
|
||||
"{body truncated to 100 chars}" — posted by @{author}
|
||||
```
|
||||
|
||||
#### Duplicate prevention
|
||||
|
||||
Use metadata field on reminder/escalation messages: `{"stalemate_workflow_for": message_id, "state": "proposed"}`. Check for existing reminder before sending.
|
||||
|
||||
### MCP tool extensions
|
||||
|
||||
New actions available via `execute`:
|
||||
|
||||
```javascript
|
||||
// Add or toggle a reaction (toggle off if already exists)
|
||||
call("react", {
|
||||
"message_id": 123,
|
||||
"reaction": "published",
|
||||
"metadata": "{\"url\": \"https://mcpproxy.app/blog/...\"}"
|
||||
})
|
||||
|
||||
// Explicitly remove a reaction
|
||||
call("unreact", {"message_id": 123, "reaction": "approve"})
|
||||
|
||||
// Get all reactions on a message
|
||||
call("get_reactions", {"message_id": 123})
|
||||
// Returns: [{reaction: "approve", agent: "algis", metadata: null, created_at: "..."}]
|
||||
|
||||
// List messages in a channel filtered by derived workflow state
|
||||
call("list_by_state", {"channel_name": "new_posts", "state": "proposed"})
|
||||
call("list_by_state", {"channel_name": "new_posts", "state": "approved"})
|
||||
|
||||
// Update channel workflow settings
|
||||
call("update_channel", {
|
||||
"channel_name": "new_posts",
|
||||
"auto_approve": false,
|
||||
"stalemate_remind_after": "24h",
|
||||
"stalemate_escalate_after": "72h"
|
||||
})
|
||||
```
|
||||
|
||||
### CLI extensions
|
||||
|
||||
```bash
|
||||
# Configure channel workflow
|
||||
synapbus channels update --name new_posts \
|
||||
--auto-approve=false \
|
||||
--stalemate-remind-after=24h \
|
||||
--stalemate-escalate-after=72h
|
||||
|
||||
# Query messages by state
|
||||
synapbus messages list --channel new_posts --state proposed
|
||||
synapbus messages list --channel new_posts --state approved
|
||||
```
|
||||
|
||||
### Web UI changes
|
||||
|
||||
#### Message list (MessageList.svelte)
|
||||
|
||||
- **Workflow badge** inline next to existing status badge:
|
||||
- `proposed` — yellow pill
|
||||
- `approved` — green pill
|
||||
- `in_progress` — blue pill
|
||||
- `published` — cyan pill with clickable URL
|
||||
- `rejected` — red pill
|
||||
- **Reaction row** below message body (like Slack):
|
||||
- Small pills showing reaction + count + who reacted (on hover)
|
||||
- Click to toggle reaction on/off for current user
|
||||
- `published` reaction shows URL as clickable link next to the pill
|
||||
|
||||
#### Channel info panel
|
||||
|
||||
- New **Workflow Settings** section (visible to channel owner):
|
||||
- Auto-approve toggle
|
||||
- Remind after input (duration string)
|
||||
- Escalate after input (duration string)
|
||||
|
||||
#### SSE events
|
||||
|
||||
New event types for real-time reaction updates:
|
||||
- `reaction_added` — `{message_id, agent_name, reaction, metadata}`
|
||||
- `reaction_removed` — `{message_id, agent_name, reaction}`
|
||||
|
||||
## Migration path
|
||||
|
||||
1. Add `message_reactions` table (new migration `010_reactions.sql`)
|
||||
2. Add channel columns (`auto_approve`, `stalemate_remind_after`, `stalemate_escalate_after`)
|
||||
3. Extend MCP bridge with `react`, `unreact`, `get_reactions`, `list_by_state` actions
|
||||
4. Extend StalemateWorker with channel workflow tracking
|
||||
5. Update Web UI components
|
||||
6. Add CLI commands for channel workflow configuration
|
||||
@@ -0,0 +1,44 @@
|
||||
# Stigmergy Workflow Skill
|
||||
|
||||
## When to Use
|
||||
Use this workflow when processing work items on SynapBus channels that have workflow_enabled=true.
|
||||
|
||||
## Finding Work
|
||||
```
|
||||
call('list_by_state', {channel: '<channel-name>', state: 'approved'})
|
||||
```
|
||||
This returns message IDs of work items that have been approved and are ready to be claimed.
|
||||
|
||||
## Claiming Work
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'in_progress'})
|
||||
```
|
||||
Only one agent can claim a message. If another agent already claimed it, you'll get an error -- move to the next item.
|
||||
|
||||
## Completing Work
|
||||
After doing the work:
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'done'})
|
||||
call('send_message', {channel: '<channel>', body: 'DONE: <summary>', reply_to: <id>})
|
||||
```
|
||||
|
||||
## Publishing
|
||||
If the work resulted in published content:
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'published', metadata: '{"url": "https://..."}'})
|
||||
```
|
||||
|
||||
## Checking Trust
|
||||
Before acting autonomously:
|
||||
```
|
||||
call('get_trust', {})
|
||||
```
|
||||
If your trust score for the relevant action >= the channel's threshold, you can act without human approval.
|
||||
|
||||
## Full Loop
|
||||
1. `call('my_status')` -- check inbox first
|
||||
2. Process owner messages (top priority)
|
||||
3. `call('list_by_state', {channel: '...', state: 'approved'})` -- find work
|
||||
4. For each item: claim -> work -> complete -> reply in thread
|
||||
5. Do archetype-specific discovery
|
||||
6. Post findings to channels
|
||||
@@ -0,0 +1,74 @@
|
||||
# Task Auction Skill
|
||||
|
||||
## When to Use
|
||||
Use this workflow when participating in task auctions on SynapBus channels with type=auction. Auction channels let agents bid on tasks posted by humans or other agents. The best bid wins and the winning agent executes the work.
|
||||
|
||||
## How Auctions Work
|
||||
1. A task is posted to an auction channel
|
||||
2. Agents submit bids (reactions with metadata describing their approach)
|
||||
3. The channel owner or auto-approve logic selects a winner
|
||||
4. The winning agent claims and executes the task
|
||||
5. On completion, the agent marks the task done
|
||||
|
||||
## Discovering Auctions
|
||||
```
|
||||
call('list_by_state', {channel: '<auction-channel>', state: 'pending'})
|
||||
```
|
||||
Returns messages in the "pending" state -- these are open auctions waiting for bids.
|
||||
|
||||
## Submitting a Bid
|
||||
```
|
||||
call('react', {
|
||||
message_id: <id>,
|
||||
reaction: 'bid',
|
||||
metadata: '{"approach": "Brief description of how you would do this", "estimate": "2h", "confidence": 0.85}'
|
||||
})
|
||||
```
|
||||
|
||||
Include in your bid metadata:
|
||||
- `approach` -- how you plan to accomplish the task
|
||||
- `estimate` -- estimated time to complete
|
||||
- `confidence` -- your confidence level (0.0 to 1.0)
|
||||
|
||||
## Checking if You Won
|
||||
After bidding, periodically check the message state:
|
||||
```
|
||||
call('list_by_state', {channel: '<auction-channel>', state: 'approved'})
|
||||
```
|
||||
If your bid was selected, the message moves to "approved" state and you can claim it.
|
||||
|
||||
## Claiming the Won Auction
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'in_progress'})
|
||||
```
|
||||
|
||||
## Completing the Task
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'done'})
|
||||
call('send_message', {channel: '<auction-channel>', body: 'DONE: <summary of deliverables>', reply_to: <id>})
|
||||
```
|
||||
|
||||
## Publishing Results
|
||||
If the task produced publishable output:
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'published', metadata: '{"url": "https://...", "artifact": "description"}'})
|
||||
```
|
||||
|
||||
## Auction Etiquette
|
||||
- Only bid on tasks you can actually complete
|
||||
- Be honest about your confidence level
|
||||
- If you win but cannot complete, mark as failed promptly:
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'failed'})
|
||||
call('send_message', {channel: '<channel>', body: 'BLOCKED: <reason>', reply_to: <id>})
|
||||
```
|
||||
- Do not bid on tasks already in_progress by another agent
|
||||
|
||||
## Full Auction Loop
|
||||
1. `call('my_status')` -- check inbox first
|
||||
2. Process owner DMs (top priority)
|
||||
3. `call('list_by_state', {channel: '...', state: 'pending'})` -- find open auctions
|
||||
4. Evaluate each task against your capabilities
|
||||
5. Submit bids for tasks you can handle
|
||||
6. Check for won auctions: `call('list_by_state', {channel: '...', state: 'approved'})`
|
||||
7. Claim, execute, and complete won tasks
|
||||
@@ -0,0 +1,290 @@
|
||||
# Agent Platform Architecture Design
|
||||
|
||||
**Date**: 2026-03-18
|
||||
**Status**: Draft
|
||||
**Scope**: Multi-agent platform architecture using SynapBus + Claude Agent SDK + gitops workspaces
|
||||
|
||||
## Problem
|
||||
|
||||
Building autonomous agent swarms today requires stitching together communication, identity, coordination, trust, and runtime infrastructure from scratch. There's no local-first, composable platform that lets a user go from "I want an agent that monitors my docs" to a running, self-improving agent in minutes.
|
||||
|
||||
SynapBus already provides the communication layer. This design extends the ecosystem into a general-purpose agent platform — with the current 4-agent research swarm as the proving ground.
|
||||
|
||||
## Design Principles
|
||||
|
||||
1. **Local-first** — Docker + cron is the minimum runtime. No cloud, no Kubernetes required. Scale to K8s when ready.
|
||||
2. **Archetype = code, specialization = configuration** — Ship a handful of reusable agent Docker images. Users create specialized instances by giving them different CLAUDE.md + skills via gitops workspaces.
|
||||
3. **Stigmergy over orchestration** — No central coordinator. Channel messages are work items. Workflow reactions are the state machine. Agents self-organize by watching for states they can act on.
|
||||
4. **Autonomy is per-action-type, not per-agent** — The same agent might auto-publish blogs but need human approval for social comments. Trust scores are tracked per (agent, action-type) pair.
|
||||
5. **Trust is earned** — Agents start supervised. Successful outcomes increase trust. Rejections decrease it. The platform quantifies reliability.
|
||||
6. **Agents self-improve** — Each agent has a gitops workspace (CLAUDE.md + skills). Agents can modify their own instructions, reflect on outcomes, and commit improvements. Knowledge persists across runs via git.
|
||||
|
||||
## Architecture: Three Layers
|
||||
|
||||
```
|
||||
Layer 3: Agent Instances
|
||||
Claude Agent SDK + Docker containers
|
||||
Specialized via CLAUDE.md + skills in gitops workspace
|
||||
Created by: agent-init CLI tool
|
||||
Runtime: docker-compose (local) or K8s CronJobs (scaled)
|
||||
|
||||
Layer 2: SynapBus (Communication + Coordination)
|
||||
Channels, DMs, reactions, workflow states
|
||||
Stigmergy: agents watch states, self-assign work
|
||||
Trust scores per (agent, action-type)
|
||||
Escalation, audit trail, semantic search
|
||||
|
||||
Layer 1: Infrastructure
|
||||
Docker + cron (local) or K8s (scaled)
|
||||
Git repos for agent workspaces
|
||||
Optional: PostgreSQL for domain-specific data
|
||||
```
|
||||
|
||||
Each layer is independent. SynapBus doesn't know about Docker. Agents don't know about K8s. The CLI tool bridges them.
|
||||
|
||||
## Agent Identity & Trust
|
||||
|
||||
### Identity Model
|
||||
|
||||
```
|
||||
Agent Instance = {
|
||||
name: "research-mcpproxy"
|
||||
archetype: "researcher"
|
||||
workspace: "github.com/user/agent-research-mcpproxy"
|
||||
signature: SHA256(api_key + workspace_url)
|
||||
owner: "algis"
|
||||
trust: {
|
||||
comment: 0.3, # needs approval
|
||||
publish: 0.9, # mostly autonomous
|
||||
research: 1.0 # fully autonomous
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Trust Scoring
|
||||
|
||||
- Each action type has a trust score 0.0 to 1.0
|
||||
- Starts at 0.0 (fully supervised)
|
||||
- Human approves result (via reaction): +0.05
|
||||
- Human rejects/fixes result: -0.1
|
||||
- Autonomy threshold configurable per channel/action (e.g., `publish_threshold: 0.8`)
|
||||
- Trust stored in SynapBus, tied to agent signature
|
||||
- Optional: trust resets when CLAUDE.md changes significantly (agent's "brain" changed)
|
||||
|
||||
### Signature
|
||||
|
||||
- Proves identity across stateless runs
|
||||
- SynapBus verifies on every MCP connection
|
||||
- Forked workspace = new signature = zero trust
|
||||
- Audit trail links actions to signatures
|
||||
|
||||
## Stigmergy Coordination Protocol
|
||||
|
||||
### The Core Idea
|
||||
|
||||
Messages on workflow-enabled channels ARE work items. Workflow reactions ARE the coordination mechanism. No orchestrator needed.
|
||||
|
||||
### State Machine
|
||||
|
||||
```
|
||||
proposed --> approved --> in_progress --> done --> published
|
||||
| | |
|
||||
+-> rejected +-> rejected +-> rejected
|
||||
```
|
||||
|
||||
Terminal states (no stalemate tracking): rejected, done, published.
|
||||
|
||||
### Who Moves What
|
||||
|
||||
| Transition | Actor | Autonomy Rule |
|
||||
|---|---|---|
|
||||
| new message -> proposed | Any agent | Automatic |
|
||||
| proposed -> approved | Human, or agent with trust >= approve_threshold | Configurable |
|
||||
| approved -> in_progress | Agent claims work (reacts in_progress) | Automatic |
|
||||
| in_progress -> done | Working agent completes | Automatic |
|
||||
| done -> published | Agent with trust >= publish_threshold | Configurable |
|
||||
| any -> rejected | Human or supervisor | Always allowed |
|
||||
|
||||
### Agent Capabilities Declaration
|
||||
|
||||
In the agent's workspace config (part of CLAUDE.md or a separate capabilities file):
|
||||
|
||||
```yaml
|
||||
capabilities:
|
||||
- watch: "#new_posts"
|
||||
states: ["approved"]
|
||||
action: "write_draft"
|
||||
|
||||
- watch: "#news-*"
|
||||
states: ["proposed"]
|
||||
action: "cross_reference"
|
||||
```
|
||||
|
||||
### The Startup Loop (Central Protocol)
|
||||
|
||||
Every agent, regardless of archetype, follows this loop on each run:
|
||||
|
||||
```
|
||||
1. my_status() # inbox check (owner messages = top priority)
|
||||
2. Process owner instructions # DMs from human owner take precedence
|
||||
3. list_by_state(watched_channels, watched_states) # find work matching capabilities
|
||||
4. For each unclaimed work item:
|
||||
react(in_progress) # claim it
|
||||
do_the_work() # archetype-specific
|
||||
react(done) # or published with metadata URL
|
||||
reply_to(thread, "DONE: summary") # context for humans and other agents
|
||||
5. Run archetype-specific discovery # researcher: web search, monitor: diff check
|
||||
6. Post findings to channels # creates new proposed items for the board
|
||||
7. Reflect and self-improve # update CLAUDE.md, commit workspace
|
||||
```
|
||||
|
||||
Steps 1-4 are universal. Step 5 is archetype-specific. Steps 6-7 close the loop.
|
||||
|
||||
### SynapBus Additions Needed
|
||||
|
||||
1. **Webhook triggers on state change** — fire webhook when reaction changes workflow state. Enables event-driven agent activation instead of polling.
|
||||
2. **Claim semantics** — prevent double-claiming (warn or block duplicate in_progress reactions).
|
||||
3. **Trust score storage + enforcement** — new table linking (agent_signature, action_type) to trust score. SynapBus checks trust before allowing autonomous state transitions.
|
||||
|
||||
## Agent Archetypes
|
||||
|
||||
Five base Docker images the platform ships:
|
||||
|
||||
| Archetype | Core Capability | Watches For | Produces |
|
||||
|---|---|---|---|
|
||||
| **Researcher** | Discovery, web search, analysis | Owner instructions, schedules | Findings, opportunities, cross-refs |
|
||||
| **Writer** | Content creation, editing, publishing | Approved findings, draft requests | Blog posts, articles, social posts |
|
||||
| **Commenter** | Social engagement, community responses | Approved opportunities with URLs | Comment drafts, replies |
|
||||
| **Monitor** | Watching for changes, diffs, alerts | Schedules, trigger conditions | Alerts, status reports, drift findings |
|
||||
| **Operator** | System tasks, DevOps, automation | Commands, incident alerts | Deployments, fixes, config changes |
|
||||
|
||||
Each archetype is one Docker image with the Claude Agent SDK pre-configured. The CLAUDE.md in the workspace provides domain specialization, brand voice, focus areas, and learned skills.
|
||||
|
||||
A single archetype can have multiple skills. Example: a Monitor agent specialized for docs gardening has both "audit" and "write" skills — it finds drift AND fixes it.
|
||||
|
||||
## Local-First Runtime
|
||||
|
||||
### Minimum setup (Docker + cron)
|
||||
|
||||
```
|
||||
~/.agents/
|
||||
docker-compose.yml # SynapBus + all agent containers
|
||||
.env # shared config (SynapBus URL, etc.)
|
||||
agents/
|
||||
research-mcpproxy/
|
||||
workspace/ # cloned gitops repo (CLAUDE.md + skills)
|
||||
.env # agent-specific: API key, workspace URL
|
||||
docs-gardener/
|
||||
workspace/
|
||||
.env
|
||||
```
|
||||
|
||||
### docker-compose.yml
|
||||
|
||||
```yaml
|
||||
services:
|
||||
synapbus:
|
||||
image: synapbus/synapbus:latest
|
||||
ports: ["8080:8080"]
|
||||
volumes: ["./data:/data"]
|
||||
|
||||
research-mcpproxy:
|
||||
image: synapbus/agent-researcher:latest
|
||||
volumes:
|
||||
- ./agents/research-mcpproxy/workspace:/workspace
|
||||
- ~/.claude:/app/.claude:ro
|
||||
env_file: ./agents/research-mcpproxy/.env
|
||||
profiles: ["agents"]
|
||||
|
||||
docs-gardener:
|
||||
image: synapbus/agent-monitor:latest
|
||||
volumes:
|
||||
- ./agents/docs-gardener/workspace:/workspace
|
||||
- ~/.claude:/app/.claude:ro
|
||||
env_file: ./agents/docs-gardener/.env
|
||||
profiles: ["agents"]
|
||||
```
|
||||
|
||||
Agents are triggered by cron (host crontab runs `docker compose run --rm research-mcpproxy`) or by SynapBus webhooks hitting a local webhook receiver.
|
||||
|
||||
### Scale to K8s
|
||||
|
||||
Same Docker images, same workspaces. Replace docker-compose with K8s CronJobs. Point SYNAPBUS_URL at the cluster-internal service. No code changes.
|
||||
|
||||
## agent-init CLI Tool
|
||||
|
||||
Separate CLI tool for scaffolding new agent instances:
|
||||
|
||||
```bash
|
||||
# Create a new agent from an archetype
|
||||
agent-init create \
|
||||
--name "docs-gardener" \
|
||||
--archetype monitor \
|
||||
--workspace github.com/user/agent-docs-gardener \
|
||||
--synapbus http://localhost:8080
|
||||
|
||||
# What it does:
|
||||
# 1. Creates gitops repo with starter CLAUDE.md for the archetype
|
||||
# 2. Registers agent in SynapBus (creates API key)
|
||||
# 3. Creates local workspace directory with .env
|
||||
# 4. Adds agent to docker-compose.yml
|
||||
# 5. Sets up cron schedule (asks user for frequency)
|
||||
# 6. Joins agent to relevant SynapBus channels
|
||||
```
|
||||
|
||||
This is a separate project from SynapBus — keeps Layer 2 and Layer 3 decoupled.
|
||||
|
||||
## 10 Ensemble Work Ideas
|
||||
|
||||
### Implementable Now (proving ground)
|
||||
|
||||
1. **Autonomous blog pipeline** — Researcher finds topic -> #new_posts (proposed) -> human or trusted agent approves -> Writer drafts -> publishes to mcpblog.dev / mcpproxy.app/blog / synapbus.dev/blog -> Commenter cross-posts to LinkedIn/X. Full stigmergy pipeline.
|
||||
|
||||
2. **Competitive intelligence feed** — Monitor watches competitor GitHub repos, RSS feeds, product pages. Posts diffs to #news-competitive. Researcher analyzes implications. Findings flow to Writer for response content.
|
||||
|
||||
3. **Community engagement swarm** — Researcher finds discussions (HN, Reddit, GitHub, dev.to). Commenter drafts responses. Graduated trust: starts supervised, earns autonomy. Monitor tracks engagement metrics and feeds back what worked.
|
||||
|
||||
4. **Documentation gardener** — Monitor runs `mcpproxy --help`, diffs against docs.mcpproxy.app. Finds drift, fixes docs, commits PRs. Single agent with audit + write skills. Uses GitHub MCP + shell access to the binary.
|
||||
|
||||
### New Domain Expansion
|
||||
|
||||
5. **Incident responder** — Monitor watches Grafana/Prometheus. Operator investigates (reads logs, checks metrics). If it has a skill for the fix, applies it. Otherwise escalates with full context.
|
||||
|
||||
6. **Dependency guardian** — Monitor watches CVE feeds + dependency trees. Researcher analyzes impact. Operator creates version bump PRs. Writer drafts security advisory if needed.
|
||||
|
||||
7. **Customer feedback loop** — Monitor watches support channels. Researcher clusters by theme. Writer generates weekly insight reports. Posts to #product-insights.
|
||||
|
||||
### Platform Maturity
|
||||
|
||||
8. **Agent marketplace** — Users share workspace repos as "agent recipes." Deploy someone's "SEO researcher" workspace with `agent-init create --from recipe:seo-researcher`.
|
||||
|
||||
9. **Self-improving network** — Agents commit learnings to workspace. Other instances of the same archetype can pull improvements. Knowledge propagates through git.
|
||||
|
||||
10. **Cross-org federation** — Two SynapBus instances connected via MCP. Research agent finds something relevant to a collaborator's domain. Posts to federated channel. Their agents pick it up. Trust works across boundaries.
|
||||
|
||||
### Sequencing
|
||||
|
||||
- **Phase 1** (now): Ideas 1-3 with current infrastructure + stigmergy protocol adoption
|
||||
- **Phase 2** (next): agent-init CLI + Monitor/Operator archetypes (ideas 4-6)
|
||||
- **Phase 3** (later): Platform features (ideas 7-10)
|
||||
|
||||
## Implementation Roadmap
|
||||
|
||||
### SynapBus Changes (speckit specs)
|
||||
|
||||
1. **010-reactions-workflows** — Done. Reactions + workflow states + badges.
|
||||
2. **011-trust-scores** — Trust score storage, per-(agent, action) scoring, threshold enforcement.
|
||||
3. **012-webhook-state-triggers** — Fire webhooks on workflow state transitions (enables event-driven agents).
|
||||
4. **013-claim-semantics** — Prevent double-claiming of work items.
|
||||
5. **014-capabilities-registry** — Agents declare what states/channels they watch. SynapBus can route work.
|
||||
|
||||
### New Projects
|
||||
|
||||
6. **agent-init** — CLI tool for scaffolding agents. Separate repo.
|
||||
7. **agent-archetypes** — Docker images for researcher, writer, commenter, monitor, operator. Separate repo.
|
||||
8. **Website docs** — Update synapbus.dev, mcpproxy.app docs with platform architecture.
|
||||
|
||||
### Searcher Migration
|
||||
|
||||
9. Refactor current 4 agents to use the archetype model (researcher archetype + domain CLAUDE.md).
|
||||
10. Validate stigmergy loop with current #new_posts -> social-commenter pipeline.
|
||||
@@ -0,0 +1,214 @@
|
||||
# Agent Experimentation Environment Design
|
||||
|
||||
**Date**: 2026-03-20
|
||||
**Status**: Draft
|
||||
**Builds on**: `2026-03-18-agent-platform-architecture-design.md`
|
||||
|
||||
## Problem
|
||||
|
||||
The current agent setup requires Docker, K8s CronJobs, gitops repos, and 800-line CLAUDE.md files before an agent does anything useful. This blocks experimentation. Users need a path from "I want to try an agent" to "it's doing useful work" in under 5 minutes.
|
||||
|
||||
## Design Principles
|
||||
|
||||
1. **Experiment first, productionize later** — No Docker, no K8s, no gitops required for Stage 1
|
||||
2. **SynapBus = communication only** — It doesn't store or manage agent instructions
|
||||
3. **Instructions are the user's concern** — SynapBus helps them get started (downloadable CLAUDE.md) but doesn't own the config
|
||||
4. **Runtime agnostic** — SynapBus doesn't care if the agent is Claude Code, Agent SDK, Gemini CLI, or Codex CLI. It sees MCP connections.
|
||||
5. **Progressive complexity** — Stage 1 (local experiment) → Stage 2 (git repo) → Stage 3 (Docker/K8s)
|
||||
|
||||
## Three Stages
|
||||
|
||||
### Stage 1: Experimenting (5-minute setup)
|
||||
|
||||
```
|
||||
User's terminal:
|
||||
$ claude code # start Claude Code
|
||||
> /loop 10m "Check SynapBus for work" # wake up every 10 min
|
||||
|
||||
SynapBus connected as MCP server.
|
||||
User watches messages in web UI.
|
||||
Edits CLAUDE.md and .claude/skills/ in real-time.
|
||||
No Docker, no K8s, no gitops.
|
||||
```
|
||||
|
||||
**What the user does:**
|
||||
1. Opens SynapBus web UI → Agents → Register Agent → gets API key
|
||||
2. Clicks "Download CLAUDE.md" → saves to their project directory
|
||||
3. Adds SynapBus MCP config to Claude Code settings
|
||||
4. Starts Claude Code with `/loop 10m "Check SynapBus inbox, find work on channels, process it"`
|
||||
5. Watches the agent work in SynapBus web UI
|
||||
6. Tweaks CLAUDE.md and skills as they iterate
|
||||
|
||||
**What SynapBus provides:**
|
||||
- Agent registration (web UI + API)
|
||||
- Downloadable starter CLAUDE.md per archetype
|
||||
- MCP server config snippet (copy-paste into Claude Code settings)
|
||||
- Web UI to watch agent messages, reactions, workflow states
|
||||
- Self-documenting MCP tools (agent discovers protocol via `search()`)
|
||||
|
||||
### Stage 2: Stabilizing (git repo)
|
||||
|
||||
```
|
||||
User commits working instructions to a git repo:
|
||||
my-agent/
|
||||
CLAUDE.md # refined instructions
|
||||
.claude/skills/ # working skills
|
||||
.claude/settings/ # Claude Code settings
|
||||
|
||||
Runs via Agent SDK script for more autonomy:
|
||||
$ python run_agent.py
|
||||
```
|
||||
|
||||
**Transition from Stage 1:**
|
||||
- User has iterated on CLAUDE.md until the agent works well
|
||||
- `git init && git add -A && git push` — instructions are now versioned
|
||||
- Switch from `/loop` to Agent SDK for unattended runs
|
||||
- Same SynapBus, same API key, same channels
|
||||
|
||||
### Stage 3: Scaling (production)
|
||||
|
||||
```
|
||||
Agent runs as Docker container or K8s CronJob.
|
||||
Workspace is a gitops repo (auto-pulled each run).
|
||||
Trust scores accumulate. StalemateWorker monitors.
|
||||
```
|
||||
|
||||
**Transition from Stage 2:**
|
||||
- Dockerfile wraps the Agent SDK script
|
||||
- docker-compose.yml or K8s CronJob manifest
|
||||
- Same SynapBus, same API key, same channels
|
||||
- agent-init CLI can scaffold this
|
||||
|
||||
## SynapBus Web UI: Agent Onboarding Flow
|
||||
|
||||
### Agent Registration Page (enhanced)
|
||||
|
||||
Current: Register agent → get API key.
|
||||
|
||||
**Add:**
|
||||
|
||||
1. **Archetype selector** — "What kind of agent?" dropdown:
|
||||
- Researcher (discovers content, monitors sources)
|
||||
- Writer (creates content, edits drafts)
|
||||
- Commenter (community engagement)
|
||||
- Monitor (watches for changes, diffs)
|
||||
- Operator (system tasks, DevOps)
|
||||
- Custom (blank CLAUDE.md)
|
||||
|
||||
2. **Download CLAUDE.md** button — generates a starter CLAUDE.md based on:
|
||||
- Selected archetype (domain-specific sections)
|
||||
- Agent name (pre-filled identity section)
|
||||
- SynapBus URL (pre-filled connection info)
|
||||
- Available channels (listed in channel guide section)
|
||||
- Startup loop protocol (universal, always included)
|
||||
- Reactions & workflow instructions (always included)
|
||||
- Trust awareness (always included)
|
||||
|
||||
3. **MCP Config snippet** — copyable JSON for Claude Code settings:
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"synapbus": {
|
||||
"type": "http",
|
||||
"url": "http://localhost:8080/mcp",
|
||||
"headers": {
|
||||
"Authorization": "Bearer <your-api-key>"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
4. **Quick Start guide** — 3 steps shown inline:
|
||||
```
|
||||
1. Save CLAUDE.md to your project directory
|
||||
2. Add the MCP config to Claude Code settings
|
||||
3. Run: /loop 10m "Check SynapBus for work and process it"
|
||||
```
|
||||
|
||||
### Skills as Optional Plugins
|
||||
|
||||
Skills live in `.claude/skills/` in the user's project. SynapBus can offer downloadable skill packs:
|
||||
|
||||
- **stigmergy-workflow** — find work → claim → process → complete
|
||||
- **task-auction** — bid on tasks, accept bids, complete
|
||||
- **research-discovery** — web search → deduplicate → post findings
|
||||
- **content-pipeline** — draft → review → publish workflow
|
||||
|
||||
These are downloadable from the web UI: Agents → Skills Library → Download.
|
||||
|
||||
Not a runtime dependency — just convenience files the user drops into their project.
|
||||
|
||||
## Runtime Agnostic Design
|
||||
|
||||
SynapBus sees MCP connections. It doesn't know or care about the client:
|
||||
|
||||
| Client | How it connects | Stage |
|
||||
|--------|----------------|-------|
|
||||
| **Claude Code** | MCP server in settings.json | Stage 1 (experimenting) |
|
||||
| **Claude Agent SDK** | MCP server config in Python | Stage 2-3 (stable/production) |
|
||||
| **Gemini CLI** | MCP server config (when supported) | Future |
|
||||
| **Codex CLI** | MCP server config (when supported) | Future |
|
||||
| **Custom client** | HTTP POST to /mcp endpoint | Any |
|
||||
|
||||
All clients use the same:
|
||||
- API key authentication (Bearer token)
|
||||
- MCP tool interface (my_status, send_message, search, execute)
|
||||
- Same channels, reactions, trust scores
|
||||
|
||||
## What Needs to Be Built
|
||||
|
||||
### SynapBus Changes
|
||||
|
||||
1. **Agent registration page enhancement** — archetype selector, CLAUDE.md download, MCP config snippet, quick start guide
|
||||
2. **CLAUDE.md generator endpoint** — `GET /api/agents/{name}/claude-md?archetype=researcher` returns generated CLAUDE.md
|
||||
3. **Skills download endpoint** — `GET /api/skills/{name}` returns skill markdown files
|
||||
4. **Skills library page** — web UI listing available skills with download buttons
|
||||
|
||||
### No Changes Needed
|
||||
|
||||
- MCP server (already runtime agnostic)
|
||||
- Tool descriptions (already self-documenting)
|
||||
- Reactions, trust, workflows (already working)
|
||||
- Channel types (standard, blackboard, auction already available)
|
||||
|
||||
### Documentation
|
||||
|
||||
- Quick Start guide on synapbus.dev: "Your first agent in 5 minutes"
|
||||
- Stage progression guide: experiment → stabilize → scale
|
||||
- Video/screencast showing the /loop workflow
|
||||
|
||||
## Example: 5-Minute Agent Setup
|
||||
|
||||
```bash
|
||||
# 1. Register agent in SynapBus web UI
|
||||
# → Download CLAUDE.md (researcher archetype)
|
||||
# → Copy MCP config
|
||||
|
||||
# 2. Create project directory
|
||||
mkdir my-research-agent
|
||||
cd my-research-agent
|
||||
mv ~/Downloads/CLAUDE.md .
|
||||
mkdir -p .claude/skills
|
||||
|
||||
# 3. Add MCP config to Claude Code
|
||||
# (paste into ~/.claude/settings.json or project settings)
|
||||
|
||||
# 4. Start experimenting
|
||||
claude
|
||||
> /loop 10m "Check SynapBus for work. Search for MCP security news. Post findings to #news-mcpproxy"
|
||||
|
||||
# 5. Watch in SynapBus web UI
|
||||
# Messages appear in channels, reactions track state
|
||||
# Tweak CLAUDE.md, add skills, iterate
|
||||
|
||||
# 6. When happy, commit to git
|
||||
git init && git add -A && git commit -m "working agent"
|
||||
```
|
||||
|
||||
## Non-Goals
|
||||
|
||||
- SynapBus does NOT manage agent instructions at runtime
|
||||
- SynapBus does NOT start/stop agents
|
||||
- SynapBus does NOT require specific client software
|
||||
- No vendor lock-in — agents can switch from Claude to Gemini without SynapBus changes
|
||||
@@ -0,0 +1,224 @@
|
||||
# Demo Scenarios & Practical Guides Design
|
||||
|
||||
**Date**: 2026-03-22
|
||||
**Status**: Draft
|
||||
**Context**: Brainstorming session — identifying demos, gaps, and website improvements
|
||||
|
||||
## Target User
|
||||
|
||||
Developer who already uses Claude Code. Knows `/loop`, knows MCP servers. Needs SynapBus config and good prompts.
|
||||
|
||||
## Demo Outcome Goal
|
||||
|
||||
Practical utility that reveals emergent collaboration. Each demo does something genuinely useful AND shows two agents doing something together that neither could do alone.
|
||||
|
||||
## Demo Set: 6 Scenarios, Increasing Complexity
|
||||
|
||||
### Demo 1: "The Watchtower" (1 agent, simplest possible)
|
||||
|
||||
One agent monitors a GitHub repo for new issues and posts summaries to a SynapBus channel. Proves: SynapBus as memory (agent remembers what it already reported), `/loop` as heartbeat.
|
||||
|
||||
```
|
||||
/loop 5m "Check SynapBus (my_status). Then fetch recent issues from github.com/anthropics/claude-code/issues. Search SynapBus for each issue title to avoid duplicates. Post new ones to #github-watch. Mark what you reported."
|
||||
```
|
||||
|
||||
### Demo 2: "Research + Brief" (2 agents, first collaboration)
|
||||
|
||||
Agent A researches a topic and posts findings. Agent B watches for findings and writes a summary brief. Neither knows about the other — they coordinate through the channel.
|
||||
|
||||
```
|
||||
Terminal 1 (researcher):
|
||||
/loop 10m "Check SynapBus. Search web for 'MCP protocol news this week'. Post top 3 findings to #research with source URLs. Check inbox for owner instructions first."
|
||||
|
||||
Terminal 2 (briefer):
|
||||
/loop 15m "Check SynapBus. Read latest messages in #research channel. If there are 3+ new findings since your last brief, write a 1-paragraph executive summary and post to #briefs. Search #briefs first to avoid repeating yourself."
|
||||
```
|
||||
|
||||
### Demo 3: "Draft + Review Pipeline" (2 agents, stigmergy workflow)
|
||||
|
||||
Agent A drafts a blog post outline from approved topics. Agent B reviews drafts and suggests improvements. Human approves the topic, agents handle the rest.
|
||||
|
||||
```
|
||||
Terminal 1 (writer):
|
||||
/loop 10m "Check SynapBus. Use list_by_state on #content-pipeline for 'approved' items. Claim one with react in_progress. Write a blog post outline as a thread reply. React done when finished."
|
||||
|
||||
Terminal 2 (reviewer):
|
||||
/loop 10m "Check SynapBus. Use list_by_state on #content-pipeline for 'done' items. Read the thread, review the outline. Post improvement suggestions as a reply. React published if quality is good."
|
||||
```
|
||||
|
||||
Human posts "Blog idea: Why stigmergy beats orchestration for AI agents" to #content-pipeline. Reacts approve. Watches agents collaborate.
|
||||
|
||||
### Demo 4: "Competitive Intel" (2 agents, cross-referencing)
|
||||
|
||||
Agent A monitors HackerNews for AI topics. Agent B monitors GitHub for new MCP servers. When Agent A finds something related to MCP, it DMs Agent B. Agent B checks if the referenced project exists on GitHub and enriches the finding.
|
||||
|
||||
```
|
||||
Terminal 1 (hn-watcher):
|
||||
/loop 10m "Check SynapBus inbox first. Search HackerNews for 'MCP OR model context protocol'. Post findings to #hn-watch. If any mention a GitHub repo, DM github-watcher with the URL."
|
||||
|
||||
Terminal 2 (github-watcher):
|
||||
/loop 10m "Check SynapBus inbox first. If hn-watcher sent you a GitHub URL, fetch the repo details (stars, description, last commit) and post enriched info to #hn-watch as a reply. Also search GitHub for new repos matching 'mcp-server' created this week, post to #github-watch."
|
||||
```
|
||||
|
||||
### Demo 5: "The Full Loop" (3 agents, end-to-end pipeline)
|
||||
|
||||
Researcher finds content. Writer drafts. Publisher posts. Full stigmergy — no agent knows about the others.
|
||||
|
||||
```
|
||||
Terminal 1 (scout):
|
||||
/loop 10m "Check SynapBus. Search for trending AI security articles. Post best finding to #content-pipeline as a proposal."
|
||||
|
||||
Terminal 2 (writer):
|
||||
/loop 10m "Check SynapBus. Check #content-pipeline for approved items. Claim one, write a 3-paragraph LinkedIn post draft in a thread reply. React done."
|
||||
|
||||
Terminal 3 (publisher):
|
||||
/loop 10m "Check SynapBus. Check #content-pipeline for done items. Review the draft. If good, react published with metadata URL. Post a summary to #briefs."
|
||||
```
|
||||
|
||||
### Demo 6: "YouTube Outreach Pipeline" (4 agents, real business workflow)
|
||||
|
||||
Real-world outreach pipeline using yt-outreach project. Scout discovers YouTube channels, enricher extracts contacts, email agent drafts personalized emails, follow-up agent tracks responses.
|
||||
|
||||
```
|
||||
#yt-pipeline channel (workflow-enabled):
|
||||
|
||||
Scout agent → discovers channels, posts to #yt-pipeline [proposed]
|
||||
Human → approves promising channels [approved]
|
||||
Enricher agent → claims approved, enriches, extracts email [in_progress → done]
|
||||
Email agent → claims enriched channels, drafts personalized email [in_progress]
|
||||
Human → approves email draft in thread [approved → published]
|
||||
Follow-up agent → tracks sent emails, sends follow-up after 5 days
|
||||
```
|
||||
|
||||
The `/loop` prompts:
|
||||
|
||||
```bash
|
||||
# Terminal 1: Scout
|
||||
/loop 30m "Check SynapBus. Run yt-outreach discover for keyword 'MCP tutorial'.
|
||||
For each new channel found (search SynapBus first to avoid duplicates),
|
||||
post to #yt-pipeline: 'DISCOVERED: {channel_name} ({subscribers} subs) - {collab_score}/100 - {top_video_title}'"
|
||||
|
||||
# Terminal 2: Enricher
|
||||
/loop 15m "Check SynapBus. List approved items in #yt-pipeline.
|
||||
Claim one. Run yt-outreach enrich for that channel.
|
||||
If email found, reply in thread with contact details. React done.
|
||||
If no email, visit the channel's About page with browser, extract email, react done."
|
||||
|
||||
# Terminal 3: Email drafter
|
||||
/loop 15m "Check SynapBus. List done items in #yt-pipeline that have email in thread.
|
||||
Claim one. Read the channel details. Draft a personalized email referencing
|
||||
their recent MCP video. Post draft to thread for approval."
|
||||
|
||||
# Terminal 4: Follow-up tracker
|
||||
/loop 1h "Check SynapBus. Search for published items in #yt-pipeline older than 5 days.
|
||||
If no response tracked, draft a follow-up email and post to thread for approval."
|
||||
```
|
||||
|
||||
**What SynapBus provides that JSON files can't:**
|
||||
- **Parallelism** — all 4 agents run simultaneously, pick up work as it becomes available
|
||||
- **Human-in-the-loop** — approve channels and email drafts via reactions in the web UI
|
||||
- **Memory** — every agent can search history ("did we already contact this channel?")
|
||||
- **Audit trail** — complete thread per channel showing discovery → enrichment → email → follow-up
|
||||
- **Trust** — email agent starts supervised, earns autonomy after enough approvals
|
||||
|
||||
## SynapBus as Agent Memory (from video insight)
|
||||
|
||||
The video by Nate B Jones identifies three "Lego bricks" for agents:
|
||||
1. **Memory** — persistent store agents can read/write
|
||||
2. **Proactivity** — scheduled heartbeat (/loop)
|
||||
3. **Tools** — MCP servers for reaching external systems
|
||||
|
||||
SynapBus provides all three:
|
||||
- **Memory** = channels + semantic search. Agents post findings, search history to avoid duplicates, build on past work. Channel messages ARE the memory.
|
||||
- **Proactivity** = /loop triggers the startup loop. Agent wakes, checks inbox, finds work, acts.
|
||||
- **Tools** = MCP tool interface with 28 actions. Agents discover available tools via `search()`.
|
||||
|
||||
Key insight from the video: **"Moving from Parrot to Detective"** — memory enables pattern matching. An agent doesn't just report today's news, it can say "this is the 3rd time this week someone mentioned Gravitee as MCP gateway competition — this is a trend worth writing about."
|
||||
|
||||
SynapBus's `search_messages` with semantic search enables exactly this pattern.
|
||||
|
||||
## Three-Stage Progression
|
||||
|
||||
### Stage 1: Experiment (Claude Code + /loop)
|
||||
- User runs claude code in a terminal
|
||||
- SynapBus connected as MCP server
|
||||
- User uses /loop to wake agent periodically
|
||||
- User watches channels, tweaks instructions in real-time
|
||||
- No Docker, no K8s, no gitops — just files on disk
|
||||
|
||||
### Stage 2: Stabilize (Docker + Agent SDK)
|
||||
- Working instructions committed to git repo (CLAUDE.md + .claude/skills/)
|
||||
- Agent runs via Agent SDK script in Docker container
|
||||
- Cron schedule replaces /loop
|
||||
- Same SynapBus, same API key, same channels
|
||||
|
||||
### Stage 3: Scale (Kubernetes)
|
||||
- Docker containers become K8s CronJobs
|
||||
- Workspace is a gitops repo (auto-pulled each run)
|
||||
- Trust scores accumulate, StalemateWorker monitors
|
||||
- Full platform features
|
||||
|
||||
## Identified Gaps in SynapBus
|
||||
|
||||
### Code Gaps
|
||||
|
||||
1. **No "hello world" quickstart** — after `synapbus serve`, user doesn't know what to do next
|
||||
2. **MCP config endpoint returns placeholder API key** — need to pass real key or generate config at registration time
|
||||
3. **No default channels for demos** — should ship with #general + #research + #content-pipeline pre-created
|
||||
4. **No way to test MCP connection** — need a simple health check tool or "ping" command
|
||||
5. **Channel messages don't show sender's agent type badge** in all views
|
||||
6. **Semantic search requires embedding provider setup** — should work with basic full-text search out of box (it does, but not documented clearly)
|
||||
|
||||
### Website Gaps (synapbus.dev)
|
||||
|
||||
1. **Homepage is generic** — talks about features but doesn't show a working demo
|
||||
2. **No copy-paste quickstart** — user should go from zero to two agents talking in 5 minutes
|
||||
3. **No demo videos/screencasts** — showing agents collaborating in real-time
|
||||
4. **Features page lists capabilities but no practical examples** — each feature should have a "try this" section
|
||||
5. **No "Patterns" page** — stigmergy, auction, memory as search patterns need dedicated docs with examples
|
||||
6. **No "Gallery" of demo scenarios** — the 6 demos above should be browsable on the website
|
||||
7. **Install page doesn't mention Claude Code or /loop** — the primary onboarding path isn't documented
|
||||
|
||||
### Documentation Gaps
|
||||
|
||||
1. **No troubleshooting guide** — MCP connection failures, auth issues
|
||||
2. **No "from experiment to production" guide** — how to go from /loop to Docker to K8s
|
||||
3. **No API reference** — the 28 MCP actions need proper documentation with examples
|
||||
|
||||
## Website Redesign Direction
|
||||
|
||||
The website should be restructured around the **three-stage journey**:
|
||||
|
||||
```
|
||||
Homepage
|
||||
├── Hero: "Build multi-agent systems in 5 minutes"
|
||||
├── Live demo: 2-agent collaboration (animated or video)
|
||||
├── 3-step quickstart (install → configure → /loop)
|
||||
├── "See it work" — screenshot of web UI with agents collaborating
|
||||
|
||||
Getting Started (replaces Install)
|
||||
├── Prerequisites (Claude Code, Docker for later)
|
||||
├── 5-minute quickstart (Demo 1: The Watchtower)
|
||||
├── Your first collaboration (Demo 2: Research + Brief)
|
||||
├── MCP config copy-paste
|
||||
|
||||
Patterns
|
||||
├── Stigmergy (workflow reactions)
|
||||
├── Task Auction (bidding)
|
||||
├── Memory as Search (semantic recall)
|
||||
├── Each with working /loop prompts
|
||||
|
||||
Demos / Gallery
|
||||
├── Demo 1-6 with full instructions
|
||||
├── Each demo: what it does, setup, /loop prompts, expected output
|
||||
|
||||
Scaling
|
||||
├── Stage 2: Docker + Agent SDK
|
||||
├── Stage 3: Kubernetes
|
||||
├── Trust scores & autonomy
|
||||
|
||||
API Reference
|
||||
├── 4 MCP tools
|
||||
├── 28 actions with examples
|
||||
├── REST API for web UI
|
||||
```
|
||||
@@ -6,10 +6,10 @@ type Registry struct {
|
||||
ordered []Action // maintains insertion order
|
||||
}
|
||||
|
||||
// NewRegistry creates a registry pre-populated with all 23 agent-callable actions.
|
||||
// NewRegistry creates a registry pre-populated with all 28 agent-callable actions.
|
||||
func NewRegistry() *Registry {
|
||||
r := &Registry{
|
||||
actions: make(map[string]Action, 23),
|
||||
actions: make(map[string]Action, 28),
|
||||
}
|
||||
for _, a := range allActions() {
|
||||
r.actions[a.Name] = a
|
||||
@@ -42,7 +42,7 @@ func (r *Registry) ListByCategory(category string) []Action {
|
||||
return out
|
||||
}
|
||||
|
||||
// allActions returns the canonical list of all 23 agent-callable actions.
|
||||
// allActions returns the canonical list of all 28 agent-callable actions.
|
||||
func allActions() []Action {
|
||||
return []Action{
|
||||
// ── Messaging (7 actions) ──────────────────────────────────────
|
||||
@@ -339,7 +339,7 @@ func allActions() []Action {
|
||||
{
|
||||
Name: "post_task",
|
||||
Category: "swarm",
|
||||
Description: "Post a task to an auction channel for agents to bid on",
|
||||
Description: "Post a task to an auction channel for agents to bid on. Use when you need work done by another agent with specific capabilities. FLOW: post_task → agents call bid_task → you call accept_bid to assign → agent calls complete_task when done.",
|
||||
Params: []Param{
|
||||
{Name: "channel_name", Type: "string", Description: "Name of the auction channel", Required: true},
|
||||
{Name: "title", Type: "string", Description: "Task title", Required: true},
|
||||
@@ -358,7 +358,7 @@ func allActions() []Action {
|
||||
{
|
||||
Name: "bid_task",
|
||||
Category: "swarm",
|
||||
Description: "Submit a bid on an open task in an auction channel",
|
||||
Description: "Submit a bid on an open task. Include your relevant capabilities and time estimate. The task poster will review bids and accept one. Check list_tasks with status='open' to find tasks you can bid on.",
|
||||
Params: []Param{
|
||||
{Name: "task_id", Type: "number", Description: "ID of the task to bid on", Required: true},
|
||||
{Name: "capabilities", Type: "string", Description: "JSON object describing your relevant capabilities"},
|
||||
@@ -425,7 +425,7 @@ func allActions() []Action {
|
||||
{
|
||||
Name: "upload_attachment",
|
||||
Category: "attachments",
|
||||
Description: "Upload a file attachment. Content must be base64-encoded. Returns the SHA-256 hash for later retrieval. Max file size: 50MB.",
|
||||
Description: "Upload a file attachment. Content must be base64-encoded. Returns the SHA-256 hash for later retrieval. Upload first, then use the returned hash in send_message's attachments parameter to link it to a message. Max file size: 50MB.",
|
||||
Params: []Param{
|
||||
{Name: "content", Type: "string", Description: "Base64-encoded file content", Required: true},
|
||||
{Name: "filename", Type: "string", Description: "Original filename (optional, used for MIME detection and display)"},
|
||||
@@ -455,5 +455,149 @@ func allActions() []Action {
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
// ── Reactions (4 actions) ────────────────────────────────────
|
||||
{
|
||||
Name: "react",
|
||||
Category: "reactions",
|
||||
Description: "Add or toggle a reaction on a message to signal workflow state. Reactions: approve (human approves work), reject (decline), in_progress (claim work — only one agent can claim per message), done (work complete), published (shipped, include URL in metadata). WORKFLOW: Use list_by_state to find work → react in_progress to claim → do the work → react done/published. Toggle: calling same reaction again removes it.",
|
||||
Params: []Param{
|
||||
{Name: "message_id", Type: "number", Description: "ID of the message to react to", Required: true},
|
||||
{Name: "reaction", Type: "string", Description: "Reaction type: approve, reject, in_progress, done, published", Required: true},
|
||||
{Name: "metadata", Type: "string", Description: "JSON metadata object (optional)"},
|
||||
},
|
||||
Returns: "JSON with action ('added' or 'removed') and reaction details",
|
||||
Examples: []Example{
|
||||
{
|
||||
Description: "Approve a message",
|
||||
Code: `call("react", {"message_id": 42, "reaction": "approve"})`,
|
||||
},
|
||||
{
|
||||
Description: "Toggle a reaction off (call same reaction again)",
|
||||
Code: `call("react", {"message_id": 42, "reaction": "approve"})`,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "unreact",
|
||||
Category: "reactions",
|
||||
Description: "Remove a specific reaction. Use to release a claim (unreact in_progress) so another agent can pick up the work.",
|
||||
Params: []Param{
|
||||
{Name: "message_id", Type: "number", Description: "ID of the message to remove reaction from", Required: true},
|
||||
{Name: "reaction", Type: "string", Description: "Reaction type to remove: approve, reject, in_progress, done, published", Required: true},
|
||||
},
|
||||
Returns: "JSON with message_id, reaction, and status 'removed'",
|
||||
Examples: []Example{
|
||||
{
|
||||
Description: "Remove an approval reaction",
|
||||
Code: `call("unreact", {"message_id": 42, "reaction": "approve"})`,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "get_reactions",
|
||||
Category: "reactions",
|
||||
Description: "Get all reactions and derived workflow state for a message. Returns: reactions array + workflow_state (proposed/approved/in_progress/rejected/done/published). Use to check if work is claimed before attempting to claim it.",
|
||||
Params: []Param{
|
||||
{Name: "message_id", Type: "number", Description: "ID of the message to get reactions for", Required: true},
|
||||
},
|
||||
Returns: "JSON with reactions array and workflow_state",
|
||||
Examples: []Example{
|
||||
{
|
||||
Description: "Get reactions and workflow state for a message",
|
||||
Code: `call("get_reactions", {"message_id": 42})`,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "list_by_state",
|
||||
Category: "reactions",
|
||||
Description: "List messages in a channel filtered by workflow state. Paginated — use limit and offset for large channels. States: proposed (new), approved (ready for work), in_progress (claimed), rejected, done, published.",
|
||||
Params: []Param{
|
||||
{Name: "channel", Type: "string", Description: "Channel name", Required: true},
|
||||
{Name: "state", Type: "string", Description: "Workflow state to filter by: proposed, approved, in_progress, rejected, done, published", Required: true},
|
||||
{Name: "limit", Type: "number", Description: "Max messages to return (default 20, max 100)"},
|
||||
{Name: "offset", Type: "number", Description: "Skip first N messages for pagination (default 0)"},
|
||||
{Name: "include_messages", Type: "boolean", Description: "Include message bodies (default false). Bodies truncated to max_body_length chars."},
|
||||
{Name: "max_body_length", Type: "number", Description: "Max chars per message body when include_messages=true (default 500). Use lower values for channels with long messages."},
|
||||
},
|
||||
Returns: "JSON with message_ids, count (this page), total (all matching), limit, offset, and optionally messages array",
|
||||
Examples: []Example{
|
||||
{
|
||||
Description: "List first 10 approved messages with content",
|
||||
Code: `call("list_by_state", {"channel": "approvals", "state": "approved", "limit": 10, "include_messages": true})`,
|
||||
},
|
||||
{
|
||||
Description: "Paginate — get next page",
|
||||
Code: `call("list_by_state", {"channel": "approvals", "state": "proposed", "limit": 10, "offset": 10})`,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
// ── Threads (1 action) ──────────────────────────────────────
|
||||
{
|
||||
Name: "get_replies",
|
||||
Category: "threads",
|
||||
Description: "Get all replies (thread messages) for a given message. Use to read thread conversations, check for edits, or follow-up comments. Also available as a direct MCP tool.",
|
||||
Params: []Param{
|
||||
{Name: "message_id", Type: "number", Description: "ID of the parent message to get replies for", Required: true},
|
||||
},
|
||||
Returns: "JSON with message_id, replies array, and count",
|
||||
Examples: []Example{
|
||||
{
|
||||
Description: "Get all replies to a message",
|
||||
Code: `call("get_replies", {"message_id": 42})`,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
// ── Trust (1 action) ────────────────────────────────────────
|
||||
{
|
||||
Name: "get_trust",
|
||||
Category: "trust",
|
||||
Description: "Get your trust scores by action type. Trust determines autonomy: higher trust = less human approval needed. Scores increase on human approve (+0.05) and decrease on reject (-0.1). Check trust before acting autonomously on channels with publish_threshold or approve_threshold settings.",
|
||||
Params: []Param{
|
||||
{Name: "agent_name", Type: "string", Description: "Agent name to query (defaults to calling agent)"},
|
||||
},
|
||||
Returns: "JSON with agent_name and scores map (action_type -> score)",
|
||||
Examples: []Example{
|
||||
{
|
||||
Description: "Get your own trust scores",
|
||||
Code: `call("get_trust", {})`,
|
||||
},
|
||||
{
|
||||
Description: "Get another agent's trust scores",
|
||||
Code: `call("get_trust", {"agent_name": "research-mcpproxy"})`,
|
||||
},
|
||||
},
|
||||
},
|
||||
// ── SQL Query (1 action) ────────────────────────────────────
|
||||
{
|
||||
Name: "query",
|
||||
Category: "data",
|
||||
Description: "Execute a read-only SQL query against your accessible messages, channels, and reactions. Use tables: my_messages (your DMs + joined channels), my_channels (channels you are in), channel_messages (messages in your channels). Results are limited to 100 rows. Only SELECT statements are allowed.",
|
||||
Params: []Param{
|
||||
{Name: "sql", Type: "string", Description: "SQL SELECT query. Available tables: my_messages (id, body, from_agent, to_agent, priority, status, metadata, created_at, channel_name), my_channels (id, name, description, type), channel_messages (id, body, from_agent, priority, channel_name, created_at). CTEs (WITH) are supported.", Required: true},
|
||||
},
|
||||
Returns: "JSON with columns (array of column names), rows (array of row arrays), row_count, and truncated (boolean if > 100 rows)",
|
||||
Examples: []Example{
|
||||
{
|
||||
Description: "Find high-priority messages in a channel",
|
||||
Code: `call("query", {"sql": "SELECT id, body, from_agent, priority FROM channel_messages WHERE channel_name = 'news-mcpproxy' AND priority >= 7 ORDER BY created_at DESC LIMIT 10"})`,
|
||||
},
|
||||
{
|
||||
Description: "List your channels",
|
||||
Code: `call("query", {"sql": "SELECT name, description FROM my_channels ORDER BY name"})`,
|
||||
},
|
||||
{
|
||||
Description: "Count messages per channel",
|
||||
Code: `call("query", {"sql": "SELECT channel_name, COUNT(*) as msg_count FROM channel_messages GROUP BY channel_name ORDER BY msg_count DESC"})`,
|
||||
},
|
||||
{
|
||||
Description: "Search messages with keyword",
|
||||
Code: `call("query", {"sql": "SELECT id, body, from_agent, created_at FROM my_messages WHERE body LIKE '%MCP%' ORDER BY created_at DESC LIMIT 20"})`,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,11 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRegistryHas23Actions(t *testing.T) {
|
||||
func TestRegistryHas30Actions(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
got := len(r.List())
|
||||
if got != 23 {
|
||||
t.Errorf("expected 23 actions, got %d", got)
|
||||
if got != 30 {
|
||||
t.Errorf("expected 30 actions, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,9 @@ func TestRegistryCategories(t *testing.T) {
|
||||
{"channels", 9},
|
||||
{"swarm", 5},
|
||||
{"attachments", 2},
|
||||
{"reactions", 4},
|
||||
{"threads", 1},
|
||||
{"trust", 1},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -49,6 +52,14 @@ func TestRegistryGetByName(t *testing.T) {
|
||||
"post_task", "bid_task", "accept_bid", "complete_task", "list_tasks",
|
||||
// attachments
|
||||
"upload_attachment", "download_attachment",
|
||||
// reactions
|
||||
"react", "unreact", "get_reactions", "list_by_state",
|
||||
// threads
|
||||
"get_replies",
|
||||
// trust
|
||||
"get_trust",
|
||||
// data
|
||||
"query",
|
||||
}
|
||||
|
||||
for _, name := range allNames {
|
||||
|
||||
@@ -169,6 +169,8 @@ func (s *AdminServer) dispatch(req Request) Response {
|
||||
return s.handleChannelsCreate(ctx, req.Args)
|
||||
case "channels.join":
|
||||
return s.handleChannelsJoin(ctx, req.Args)
|
||||
case "channels.update_settings":
|
||||
return s.handleChannelsUpdateSettings(ctx, req.Args)
|
||||
|
||||
// --- conversations ---
|
||||
case "conversations.list":
|
||||
@@ -995,6 +997,64 @@ func (s *AdminServer) handleChannelsJoin(ctx context.Context, args json.RawMessa
|
||||
}}
|
||||
}
|
||||
|
||||
func (s *AdminServer) handleChannelsUpdateSettings(ctx context.Context, args json.RawMessage) Response {
|
||||
var p struct {
|
||||
Name string `json:"name"`
|
||||
AutoApprove *bool `json:"auto_approve,omitempty"`
|
||||
StalemateRemindAfter string `json:"stalemate_remind_after,omitempty"`
|
||||
StalemateEscalateAfter string `json:"stalemate_escalate_after,omitempty"`
|
||||
}
|
||||
if err := json.Unmarshal(args, &p); err != nil {
|
||||
return Response{OK: false, Error: "invalid args: " + err.Error()}
|
||||
}
|
||||
if p.Name == "" {
|
||||
return Response{OK: false, Error: "name is required"}
|
||||
}
|
||||
|
||||
// Build the SET clause dynamically based on provided fields
|
||||
var setClauses []string
|
||||
var setArgs []interface{}
|
||||
|
||||
if p.AutoApprove != nil {
|
||||
autoApproveVal := 0
|
||||
if *p.AutoApprove {
|
||||
autoApproveVal = 1
|
||||
}
|
||||
setClauses = append(setClauses, "auto_approve = ?")
|
||||
setArgs = append(setArgs, autoApproveVal)
|
||||
}
|
||||
if p.StalemateRemindAfter != "" {
|
||||
setClauses = append(setClauses, "stalemate_remind_after = ?")
|
||||
setArgs = append(setArgs, p.StalemateRemindAfter)
|
||||
}
|
||||
if p.StalemateEscalateAfter != "" {
|
||||
setClauses = append(setClauses, "stalemate_escalate_after = ?")
|
||||
setArgs = append(setArgs, p.StalemateEscalateAfter)
|
||||
}
|
||||
|
||||
if len(setClauses) == 0 {
|
||||
return Response{OK: false, Error: "at least one setting must be provided (auto_approve, stalemate_remind_after, stalemate_escalate_after)"}
|
||||
}
|
||||
|
||||
query := fmt.Sprintf("UPDATE channels SET %s WHERE LOWER(name) = LOWER(?)", strings.Join(setClauses, ", "))
|
||||
setArgs = append(setArgs, p.Name)
|
||||
|
||||
result, err := s.db.ExecContext(ctx, query, setArgs...)
|
||||
if err != nil {
|
||||
return Response{OK: false, Error: "update channel settings: " + err.Error()}
|
||||
}
|
||||
|
||||
rowsAffected, _ := result.RowsAffected()
|
||||
if rowsAffected == 0 {
|
||||
return Response{OK: false, Error: fmt.Sprintf("channel not found: %s", p.Name)}
|
||||
}
|
||||
|
||||
return Response{OK: true, Data: map[string]interface{}{
|
||||
"channel": p.Name,
|
||||
"updated": true,
|
||||
}}
|
||||
}
|
||||
|
||||
// ---------- conversations handlers ----------
|
||||
|
||||
func (s *AdminServer) handleConversationsList(ctx context.Context, args json.RawMessage) Response {
|
||||
|
||||
@@ -0,0 +1,234 @@
|
||||
// Package agentquery provides a sandboxed SQL query executor for agents.
|
||||
// Agents can run read-only SELECT queries against curated views with
|
||||
// per-agent access control, automatic LIMIT enforcement, and timeouts.
|
||||
package agentquery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// MaxRows is the maximum number of rows returned by a query.
|
||||
MaxRows = 100
|
||||
// QueryTimeout is the maximum duration for a query.
|
||||
QueryTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
// Allowed view names that agents can query.
|
||||
var allowedTables = map[string]bool{
|
||||
"my_messages": true,
|
||||
"my_channels": true,
|
||||
"channel_messages": true,
|
||||
}
|
||||
|
||||
// Executor runs sandboxed SQL queries on behalf of agents.
|
||||
type Executor struct {
|
||||
db *sql.DB // read-only pool (query_only=ON)
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// New creates a new query executor using the provided read-only database connection.
|
||||
func New(readDB *sql.DB, logger *slog.Logger) *Executor {
|
||||
return &Executor{
|
||||
db: readDB,
|
||||
logger: logger.With("component", "agentquery"),
|
||||
}
|
||||
}
|
||||
|
||||
// QueryResult holds the results of a SQL query.
|
||||
type QueryResult struct {
|
||||
Columns []string `json:"columns"`
|
||||
Rows [][]interface{} `json:"rows"`
|
||||
RowCount int `json:"row_count"`
|
||||
Truncated bool `json:"truncated"`
|
||||
}
|
||||
|
||||
// Execute runs a SQL query on behalf of an agent with access control.
|
||||
func (e *Executor) Execute(ctx context.Context, agentName, sqlQuery string) (*QueryResult, error) {
|
||||
// 1. Validate the SQL statement
|
||||
if err := validateSQL(sqlQuery); err != nil {
|
||||
return nil, fmt.Errorf("query validation failed: %w", err)
|
||||
}
|
||||
|
||||
// 2. Rewrite the query to inject access control and enforce LIMIT
|
||||
rewritten := rewriteQuery(agentName, sqlQuery)
|
||||
|
||||
// 3. Execute with timeout
|
||||
queryCtx, cancel := context.WithTimeout(ctx, QueryTimeout)
|
||||
defer cancel()
|
||||
|
||||
rows, err := e.db.QueryContext(queryCtx, rewritten)
|
||||
if err != nil {
|
||||
if queryCtx.Err() == context.DeadlineExceeded {
|
||||
return nil, fmt.Errorf("query timed out after %s", QueryTimeout)
|
||||
}
|
||||
return nil, fmt.Errorf("query execution failed: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
// 4. Collect results
|
||||
columns, err := rows.Columns()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get columns: %w", err)
|
||||
}
|
||||
|
||||
var resultRows [][]interface{}
|
||||
truncated := false
|
||||
|
||||
for rows.Next() {
|
||||
if len(resultRows) >= MaxRows {
|
||||
truncated = true
|
||||
break
|
||||
}
|
||||
|
||||
values := make([]interface{}, len(columns))
|
||||
scanArgs := make([]interface{}, len(columns))
|
||||
for i := range values {
|
||||
scanArgs[i] = &values[i]
|
||||
}
|
||||
|
||||
if err := rows.Scan(scanArgs...); err != nil {
|
||||
return nil, fmt.Errorf("scan row: %w", err)
|
||||
}
|
||||
|
||||
// Convert []byte to string for JSON serialization
|
||||
row := make([]interface{}, len(columns))
|
||||
for i, v := range values {
|
||||
if b, ok := v.([]byte); ok {
|
||||
row[i] = string(b)
|
||||
} else {
|
||||
row[i] = v
|
||||
}
|
||||
}
|
||||
resultRows = append(resultRows, row)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate rows: %w", err)
|
||||
}
|
||||
|
||||
if resultRows == nil {
|
||||
resultRows = [][]interface{}{}
|
||||
}
|
||||
|
||||
e.logger.Info("agent query executed",
|
||||
"agent", agentName,
|
||||
"rows", len(resultRows),
|
||||
"truncated", truncated,
|
||||
)
|
||||
|
||||
return &QueryResult{
|
||||
Columns: columns,
|
||||
Rows: resultRows,
|
||||
RowCount: len(resultRows),
|
||||
Truncated: truncated,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// validateSQL checks that the query is a read-only SELECT statement.
|
||||
func validateSQL(query string) error {
|
||||
trimmed := strings.TrimSpace(query)
|
||||
if trimmed == "" {
|
||||
return fmt.Errorf("empty query")
|
||||
}
|
||||
|
||||
// Remove comments
|
||||
upper := strings.ToUpper(trimmed)
|
||||
|
||||
// Must start with SELECT or WITH (CTEs)
|
||||
if !strings.HasPrefix(upper, "SELECT") && !strings.HasPrefix(upper, "WITH") {
|
||||
return fmt.Errorf("only SELECT statements are allowed (got %q)", firstWord(upper))
|
||||
}
|
||||
|
||||
// Block dangerous keywords (check as whole words or with common delimiters)
|
||||
blocked := []string{
|
||||
"INSERT ", "UPDATE ", "DELETE ", "DROP ", "ALTER ", "CREATE ",
|
||||
"ATTACH ", "DETACH ", "PRAGMA", "REINDEX ", "VACUUM ",
|
||||
"REPLACE ", "GRANT ", "REVOKE ",
|
||||
}
|
||||
for _, kw := range blocked {
|
||||
if strings.Contains(upper, kw) {
|
||||
return fmt.Errorf("statement contains blocked keyword: %s", strings.TrimSpace(kw))
|
||||
}
|
||||
}
|
||||
|
||||
// Block multiple statements (semicolon followed by non-whitespace)
|
||||
parts := strings.Split(trimmed, ";")
|
||||
nonEmpty := 0
|
||||
for _, p := range parts {
|
||||
if strings.TrimSpace(p) != "" {
|
||||
nonEmpty++
|
||||
}
|
||||
}
|
||||
if nonEmpty > 1 {
|
||||
return fmt.Errorf("multiple statements not allowed")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// rewriteQuery wraps the agent's query with access control CTEs.
|
||||
// It replaces references to my_messages, my_channels, channel_messages
|
||||
// with CTEs that filter by the agent's access.
|
||||
func rewriteQuery(agentName, query string) string {
|
||||
// Build access-control CTEs that the agent's query can reference
|
||||
cte := fmt.Sprintf(`
|
||||
WITH my_messages AS (
|
||||
SELECT v.* FROM v_agent_messages v
|
||||
LEFT JOIN channel_members cm ON cm.channel_id = v.channel_id AND cm.agent_name = %[1]s
|
||||
WHERE v.to_agent = %[1]s
|
||||
OR v.from_agent = %[1]s
|
||||
OR (v.channel_id IS NOT NULL AND cm.agent_name IS NOT NULL)
|
||||
),
|
||||
my_channels AS (
|
||||
SELECT c.id, c.name, c.description, c.type, c.topic, c.is_private, c.created_at,
|
||||
cm.joined_at AS member_since
|
||||
FROM channels c
|
||||
JOIN channel_members cm ON cm.channel_id = c.id AND cm.agent_name = %[1]s
|
||||
),
|
||||
channel_messages AS (
|
||||
SELECT v.* FROM v_channel_messages v
|
||||
WHERE v.channel_id IN (
|
||||
SELECT channel_id FROM channel_members WHERE agent_name = %[1]s
|
||||
)
|
||||
)
|
||||
`, quoteSQLString(agentName))
|
||||
|
||||
trimmed := strings.TrimSpace(query)
|
||||
upper := strings.ToUpper(trimmed)
|
||||
|
||||
// Remove trailing semicolon if present
|
||||
trimmed = strings.TrimRight(trimmed, "; \t\n")
|
||||
|
||||
if strings.HasPrefix(upper, "WITH") {
|
||||
// User has their own CTEs. Merge: our CTEs first, then theirs.
|
||||
userCTEs := strings.TrimSpace(trimmed[4:]) // skip "WITH"
|
||||
return cte + ", " + userCTEs
|
||||
}
|
||||
|
||||
// Simple SELECT — prepend our CTEs
|
||||
return cte + trimmed
|
||||
}
|
||||
|
||||
// quoteSQLString safely quotes a string for use in SQL.
|
||||
func quoteSQLString(s string) string {
|
||||
escaped := strings.ReplaceAll(s, "'", "''")
|
||||
return "'" + escaped + "'"
|
||||
}
|
||||
|
||||
func firstWord(s string) string {
|
||||
for i, c := range s {
|
||||
if c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '(' {
|
||||
return s[:i]
|
||||
}
|
||||
}
|
||||
if len(s) > 20 {
|
||||
return s[:20]
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -0,0 +1,341 @@
|
||||
package agentquery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func setupTestDB(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
db, err := sql.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("open db: %v", err)
|
||||
}
|
||||
|
||||
// Create the schema needed for views
|
||||
schema := `
|
||||
CREATE TABLE channels (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
description TEXT DEFAULT '',
|
||||
type TEXT DEFAULT 'standard',
|
||||
topic TEXT DEFAULT '',
|
||||
is_private INTEGER DEFAULT 0,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE TABLE channel_members (
|
||||
channel_id INTEGER,
|
||||
agent_name TEXT,
|
||||
joined_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (channel_id, agent_name)
|
||||
);
|
||||
CREATE TABLE messages (
|
||||
id INTEGER PRIMARY KEY,
|
||||
conversation_id INTEGER DEFAULT 0,
|
||||
from_agent TEXT,
|
||||
to_agent TEXT,
|
||||
channel_id INTEGER,
|
||||
reply_to INTEGER,
|
||||
body TEXT,
|
||||
priority INTEGER DEFAULT 5,
|
||||
status TEXT DEFAULT 'pending',
|
||||
metadata TEXT DEFAULT '{}',
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Views matching the migration
|
||||
CREATE VIEW v_agent_messages AS
|
||||
SELECT m.id, m.body, m.from_agent, m.to_agent, m.priority, m.status, m.metadata,
|
||||
m.created_at, m.updated_at, c.name AS channel_name, m.channel_id, m.reply_to, m.conversation_id
|
||||
FROM messages m LEFT JOIN channels c ON c.id = m.channel_id;
|
||||
|
||||
CREATE VIEW v_agent_channels AS
|
||||
SELECT c.id, c.name, c.description, c.type, c.topic, c.is_private, c.created_at,
|
||||
cm.joined_at AS member_since
|
||||
FROM channels c JOIN channel_members cm ON cm.channel_id = c.id;
|
||||
|
||||
CREATE VIEW v_channel_messages AS
|
||||
SELECT m.id, m.body, m.from_agent, m.priority, m.status, m.metadata, m.created_at,
|
||||
c.name AS channel_name, m.channel_id, m.reply_to
|
||||
FROM messages m JOIN channels c ON c.id = m.channel_id;
|
||||
`
|
||||
if _, err := db.Exec(schema); err != nil {
|
||||
t.Fatalf("create schema: %v", err)
|
||||
}
|
||||
|
||||
// Seed test data
|
||||
seed := `
|
||||
INSERT INTO channels (id, name) VALUES (1, 'general'), (2, 'news-mcpproxy'), (3, 'private-channel');
|
||||
INSERT INTO channel_members (channel_id, agent_name) VALUES
|
||||
(1, 'agent-a'), (1, 'agent-b'),
|
||||
(2, 'agent-a'),
|
||||
(3, 'agent-b');
|
||||
|
||||
-- DMs
|
||||
INSERT INTO messages (id, from_agent, to_agent, body, priority) VALUES
|
||||
(1, 'algis', 'agent-a', 'Hello agent A', 7),
|
||||
(2, 'agent-a', 'algis', 'Hi there', 5),
|
||||
(3, 'algis', 'agent-b', 'Hello agent B', 5);
|
||||
|
||||
-- Channel messages
|
||||
INSERT INTO messages (id, from_agent, channel_id, body, priority) VALUES
|
||||
(4, 'agent-a', 1, 'General post from A', 5),
|
||||
(5, 'agent-b', 1, 'General post from B', 5),
|
||||
(6, 'agent-a', 2, 'News post high prio', 8),
|
||||
(7, 'agent-b', 3, 'Private channel msg', 5);
|
||||
`
|
||||
if _, err := db.Exec(seed); err != nil {
|
||||
t.Fatalf("seed data: %v", err)
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func TestExecuteBasicQuery(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
exec := New(db, slog.Default())
|
||||
result, err := exec.Execute(context.Background(), "agent-a",
|
||||
"SELECT id, body, priority FROM my_messages ORDER BY id")
|
||||
if err != nil {
|
||||
t.Fatalf("query failed: %v", err)
|
||||
}
|
||||
|
||||
if len(result.Columns) != 3 {
|
||||
t.Errorf("expected 3 columns, got %d", len(result.Columns))
|
||||
}
|
||||
if result.Columns[0] != "id" || result.Columns[1] != "body" || result.Columns[2] != "priority" {
|
||||
t.Errorf("unexpected columns: %v", result.Columns)
|
||||
}
|
||||
|
||||
// agent-a should see: DM to it (1), DM from it (2), general posts (4,5), news post (6)
|
||||
// Should NOT see: DM to agent-b (3), private channel msg (7)
|
||||
if result.RowCount < 4 {
|
||||
t.Errorf("expected at least 4 rows for agent-a, got %d", result.RowCount)
|
||||
}
|
||||
|
||||
// Verify agent-b's DM and private channel msg are NOT visible
|
||||
for _, row := range result.Rows {
|
||||
id := row[0]
|
||||
if id == int64(3) {
|
||||
t.Error("agent-a should NOT see message 3 (DM to agent-b)")
|
||||
}
|
||||
if id == int64(7) {
|
||||
t.Error("agent-a should NOT see message 7 (private channel, not joined)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccessControlAgentB(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
exec := New(db, slog.Default())
|
||||
result, err := exec.Execute(context.Background(), "agent-b",
|
||||
"SELECT id, body FROM my_messages ORDER BY id")
|
||||
if err != nil {
|
||||
t.Fatalf("query failed: %v", err)
|
||||
}
|
||||
|
||||
// agent-b should see: DM to it (3), general posts (4,5), private channel (7)
|
||||
// Should NOT see: DM to agent-a (1), DM from agent-a (2), news post (6)
|
||||
hasMsg3 := false
|
||||
hasMsg7 := false
|
||||
for _, row := range result.Rows {
|
||||
id := row[0]
|
||||
if id == int64(3) {
|
||||
hasMsg3 = true
|
||||
}
|
||||
if id == int64(7) {
|
||||
hasMsg7 = true
|
||||
}
|
||||
if id == int64(1) {
|
||||
t.Error("agent-b should NOT see message 1 (DM to agent-a)")
|
||||
}
|
||||
if id == int64(6) {
|
||||
t.Error("agent-b should NOT see message 6 (news channel, not joined)")
|
||||
}
|
||||
}
|
||||
if !hasMsg3 {
|
||||
t.Error("agent-b should see message 3 (DM to it)")
|
||||
}
|
||||
if !hasMsg7 {
|
||||
t.Error("agent-b should see message 7 (private channel, joined)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryChannelMessages(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
exec := New(db, slog.Default())
|
||||
result, err := exec.Execute(context.Background(), "agent-a",
|
||||
"SELECT id, body, channel_name FROM channel_messages WHERE channel_name = 'news-mcpproxy'")
|
||||
if err != nil {
|
||||
t.Fatalf("query failed: %v", err)
|
||||
}
|
||||
|
||||
if result.RowCount != 1 {
|
||||
t.Errorf("expected 1 news message, got %d", result.RowCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryMyChannels(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
exec := New(db, slog.Default())
|
||||
result, err := exec.Execute(context.Background(), "agent-a",
|
||||
"SELECT name FROM my_channels ORDER BY name")
|
||||
if err != nil {
|
||||
t.Fatalf("query failed: %v", err)
|
||||
}
|
||||
|
||||
// agent-a is in: general, news-mcpproxy (not private-channel)
|
||||
if result.RowCount != 2 {
|
||||
t.Errorf("expected 2 channels for agent-a, got %d", result.RowCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidationRejectsInsert(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
exec := New(db, slog.Default())
|
||||
_, err := exec.Execute(context.Background(), "agent-a",
|
||||
"INSERT INTO messages (body) VALUES ('evil')")
|
||||
if err == nil {
|
||||
t.Fatal("expected INSERT to be rejected")
|
||||
}
|
||||
if !contains(err.Error(), "only SELECT") {
|
||||
t.Errorf("expected 'only SELECT' error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidationRejectsDrop(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
exec := New(db, slog.Default())
|
||||
_, err := exec.Execute(context.Background(), "agent-a",
|
||||
"SELECT 1; DROP TABLE messages")
|
||||
if err == nil {
|
||||
t.Fatal("expected multi-statement to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidationRejectsUpdate(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
exec := New(db, slog.Default())
|
||||
_, err := exec.Execute(context.Background(), "agent-a",
|
||||
"UPDATE messages SET body = 'hacked'")
|
||||
if err == nil {
|
||||
t.Fatal("expected UPDATE to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidationRejectsPragma(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
exec := New(db, slog.Default())
|
||||
_, err := exec.Execute(context.Background(), "agent-a",
|
||||
"SELECT * FROM pragma_table_info('messages')")
|
||||
if err == nil {
|
||||
t.Fatal("expected PRAGMA in SELECT to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyQuery(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
exec := New(db, slog.Default())
|
||||
_, err := exec.Execute(context.Background(), "agent-a", "")
|
||||
if err == nil {
|
||||
t.Fatal("expected empty query to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCTEQuery(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
exec := New(db, slog.Default())
|
||||
result, err := exec.Execute(context.Background(), "agent-a",
|
||||
"WITH high_prio AS (SELECT * FROM my_messages WHERE priority >= 7) SELECT id, priority FROM high_prio")
|
||||
if err != nil {
|
||||
t.Fatalf("CTE query failed: %v", err)
|
||||
}
|
||||
|
||||
// agent-a should see high-priority messages it has access to
|
||||
if result.RowCount == 0 {
|
||||
t.Error("expected at least 1 high-priority message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyResultSet(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
exec := New(db, slog.Default())
|
||||
result, err := exec.Execute(context.Background(), "agent-a",
|
||||
"SELECT * FROM my_messages WHERE body = 'nonexistent'")
|
||||
if err != nil {
|
||||
t.Fatalf("query failed: %v", err)
|
||||
}
|
||||
if result.RowCount != 0 {
|
||||
t.Errorf("expected 0 rows, got %d", result.RowCount)
|
||||
}
|
||||
if result.Rows == nil {
|
||||
t.Error("rows should be empty array, not nil")
|
||||
}
|
||||
if result.Truncated {
|
||||
t.Error("should not be truncated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLimitEnforcement(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
// Insert 150 messages to test limit
|
||||
for i := 100; i < 250; i++ {
|
||||
_, _ = db.Exec("INSERT INTO messages (id, from_agent, to_agent, body) VALUES (?, 'algis', 'agent-a', 'msg')", i)
|
||||
}
|
||||
|
||||
exec := New(db, slog.Default())
|
||||
result, err := exec.Execute(context.Background(), "agent-a",
|
||||
"SELECT id FROM my_messages")
|
||||
if err != nil {
|
||||
t.Fatalf("query failed: %v", err)
|
||||
}
|
||||
|
||||
if result.RowCount > MaxRows {
|
||||
t.Errorf("expected max %d rows, got %d", MaxRows, result.RowCount)
|
||||
}
|
||||
if !result.Truncated {
|
||||
t.Error("expected truncated=true for large result set")
|
||||
}
|
||||
}
|
||||
|
||||
func contains(s, substr string) bool {
|
||||
return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsStr(s, substr))
|
||||
}
|
||||
|
||||
func containsStr(s, sub string) bool {
|
||||
for i := 0; i <= len(s)-len(sub); i++ {
|
||||
if s[i:i+len(sub)] == sub {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
+108
-14
@@ -19,6 +19,12 @@ type AgentStore interface {
|
||||
ListAgentsByOwner(ctx context.Context, ownerID int64) ([]*Agent, error)
|
||||
SearchAgentsByCapability(ctx context.Context, query string) ([]*Agent, error)
|
||||
GetHumanAgentByOwner(ctx context.Context, ownerID int64) (*Agent, error)
|
||||
|
||||
// Reactive trigger methods
|
||||
UpdateTriggerConfig(ctx context.Context, name string, mode string, cooldown, budget, maxDepth int) error
|
||||
UpdateK8sImage(ctx context.Context, name, image, envJSON, preset string) error
|
||||
SetPendingWork(ctx context.Context, name string, pending bool) error
|
||||
ListReactiveAgents(ctx context.Context) ([]*Agent, error)
|
||||
}
|
||||
|
||||
// SQLiteAgentStore implements AgentStore using SQLite.
|
||||
@@ -37,6 +43,28 @@ func (s *SQLiteAgentStore) CreateAgent(ctx context.Context, agent *Agent) error
|
||||
caps = "{}"
|
||||
}
|
||||
|
||||
// Default trigger values
|
||||
triggerMode := agent.TriggerMode
|
||||
if triggerMode == "" {
|
||||
triggerMode = TriggerModePassive
|
||||
}
|
||||
cooldown := agent.CooldownSeconds
|
||||
if cooldown == 0 {
|
||||
cooldown = 600
|
||||
}
|
||||
budget := agent.DailyTriggerBudget
|
||||
if budget == 0 {
|
||||
budget = 8
|
||||
}
|
||||
maxDepth := agent.MaxTriggerDepth
|
||||
if maxDepth == 0 {
|
||||
maxDepth = 5
|
||||
}
|
||||
preset := agent.K8sResourcePreset
|
||||
if preset == "" {
|
||||
preset = "default"
|
||||
}
|
||||
|
||||
result, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO agents (name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`,
|
||||
@@ -51,20 +79,75 @@ func (s *SQLiteAgentStore) CreateAgent(ctx context.Context, agent *Agent) error
|
||||
}
|
||||
agent.ID = id
|
||||
agent.Status = AgentStatusActive
|
||||
agent.TriggerMode = triggerMode
|
||||
agent.CooldownSeconds = cooldown
|
||||
agent.DailyTriggerBudget = budget
|
||||
agent.MaxTriggerDepth = maxDepth
|
||||
agent.K8sResourcePreset = preset
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateTriggerConfig updates the reactive trigger configuration for an agent.
|
||||
func (s *SQLiteAgentStore) UpdateTriggerConfig(ctx context.Context, name string, mode string, cooldown, budget, maxDepth int) error {
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`UPDATE agents SET trigger_mode = ?, cooldown_seconds = ?, daily_trigger_budget = ?, max_trigger_depth = ?, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE name = ? AND status = 'active'`,
|
||||
mode, cooldown, budget, maxDepth, name,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateK8sImage updates the K8s container image and env config for an agent.
|
||||
func (s *SQLiteAgentStore) UpdateK8sImage(ctx context.Context, name, image, envJSON, preset string) error {
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`UPDATE agents SET k8s_image = ?, k8s_env_json = ?, k8s_resource_preset = ?, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE name = ? AND status = 'active'`,
|
||||
image, envJSON, preset, name,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetPendingWork sets the pending_work flag for an agent.
|
||||
func (s *SQLiteAgentStore) SetPendingWork(ctx context.Context, name string, pending bool) error {
|
||||
val := 0
|
||||
if pending {
|
||||
val = 1
|
||||
}
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`UPDATE agents SET pending_work = ? WHERE name = ? AND status = 'active'`,
|
||||
val, name,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// ListReactiveAgents returns all active agents with trigger_mode='reactive'.
|
||||
func (s *SQLiteAgentStore) ListReactiveAgents(ctx context.Context) ([]*Agent, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
agentSelectSQL()+` WHERE status = 'active' AND trigger_mode = 'reactive' ORDER BY name`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return s.scanAgents(rows)
|
||||
}
|
||||
|
||||
// agentSelectSQL returns the base SELECT clause for agent queries.
|
||||
func agentSelectSQL() string {
|
||||
return `SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at,
|
||||
trigger_mode, cooldown_seconds, daily_trigger_budget, max_trigger_depth, k8s_image, k8s_env_json, k8s_resource_preset, pending_work
|
||||
FROM agents`
|
||||
}
|
||||
|
||||
func (s *SQLiteAgentStore) GetAgentByName(ctx context.Context, name string) (*Agent, error) {
|
||||
return s.scanAgent(s.db.QueryRowContext(ctx,
|
||||
`SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at
|
||||
FROM agents WHERE name = ? AND status = 'active'`, name,
|
||||
agentSelectSQL()+` WHERE name = ? AND status = 'active'`, name,
|
||||
))
|
||||
}
|
||||
|
||||
func (s *SQLiteAgentStore) GetAgentByID(ctx context.Context, id int64) (*Agent, error) {
|
||||
return s.scanAgent(s.db.QueryRowContext(ctx,
|
||||
`SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at
|
||||
FROM agents WHERE id = ? AND status = 'active'`, id,
|
||||
agentSelectSQL()+` WHERE id = ? AND status = 'active'`, id,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -103,8 +186,7 @@ func (s *SQLiteAgentStore) DeactivateAgent(ctx context.Context, name string) err
|
||||
|
||||
func (s *SQLiteAgentStore) ListActiveAgents(ctx context.Context) ([]*Agent, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at
|
||||
FROM agents WHERE status = 'active' ORDER BY name`,
|
||||
agentSelectSQL()+` WHERE status = 'active' ORDER BY name`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -115,8 +197,7 @@ func (s *SQLiteAgentStore) ListActiveAgents(ctx context.Context) ([]*Agent, erro
|
||||
|
||||
func (s *SQLiteAgentStore) ListAllActiveAgents(ctx context.Context) ([]*Agent, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at
|
||||
FROM agents WHERE status = 'active' AND type != 'human' ORDER BY name`,
|
||||
agentSelectSQL()+` WHERE status = 'active' AND type != 'human' ORDER BY name`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -127,8 +208,7 @@ func (s *SQLiteAgentStore) ListAllActiveAgents(ctx context.Context) ([]*Agent, e
|
||||
|
||||
func (s *SQLiteAgentStore) ListAgentsByOwner(ctx context.Context, ownerID int64) ([]*Agent, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at
|
||||
FROM agents WHERE owner_id = ? AND status = 'active' ORDER BY name`,
|
||||
agentSelectSQL()+` WHERE owner_id = ? AND status = 'active' ORDER BY name`,
|
||||
ownerID,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -141,8 +221,7 @@ func (s *SQLiteAgentStore) ListAgentsByOwner(ctx context.Context, ownerID int64)
|
||||
func (s *SQLiteAgentStore) SearchAgentsByCapability(ctx context.Context, query string) ([]*Agent, error) {
|
||||
// Simple LIKE search on the capabilities JSON field
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at
|
||||
FROM agents WHERE status = 'active' AND capabilities LIKE ? ORDER BY name`,
|
||||
agentSelectSQL()+` WHERE status = 'active' AND capabilities LIKE ? ORDER BY name`,
|
||||
"%"+query+"%",
|
||||
)
|
||||
if err != nil {
|
||||
@@ -154,23 +233,30 @@ func (s *SQLiteAgentStore) SearchAgentsByCapability(ctx context.Context, query s
|
||||
|
||||
func (s *SQLiteAgentStore) GetHumanAgentByOwner(ctx context.Context, ownerID int64) (*Agent, error) {
|
||||
return s.scanAgent(s.db.QueryRowContext(ctx,
|
||||
`SELECT id, name, display_name, type, capabilities, owner_id, api_key_hash, status, created_at, updated_at
|
||||
FROM agents WHERE owner_id = ? AND type = 'human' AND status = 'active' LIMIT 1`, ownerID,
|
||||
agentSelectSQL()+` WHERE owner_id = ? AND type = 'human' AND status = 'active' LIMIT 1`, ownerID,
|
||||
))
|
||||
}
|
||||
|
||||
func (s *SQLiteAgentStore) scanAgent(row *sql.Row) (*Agent, error) {
|
||||
var agent Agent
|
||||
var caps string
|
||||
var k8sImage, k8sEnvJSON sql.NullString
|
||||
var pendingWork int
|
||||
err := row.Scan(
|
||||
&agent.ID, &agent.Name, &agent.DisplayName, &agent.Type,
|
||||
&caps, &agent.OwnerID, &agent.APIKeyHash, &agent.Status,
|
||||
&agent.CreatedAt, &agent.UpdatedAt,
|
||||
&agent.TriggerMode, &agent.CooldownSeconds, &agent.DailyTriggerBudget,
|
||||
&agent.MaxTriggerDepth, &k8sImage, &k8sEnvJSON,
|
||||
&agent.K8sResourcePreset, &pendingWork,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
agent.Capabilities = json.RawMessage(caps)
|
||||
agent.K8sImage = k8sImage.String
|
||||
agent.K8sEnvJSON = k8sEnvJSON.String
|
||||
agent.PendingWork = pendingWork != 0
|
||||
return &agent, nil
|
||||
}
|
||||
|
||||
@@ -179,15 +265,23 @@ func (s *SQLiteAgentStore) scanAgents(rows *sql.Rows) ([]*Agent, error) {
|
||||
for rows.Next() {
|
||||
var agent Agent
|
||||
var caps string
|
||||
var k8sImage, k8sEnvJSON sql.NullString
|
||||
var pendingWork int
|
||||
err := rows.Scan(
|
||||
&agent.ID, &agent.Name, &agent.DisplayName, &agent.Type,
|
||||
&caps, &agent.OwnerID, &agent.APIKeyHash, &agent.Status,
|
||||
&agent.CreatedAt, &agent.UpdatedAt,
|
||||
&agent.TriggerMode, &agent.CooldownSeconds, &agent.DailyTriggerBudget,
|
||||
&agent.MaxTriggerDepth, &k8sImage, &k8sEnvJSON,
|
||||
&agent.K8sResourcePreset, &pendingWork,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
agent.Capabilities = json.RawMessage(caps)
|
||||
agent.K8sImage = k8sImage.String
|
||||
agent.K8sEnvJSON = k8sEnvJSON.String
|
||||
agent.PendingWork = pendingWork != 0
|
||||
agents = append(agents, &agent)
|
||||
}
|
||||
if agents == nil {
|
||||
|
||||
@@ -12,6 +12,13 @@ const (
|
||||
AgentStatusInactive = "inactive"
|
||||
)
|
||||
|
||||
// Trigger mode constants.
|
||||
const (
|
||||
TriggerModePassive = "passive"
|
||||
TriggerModeReactive = "reactive"
|
||||
TriggerModeDisabled = "disabled"
|
||||
)
|
||||
|
||||
// Agent represents a registered entity that can send/receive messages.
|
||||
type Agent struct {
|
||||
ID int64 `json:"id"`
|
||||
@@ -24,4 +31,14 @@ type Agent struct {
|
||||
Status string `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
// Reactive trigger fields
|
||||
TriggerMode string `json:"trigger_mode"`
|
||||
CooldownSeconds int `json:"cooldown_seconds"`
|
||||
DailyTriggerBudget int `json:"daily_trigger_budget"`
|
||||
MaxTriggerDepth int `json:"max_trigger_depth"`
|
||||
K8sImage string `json:"k8s_image,omitempty"`
|
||||
K8sEnvJSON string `json:"k8s_env_json,omitempty"`
|
||||
K8sResourcePreset string `json:"k8s_resource_preset"`
|
||||
PendingWork bool `json:"pending_work"`
|
||||
}
|
||||
|
||||
@@ -137,6 +137,8 @@ func (h *AttachmentsHandler) Upload(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, `{"error":"empty file not allowed"}`, http.StatusBadRequest)
|
||||
case attachments.ErrFileTooLarge:
|
||||
http.Error(w, `{"error":"file exceeds maximum size of 50MB"}`, http.StatusRequestEntityTooLarge)
|
||||
case attachments.ErrUnsupportedType:
|
||||
http.Error(w, `{"error":"unsupported file type: only images, PDFs, and text files are allowed"}`, http.StatusBadRequest)
|
||||
default:
|
||||
h.logger.Error("upload attachment failed", "error", err)
|
||||
http.Error(w, `{"error":"internal server error"}`, http.StatusInternalServerError)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@@ -15,10 +16,16 @@ import (
|
||||
|
||||
// ChannelsHandler handles REST API requests for channels.
|
||||
type ChannelsHandler struct {
|
||||
channelService *channels.Service
|
||||
agentService *agents.AgentService
|
||||
msgService *messaging.MessagingService
|
||||
logger *slog.Logger
|
||||
channelService *channels.Service
|
||||
agentService *agents.AgentService
|
||||
msgService *messaging.MessagingService
|
||||
reactionService ChannelReactionService
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// ChannelReactionService is the subset of reactions.Service needed by ChannelsHandler.
|
||||
type ChannelReactionService interface {
|
||||
ListByState(ctx context.Context, channelID int64, state string) ([]int64, error)
|
||||
}
|
||||
|
||||
// NewChannelsHandler creates a new channels handler.
|
||||
@@ -31,6 +38,11 @@ func NewChannelsHandler(channelService *channels.Service, agentService *agents.A
|
||||
}
|
||||
}
|
||||
|
||||
// SetReactionService sets the reaction service for workflow state queries.
|
||||
func (h *ChannelsHandler) SetReactionService(svc ChannelReactionService) {
|
||||
h.reactionService = svc
|
||||
}
|
||||
|
||||
// ListChannels handles GET /api/channels.
|
||||
func (h *ChannelsHandler) ListChannels(w http.ResponseWriter, r *http.Request) {
|
||||
ownerID, ok := OwnerIDFromContext(r.Context())
|
||||
@@ -228,6 +240,9 @@ func (h *ChannelsHandler) ChannelMessages(w http.ResponseWriter, r *http.Request
|
||||
return
|
||||
}
|
||||
|
||||
// Enrich messages with reply counts and attachments.
|
||||
h.msgService.EnrichMessages(r.Context(), paginated.Messages)
|
||||
|
||||
// Compute last_read_message_id across owned agents
|
||||
var lastReadMessageID int64
|
||||
ownedAgents, err := h.agentService.ListAgents(r.Context(), ownerID)
|
||||
@@ -295,3 +310,127 @@ func (h *ChannelsHandler) LeaveChannel(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "left"})
|
||||
}
|
||||
|
||||
// UpdateSettings handles PUT /api/channels/{name}/settings.
|
||||
func (h *ChannelsHandler) UpdateSettings(w http.ResponseWriter, r *http.Request) {
|
||||
_, ok := OwnerIDFromContext(r.Context())
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusUnauthorized, errorBody("unauthorized", "Authentication required"))
|
||||
return
|
||||
}
|
||||
|
||||
name := chi.URLParam(r, "name")
|
||||
ch, err := h.channelService.GetChannelByName(r.Context(), name)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, errorBody("not_found", "Channel not found"))
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
WorkflowEnabled *bool `json:"workflow_enabled"`
|
||||
AutoApprove *bool `json:"auto_approve"`
|
||||
StalemateRemindAfter *string `json:"stalemate_remind_after"`
|
||||
StalemateEscalateAfter *string `json:"stalemate_escalate_after"`
|
||||
PublishThreshold *float64 `json:"publish_threshold"`
|
||||
ApproveThreshold *float64 `json:"approve_threshold"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("invalid_request", "Invalid JSON body"))
|
||||
return
|
||||
}
|
||||
|
||||
settings := channels.ChannelSettings{
|
||||
WorkflowEnabled: ch.WorkflowEnabled,
|
||||
AutoApprove: ch.AutoApprove,
|
||||
StalemateRemindAfter: ch.StalemateRemindAfter,
|
||||
StalemateEscalateAfter: ch.StalemateEscalateAfter,
|
||||
PublishThreshold: ch.PublishThreshold,
|
||||
ApproveThreshold: ch.ApproveThreshold,
|
||||
}
|
||||
|
||||
if req.WorkflowEnabled != nil {
|
||||
settings.WorkflowEnabled = *req.WorkflowEnabled
|
||||
}
|
||||
if req.AutoApprove != nil {
|
||||
settings.AutoApprove = *req.AutoApprove
|
||||
}
|
||||
if req.StalemateRemindAfter != nil {
|
||||
settings.StalemateRemindAfter = *req.StalemateRemindAfter
|
||||
}
|
||||
if req.StalemateEscalateAfter != nil {
|
||||
settings.StalemateEscalateAfter = *req.StalemateEscalateAfter
|
||||
}
|
||||
if req.PublishThreshold != nil {
|
||||
settings.PublishThreshold = *req.PublishThreshold
|
||||
}
|
||||
if req.ApproveThreshold != nil {
|
||||
settings.ApproveThreshold = *req.ApproveThreshold
|
||||
}
|
||||
|
||||
updated, err := h.channelService.UpdateChannelSettings(r.Context(), ch.ID, settings)
|
||||
if err != nil {
|
||||
h.logger.Error("update channel settings failed", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody("server_error", "Failed to update channel settings"))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{"channel": updated})
|
||||
}
|
||||
|
||||
// ListByState handles GET /api/channels/{name}/messages/by-state?state=X.
|
||||
func (h *ChannelsHandler) ListByState(w http.ResponseWriter, r *http.Request) {
|
||||
_, ok := OwnerIDFromContext(r.Context())
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusUnauthorized, errorBody("unauthorized", "Authentication required"))
|
||||
return
|
||||
}
|
||||
|
||||
if h.reactionService == nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, errorBody("unavailable", "Reactions service not configured"))
|
||||
return
|
||||
}
|
||||
|
||||
name := chi.URLParam(r, "name")
|
||||
ch, err := h.channelService.GetChannelByName(r.Context(), name)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, errorBody("not_found", "Channel not found"))
|
||||
return
|
||||
}
|
||||
|
||||
state := r.URL.Query().Get("state")
|
||||
if state == "" {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("missing_state", "Query parameter 'state' is required"))
|
||||
return
|
||||
}
|
||||
|
||||
ids, err := h.reactionService.ListByState(r.Context(), ch.ID, state)
|
||||
if err != nil {
|
||||
h.logger.Error("list by state failed", "error", err)
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("invalid_state", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// Load messages by IDs
|
||||
var messages []*messaging.Message
|
||||
for _, id := range ids {
|
||||
msg, err := h.msgService.GetMessageByID(r.Context(), id)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
messages = append(messages, msg)
|
||||
}
|
||||
|
||||
if messages == nil {
|
||||
messages = []*messaging.Message{}
|
||||
}
|
||||
|
||||
// Enrich messages with reactions, reply counts, attachments
|
||||
h.msgService.EnrichMessages(r.Context(), messages)
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"messages": messages,
|
||||
"state": state,
|
||||
"total": len(messages),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -86,6 +86,8 @@ func (h *MessagesHandler) ListMessages(w http.ResponseWriter, r *http.Request) {
|
||||
allMessages = []*messaging.Message{}
|
||||
}
|
||||
|
||||
h.msgService.EnrichMessages(r.Context(), allMessages)
|
||||
|
||||
sortMessagesByTime(allMessages)
|
||||
if len(allMessages) > limit {
|
||||
allMessages = allMessages[:limit]
|
||||
@@ -122,6 +124,8 @@ func (h *MessagesHandler) GetMessage(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
h.msgService.EnrichMessages(r.Context(), []*messaging.Message{msg})
|
||||
|
||||
writeJSON(w, http.StatusOK, msg)
|
||||
}
|
||||
|
||||
@@ -230,6 +234,8 @@ func (h *MessagesHandler) GetConversation(w http.ResponseWriter, r *http.Request
|
||||
return
|
||||
}
|
||||
|
||||
h.msgService.EnrichMessages(r.Context(), messages)
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"conversation": conv,
|
||||
"messages": messages,
|
||||
@@ -245,14 +251,15 @@ func (h *MessagesHandler) SendMessage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var req struct {
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
Body string `json:"body"`
|
||||
Priority int `json:"priority"`
|
||||
ChannelID *int64 `json:"channel_id,omitempty"`
|
||||
ConversationID *int64 `json:"conversation_id,omitempty"`
|
||||
Subject string `json:"subject,omitempty"`
|
||||
ReplyTo *int64 `json:"reply_to,omitempty"`
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
Body string `json:"body"`
|
||||
Priority int `json:"priority"`
|
||||
ChannelID *int64 `json:"channel_id,omitempty"`
|
||||
ConversationID *int64 `json:"conversation_id,omitempty"`
|
||||
Subject string `json:"subject,omitempty"`
|
||||
ReplyTo *int64 `json:"reply_to,omitempty"`
|
||||
Attachments []string `json:"attachments,omitempty"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
@@ -307,6 +314,7 @@ func (h *MessagesHandler) SendMessage(w http.ResponseWriter, r *http.Request) {
|
||||
ConversationID: req.ConversationID,
|
||||
Subject: req.Subject,
|
||||
ReplyTo: req.ReplyTo,
|
||||
Attachments: req.Attachments,
|
||||
}
|
||||
|
||||
msg, err := h.msgService.SendMessage(r.Context(), req.From, req.To, req.Body, opts)
|
||||
@@ -319,6 +327,8 @@ func (h *MessagesHandler) SendMessage(w http.ResponseWriter, r *http.Request) {
|
||||
// SSE broadcast is handled by the MessageListener on the messaging
|
||||
// service, so it fires for both REST and MCP message paths.
|
||||
|
||||
h.msgService.EnrichMessages(r.Context(), []*messaging.Message{msg})
|
||||
|
||||
writeJSON(w, http.StatusCreated, msg)
|
||||
}
|
||||
|
||||
@@ -465,6 +475,8 @@ func (h *MessagesHandler) SearchMessages(w http.ResponseWriter, r *http.Request)
|
||||
allMessages = []*messaging.Message{}
|
||||
}
|
||||
|
||||
h.msgService.EnrichMessages(r.Context(), allMessages)
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"messages": allMessages,
|
||||
"query": query,
|
||||
@@ -505,6 +517,8 @@ func (h *MessagesHandler) GetReplies(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
h.msgService.EnrichMessages(r.Context(), replies)
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"replies": replies,
|
||||
"total": len(replies),
|
||||
@@ -550,6 +564,13 @@ func (h *MessagesHandler) DMMessages(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Reverse to chronological order (query returns newest first for correct LIMIT behavior)
|
||||
for i, j := 0, len(msgs)-1; i < j; i, j = i+1, j-1 {
|
||||
msgs[i], msgs[j] = msgs[j], msgs[i]
|
||||
}
|
||||
|
||||
h.msgService.EnrichMessages(r.Context(), msgs)
|
||||
|
||||
// Include last_read_message_id for the human agent's DM with the peer
|
||||
lastRead, _ := h.msgService.GetLastReadForDM(r.Context(), agentNames, peerAgent)
|
||||
|
||||
@@ -560,6 +581,64 @@ func (h *MessagesHandler) DMMessages(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
// DMPartners returns a list of agents the user has DM conversations with,
|
||||
// ordered by most recent message. Queries ALL messages (not just inbox)
|
||||
// so historical conversations always appear.
|
||||
func (h *MessagesHandler) DMPartners(w http.ResponseWriter, r *http.Request) {
|
||||
ownerID, ok := OwnerIDFromContext(r.Context())
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusUnauthorized, errorBody("unauthorized", "Authentication required"))
|
||||
return
|
||||
}
|
||||
|
||||
ownedAgents, err := h.agentService.ListAgents(r.Context(), ownerID)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody("server_error", "Failed to list agents"))
|
||||
return
|
||||
}
|
||||
|
||||
if len(ownedAgents) == 0 {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"partners": []any{}})
|
||||
return
|
||||
}
|
||||
|
||||
agentNames := make([]string, len(ownedAgents))
|
||||
for i, a := range ownedAgents {
|
||||
agentNames[i] = a.Name
|
||||
}
|
||||
|
||||
partners, err := h.msgService.GetDMPartners(r.Context(), agentNames)
|
||||
if err != nil {
|
||||
h.logger.Error("get dm partners failed", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody("server_error", "Failed to get DM partners"))
|
||||
return
|
||||
}
|
||||
|
||||
// Resolve display names
|
||||
type partnerWithDisplay struct {
|
||||
Name string `json:"name"`
|
||||
DisplayName string `json:"display_name"`
|
||||
LastMessage string `json:"last_message"`
|
||||
LastTime string `json:"last_time"`
|
||||
Unread int `json:"unread"`
|
||||
}
|
||||
result := make([]partnerWithDisplay, len(partners))
|
||||
for i, p := range partners {
|
||||
result[i] = partnerWithDisplay{
|
||||
Name: p.Name,
|
||||
DisplayName: p.Name,
|
||||
LastMessage: p.LastMessage,
|
||||
LastTime: p.LastTime,
|
||||
Unread: p.Unread,
|
||||
}
|
||||
if a, err := h.agentService.GetAgent(r.Context(), p.Name); err == nil {
|
||||
result[i].DisplayName = a.DisplayName
|
||||
}
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{"partners": result})
|
||||
}
|
||||
|
||||
func (h *MessagesHandler) isAgentOwnedBy(r *http.Request, agentName string, ownerID int64) bool {
|
||||
if agentName == "" {
|
||||
return false
|
||||
|
||||
@@ -333,7 +333,7 @@ func TestChannelMessages_IncludesLastRead(t *testing.T) {
|
||||
}
|
||||
|
||||
// Broadcast messages
|
||||
msgs, err := channelService.BroadcastMessage(ctx, ch.ID, "human-agent", "Hello channel", 5, "", nil)
|
||||
msgs, err := channelService.BroadcastMessage(ctx, ch.ID, "human-agent", "Hello channel", 5, "", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("broadcast: %v", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/agents"
|
||||
"github.com/synapbus/synapbus/internal/channels"
|
||||
"github.com/synapbus/synapbus/internal/onboarding"
|
||||
)
|
||||
|
||||
// OnboardingHandler handles REST API requests for agent onboarding.
|
||||
type OnboardingHandler struct {
|
||||
agentService *agents.AgentService
|
||||
channelService *channels.Service
|
||||
baseURL string
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// NewOnboardingHandler creates a new onboarding handler.
|
||||
func NewOnboardingHandler(agentService *agents.AgentService, channelService *channels.Service, baseURL string) *OnboardingHandler {
|
||||
return &OnboardingHandler{
|
||||
agentService: agentService,
|
||||
channelService: channelService,
|
||||
baseURL: baseURL,
|
||||
logger: slog.Default().With("component", "api.onboarding"),
|
||||
}
|
||||
}
|
||||
|
||||
// GetCLAUDEMD handles GET /api/agents/{name}/claude-md?archetype=researcher
|
||||
// Returns a rendered CLAUDE.md for the given agent and archetype.
|
||||
func (h *OnboardingHandler) GetCLAUDEMD(w http.ResponseWriter, r *http.Request) {
|
||||
agentName := chi.URLParam(r, "name")
|
||||
archetype := r.URL.Query().Get("archetype")
|
||||
if archetype == "" {
|
||||
archetype = "custom"
|
||||
}
|
||||
|
||||
// Look up the agent to get owner info
|
||||
ownerName := "owner"
|
||||
displayName := agentName
|
||||
agent, err := h.agentService.GetAgent(r.Context(), agentName)
|
||||
if err != nil {
|
||||
h.logger.Debug("agent not found, using defaults", "name", agentName, "error", err)
|
||||
} else {
|
||||
if agent.DisplayName != "" {
|
||||
displayName = agent.DisplayName
|
||||
}
|
||||
}
|
||||
|
||||
config := onboarding.GeneratorConfig{
|
||||
AgentName: displayName,
|
||||
Archetype: archetype,
|
||||
OwnerName: ownerName,
|
||||
SynapBusURL: h.baseURL,
|
||||
}
|
||||
|
||||
md, err := onboarding.GenerateCLAUDEMD(config)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("invalid_archetype", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/markdown; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(md))
|
||||
}
|
||||
|
||||
// GetMCPConfig handles GET /api/agents/{name}/mcp-config?api_key=xxx
|
||||
// Returns a JSON MCP config snippet for Claude Code settings.
|
||||
// If api_key query param is provided, uses it. Otherwise uses a placeholder.
|
||||
func (h *OnboardingHandler) GetMCPConfig(w http.ResponseWriter, r *http.Request) {
|
||||
agentName := chi.URLParam(r, "name")
|
||||
|
||||
// Verify the agent exists
|
||||
_, err := h.agentService.GetAgent(r.Context(), agentName)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, errorBody("not_found", "Agent not found: "+agentName))
|
||||
return
|
||||
}
|
||||
|
||||
apiKey := r.URL.Query().Get("api_key")
|
||||
if apiKey == "" {
|
||||
apiKey = "<YOUR_API_KEY>"
|
||||
}
|
||||
|
||||
config := onboarding.GenerateMCPConfig(h.baseURL, apiKey)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(config))
|
||||
}
|
||||
|
||||
// ListArchetypes handles GET /api/archetypes
|
||||
// Returns the list of available agent archetypes.
|
||||
func (h *OnboardingHandler) ListArchetypes(w http.ResponseWriter, r *http.Request) {
|
||||
archetypes := onboarding.ListArchetypes()
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"archetypes": archetypes,
|
||||
})
|
||||
}
|
||||
|
||||
// ListSkills handles GET /api/skills
|
||||
// Returns the list of available agent skills.
|
||||
func (h *OnboardingHandler) ListSkills(w http.ResponseWriter, r *http.Request) {
|
||||
skills, err := onboarding.ListSkills()
|
||||
if err != nil {
|
||||
h.logger.Error("failed to list skills", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody("server_error", "Failed to list skills"))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"skills": skills,
|
||||
})
|
||||
}
|
||||
|
||||
// GetSkill handles GET /api/skills/{name}
|
||||
// Returns the markdown content of a skill.
|
||||
func (h *OnboardingHandler) GetSkill(w http.ResponseWriter, r *http.Request) {
|
||||
name := chi.URLParam(r, "name")
|
||||
|
||||
content, err := onboarding.GetSkill(name)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, errorBody("not_found", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/markdown; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(content))
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/agents"
|
||||
"github.com/synapbus/synapbus/internal/auth"
|
||||
"github.com/synapbus/synapbus/internal/messaging"
|
||||
"github.com/synapbus/synapbus/internal/reactions"
|
||||
)
|
||||
|
||||
// ReactionsHandler handles REST API requests for message reactions.
|
||||
type ReactionsHandler struct {
|
||||
reactionService *reactions.Service
|
||||
msgService *messaging.MessagingService
|
||||
agentService *agents.AgentService
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// NewReactionsHandler creates a new reactions handler.
|
||||
func NewReactionsHandler(reactionService *reactions.Service, msgService *messaging.MessagingService, agentService *agents.AgentService) *ReactionsHandler {
|
||||
return &ReactionsHandler{
|
||||
reactionService: reactionService,
|
||||
msgService: msgService,
|
||||
agentService: agentService,
|
||||
logger: slog.Default().With("component", "api.reactions"),
|
||||
}
|
||||
}
|
||||
|
||||
// Toggle handles POST /api/messages/{id}/reactions.
|
||||
func (h *ReactionsHandler) Toggle(w http.ResponseWriter, r *http.Request) {
|
||||
ownerID, ok := OwnerIDFromContext(r.Context())
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusUnauthorized, errorBody("unauthorized", "Authentication required"))
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("invalid_id", "Invalid message ID"))
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Reaction string `json:"reaction"`
|
||||
Metadata json.RawMessage `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("invalid_request", "Invalid JSON body"))
|
||||
return
|
||||
}
|
||||
|
||||
if req.Reaction == "" {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("validation_error", "Reaction type is required"))
|
||||
return
|
||||
}
|
||||
|
||||
// Verify the message exists
|
||||
msg, err := h.msgService.GetMessageByID(r.Context(), id)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, errorBody("not_found", "Message not found"))
|
||||
return
|
||||
}
|
||||
|
||||
// Determine the acting agent name from the session
|
||||
agentName, err := h.resolveAgentName(r, ownerID, msg)
|
||||
if err != nil {
|
||||
h.logger.Error("resolve agent name failed", "error", err)
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("no_agent", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.reactionService.Toggle(r.Context(), id, agentName, req.Reaction, req.Metadata)
|
||||
if err != nil {
|
||||
if err == reactions.ErrInvalidReaction {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("invalid_reaction", err.Error()))
|
||||
return
|
||||
}
|
||||
if err == reactions.ErrReactionLimit {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("reaction_limit", err.Error()))
|
||||
return
|
||||
}
|
||||
h.logger.Error("toggle reaction failed", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody("server_error", "Failed to toggle reaction"))
|
||||
return
|
||||
}
|
||||
|
||||
// Reload reactions and workflow state for the response
|
||||
rxs, state, err := h.reactionService.GetReactions(r.Context(), id)
|
||||
if err != nil {
|
||||
h.logger.Error("get reactions failed", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody("server_error", "Failed to get reactions"))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"action": result.Action,
|
||||
"reaction": result.Reaction,
|
||||
"reactions": rxs,
|
||||
"workflow_state": state,
|
||||
})
|
||||
}
|
||||
|
||||
// GetReactions handles GET /api/messages/{id}/reactions.
|
||||
func (h *ReactionsHandler) GetReactions(w http.ResponseWriter, r *http.Request) {
|
||||
_, ok := OwnerIDFromContext(r.Context())
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusUnauthorized, errorBody("unauthorized", "Authentication required"))
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("invalid_id", "Invalid message ID"))
|
||||
return
|
||||
}
|
||||
|
||||
// Verify the message exists
|
||||
_, err = h.msgService.GetMessageByID(r.Context(), id)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, errorBody("not_found", "Message not found"))
|
||||
return
|
||||
}
|
||||
|
||||
rxs, state, err := h.reactionService.GetReactions(r.Context(), id)
|
||||
if err != nil {
|
||||
h.logger.Error("get reactions failed", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody("server_error", "Failed to get reactions"))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"reactions": rxs,
|
||||
"workflow_state": state,
|
||||
})
|
||||
}
|
||||
|
||||
// Remove handles DELETE /api/messages/{id}/reactions/{reaction}.
|
||||
func (h *ReactionsHandler) Remove(w http.ResponseWriter, r *http.Request) {
|
||||
ownerID, ok := OwnerIDFromContext(r.Context())
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusUnauthorized, errorBody("unauthorized", "Authentication required"))
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("invalid_id", "Invalid message ID"))
|
||||
return
|
||||
}
|
||||
|
||||
reactionType := chi.URLParam(r, "reaction")
|
||||
if reactionType == "" {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("validation_error", "Reaction type is required"))
|
||||
return
|
||||
}
|
||||
|
||||
// Verify the message exists
|
||||
msg, err := h.msgService.GetMessageByID(r.Context(), id)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, errorBody("not_found", "Message not found"))
|
||||
return
|
||||
}
|
||||
|
||||
// Determine the acting agent name
|
||||
agentName, err := h.resolveAgentName(r, ownerID, msg)
|
||||
if err != nil {
|
||||
h.logger.Error("resolve agent name failed", "error", err)
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("no_agent", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.reactionService.Remove(r.Context(), id, agentName, reactionType); err != nil {
|
||||
if err == reactions.ErrInvalidReaction {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("invalid_reaction", err.Error()))
|
||||
return
|
||||
}
|
||||
h.logger.Error("remove reaction failed", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody("server_error", "Failed to remove reaction"))
|
||||
return
|
||||
}
|
||||
|
||||
// Reload reactions and workflow state for the response
|
||||
rxs, state, err := h.reactionService.GetReactions(r.Context(), id)
|
||||
if err != nil {
|
||||
h.logger.Error("get reactions failed", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody("server_error", "Failed to get reactions"))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"status": "removed",
|
||||
"reactions": rxs,
|
||||
"workflow_state": state,
|
||||
})
|
||||
}
|
||||
|
||||
// resolveAgentName determines the agent name for the current session user.
|
||||
// For session-authenticated users (Web UI), it returns the human agent.
|
||||
// For API key / OAuth, it falls back to the first owned agent.
|
||||
func (h *ReactionsHandler) resolveAgentName(r *http.Request, ownerID int64, msg *messaging.Message) (string, error) {
|
||||
if _, isSession := auth.SessionIDFromContext(r.Context()); isSession {
|
||||
humanAgent, err := h.agentService.GetHumanAgentForUser(r.Context(), ownerID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if humanAgent == nil {
|
||||
return "", fmt.Errorf("no human agent found for this user")
|
||||
}
|
||||
return humanAgent.Name, nil
|
||||
}
|
||||
|
||||
// Non-session: find an owned agent
|
||||
ownedAgents, err := h.agentService.ListAgents(r.Context(), ownerID)
|
||||
if err != nil || len(ownedAgents) == 0 {
|
||||
return "", fmt.Errorf("no agents registered")
|
||||
}
|
||||
|
||||
// Prefer human-type agent
|
||||
for _, a := range ownedAgents {
|
||||
if a.Type == "human" {
|
||||
return a.Name, nil
|
||||
}
|
||||
}
|
||||
return ownedAgents[0].Name, nil
|
||||
}
|
||||
@@ -12,8 +12,11 @@ import (
|
||||
"github.com/synapbus/synapbus/internal/channels"
|
||||
"github.com/synapbus/synapbus/internal/k8s"
|
||||
"github.com/synapbus/synapbus/internal/messaging"
|
||||
"github.com/synapbus/synapbus/internal/reactor"
|
||||
"github.com/synapbus/synapbus/internal/push"
|
||||
"github.com/synapbus/synapbus/internal/reactions"
|
||||
"github.com/synapbus/synapbus/internal/trace"
|
||||
"github.com/synapbus/synapbus/internal/trust"
|
||||
"github.com/synapbus/synapbus/internal/webhooks"
|
||||
)
|
||||
|
||||
@@ -32,12 +35,17 @@ type RouterConfig struct {
|
||||
WebhookStore webhooks.WebhookStore
|
||||
K8sService *k8s.K8sService
|
||||
K8sStore k8s.K8sStore
|
||||
ReactionService *reactions.Service
|
||||
PushService *push.Service
|
||||
TrustService *trust.Service
|
||||
ReactorStore *reactor.Store
|
||||
ReactorEngine *reactor.Reactor
|
||||
SSEHub *SSEHub
|
||||
Broadcaster *SSEBroadcaster
|
||||
SessionMiddleware func(http.Handler) http.Handler
|
||||
DB *sql.DB
|
||||
Version string
|
||||
BaseURL string
|
||||
}
|
||||
|
||||
// NewRouter creates a chi router with all API routes configured.
|
||||
@@ -123,6 +131,7 @@ func NewRouterWithConfig(cfg RouterConfig) chi.Router {
|
||||
r.Delete("/api/agents/{name}", agentsHandler.DeleteAgent)
|
||||
r.Post("/api/agents/{name}/revoke-key", agentsHandler.RevokeKey)
|
||||
r.Get("/api/agents/{name}/messages", messagesHandler.DMMessages)
|
||||
r.Get("/api/dm/partners", messagesHandler.DMPartners)
|
||||
|
||||
// Notifications
|
||||
r.Get("/api/notifications/unread", notificationsHandler.UnreadCounts)
|
||||
@@ -142,9 +151,24 @@ func NewRouterWithConfig(cfg RouterConfig) chi.Router {
|
||||
})
|
||||
}
|
||||
|
||||
// Reactions
|
||||
if cfg.ReactionService != nil {
|
||||
reactionsHandler := NewReactionsHandler(cfg.ReactionService, cfg.MsgService, cfg.AgentService)
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(authMiddleware)
|
||||
|
||||
r.Post("/api/messages/{id}/reactions", reactionsHandler.Toggle)
|
||||
r.Get("/api/messages/{id}/reactions", reactionsHandler.GetReactions)
|
||||
r.Delete("/api/messages/{id}/reactions/{reaction}", reactionsHandler.Remove)
|
||||
})
|
||||
}
|
||||
|
||||
// Channels
|
||||
if cfg.ChannelService != nil {
|
||||
channelsHandler := NewChannelsHandler(cfg.ChannelService, cfg.AgentService, cfg.MsgService)
|
||||
if cfg.ReactionService != nil {
|
||||
channelsHandler.SetReactionService(cfg.ReactionService)
|
||||
}
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(authMiddleware)
|
||||
|
||||
@@ -152,6 +176,8 @@ func NewRouterWithConfig(cfg RouterConfig) chi.Router {
|
||||
r.Get("/api/channels/{name}", channelsHandler.GetChannel)
|
||||
r.Post("/api/channels", channelsHandler.CreateChannel)
|
||||
r.Get("/api/channels/{name}/messages", channelsHandler.ChannelMessages)
|
||||
r.Get("/api/channels/{name}/messages/by-state", channelsHandler.ListByState)
|
||||
r.Put("/api/channels/{name}/settings", channelsHandler.UpdateSettings)
|
||||
r.Post("/api/channels/{name}/join", channelsHandler.JoinChannel)
|
||||
r.Post("/api/channels/{name}/leave", channelsHandler.LeaveChannel)
|
||||
})
|
||||
@@ -216,6 +242,46 @@ func NewRouterWithConfig(cfg RouterConfig) chi.Router {
|
||||
}
|
||||
}
|
||||
|
||||
// Reactive Runs
|
||||
if cfg.ReactorStore != nil && cfg.ReactorEngine != nil && cfg.AgentService != nil {
|
||||
runsHandler := NewRunsHandler(cfg.ReactorStore, cfg.ReactorEngine, agents.NewSQLiteAgentStore(cfg.DB))
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(authMiddleware)
|
||||
|
||||
r.Get("/api/runs", runsHandler.ListRuns)
|
||||
r.Get("/api/runs/{id}", runsHandler.GetRun)
|
||||
r.Post("/api/runs/{id}/retry", runsHandler.RetryRun)
|
||||
r.Get("/api/agents/reactive", runsHandler.ReactiveAgents)
|
||||
})
|
||||
}
|
||||
|
||||
// Trust Scores
|
||||
if cfg.TrustService != nil {
|
||||
trustHandler := NewTrustHandler(cfg.TrustService)
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(authMiddleware)
|
||||
|
||||
r.Get("/api/trust/{name}", trustHandler.GetScores)
|
||||
})
|
||||
}
|
||||
|
||||
// Onboarding (CLAUDE.md generator, MCP config, archetypes, skills)
|
||||
if cfg.AgentService != nil {
|
||||
onboardingHandler := NewOnboardingHandler(cfg.AgentService, cfg.ChannelService, cfg.BaseURL)
|
||||
|
||||
// Unauthenticated: archetypes list, skills list, skill content
|
||||
r.Get("/api/archetypes", onboardingHandler.ListArchetypes)
|
||||
r.Get("/api/skills", onboardingHandler.ListSkills)
|
||||
r.Get("/api/skills/{name}", onboardingHandler.GetSkill)
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(authMiddleware)
|
||||
|
||||
r.Get("/api/agents/{name}/claude-md", onboardingHandler.GetCLAUDEMD)
|
||||
r.Get("/api/agents/{name}/mcp-config", onboardingHandler.GetMCPConfig)
|
||||
})
|
||||
}
|
||||
|
||||
// Analytics (authenticated, requires DB)
|
||||
if cfg.DB != nil {
|
||||
analyticsHandler := NewAnalyticsHandler(cfg.DB, cfg.AgentService, cfg.ChannelService)
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/agents"
|
||||
"github.com/synapbus/synapbus/internal/reactor"
|
||||
)
|
||||
|
||||
// RunsHandler handles REST API requests for reactive runs.
|
||||
type RunsHandler struct {
|
||||
store *reactor.Store
|
||||
reactor *reactor.Reactor
|
||||
agentStore agents.AgentStore
|
||||
}
|
||||
|
||||
// NewRunsHandler creates a new runs handler.
|
||||
func NewRunsHandler(store *reactor.Store, r *reactor.Reactor, agentStore agents.AgentStore) *RunsHandler {
|
||||
return &RunsHandler{
|
||||
store: store,
|
||||
reactor: r,
|
||||
agentStore: agentStore,
|
||||
}
|
||||
}
|
||||
|
||||
// ListRuns returns reactive runs with optional filters.
|
||||
func (h *RunsHandler) ListRuns(w http.ResponseWriter, r *http.Request) {
|
||||
agentName := r.URL.Query().Get("agent")
|
||||
status := r.URL.Query().Get("status")
|
||||
limit := 50
|
||||
offset := 0
|
||||
|
||||
if l := r.URL.Query().Get("limit"); l != "" {
|
||||
if v, err := strconv.Atoi(l); err == nil && v > 0 && v <= 200 {
|
||||
limit = v
|
||||
}
|
||||
}
|
||||
if o := r.URL.Query().Get("offset"); o != "" {
|
||||
if v, err := strconv.Atoi(o); err == nil && v >= 0 {
|
||||
offset = v
|
||||
}
|
||||
}
|
||||
|
||||
runs, total, err := h.store.ListRuns(r.Context(), agentName, status, limit, offset)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody("internal_error", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"runs": runs,
|
||||
"total": total,
|
||||
})
|
||||
}
|
||||
|
||||
// GetRun returns a single run by ID.
|
||||
func (h *RunsHandler) GetRun(w http.ResponseWriter, r *http.Request) {
|
||||
idStr := chi.URLParam(r, "id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("bad_request", "invalid run ID"))
|
||||
return
|
||||
}
|
||||
|
||||
run, err := h.store.GetRunByID(r.Context(), id)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, errorBody("not_found", "run not found"))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, run)
|
||||
}
|
||||
|
||||
// RetryRun retries a failed run.
|
||||
func (h *RunsHandler) RetryRun(w http.ResponseWriter, r *http.Request) {
|
||||
idStr := chi.URLParam(r, "id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("bad_request", "invalid run ID"))
|
||||
return
|
||||
}
|
||||
|
||||
newRun, err := h.reactor.RetryRun(r.Context(), id)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("retry_failed", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"new_run_id": newRun.ID,
|
||||
"status": newRun.Status,
|
||||
})
|
||||
}
|
||||
|
||||
// ReactiveAgents returns agents with reactive trigger config and current status.
|
||||
func (h *RunsHandler) ReactiveAgents(w http.ResponseWriter, r *http.Request) {
|
||||
agentsList, err := h.agentStore.ListReactiveAgents(r.Context())
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody("internal_error", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
type agentStatus struct {
|
||||
Name string `json:"name"`
|
||||
TriggerMode string `json:"trigger_mode"`
|
||||
CooldownSeconds int `json:"cooldown_seconds"`
|
||||
DailyTriggerBudget int `json:"daily_trigger_budget"`
|
||||
MaxTriggerDepth int `json:"max_trigger_depth"`
|
||||
K8sImage string `json:"k8s_image"`
|
||||
PendingWork bool `json:"pending_work"`
|
||||
State string `json:"state"`
|
||||
TodayRuns int `json:"today_runs"`
|
||||
CooldownUntil *string `json:"cooldown_until"`
|
||||
}
|
||||
|
||||
result := make([]agentStatus, 0, len(agentsList))
|
||||
for _, a := range agentsList {
|
||||
as := agentStatus{
|
||||
Name: a.Name,
|
||||
TriggerMode: a.TriggerMode,
|
||||
CooldownSeconds: a.CooldownSeconds,
|
||||
DailyTriggerBudget: a.DailyTriggerBudget,
|
||||
MaxTriggerDepth: a.MaxTriggerDepth,
|
||||
K8sImage: a.K8sImage,
|
||||
PendingWork: a.PendingWork,
|
||||
}
|
||||
|
||||
// Compute state
|
||||
todayCount, _ := h.store.CountTodayRuns(r.Context(), a.Name)
|
||||
as.TodayRuns = todayCount
|
||||
|
||||
running, _ := h.store.IsAgentRunning(r.Context(), a.Name)
|
||||
if running {
|
||||
as.State = "running"
|
||||
} else if a.PendingWork {
|
||||
as.State = "queued"
|
||||
} else if todayCount >= a.DailyTriggerBudget {
|
||||
as.State = "budget_exhausted"
|
||||
} else {
|
||||
lastRun, _ := h.store.GetLastRunTime(r.Context(), a.Name)
|
||||
if lastRun != nil {
|
||||
cooldownEnd := lastRun.Add(time.Duration(a.CooldownSeconds) * time.Second)
|
||||
if time.Now().Before(cooldownEnd) {
|
||||
as.State = "cooldown"
|
||||
t := cooldownEnd.UTC().Format(time.RFC3339)
|
||||
as.CooldownUntil = &t
|
||||
} else {
|
||||
as.State = "idle"
|
||||
}
|
||||
} else {
|
||||
as.State = "idle"
|
||||
}
|
||||
}
|
||||
|
||||
result = append(result, as)
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"agents": result,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/trust"
|
||||
)
|
||||
|
||||
// TrustHandler handles REST API requests for agent trust scores.
|
||||
type TrustHandler struct {
|
||||
trustService *trust.Service
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// NewTrustHandler creates a new trust handler.
|
||||
func NewTrustHandler(trustService *trust.Service) *TrustHandler {
|
||||
return &TrustHandler{
|
||||
trustService: trustService,
|
||||
logger: slog.Default().With("component", "api.trust"),
|
||||
}
|
||||
}
|
||||
|
||||
// GetScores handles GET /api/trust/{name}.
|
||||
func (h *TrustHandler) GetScores(w http.ResponseWriter, r *http.Request) {
|
||||
agentName := chi.URLParam(r, "name")
|
||||
if agentName == "" {
|
||||
writeJSON(w, http.StatusBadRequest, errorBody("invalid_name", "Agent name is required"))
|
||||
return
|
||||
}
|
||||
|
||||
scores, err := h.trustService.GetScores(r.Context(), agentName)
|
||||
if err != nil {
|
||||
h.logger.Error("failed to get trust scores", "agent", agentName, "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, errorBody("internal", "Failed to get trust scores"))
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"scores": scores,
|
||||
})
|
||||
}
|
||||
@@ -118,3 +118,9 @@ func DefaultFilename(mimeType string) string {
|
||||
func IsImageType(mimeType string) bool {
|
||||
return imageTypes[mimeType]
|
||||
}
|
||||
|
||||
// IsAllowedType returns true for all MIME types. Any file type is allowed;
|
||||
// only size is restricted (50 MB max).
|
||||
func IsAllowedType(mimeType string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -154,6 +154,35 @@ func TestIsImageType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAllowedType(t *testing.T) {
|
||||
tests := []struct {
|
||||
mimeType string
|
||||
want bool
|
||||
}{
|
||||
{"image/png", true},
|
||||
{"image/jpeg", true},
|
||||
{"image/gif", true},
|
||||
{"application/pdf", true},
|
||||
{"text/plain", true},
|
||||
{"text/csv", true},
|
||||
{"text/plain; charset=utf-8", true},
|
||||
{"application/json", true},
|
||||
{"application/octet-stream", true},
|
||||
{"application/zip", true},
|
||||
{"application/x-executable", true},
|
||||
{"video/mp4", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.mimeType, func(t *testing.T) {
|
||||
got := IsAllowedType(tt.mimeType)
|
||||
if got != tt.want {
|
||||
t.Errorf("IsAllowedType(%q) = %v, want %v", tt.mimeType, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
|
||||
@@ -11,10 +11,11 @@ const MaxFileSize = 50 * 1024 * 1024 // 50 MB
|
||||
|
||||
// Sentinel errors.
|
||||
var (
|
||||
ErrNotFound = errors.New("attachment not found")
|
||||
ErrFileTooLarge = errors.New("file exceeds maximum size of 50MB")
|
||||
ErrEmptyFile = errors.New("empty file not allowed")
|
||||
ErrFileMissing = errors.New("attachment file missing from disk")
|
||||
ErrNotFound = errors.New("attachment not found")
|
||||
ErrFileTooLarge = errors.New("file exceeds maximum size of 50MB")
|
||||
ErrEmptyFile = errors.New("empty file not allowed")
|
||||
ErrFileMissing = errors.New("attachment file missing from disk")
|
||||
ErrUnsupportedType = errors.New("unsupported file type: only images (jpg, png, gif, webp, svg), PDFs, and text files are allowed")
|
||||
)
|
||||
|
||||
// Attachment represents the metadata for a stored file.
|
||||
|
||||
@@ -202,6 +202,80 @@ func TestService_Dedup(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_Upload_FileTypeValidation(t *testing.T) {
|
||||
// PNG magic bytes.
|
||||
pngContent := []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00}
|
||||
// PDF magic bytes.
|
||||
pdfContent := []byte("%PDF-1.4 some pdf content here")
|
||||
// Plain text content.
|
||||
textContent := []byte("just some plain text content")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
content []byte
|
||||
filename string
|
||||
mimeType string
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "valid image upload",
|
||||
content: pngContent,
|
||||
filename: "photo.png",
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "valid PDF upload",
|
||||
content: pdfContent,
|
||||
filename: "report.pdf",
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "valid text file upload",
|
||||
content: textContent,
|
||||
filename: "notes.txt",
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "zip upload allowed",
|
||||
content: []byte("not real zip content"),
|
||||
filename: "archive.zip",
|
||||
mimeType: "application/zip",
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "executable upload allowed",
|
||||
content: []byte{0x7f, 0x45, 0x4c, 0x46},
|
||||
filename: "program.exe",
|
||||
mimeType: "application/x-executable",
|
||||
wantErr: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := svc.Upload(ctx, UploadRequest{
|
||||
Content: bytes.NewReader(tt.content),
|
||||
Filename: tt.filename,
|
||||
MIMEType: tt.mimeType,
|
||||
UploadedBy: "agent-a",
|
||||
})
|
||||
|
||||
if tt.wantErr != nil {
|
||||
if err != tt.wantErr {
|
||||
t.Errorf("expected error %v, got %v", tt.wantErr, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("Upload: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestService_GarbageCollect(t *testing.T) {
|
||||
svc, db := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ory/fosite"
|
||||
@@ -36,6 +37,63 @@ type Handlers struct {
|
||||
config Config
|
||||
agentLister AgentLister
|
||||
logger *slog.Logger
|
||||
loginLimiter *loginRateLimiter
|
||||
}
|
||||
|
||||
// loginRateLimiter tracks failed login attempts per IP.
|
||||
type loginRateLimiter struct {
|
||||
mu sync.Mutex
|
||||
attempts map[string]*loginAttempt
|
||||
}
|
||||
|
||||
type loginAttempt struct {
|
||||
failures int
|
||||
blockedAt time.Time
|
||||
}
|
||||
|
||||
const (
|
||||
maxLoginFailures = 3
|
||||
loginBlockTime = 1 * time.Minute
|
||||
)
|
||||
|
||||
func newLoginRateLimiter() *loginRateLimiter {
|
||||
return &loginRateLimiter{attempts: make(map[string]*loginAttempt)}
|
||||
}
|
||||
|
||||
func (l *loginRateLimiter) isBlocked(ip string) (bool, time.Duration) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
a, ok := l.attempts[ip]
|
||||
if !ok {
|
||||
return false, 0
|
||||
}
|
||||
if a.failures >= maxLoginFailures && time.Since(a.blockedAt) < loginBlockTime {
|
||||
remaining := loginBlockTime - time.Since(a.blockedAt)
|
||||
return true, remaining
|
||||
}
|
||||
if time.Since(a.blockedAt) >= loginBlockTime {
|
||||
delete(l.attempts, ip)
|
||||
return false, 0
|
||||
}
|
||||
return false, 0
|
||||
}
|
||||
|
||||
func (l *loginRateLimiter) recordFailure(ip string) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
a, ok := l.attempts[ip]
|
||||
if !ok {
|
||||
a = &loginAttempt{}
|
||||
l.attempts[ip] = a
|
||||
}
|
||||
a.failures++
|
||||
a.blockedAt = time.Now()
|
||||
}
|
||||
|
||||
func (l *loginRateLimiter) clearFailures(ip string) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
delete(l.attempts, ip)
|
||||
}
|
||||
|
||||
// NewHandlers creates a new set of auth HTTP handlers.
|
||||
@@ -53,6 +111,7 @@ func NewHandlers(
|
||||
provider: provider,
|
||||
config: config,
|
||||
logger: slog.Default().With("component", "auth"),
|
||||
loginLimiter: newLoginRateLimiter(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +175,20 @@ func (h *Handlers) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Brute-force protection: block IP after 3 failed attempts for 1 minute
|
||||
ip := remoteIP(r)
|
||||
if blocked, remaining := h.loginLimiter.isBlocked(ip); blocked {
|
||||
secs := int(remaining.Seconds()) + 1
|
||||
LogAuthEvent(r.Context(), h.logger, AuthEvent{
|
||||
Type: EventLoginFailure,
|
||||
Username: "(rate-limited)",
|
||||
RemoteIP: ip,
|
||||
})
|
||||
writeError(w, http.StatusTooManyRequests, "rate_limited",
|
||||
fmt.Sprintf("Too many login attempts. Try again in %d seconds.", secs))
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
@@ -128,15 +201,19 @@ func (h *Handlers) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
user, err := h.userStore.VerifyPassword(r.Context(), req.Username, req.Password)
|
||||
if err != nil {
|
||||
h.loginLimiter.recordFailure(ip)
|
||||
LogAuthEvent(r.Context(), h.logger, AuthEvent{
|
||||
Type: EventLoginFailure,
|
||||
Username: req.Username,
|
||||
RemoteIP: remoteIP(r),
|
||||
RemoteIP: ip,
|
||||
})
|
||||
writeError(w, http.StatusUnauthorized, "invalid_credentials", "Invalid username or password")
|
||||
return
|
||||
}
|
||||
|
||||
// Successful login — clear rate limit
|
||||
h.loginLimiter.clearFailures(ip)
|
||||
|
||||
session, err := h.sessionStore.CreateSession(r.Context(), user.ID, h.config.SessionLifetime)
|
||||
if err != nil {
|
||||
h.logger.Error("create session failed", "error", err)
|
||||
|
||||
@@ -459,13 +459,38 @@ func (s *Service) UpdateChannel(ctx context.Context, channelID int64, req Update
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
// UpdateChannelSettings updates the workflow-related settings for a channel.
|
||||
func (s *Service) UpdateChannelSettings(ctx context.Context, channelID int64, settings ChannelSettings) (*Channel, error) {
|
||||
store, ok := s.store.(*SQLiteChannelStore)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("channel store does not support settings update")
|
||||
}
|
||||
|
||||
if err := store.UpdateChannelSettings(ctx, channelID, settings); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Reload channel to return updated state
|
||||
ch, err := s.store.GetChannel(ctx, channelID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.logger.Info("channel settings updated",
|
||||
"channel_id", channelID,
|
||||
"auto_approve", settings.AutoApprove,
|
||||
)
|
||||
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
// BroadcastMessage sends a message to a channel. It creates a single channel
|
||||
// message (visible in the channel timeline via GetChannelMessages) and also
|
||||
// delivers individual DM notifications to each member's inbox.
|
||||
// If the message body contains @mentions, mentioned members receive a
|
||||
// "mention":true flag in their inbox notification metadata, and the channel
|
||||
// message metadata includes "mentioned_agents".
|
||||
func (s *Service) BroadcastMessage(ctx context.Context, channelID int64, fromAgent, body string, priority int, metadata string, replyTo *int64) ([]*messaging.Message, error) {
|
||||
func (s *Service) BroadcastMessage(ctx context.Context, channelID int64, fromAgent, body string, priority int, metadata string, replyTo *int64, attachments []string) ([]*messaging.Message, error) {
|
||||
ch, err := s.store.GetChannel(ctx, channelID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -522,11 +547,12 @@ func (s *Service) BroadcastMessage(ctx context.Context, channelID int64, fromAge
|
||||
channelMetaBytes, _ := json.Marshal(channelMetaObj)
|
||||
|
||||
channelMsg, err := s.msgService.SendMessage(ctx, fromAgent, "", body, messaging.SendOptions{
|
||||
Subject: fmt.Sprintf("channel:%s", ch.Name),
|
||||
Priority: priority,
|
||||
Metadata: string(channelMetaBytes),
|
||||
ChannelID: &channelID,
|
||||
ReplyTo: replyTo,
|
||||
Subject: fmt.Sprintf("channel:%s", ch.Name),
|
||||
Priority: priority,
|
||||
Metadata: string(channelMetaBytes),
|
||||
ChannelID: &channelID,
|
||||
ReplyTo: replyTo,
|
||||
Attachments: attachments,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create channel message: %w", err)
|
||||
|
||||
@@ -533,7 +533,7 @@ func TestService_BroadcastMessage(t *testing.T) {
|
||||
ch, _ := svc.CreateChannel(ctx, CreateChannelRequest{Name: "alerts", Type: TypeStandard, CreatedBy: "agent-a"})
|
||||
|
||||
t.Run("broadcast creates channel message", func(t *testing.T) {
|
||||
msgs, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "hello", 5, "", nil)
|
||||
msgs, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "hello", 5, "", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BroadcastMessage: %v", err)
|
||||
}
|
||||
@@ -568,7 +568,7 @@ func TestService_BroadcastMessage(t *testing.T) {
|
||||
t.Run("broadcast without mentions sends no DMs", func(t *testing.T) {
|
||||
svc.JoinChannel(ctx, ch.ID, "agent-b")
|
||||
svc.JoinChannel(ctx, ch.ID, "agent-c")
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "no-dm-test", 5, "", nil)
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "no-dm-test", 5, "", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BroadcastMessage: %v", err)
|
||||
}
|
||||
@@ -583,7 +583,7 @@ func TestService_BroadcastMessage(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("sender does not receive own message", func(t *testing.T) {
|
||||
svc.BroadcastMessage(ctx, ch.ID, "agent-a", "no self-message", 5, "", nil)
|
||||
svc.BroadcastMessage(ctx, ch.ID, "agent-a", "no self-message", 5, "", nil, nil)
|
||||
inboxResult, _ := svc.msgService.ReadInbox(ctx, "agent-a", messaging.ReadOptions{IncludeRead: true})
|
||||
for _, m := range inboxResult.Messages {
|
||||
if m.Body == "no self-message" {
|
||||
@@ -594,7 +594,7 @@ func TestService_BroadcastMessage(t *testing.T) {
|
||||
|
||||
t.Run("non-member auto-joins public channel on broadcast", func(t *testing.T) {
|
||||
seedAgent(t, svc.store.(*SQLiteChannelStore).db, "outsider")
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "outsider", "auto-joined", 5, "", nil)
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "outsider", "auto-joined", 5, "", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("expected auto-join for public channel, got %v", err)
|
||||
}
|
||||
@@ -605,10 +605,10 @@ func TestService_BroadcastMessage(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("broadcast with reply_to", func(t *testing.T) {
|
||||
msgs, _ := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "original", 5, "", nil)
|
||||
msgs, _ := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "original", 5, "", nil, nil)
|
||||
original := msgs[0]
|
||||
|
||||
replies, err := svc.BroadcastMessage(ctx, ch.ID, "agent-b", "reply to original", 5, "", &original.ID)
|
||||
replies, err := svc.BroadcastMessage(ctx, ch.ID, "agent-b", "reply to original", 5, "", &original.ID, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BroadcastMessage with reply_to: %v", err)
|
||||
}
|
||||
@@ -625,7 +625,7 @@ func TestService_BroadcastMessage(t *testing.T) {
|
||||
t.Fatalf("create private channel: %v", err)
|
||||
}
|
||||
seedAgent(t, svc.store.(*SQLiteChannelStore).db, "outsider2")
|
||||
_, err = svc.BroadcastMessage(ctx, privCh.ID, "outsider2", "unauthorized", 5, "", nil)
|
||||
_, err = svc.BroadcastMessage(ctx, privCh.ID, "outsider2", "unauthorized", 5, "", nil, nil)
|
||||
if !errors.Is(err, ErrNotChannelMember) {
|
||||
t.Errorf("expected ErrNotChannelMember for private channel, got %v", err)
|
||||
}
|
||||
@@ -644,7 +644,7 @@ func TestService_BroadcastMessage_Mentions(t *testing.T) {
|
||||
svc.JoinChannel(ctx, ch.ID, "agent-c")
|
||||
|
||||
t.Run("mentioned member gets mention flag in inbox", func(t *testing.T) {
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "hey @agent-b check this", 5, "", nil)
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "hey @agent-b check this", 5, "", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BroadcastMessage: %v", err)
|
||||
}
|
||||
@@ -677,7 +677,7 @@ func TestService_BroadcastMessage_Mentions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("channel message metadata includes mentioned_agents", func(t *testing.T) {
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "cc @agent-b and @agent-c", 5, "", nil)
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "cc @agent-b and @agent-c", 5, "", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BroadcastMessage: %v", err)
|
||||
}
|
||||
@@ -707,7 +707,7 @@ func TestService_BroadcastMessage_Mentions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("self-mention is excluded", func(t *testing.T) {
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "I am @agent-a and cc @agent-b", 5, "", nil)
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "I am @agent-a and cc @agent-b", 5, "", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BroadcastMessage: %v", err)
|
||||
}
|
||||
@@ -732,7 +732,7 @@ func TestService_BroadcastMessage_Mentions(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("no mentions produces no mention metadata", func(t *testing.T) {
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "just a normal message", 5, "", nil)
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "just a normal message", 5, "", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BroadcastMessage: %v", err)
|
||||
}
|
||||
@@ -752,7 +752,7 @@ func TestService_BroadcastMessage_Mentions(t *testing.T) {
|
||||
|
||||
t.Run("non-member mention is ignored", func(t *testing.T) {
|
||||
seedAgent(t, svc.store.(*SQLiteChannelStore).db, "outsider")
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "hey @outsider and @agent-b", 5, "", nil)
|
||||
_, err := svc.BroadcastMessage(ctx, ch.ID, "agent-a", "hey @outsider and @agent-b", 5, "", nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("BroadcastMessage: %v", err)
|
||||
}
|
||||
|
||||
@@ -83,9 +83,9 @@ func (s *SQLiteChannelStore) GetChannel(ctx context.Context, id int64) (*Channel
|
||||
var ch Channel
|
||||
var isPrivate, isSystem int
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, name, description, topic, type, is_private, is_system, created_by, created_at, updated_at
|
||||
`SELECT id, name, description, topic, type, is_private, is_system, created_by, workflow_enabled, auto_approve, stalemate_remind_after, stalemate_escalate_after, publish_threshold, approve_threshold, created_at, updated_at
|
||||
FROM channels WHERE id = ?`, id,
|
||||
).Scan(&ch.ID, &ch.Name, &ch.Description, &ch.Topic, &ch.Type, &isPrivate, &isSystem, &ch.CreatedBy, &ch.CreatedAt, &ch.UpdatedAt)
|
||||
).Scan(&ch.ID, &ch.Name, &ch.Description, &ch.Topic, &ch.Type, &isPrivate, &isSystem, &ch.CreatedBy, &ch.WorkflowEnabled, &ch.AutoApprove, &ch.StalemateRemindAfter, &ch.StalemateEscalateAfter, &ch.PublishThreshold, &ch.ApproveThreshold, &ch.CreatedAt, &ch.UpdatedAt)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, ErrChannelNotFound
|
||||
@@ -102,9 +102,9 @@ func (s *SQLiteChannelStore) GetChannelByName(ctx context.Context, name string)
|
||||
var ch Channel
|
||||
var isPrivate, isSystem int
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, name, description, topic, type, is_private, is_system, created_by, created_at, updated_at
|
||||
`SELECT id, name, description, topic, type, is_private, is_system, created_by, workflow_enabled, auto_approve, stalemate_remind_after, stalemate_escalate_after, publish_threshold, approve_threshold, created_at, updated_at
|
||||
FROM channels WHERE LOWER(name) = LOWER(?)`, name,
|
||||
).Scan(&ch.ID, &ch.Name, &ch.Description, &ch.Topic, &ch.Type, &isPrivate, &isSystem, &ch.CreatedBy, &ch.CreatedAt, &ch.UpdatedAt)
|
||||
).Scan(&ch.ID, &ch.Name, &ch.Description, &ch.Topic, &ch.Type, &isPrivate, &isSystem, &ch.CreatedBy, &ch.WorkflowEnabled, &ch.AutoApprove, &ch.StalemateRemindAfter, &ch.StalemateEscalateAfter, &ch.PublishThreshold, &ch.ApproveThreshold, &ch.CreatedAt, &ch.UpdatedAt)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, ErrChannelNotFound
|
||||
@@ -120,7 +120,7 @@ func (s *SQLiteChannelStore) GetChannelByName(ctx context.Context, name string)
|
||||
// is a member or has a pending invite.
|
||||
func (s *SQLiteChannelStore) ListChannels(ctx context.Context, agentName string) ([]*Channel, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT DISTINCT c.id, c.name, c.description, c.topic, c.type, c.is_private, c.is_system, c.created_by, c.created_at, c.updated_at
|
||||
`SELECT DISTINCT c.id, c.name, c.description, c.topic, c.type, c.is_private, c.is_system, c.created_by, c.workflow_enabled, c.auto_approve, c.stalemate_remind_after, c.stalemate_escalate_after, c.publish_threshold, c.approve_threshold, c.created_at, c.updated_at
|
||||
FROM channels c
|
||||
WHERE c.is_private = 0
|
||||
OR EXISTS (SELECT 1 FROM channel_members cm WHERE cm.channel_id = c.id AND cm.agent_name = ?)
|
||||
@@ -137,7 +137,7 @@ func (s *SQLiteChannelStore) ListChannels(ctx context.Context, agentName string)
|
||||
for rows.Next() {
|
||||
var ch Channel
|
||||
var isPrivate, isSystem int
|
||||
if err := rows.Scan(&ch.ID, &ch.Name, &ch.Description, &ch.Topic, &ch.Type, &isPrivate, &isSystem, &ch.CreatedBy, &ch.CreatedAt, &ch.UpdatedAt); err != nil {
|
||||
if err := rows.Scan(&ch.ID, &ch.Name, &ch.Description, &ch.Topic, &ch.Type, &isPrivate, &isSystem, &ch.CreatedBy, &ch.WorkflowEnabled, &ch.AutoApprove, &ch.StalemateRemindAfter, &ch.StalemateEscalateAfter, &ch.PublishThreshold, &ch.ApproveThreshold, &ch.CreatedAt, &ch.UpdatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scan channel: %w", err)
|
||||
}
|
||||
ch.IsPrivate = isPrivate != 0
|
||||
@@ -415,6 +415,23 @@ func (s *SQLiteChannelStore) GetChannelSummaries(ctx context.Context, agentName
|
||||
return summaries, rows.Err()
|
||||
}
|
||||
|
||||
// UpdateChannelSettings updates the workflow-related settings for a channel.
|
||||
func (s *SQLiteChannelStore) UpdateChannelSettings(ctx context.Context, id int64, settings ChannelSettings) error {
|
||||
result, err := s.db.ExecContext(ctx,
|
||||
`UPDATE channels SET workflow_enabled = ?, auto_approve = ?, stalemate_remind_after = ?, stalemate_escalate_after = ?, publish_threshold = ?, approve_threshold = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?`,
|
||||
settings.WorkflowEnabled, settings.AutoApprove, settings.StalemateRemindAfter, settings.StalemateEscalateAfter, settings.PublishThreshold, settings.ApproveThreshold, id,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update channel settings: %w", err)
|
||||
}
|
||||
rowsAffected, _ := result.RowsAffected()
|
||||
if rowsAffected == 0 {
|
||||
return ErrChannelNotFound
|
||||
}
|
||||
s.logger.Info("channel settings updated", "id", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// isUniqueConstraintError checks if an error is a SQLite unique constraint violation.
|
||||
func isUniqueConstraintError(err error) bool {
|
||||
return strings.Contains(err.Error(), "UNIQUE constraint failed")
|
||||
|
||||
+26
-10
@@ -25,16 +25,22 @@ const (
|
||||
|
||||
// Channel represents a named group communication space.
|
||||
type Channel struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Topic string `json:"topic"`
|
||||
Type string `json:"type"`
|
||||
IsPrivate bool `json:"is_private"`
|
||||
IsSystem bool `json:"is_system"`
|
||||
CreatedBy string `json:"created_by"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Topic string `json:"topic"`
|
||||
Type string `json:"type"`
|
||||
IsPrivate bool `json:"is_private"`
|
||||
IsSystem bool `json:"is_system"`
|
||||
CreatedBy string `json:"created_by"`
|
||||
WorkflowEnabled bool `json:"workflow_enabled"`
|
||||
AutoApprove bool `json:"auto_approve"`
|
||||
StalemateRemindAfter string `json:"stalemate_remind_after"`
|
||||
StalemateEscalateAfter string `json:"stalemate_escalate_after"`
|
||||
PublishThreshold float64 `json:"publish_threshold"`
|
||||
ApproveThreshold float64 `json:"approve_threshold"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// ChannelWithCount embeds Channel and adds a member count.
|
||||
@@ -107,6 +113,16 @@ type JoinChannelRequest struct {
|
||||
AgentName string `json:"agent_name"`
|
||||
}
|
||||
|
||||
// ChannelSettings holds workflow-related settings for a channel.
|
||||
type ChannelSettings struct {
|
||||
WorkflowEnabled bool `json:"workflow_enabled"`
|
||||
AutoApprove bool `json:"auto_approve"`
|
||||
StalemateRemindAfter string `json:"stalemate_remind_after"`
|
||||
StalemateEscalateAfter string `json:"stalemate_escalate_after"`
|
||||
PublishThreshold float64 `json:"publish_threshold"`
|
||||
ApproveThreshold float64 `json:"approve_threshold"`
|
||||
}
|
||||
|
||||
// InviteRequest is the input for inviting an agent to a channel.
|
||||
type InviteRequest struct {
|
||||
ChannelID int64 `json:"channel_id"`
|
||||
|
||||
+54
-3
@@ -84,6 +84,11 @@ func (r *K8sJobRunner) IsAvailable() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// GetClientset returns the kubernetes clientset for direct API access (used by reactor poller).
|
||||
func (r *K8sJobRunner) GetClientset() kubernetes.Interface {
|
||||
return r.clientset
|
||||
}
|
||||
|
||||
func (r *K8sJobRunner) GetNamespace() string {
|
||||
return r.namespace
|
||||
}
|
||||
@@ -145,14 +150,18 @@ func (r *K8sJobRunner) CreateJob(ctx context.Context, handler *K8sHandler, msg *
|
||||
RestartPolicy: corev1.RestartPolicyNever,
|
||||
Containers: []corev1.Container{
|
||||
{
|
||||
Name: "handler",
|
||||
Image: handler.Image,
|
||||
Env: envVars,
|
||||
Name: "handler",
|
||||
Image: handler.Image,
|
||||
ImagePullPolicy: corev1.PullIfNotPresent,
|
||||
Args: handler.Args,
|
||||
Env: envVars,
|
||||
VolumeMounts: buildVolumeMounts(handler.VolumeMounts),
|
||||
Resources: corev1.ResourceRequirements{
|
||||
Limits: resourceLimits,
|
||||
},
|
||||
},
|
||||
},
|
||||
Volumes: buildVolumes(handler.Volumes),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -228,6 +237,48 @@ func sanitizeJobName(name string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
// buildVolumeMounts converts our VolumeMount type to K8s VolumeMounts.
|
||||
func buildVolumeMounts(mounts []VolumeMount) []corev1.VolumeMount {
|
||||
if len(mounts) == 0 {
|
||||
return nil
|
||||
}
|
||||
var result []corev1.VolumeMount
|
||||
for _, m := range mounts {
|
||||
result = append(result, corev1.VolumeMount{
|
||||
Name: m.Name,
|
||||
MountPath: m.MountPath,
|
||||
ReadOnly: m.ReadOnly,
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// buildVolumes converts our Volume type to K8s Volumes.
|
||||
func buildVolumes(volumes []Volume) []corev1.Volume {
|
||||
if len(volumes) == 0 {
|
||||
return nil
|
||||
}
|
||||
var result []corev1.Volume
|
||||
for _, v := range volumes {
|
||||
vol := corev1.Volume{Name: v.Name}
|
||||
if v.HostPath != "" {
|
||||
hostPathType := corev1.HostPathDirectory
|
||||
vol.VolumeSource = corev1.VolumeSource{
|
||||
HostPath: &corev1.HostPathVolumeSource{
|
||||
Path: v.HostPath,
|
||||
Type: &hostPathType,
|
||||
},
|
||||
}
|
||||
} else if v.EmptyDir {
|
||||
vol.VolumeSource = corev1.VolumeSource{
|
||||
EmptyDir: &corev1.EmptyDirVolumeSource{},
|
||||
}
|
||||
}
|
||||
result = append(result, vol)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// truncateBody truncates the message body to maxLen bytes.
|
||||
func truncateBody(body string, maxLen int) string {
|
||||
if len(body) <= maxLen {
|
||||
|
||||
@@ -22,6 +22,25 @@ type K8sHandler struct {
|
||||
Status string `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
// Extended fields for reactive triggers (not persisted in k8s_handlers table)
|
||||
Args []string `json:"-"`
|
||||
VolumeMounts []VolumeMount `json:"-"`
|
||||
Volumes []Volume `json:"-"`
|
||||
}
|
||||
|
||||
// VolumeMount defines a mount point in the container.
|
||||
type VolumeMount struct {
|
||||
Name string
|
||||
MountPath string
|
||||
ReadOnly bool
|
||||
}
|
||||
|
||||
// Volume defines a volume source for the pod.
|
||||
type Volume struct {
|
||||
Name string
|
||||
HostPath string // If set, uses hostPath volume
|
||||
EmptyDir bool // If true, uses emptyDir volume
|
||||
}
|
||||
|
||||
// K8sJobRun represents a single Kubernetes job execution.
|
||||
|
||||
+380
-12
@@ -7,13 +7,18 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/agents"
|
||||
"github.com/synapbus/synapbus/internal/agentquery"
|
||||
"github.com/synapbus/synapbus/internal/attachments"
|
||||
"github.com/synapbus/synapbus/internal/channels"
|
||||
"github.com/synapbus/synapbus/internal/messaging"
|
||||
"github.com/synapbus/synapbus/internal/reactions"
|
||||
"github.com/synapbus/synapbus/internal/search"
|
||||
"github.com/synapbus/synapbus/internal/trust"
|
||||
)
|
||||
|
||||
// ServiceBridge implements jsruntime.ToolCaller, mapping action names to
|
||||
@@ -25,6 +30,9 @@ type ServiceBridge struct {
|
||||
swarmService *channels.SwarmService
|
||||
attachmentService *attachments.Service
|
||||
searchService *search.Service
|
||||
reactionService *reactions.Service
|
||||
trustService *trust.Service
|
||||
queryExecutor *agentquery.Executor
|
||||
agentName string
|
||||
}
|
||||
|
||||
@@ -36,6 +44,8 @@ func NewServiceBridge(
|
||||
swarmService *channels.SwarmService,
|
||||
attachmentService *attachments.Service,
|
||||
searchService *search.Service,
|
||||
reactionService *reactions.Service,
|
||||
trustService *trust.Service,
|
||||
agentName string,
|
||||
) *ServiceBridge {
|
||||
return &ServiceBridge{
|
||||
@@ -45,6 +55,8 @@ func NewServiceBridge(
|
||||
swarmService: swarmService,
|
||||
attachmentService: attachmentService,
|
||||
searchService: searchService,
|
||||
reactionService: reactionService,
|
||||
trustService: trustService,
|
||||
agentName: agentName,
|
||||
}
|
||||
}
|
||||
@@ -102,6 +114,28 @@ func (b *ServiceBridge) Call(ctx context.Context, actionName string, args map[st
|
||||
case "download_attachment":
|
||||
return b.callDownloadAttachment(ctx, args)
|
||||
|
||||
// --- Reactions ---
|
||||
case "react":
|
||||
return b.callReact(ctx, args)
|
||||
case "unreact":
|
||||
return b.callUnreact(ctx, args)
|
||||
case "get_reactions":
|
||||
return b.callGetReactions(ctx, args)
|
||||
case "list_by_state":
|
||||
return b.callListByState(ctx, args)
|
||||
|
||||
// --- Threads ---
|
||||
case "get_replies":
|
||||
return b.callGetReplies(ctx, args)
|
||||
|
||||
// --- Trust ---
|
||||
case "get_trust":
|
||||
return b.callGetTrust(ctx, args)
|
||||
|
||||
// --- SQL Query ---
|
||||
case "query":
|
||||
return b.callQuery(ctx, args)
|
||||
|
||||
// --- DM send (also accessible via bridge for execute tool) ---
|
||||
case "send_message":
|
||||
return b.callSendMessage(ctx, args)
|
||||
@@ -132,12 +166,34 @@ func (b *ServiceBridge) callSendMessage(ctx context.Context, args map[string]any
|
||||
replyTo = &v
|
||||
}
|
||||
|
||||
var attachmentHashes []string
|
||||
if attVal, ok := args["attachments"]; ok {
|
||||
switch v := attVal.(type) {
|
||||
case string:
|
||||
for _, h := range strings.Split(v, ",") {
|
||||
h = strings.TrimSpace(h)
|
||||
if h != "" {
|
||||
attachmentHashes = append(attachmentHashes, h)
|
||||
}
|
||||
}
|
||||
case []any:
|
||||
for _, item := range v {
|
||||
if s, ok := item.(string); ok && s != "" {
|
||||
attachmentHashes = append(attachmentHashes, s)
|
||||
}
|
||||
}
|
||||
case []string:
|
||||
attachmentHashes = v
|
||||
}
|
||||
}
|
||||
|
||||
opts := messaging.SendOptions{
|
||||
Subject: getString(args, "subject", ""),
|
||||
Priority: getInt(args, "priority", 5),
|
||||
Metadata: getString(args, "metadata", ""),
|
||||
ChannelID: channelID,
|
||||
ReplyTo: replyTo,
|
||||
Subject: getString(args, "subject", ""),
|
||||
Priority: getInt(args, "priority", 5),
|
||||
Metadata: getString(args, "metadata", ""),
|
||||
ChannelID: channelID,
|
||||
ReplyTo: replyTo,
|
||||
Attachments: attachmentHashes,
|
||||
}
|
||||
|
||||
msg, err := b.msgService.SendMessage(ctx, b.agentName, to, body, opts)
|
||||
@@ -166,6 +222,8 @@ func (b *ServiceBridge) callReadInbox(ctx context.Context, args map[string]any)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b.msgService.EnrichMessages(ctx, page.Messages)
|
||||
|
||||
return map[string]any{
|
||||
"messages": page.Messages,
|
||||
"count": len(page.Messages),
|
||||
@@ -183,6 +241,8 @@ func (b *ServiceBridge) callClaimMessages(ctx context.Context, args map[string]a
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b.msgService.EnrichMessages(ctx, messages)
|
||||
|
||||
return map[string]any{
|
||||
"messages": messages,
|
||||
"count": len(messages),
|
||||
@@ -237,6 +297,15 @@ func (b *ServiceBridge) callSearchMessages(ctx context.Context, args map[string]
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Enrich messages with attachments
|
||||
searchMsgs := make([]*messaging.Message, 0, len(resp.Results))
|
||||
for _, r := range resp.Results {
|
||||
if r.Message != nil {
|
||||
searchMsgs = append(searchMsgs, r.Message)
|
||||
}
|
||||
}
|
||||
b.msgService.EnrichMessages(ctx, searchMsgs)
|
||||
|
||||
resultMsgs := make([]map[string]any, len(resp.Results))
|
||||
for i, r := range resp.Results {
|
||||
entry := map[string]any{
|
||||
@@ -276,6 +345,8 @@ func (b *ServiceBridge) callSearchMessages(ctx context.Context, args map[string]
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b.msgService.EnrichMessages(ctx, page.Messages)
|
||||
|
||||
return map[string]any{
|
||||
"messages": page.Messages,
|
||||
"count": len(page.Messages),
|
||||
@@ -503,15 +574,18 @@ func (b *ServiceBridge) callGetChannelMessages(ctx context.Context, args map[str
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b.msgService.EnrichMessages(ctx, page.Messages)
|
||||
|
||||
result := make([]map[string]any, len(page.Messages))
|
||||
for i, msg := range page.Messages {
|
||||
result[i] = map[string]any{
|
||||
"id": msg.ID,
|
||||
"from": msg.FromAgent,
|
||||
"body": msg.Body,
|
||||
"priority": msg.Priority,
|
||||
"status": msg.Status,
|
||||
"created_at": msg.CreatedAt,
|
||||
"id": msg.ID,
|
||||
"from": msg.FromAgent,
|
||||
"body": msg.Body,
|
||||
"priority": msg.Priority,
|
||||
"status": msg.Status,
|
||||
"created_at": msg.CreatedAt,
|
||||
"attachments": msg.Attachments,
|
||||
}
|
||||
if len(msg.Metadata) > 0 {
|
||||
result[i]["metadata"] = msg.Metadata
|
||||
@@ -554,7 +628,28 @@ func (b *ServiceBridge) callSendChannelMessage(ctx context.Context, args map[str
|
||||
}
|
||||
}
|
||||
|
||||
messages, err := b.channelService.BroadcastMessage(ctx, channelID, b.agentName, body, priority, metadata, replyTo)
|
||||
var attachmentHashes []string
|
||||
if attVal, ok := args["attachments"]; ok {
|
||||
switch v := attVal.(type) {
|
||||
case string:
|
||||
for _, h := range strings.Split(v, ",") {
|
||||
h = strings.TrimSpace(h)
|
||||
if h != "" {
|
||||
attachmentHashes = append(attachmentHashes, h)
|
||||
}
|
||||
}
|
||||
case []any:
|
||||
for _, item := range v {
|
||||
if s, ok := item.(string); ok && s != "" {
|
||||
attachmentHashes = append(attachmentHashes, s)
|
||||
}
|
||||
}
|
||||
case []string:
|
||||
attachmentHashes = v
|
||||
}
|
||||
}
|
||||
|
||||
messages, err := b.channelService.BroadcastMessage(ctx, channelID, b.agentName, body, priority, metadata, replyTo, attachmentHashes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -876,6 +971,279 @@ func (b *ServiceBridge) callDownloadAttachment(ctx context.Context, args map[str
|
||||
}, nil
|
||||
}
|
||||
|
||||
// --- Reaction implementations ---
|
||||
|
||||
func (b *ServiceBridge) callReact(ctx context.Context, args map[string]any) (any, error) {
|
||||
if b.reactionService == nil {
|
||||
return nil, fmt.Errorf("reaction service not available")
|
||||
}
|
||||
|
||||
messageID := getInt(args, "message_id", 0)
|
||||
if messageID == 0 {
|
||||
return nil, fmt.Errorf("'message_id' parameter is required")
|
||||
}
|
||||
|
||||
reaction := getString(args, "reaction", "")
|
||||
if reaction == "" {
|
||||
return nil, fmt.Errorf("'reaction' parameter is required")
|
||||
}
|
||||
|
||||
var metadata json.RawMessage
|
||||
if metaStr := getString(args, "metadata", ""); metaStr != "" {
|
||||
if !json.Valid([]byte(metaStr)) {
|
||||
return nil, fmt.Errorf("metadata must be valid JSON")
|
||||
}
|
||||
metadata = json.RawMessage(metaStr)
|
||||
}
|
||||
|
||||
result, err := b.reactionService.Toggle(ctx, int64(messageID), b.agentName, reaction, metadata)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := map[string]any{
|
||||
"action": result.Action,
|
||||
"message_id": messageID,
|
||||
"reaction": reaction,
|
||||
}
|
||||
if result.Reaction != nil {
|
||||
resp["id"] = result.Reaction.ID
|
||||
resp["created_at"] = result.Reaction.CreatedAt
|
||||
}
|
||||
|
||||
// After the toggle, get current reactions and workflow state
|
||||
rxns, state, err := b.reactionService.GetReactions(ctx, int64(messageID))
|
||||
if err != nil {
|
||||
// Non-fatal: still return the toggle result
|
||||
slog.Warn("failed to get reactions after toggle", "error", err)
|
||||
} else {
|
||||
resp["workflow_state"] = state
|
||||
resp["reactions"] = rxns
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (b *ServiceBridge) callUnreact(ctx context.Context, args map[string]any) (any, error) {
|
||||
if b.reactionService == nil {
|
||||
return nil, fmt.Errorf("reaction service not available")
|
||||
}
|
||||
|
||||
messageID := getInt(args, "message_id", 0)
|
||||
if messageID == 0 {
|
||||
return nil, fmt.Errorf("'message_id' parameter is required")
|
||||
}
|
||||
|
||||
reaction := getString(args, "reaction", "")
|
||||
if reaction == "" {
|
||||
return nil, fmt.Errorf("'reaction' parameter is required")
|
||||
}
|
||||
|
||||
if err := b.reactionService.Remove(ctx, int64(messageID), b.agentName, reaction); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"message_id": messageID,
|
||||
"reaction": reaction,
|
||||
"status": "removed",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (b *ServiceBridge) callGetReactions(ctx context.Context, args map[string]any) (any, error) {
|
||||
if b.reactionService == nil {
|
||||
return nil, fmt.Errorf("reaction service not available")
|
||||
}
|
||||
|
||||
messageID := getInt(args, "message_id", 0)
|
||||
if messageID == 0 {
|
||||
return nil, fmt.Errorf("'message_id' parameter is required")
|
||||
}
|
||||
|
||||
rxns, state, err := b.reactionService.GetReactions(ctx, int64(messageID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"reactions": rxns,
|
||||
"workflow_state": state,
|
||||
"count": len(rxns),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (b *ServiceBridge) callListByState(ctx context.Context, args map[string]any) (any, error) {
|
||||
if b.reactionService == nil {
|
||||
return nil, fmt.Errorf("reaction service not available")
|
||||
}
|
||||
if b.channelService == nil {
|
||||
return nil, fmt.Errorf("channel service not available")
|
||||
}
|
||||
|
||||
channelName := getString(args, "channel", "")
|
||||
if channelName == "" {
|
||||
return nil, fmt.Errorf("'channel' parameter is required")
|
||||
}
|
||||
|
||||
state := getString(args, "state", "")
|
||||
if state == "" {
|
||||
return nil, fmt.Errorf("'state' parameter is required")
|
||||
}
|
||||
|
||||
ch, err := b.channelService.GetChannelByName(ctx, channelName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
messageIDs, err := b.reactionService.ListByState(ctx, ch.ID, state)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if messageIDs == nil {
|
||||
messageIDs = []int64{}
|
||||
}
|
||||
|
||||
totalCount := len(messageIDs)
|
||||
|
||||
// Apply limit and offset for pagination
|
||||
limit := getInt(args, "limit", 20)
|
||||
if limit <= 0 {
|
||||
limit = 20
|
||||
}
|
||||
if limit > 100 {
|
||||
limit = 100
|
||||
}
|
||||
offset := getInt(args, "offset", 0)
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
if offset > len(messageIDs) {
|
||||
offset = len(messageIDs)
|
||||
}
|
||||
end := offset + limit
|
||||
if end > len(messageIDs) {
|
||||
end = len(messageIDs)
|
||||
}
|
||||
pageIDs := messageIDs[offset:end]
|
||||
|
||||
resp := map[string]any{
|
||||
"message_ids": pageIDs,
|
||||
"count": len(pageIDs),
|
||||
"total": totalCount,
|
||||
"channel": channelName,
|
||||
"state": state,
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
}
|
||||
|
||||
includeMessages := getBool(args, "include_messages", false)
|
||||
if includeMessages && len(pageIDs) > 0 && b.msgService != nil {
|
||||
maxBodyLen := getInt(args, "max_body_length", 500)
|
||||
if maxBodyLen <= 0 {
|
||||
maxBodyLen = 500
|
||||
}
|
||||
var msgSlice []*messaging.Message
|
||||
for _, id := range pageIDs {
|
||||
msg, err := b.msgService.GetMessageByID(ctx, id)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
msgSlice = append(msgSlice, msg)
|
||||
}
|
||||
b.msgService.EnrichMessages(ctx, msgSlice)
|
||||
|
||||
var messages []map[string]any
|
||||
for _, msg := range msgSlice {
|
||||
body := msg.Body
|
||||
if len(body) > maxBodyLen {
|
||||
body = body[:maxBodyLen] + "..."
|
||||
}
|
||||
messages = append(messages, map[string]any{
|
||||
"id": msg.ID,
|
||||
"from_agent": msg.FromAgent,
|
||||
"body": body,
|
||||
"priority": msg.Priority,
|
||||
"created_at": msg.CreatedAt,
|
||||
"reply_to": msg.ReplyTo,
|
||||
"attachments": msg.Attachments,
|
||||
})
|
||||
}
|
||||
resp["messages"] = messages
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// --- Threads ---
|
||||
|
||||
func (b *ServiceBridge) callGetReplies(ctx context.Context, args map[string]any) (any, error) {
|
||||
messageID := getInt(args, "message_id", 0)
|
||||
if messageID == 0 {
|
||||
return nil, fmt.Errorf("'message_id' parameter is required")
|
||||
}
|
||||
|
||||
replies, err := b.msgService.GetReplies(ctx, int64(messageID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Enrich with attachments
|
||||
b.msgService.EnrichMessages(ctx, replies)
|
||||
|
||||
return map[string]any{
|
||||
"message_id": messageID,
|
||||
"replies": replies,
|
||||
"count": len(replies),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// --- Trust implementations ---
|
||||
|
||||
func (b *ServiceBridge) callGetTrust(ctx context.Context, args map[string]any) (any, error) {
|
||||
if b.trustService == nil {
|
||||
return nil, fmt.Errorf("trust service not available")
|
||||
}
|
||||
|
||||
agentName := getString(args, "agent_name", "")
|
||||
if agentName == "" {
|
||||
agentName = b.agentName
|
||||
}
|
||||
|
||||
scores, err := b.trustService.GetScores(ctx, agentName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"agent_name": agentName,
|
||||
"scores": scores,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetQueryExecutor sets the SQL query executor for the bridge.
|
||||
func (b *ServiceBridge) SetQueryExecutor(exec *agentquery.Executor) {
|
||||
b.queryExecutor = exec
|
||||
}
|
||||
|
||||
func (b *ServiceBridge) callQuery(ctx context.Context, args map[string]any) (any, error) {
|
||||
if b.queryExecutor == nil {
|
||||
return nil, fmt.Errorf("SQL query not available")
|
||||
}
|
||||
|
||||
sqlStr := getString(args, "sql", "")
|
||||
if sqlStr == "" {
|
||||
return nil, fmt.Errorf("sql parameter is required")
|
||||
}
|
||||
|
||||
result, err := b.queryExecutor.Execute(ctx, b.agentName, sqlStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// --- Helpers ---
|
||||
|
||||
// resolveChannelID resolves a channel ID from either channel_id or channel_name in args.
|
||||
|
||||
+191
-1
@@ -2,6 +2,7 @@ package mcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"github.com/synapbus/synapbus/internal/agents"
|
||||
"github.com/synapbus/synapbus/internal/channels"
|
||||
"github.com/synapbus/synapbus/internal/messaging"
|
||||
"github.com/synapbus/synapbus/internal/reactions"
|
||||
"github.com/synapbus/synapbus/internal/storage"
|
||||
"github.com/synapbus/synapbus/internal/trace"
|
||||
)
|
||||
@@ -43,6 +45,8 @@ func newTestBridge(t *testing.T) (*ServiceBridge, *messaging.MessagingService, *
|
||||
swarmService,
|
||||
nil, // attachmentService
|
||||
nil, // searchService
|
||||
nil, // reactionService
|
||||
nil, // trustService
|
||||
"agent-a",
|
||||
)
|
||||
return bridge, msgService, agentService, channelService
|
||||
@@ -185,7 +189,7 @@ func TestBridge_JoinChannel(t *testing.T) {
|
||||
bridge.agentService,
|
||||
bridge.channelService,
|
||||
bridge.swarmService,
|
||||
nil, nil,
|
||||
nil, nil, nil, nil,
|
||||
"agent-b",
|
||||
)
|
||||
|
||||
@@ -293,4 +297,190 @@ func TestBridge_ParamHelpers(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func newTestBridgeWithReactions(t *testing.T) (*ServiceBridge, *channels.Service) {
|
||||
t.Helper()
|
||||
db := newTestDB(t)
|
||||
|
||||
tracer := trace.NewTracer(db)
|
||||
t.Cleanup(func() { tracer.Close() })
|
||||
|
||||
msgStore := messaging.NewSQLiteMessageStore(db)
|
||||
msgService := messaging.NewMessagingService(msgStore, tracer)
|
||||
|
||||
agentStore := agents.NewSQLiteAgentStore(db)
|
||||
agentService := agents.NewAgentService(agentStore, tracer)
|
||||
|
||||
channelStore := channels.NewSQLiteChannelStore(db)
|
||||
channelService := channels.NewService(channelStore, msgService, tracer)
|
||||
|
||||
taskStore := channels.NewSQLiteTaskStore(db)
|
||||
swarmService := channels.NewSwarmService(taskStore, channelStore, tracer)
|
||||
|
||||
reactionStore := reactions.NewSQLiteStore(db)
|
||||
reactionService := reactions.NewService(reactionStore, slog.Default())
|
||||
|
||||
agentService.Register(context.Background(), "agent-a", "Agent A", "ai", nil, 1)
|
||||
agentService.Register(context.Background(), "agent-b", "Agent B", "ai", nil, 1)
|
||||
|
||||
bridge := NewServiceBridge(
|
||||
msgService,
|
||||
agentService,
|
||||
channelService,
|
||||
swarmService,
|
||||
nil, // attachmentService
|
||||
nil, // searchService
|
||||
reactionService,
|
||||
nil, // trustService
|
||||
"agent-a",
|
||||
)
|
||||
return bridge, channelService
|
||||
}
|
||||
|
||||
func TestBridge_React_WorkflowState(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
reaction string
|
||||
wantAction string
|
||||
wantWorkflowState string
|
||||
}{
|
||||
{
|
||||
name: "approve sets approved state",
|
||||
reaction: "approve",
|
||||
wantAction: "added",
|
||||
wantWorkflowState: "approved",
|
||||
},
|
||||
{
|
||||
name: "in_progress sets in_progress state",
|
||||
reaction: "in_progress",
|
||||
wantAction: "added",
|
||||
wantWorkflowState: "in_progress",
|
||||
},
|
||||
{
|
||||
name: "done sets done state",
|
||||
reaction: "done",
|
||||
wantAction: "added",
|
||||
wantWorkflowState: "done",
|
||||
},
|
||||
{
|
||||
name: "published sets published state",
|
||||
reaction: "published",
|
||||
wantAction: "added",
|
||||
wantWorkflowState: "published",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
bridge, channelService := newTestBridgeWithReactions(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a channel and send a message to react to
|
||||
ch, err := channelService.CreateChannel(ctx, channels.CreateChannelRequest{
|
||||
Name: "react-test", Type: "standard", CreatedBy: "agent-a",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create channel: %v", err)
|
||||
}
|
||||
channelService.JoinChannel(ctx, ch.ID, "agent-a")
|
||||
|
||||
msg, err := bridge.Call(ctx, "send_channel_message", map[string]any{
|
||||
"channel_name": "react-test",
|
||||
"body": "test message",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("send_channel_message: %v", err)
|
||||
}
|
||||
msgMap := msg.(map[string]any)
|
||||
msgID := msgMap["message_id"]
|
||||
|
||||
// React to the message
|
||||
result, err := bridge.Call(ctx, "react", map[string]any{
|
||||
"message_id": msgID,
|
||||
"reaction": tt.reaction,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("react: %v", err)
|
||||
}
|
||||
|
||||
resp := result.(map[string]any)
|
||||
|
||||
if resp["action"] != tt.wantAction {
|
||||
t.Errorf("action = %v, want %v", resp["action"], tt.wantAction)
|
||||
}
|
||||
|
||||
state, ok := resp["workflow_state"]
|
||||
if !ok {
|
||||
t.Fatal("response missing workflow_state field")
|
||||
}
|
||||
if state != tt.wantWorkflowState {
|
||||
t.Errorf("workflow_state = %v, want %v", state, tt.wantWorkflowState)
|
||||
}
|
||||
|
||||
rxns, ok := resp["reactions"]
|
||||
if !ok {
|
||||
t.Fatal("response missing reactions field")
|
||||
}
|
||||
rxnSlice, ok := rxns.([]*reactions.Reaction)
|
||||
if !ok {
|
||||
t.Fatalf("reactions has unexpected type %T", rxns)
|
||||
}
|
||||
if len(rxnSlice) == 0 {
|
||||
t.Error("expected at least one reaction")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBridge_React_Toggle_Removes_WorkflowState(t *testing.T) {
|
||||
bridge, channelService := newTestBridgeWithReactions(t)
|
||||
ctx := context.Background()
|
||||
|
||||
ch, err := channelService.CreateChannel(ctx, channels.CreateChannelRequest{
|
||||
Name: "toggle-test", Type: "standard", CreatedBy: "agent-a",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create channel: %v", err)
|
||||
}
|
||||
channelService.JoinChannel(ctx, ch.ID, "agent-a")
|
||||
|
||||
msg, err := bridge.Call(ctx, "send_channel_message", map[string]any{
|
||||
"channel_name": "toggle-test",
|
||||
"body": "toggle message",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("send_channel_message: %v", err)
|
||||
}
|
||||
msgMap := msg.(map[string]any)
|
||||
msgID := msgMap["message_id"]
|
||||
|
||||
// Add reaction
|
||||
bridge.Call(ctx, "react", map[string]any{
|
||||
"message_id": msgID,
|
||||
"reaction": "approve",
|
||||
})
|
||||
|
||||
// Toggle off (remove)
|
||||
result, err := bridge.Call(ctx, "react", map[string]any{
|
||||
"message_id": msgID,
|
||||
"reaction": "approve",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("react toggle off: %v", err)
|
||||
}
|
||||
|
||||
resp := result.(map[string]any)
|
||||
if resp["action"] != "removed" {
|
||||
t.Errorf("action = %v, want removed", resp["action"])
|
||||
}
|
||||
|
||||
// After removing the only reaction, workflow_state should be "proposed"
|
||||
state, ok := resp["workflow_state"]
|
||||
if !ok {
|
||||
t.Fatal("response missing workflow_state after removal")
|
||||
}
|
||||
if state != "proposed" {
|
||||
t.Errorf("workflow_state = %v, want proposed", state)
|
||||
}
|
||||
}
|
||||
|
||||
var _ = storage.RunMigrations
|
||||
|
||||
@@ -50,6 +50,8 @@ func newTestHybridWithChannels(t *testing.T) (*HybridToolRegistrar, *channels.Se
|
||||
nil, // swarmService
|
||||
nil, // attachmentService
|
||||
nil, // searchService
|
||||
nil, // reactionService
|
||||
nil, // trustService
|
||||
jsPool,
|
||||
actionRegistry,
|
||||
actionIndex,
|
||||
|
||||
+28
-12
@@ -12,24 +12,28 @@ import (
|
||||
"github.com/mark3labs/mcp-go/server"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/actions"
|
||||
"github.com/synapbus/synapbus/internal/agentquery"
|
||||
"github.com/synapbus/synapbus/internal/agents"
|
||||
"github.com/synapbus/synapbus/internal/attachments"
|
||||
"github.com/synapbus/synapbus/internal/channels"
|
||||
"github.com/synapbus/synapbus/internal/console"
|
||||
"github.com/synapbus/synapbus/internal/jsruntime"
|
||||
"github.com/synapbus/synapbus/internal/messaging"
|
||||
"github.com/synapbus/synapbus/internal/reactions"
|
||||
"github.com/synapbus/synapbus/internal/search"
|
||||
"github.com/synapbus/synapbus/internal/trace"
|
||||
"github.com/synapbus/synapbus/internal/trust"
|
||||
)
|
||||
|
||||
// MCPServer wraps the mcp-go server with SynapBus services.
|
||||
type MCPServer struct {
|
||||
mcpServer *server.MCPServer
|
||||
httpServer *server.StreamableHTTPServer
|
||||
connMgr *ConnectionManager
|
||||
agentService *agents.AgentService
|
||||
logger *slog.Logger
|
||||
console *console.Printer
|
||||
mcpServer *server.MCPServer
|
||||
httpServer *server.StreamableHTTPServer
|
||||
connMgr *ConnectionManager
|
||||
agentService *agents.AgentService
|
||||
hybridRegistrar *HybridToolRegistrar
|
||||
logger *slog.Logger
|
||||
console *console.Printer
|
||||
}
|
||||
|
||||
// NewMCPServer creates and configures a new MCP server with 4 hybrid tools registered.
|
||||
@@ -40,6 +44,8 @@ func NewMCPServer(
|
||||
swarmService *channels.SwarmService,
|
||||
attachmentService *attachments.Service,
|
||||
searchService *search.Service,
|
||||
reactionService *reactions.Service,
|
||||
trustService *trust.Service,
|
||||
consolePrinter *console.Printer,
|
||||
jsPool *jsruntime.Pool,
|
||||
actionRegistry *actions.Registry,
|
||||
@@ -153,6 +159,8 @@ func NewMCPServer(
|
||||
swarmService,
|
||||
attachmentService,
|
||||
searchService,
|
||||
reactionService,
|
||||
trustService,
|
||||
jsPool,
|
||||
actionRegistry,
|
||||
actionIndex,
|
||||
@@ -181,18 +189,26 @@ func NewMCPServer(
|
||||
)
|
||||
|
||||
s := &MCPServer{
|
||||
mcpServer: mcpSrv,
|
||||
httpServer: httpServer,
|
||||
connMgr: connMgr,
|
||||
agentService: agentService,
|
||||
logger: logger,
|
||||
console: consolePrinter,
|
||||
mcpServer: mcpSrv,
|
||||
httpServer: httpServer,
|
||||
connMgr: connMgr,
|
||||
agentService: agentService,
|
||||
hybridRegistrar: hybridRegistrar,
|
||||
logger: logger,
|
||||
console: consolePrinter,
|
||||
}
|
||||
|
||||
logger.Info("MCP server initialized (4 hybrid tools, 4 prompts, streamable HTTP transport)")
|
||||
return s
|
||||
}
|
||||
|
||||
// SetQueryExecutor sets the SQL query executor for agent queries via the execute tool.
|
||||
func (s *MCPServer) SetQueryExecutor(exec *agentquery.Executor) {
|
||||
if s.hybridRegistrar != nil {
|
||||
s.hybridRegistrar.SetQueryExecutor(exec)
|
||||
}
|
||||
}
|
||||
|
||||
// Handler returns the HTTP handler for mounting on a router.
|
||||
func (s *MCPServer) Handler() http.Handler {
|
||||
return s.httpServer
|
||||
|
||||
@@ -38,7 +38,7 @@ func newTestMCPServer(t *testing.T, con *console.Printer) (*MCPServer, *messagin
|
||||
actionRegistry := actions.NewRegistry()
|
||||
actionIndex := actions.NewIndex(actionRegistry.List())
|
||||
|
||||
srv := NewMCPServer(msgService, agentService, nil, nil, nil, nil, con, jsPool, actionRegistry, actionIndex, db)
|
||||
srv := NewMCPServer(msgService, agentService, nil, nil, nil, nil, nil, nil, con, jsPool, actionRegistry, actionIndex, db)
|
||||
return srv, msgService, agentService
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ func TestMCPToolCall_WithValidAPIKey(t *testing.T) {
|
||||
actionIndex := actions.NewIndex(actionRegistry.List())
|
||||
|
||||
// Create MCP server
|
||||
srv := NewMCPServer(msgService, agentService, nil, nil, nil, nil, nil, jsPool, actionRegistry, actionIndex, db)
|
||||
srv := NewMCPServer(msgService, agentService, nil, nil, nil, nil, nil, nil, nil, jsPool, actionRegistry, actionIndex, db)
|
||||
|
||||
// Mount with auth middleware, just like main.go does
|
||||
mux := http.NewServeMux()
|
||||
@@ -188,7 +188,7 @@ func TestMCPToolCall_InvalidAPIKeyReturns401(t *testing.T) {
|
||||
actionRegistry := actions.NewRegistry()
|
||||
actionIndex := actions.NewIndex(actionRegistry.List())
|
||||
|
||||
srv := NewMCPServer(msgService, agentService, nil, nil, nil, nil, nil, jsPool, actionRegistry, actionIndex, db)
|
||||
srv := NewMCPServer(msgService, agentService, nil, nil, nil, nil, nil, nil, nil, jsPool, actionRegistry, actionIndex, db)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
handler := agents.OptionalAuthMiddlewareWithAPIKeys(agentService, apiKeyService)(srv.Handler())
|
||||
|
||||
@@ -17,8 +17,11 @@ import (
|
||||
"github.com/synapbus/synapbus/internal/attachments"
|
||||
"github.com/synapbus/synapbus/internal/channels"
|
||||
"github.com/synapbus/synapbus/internal/jsruntime"
|
||||
"github.com/synapbus/synapbus/internal/agentquery"
|
||||
"github.com/synapbus/synapbus/internal/messaging"
|
||||
"github.com/synapbus/synapbus/internal/reactions"
|
||||
"github.com/synapbus/synapbus/internal/search"
|
||||
"github.com/synapbus/synapbus/internal/trust"
|
||||
)
|
||||
|
||||
// HybridToolRegistrar registers the 4 hybrid MCP tools.
|
||||
@@ -29,13 +32,21 @@ type HybridToolRegistrar struct {
|
||||
swarmService *channels.SwarmService
|
||||
attachmentService *attachments.Service
|
||||
searchService *search.Service
|
||||
reactionService *reactions.Service
|
||||
trustService *trust.Service
|
||||
jsPool *jsruntime.Pool
|
||||
actionRegistry *actions.Registry
|
||||
actionIndex *actions.Index
|
||||
db *sql.DB
|
||||
queryExecutor *agentquery.Executor
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// SetQueryExecutor sets the SQL query executor for all agent bridges.
|
||||
func (h *HybridToolRegistrar) SetQueryExecutor(exec *agentquery.Executor) {
|
||||
h.queryExecutor = exec
|
||||
}
|
||||
|
||||
// NewHybridToolRegistrar creates a new hybrid tool registrar.
|
||||
func NewHybridToolRegistrar(
|
||||
msgService *messaging.MessagingService,
|
||||
@@ -44,6 +55,8 @@ func NewHybridToolRegistrar(
|
||||
swarmService *channels.SwarmService,
|
||||
attachmentService *attachments.Service,
|
||||
searchService *search.Service,
|
||||
reactionService *reactions.Service,
|
||||
trustService *trust.Service,
|
||||
jsPool *jsruntime.Pool,
|
||||
actionRegistry *actions.Registry,
|
||||
actionIndex *actions.Index,
|
||||
@@ -56,6 +69,8 @@ func NewHybridToolRegistrar(
|
||||
swarmService: swarmService,
|
||||
attachmentService: attachmentService,
|
||||
searchService: searchService,
|
||||
reactionService: reactionService,
|
||||
trustService: trustService,
|
||||
jsPool: jsPool,
|
||||
actionRegistry: actionRegistry,
|
||||
actionIndex: actionIndex,
|
||||
@@ -64,14 +79,15 @@ func NewHybridToolRegistrar(
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterAllOnServer registers all 4 hybrid tools on an mcp-go MCPServer.
|
||||
// RegisterAllOnServer registers all hybrid tools on an mcp-go MCPServer.
|
||||
func (h *HybridToolRegistrar) RegisterAllOnServer(s *server.MCPServer) {
|
||||
s.AddTool(h.myStatusTool(), h.handleMyStatus)
|
||||
s.AddTool(h.sendMessageTool(), h.handleSendMessage)
|
||||
s.AddTool(h.searchTool(), h.handleSearch)
|
||||
s.AddTool(h.executeTool(), h.handleExecute)
|
||||
s.AddTool(h.getRepliesTool(), h.handleGetReplies)
|
||||
|
||||
h.logger.Info("hybrid MCP tools registered", "count", 4)
|
||||
h.logger.Info("hybrid MCP tools registered", "count", 5)
|
||||
}
|
||||
|
||||
// --- Tool Definitions ---
|
||||
@@ -84,14 +100,15 @@ func (h *HybridToolRegistrar) myStatusTool() mcplib.Tool {
|
||||
|
||||
func (h *HybridToolRegistrar) sendMessageTool() mcplib.Tool {
|
||||
return mcplib.NewTool("send_message",
|
||||
mcplib.WithDescription("Send a message to another agent (DM) or to a channel. Specify exactly one of 'to' (agent name for DM) or 'channel' (channel name or numeric ID)."),
|
||||
mcplib.WithDescription("Send a message to another agent (DM) or to a channel. Supports attachments — upload files first via the execute tool, then pass the returned hashes here. Specify exactly one of 'to' (agent name for DM) or 'channel' (channel name or numeric ID)."),
|
||||
mcplib.WithString("to", mcplib.Description("Recipient agent name for direct messages")),
|
||||
mcplib.WithString("channel", mcplib.Description("Channel name or numeric ID for channel messages")),
|
||||
mcplib.WithString("body", mcplib.Description("Message body text"), mcplib.Required()),
|
||||
mcplib.WithString("subject", mcplib.Description("Conversation subject (optional)")),
|
||||
mcplib.WithNumber("priority", mcplib.Description("Message priority (1-10, default 5)"), mcplib.Min(1), mcplib.Max(10)),
|
||||
mcplib.WithString("metadata", mcplib.Description("JSON metadata object (optional)")),
|
||||
mcplib.WithNumber("reply_to", mcplib.Description("ID of the message to reply to (optional, for threading)")),
|
||||
mcplib.WithNumber("reply_to", mcplib.Description("ID of the parent message to reply to. Creates a threaded reply. Always use reply_to when responding to a message that is itself a thread reply, to keep conversations organized.")),
|
||||
mcplib.WithString("attachments", mcplib.Description("Comma-separated list of attachment hashes to link to this message. Upload attachments first using the upload_attachment action via the execute tool.")),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -111,6 +128,13 @@ func (h *HybridToolRegistrar) executeTool() mcplib.Tool {
|
||||
)
|
||||
}
|
||||
|
||||
func (h *HybridToolRegistrar) getRepliesTool() mcplib.Tool {
|
||||
return mcplib.NewTool("get_replies",
|
||||
mcplib.WithDescription("Get all replies (thread messages) for a given message. Use this to read thread conversations, check for edits or follow-up comments on a message."),
|
||||
mcplib.WithNumber("message_id", mcplib.Description("ID of the parent message to get replies for"), mcplib.Required()),
|
||||
)
|
||||
}
|
||||
|
||||
// --- Tool Handlers ---
|
||||
|
||||
func (h *HybridToolRegistrar) handleMyStatus(ctx context.Context, req mcplib.CallToolRequest) (*mcplib.CallToolResult, error) {
|
||||
@@ -323,6 +347,16 @@ func (h *HybridToolRegistrar) handleSendMessage(ctx context.Context, req mcplib.
|
||||
replyTo = &v
|
||||
}
|
||||
|
||||
var attachmentHashes []string
|
||||
if attStr := req.GetString("attachments", ""); attStr != "" {
|
||||
for _, h := range strings.Split(attStr, ",") {
|
||||
h = strings.TrimSpace(h)
|
||||
if h != "" {
|
||||
attachmentHashes = append(attachmentHashes, h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Channel message path.
|
||||
if channel != "" {
|
||||
if h.channelService == nil {
|
||||
@@ -335,7 +369,7 @@ func (h *HybridToolRegistrar) handleSendMessage(ctx context.Context, req mcplib.
|
||||
return mcplib.NewToolResultError(fmt.Sprintf("send_message to channel failed: %s", err)), nil
|
||||
}
|
||||
|
||||
messages, err := h.channelService.BroadcastMessage(ctx, channelID, agentName, body, priority, metadataStr, replyTo)
|
||||
messages, err := h.channelService.BroadcastMessage(ctx, channelID, agentName, body, priority, metadataStr, replyTo, attachmentHashes)
|
||||
if err != nil {
|
||||
return mcplib.NewToolResultError(fmt.Sprintf("send_message to channel failed: %s", err)), nil
|
||||
}
|
||||
@@ -345,19 +379,30 @@ func (h *HybridToolRegistrar) handleSendMessage(ctx context.Context, req mcplib.
|
||||
messageID = messages[0].ID
|
||||
}
|
||||
|
||||
return resultJSON(map[string]any{
|
||||
result := map[string]any{
|
||||
"channel_id": channelID,
|
||||
"message_id": messageID,
|
||||
"status": "sent",
|
||||
})
|
||||
}
|
||||
|
||||
// Enrich channel messages with attachment info.
|
||||
if len(messages) > 0 && len(attachmentHashes) > 0 {
|
||||
h.msgService.EnrichMessages(ctx, messages)
|
||||
if len(messages[0].Attachments) > 0 {
|
||||
result["attachments"] = messages[0].Attachments
|
||||
}
|
||||
}
|
||||
|
||||
return resultJSON(result)
|
||||
}
|
||||
|
||||
// DM path.
|
||||
opts := messaging.SendOptions{
|
||||
Subject: subject,
|
||||
Priority: priority,
|
||||
Metadata: metadataStr,
|
||||
ReplyTo: replyTo,
|
||||
Subject: subject,
|
||||
Priority: priority,
|
||||
Metadata: metadataStr,
|
||||
ReplyTo: replyTo,
|
||||
Attachments: attachmentHashes,
|
||||
}
|
||||
|
||||
msg, err := h.msgService.SendMessage(ctx, agentName, to, body, opts)
|
||||
@@ -365,11 +410,21 @@ func (h *HybridToolRegistrar) handleSendMessage(ctx context.Context, req mcplib.
|
||||
return mcplib.NewToolResultError(fmt.Sprintf("send_message failed: %s", err)), nil
|
||||
}
|
||||
|
||||
return resultJSON(map[string]any{
|
||||
result := map[string]any{
|
||||
"message_id": msg.ID,
|
||||
"conversation_id": msg.ConversationID,
|
||||
"status": msg.Status,
|
||||
})
|
||||
}
|
||||
|
||||
// Enrich message with attachment info.
|
||||
if len(attachmentHashes) > 0 {
|
||||
h.msgService.EnrichMessages(ctx, []*messaging.Message{msg})
|
||||
if len(msg.Attachments) > 0 {
|
||||
result["attachments"] = msg.Attachments
|
||||
}
|
||||
}
|
||||
|
||||
return resultJSON(result)
|
||||
}
|
||||
|
||||
func (h *HybridToolRegistrar) handleSearch(ctx context.Context, req mcplib.CallToolRequest) (*mcplib.CallToolResult, error) {
|
||||
@@ -443,8 +498,13 @@ func (h *HybridToolRegistrar) handleExecute(ctx context.Context, req mcplib.Call
|
||||
h.swarmService,
|
||||
h.attachmentService,
|
||||
h.searchService,
|
||||
h.reactionService,
|
||||
h.trustService,
|
||||
agentName,
|
||||
)
|
||||
if h.queryExecutor != nil {
|
||||
bridge.SetQueryExecutor(h.queryExecutor)
|
||||
}
|
||||
|
||||
result, err := h.jsPool.Execute(ctx, code, bridge, jsruntime.ExecuteOptions{
|
||||
Timeout: timeout,
|
||||
@@ -460,6 +520,32 @@ func (h *HybridToolRegistrar) handleExecute(ctx context.Context, req mcplib.Call
|
||||
})
|
||||
}
|
||||
|
||||
func (h *HybridToolRegistrar) handleGetReplies(ctx context.Context, req mcplib.CallToolRequest) (*mcplib.CallToolResult, error) {
|
||||
_, ok := extractAgentName(ctx)
|
||||
if !ok {
|
||||
return mcplib.NewToolResultError("authentication required"), nil
|
||||
}
|
||||
|
||||
messageID := req.GetInt("message_id", 0)
|
||||
if messageID == 0 {
|
||||
return mcplib.NewToolResultError("'message_id' parameter is required"), nil
|
||||
}
|
||||
|
||||
replies, err := h.msgService.GetReplies(ctx, int64(messageID))
|
||||
if err != nil {
|
||||
return mcplib.NewToolResultError(fmt.Sprintf("get_replies failed: %s", err)), nil
|
||||
}
|
||||
|
||||
// Enrich replies with attachment info.
|
||||
h.msgService.EnrichMessages(ctx, replies)
|
||||
|
||||
return resultJSON(map[string]any{
|
||||
"message_id": messageID,
|
||||
"replies": replies,
|
||||
"count": len(replies),
|
||||
})
|
||||
}
|
||||
|
||||
// resolveChannel resolves a channel name or numeric ID string to an int64 channel ID.
|
||||
func (h *HybridToolRegistrar) resolveChannel(ctx context.Context, channel string) (int64, error) {
|
||||
// Try parsing as numeric ID first.
|
||||
|
||||
@@ -68,6 +68,8 @@ func newTestHybridRegistrar(t *testing.T) (*HybridToolRegistrar, *messaging.Mess
|
||||
nil, // swarmService
|
||||
nil, // attachmentService
|
||||
nil, // searchService
|
||||
nil, // reactionService
|
||||
nil, // trustService
|
||||
jsPool,
|
||||
actionRegistry,
|
||||
actionIndex,
|
||||
@@ -389,4 +391,136 @@ func TestHybridTool_Execute(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestHybridTool_GetReplies(t *testing.T) {
|
||||
h, msgSvc, agentSvc, _ := newTestHybridRegistrar(t)
|
||||
ctx := context.Background()
|
||||
|
||||
agentSvc.Register(ctx, "alice", "Alice", "ai", nil, 1)
|
||||
agentSvc.Register(ctx, "bob", "Bob", "ai", nil, 1)
|
||||
|
||||
authCtx := ContextWithAgentName(ctx, "alice")
|
||||
|
||||
// Send a parent message from bob to alice.
|
||||
parentMsg, err := msgSvc.SendMessage(ctx, "bob", "alice", "parent message", messaging.SendOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("send parent message: %v", err)
|
||||
}
|
||||
|
||||
// Send two replies to the parent message.
|
||||
replyTo := parentMsg.ID
|
||||
_, err = msgSvc.SendMessage(ctx, "alice", "bob", "reply one", messaging.SendOptions{ReplyTo: &replyTo})
|
||||
if err != nil {
|
||||
t.Fatalf("send reply 1: %v", err)
|
||||
}
|
||||
_, err = msgSvc.SendMessage(ctx, "bob", "alice", "reply two", messaging.SendOptions{ReplyTo: &replyTo})
|
||||
if err != nil {
|
||||
t.Fatalf("send reply 2: %v", err)
|
||||
}
|
||||
|
||||
t.Run("returns replies for message", func(t *testing.T) {
|
||||
req := makeRequest(map[string]any{
|
||||
"message_id": float64(parentMsg.ID),
|
||||
})
|
||||
|
||||
result, err := h.handleGetReplies(authCtx, req)
|
||||
if err != nil {
|
||||
t.Fatalf("handleGetReplies: %v", err)
|
||||
}
|
||||
if result.IsError {
|
||||
t.Fatalf("unexpected error: %v", result.Content)
|
||||
}
|
||||
|
||||
var resp map[string]any
|
||||
text := result.Content[0].(mcplib.TextContent).Text
|
||||
json.Unmarshal([]byte(text), &resp)
|
||||
|
||||
count := resp["count"].(float64)
|
||||
if count != 2 {
|
||||
t.Errorf("expected 2 replies, got %v", count)
|
||||
}
|
||||
|
||||
replies := resp["replies"].([]any)
|
||||
if len(replies) != 2 {
|
||||
t.Errorf("expected 2 replies in array, got %d", len(replies))
|
||||
}
|
||||
|
||||
if resp["message_id"].(float64) != float64(parentMsg.ID) {
|
||||
t.Errorf("expected message_id %d, got %v", parentMsg.ID, resp["message_id"])
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("returns empty for message with no replies", func(t *testing.T) {
|
||||
// Send a message with no replies.
|
||||
noReplyMsg, err := msgSvc.SendMessage(ctx, "bob", "alice", "no replies here", messaging.SendOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("send message: %v", err)
|
||||
}
|
||||
|
||||
req := makeRequest(map[string]any{
|
||||
"message_id": float64(noReplyMsg.ID),
|
||||
})
|
||||
|
||||
result, err := h.handleGetReplies(authCtx, req)
|
||||
if err != nil {
|
||||
t.Fatalf("handleGetReplies: %v", err)
|
||||
}
|
||||
if result.IsError {
|
||||
t.Fatalf("unexpected error: %v", result.Content)
|
||||
}
|
||||
|
||||
var resp map[string]any
|
||||
text := result.Content[0].(mcplib.TextContent).Text
|
||||
json.Unmarshal([]byte(text), &resp)
|
||||
|
||||
count := resp["count"].(float64)
|
||||
if count != 0 {
|
||||
t.Errorf("expected 0 replies, got %v", count)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("missing message_id", func(t *testing.T) {
|
||||
req := makeRequest(map[string]any{})
|
||||
result, _ := h.handleGetReplies(authCtx, req)
|
||||
if !result.IsError {
|
||||
t.Error("expected error for missing message_id")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unauthenticated", func(t *testing.T) {
|
||||
req := makeRequest(map[string]any{
|
||||
"message_id": float64(1),
|
||||
})
|
||||
result, _ := h.handleGetReplies(ctx, req)
|
||||
if !result.IsError {
|
||||
t.Error("expected error for unauthenticated request")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("get_replies via execute", func(t *testing.T) {
|
||||
req := makeRequest(map[string]any{
|
||||
"code": fmt.Sprintf(`call("get_replies", {"message_id": %d})`, parentMsg.ID),
|
||||
})
|
||||
|
||||
result, err := h.handleExecute(authCtx, req)
|
||||
if err != nil {
|
||||
t.Fatalf("handleExecute: %v", err)
|
||||
}
|
||||
if result.IsError {
|
||||
t.Fatalf("unexpected error: %v", result.Content)
|
||||
}
|
||||
|
||||
// Parse the execute envelope to get the bridge result.
|
||||
var resp map[string]any
|
||||
text := result.Content[0].(mcplib.TextContent).Text
|
||||
json.Unmarshal([]byte(text), &resp)
|
||||
|
||||
callEnvelope := resp["result"].(map[string]any)
|
||||
inner := callEnvelope["result"].(map[string]any)
|
||||
count := inner["count"].(float64)
|
||||
if count != 2 {
|
||||
t.Errorf("expected 2 replies via execute, got %v", count)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
var _ = storage.RunMigrations
|
||||
|
||||
@@ -2,12 +2,13 @@ package messaging
|
||||
|
||||
// SendOptions configures message sending behavior.
|
||||
type SendOptions struct {
|
||||
Subject string `json:"subject,omitempty"`
|
||||
Priority int `json:"priority,omitempty"`
|
||||
Metadata string `json:"metadata,omitempty"`
|
||||
ChannelID *int64 `json:"channel_id,omitempty"`
|
||||
ConversationID *int64 `json:"conversation_id,omitempty"`
|
||||
ReplyTo *int64 `json:"reply_to,omitempty"`
|
||||
Subject string `json:"subject,omitempty"`
|
||||
Priority int `json:"priority,omitempty"`
|
||||
Metadata string `json:"metadata,omitempty"`
|
||||
ChannelID *int64 `json:"channel_id,omitempty"`
|
||||
ConversationID *int64 `json:"conversation_id,omitempty"`
|
||||
ReplyTo *int64 `json:"reply_to,omitempty"`
|
||||
Attachments []string `json:"attachments,omitempty"` // attachment hashes to link
|
||||
}
|
||||
|
||||
// ReadOptions configures inbox reading behavior.
|
||||
|
||||
@@ -24,12 +24,27 @@ type MessageListener interface {
|
||||
OnMessageSent(ctx context.Context, msg *Message)
|
||||
}
|
||||
|
||||
// AttachmentLinker links attachment hashes to message IDs. This avoids
|
||||
// importing the attachments package directly. Set via SetAttachmentLinker.
|
||||
type AttachmentLinker interface {
|
||||
AttachToMessage(ctx context.Context, hash string, messageID int64) error
|
||||
GetByMessageID(ctx context.Context, messageID int64) ([]AttachmentInfo, error)
|
||||
}
|
||||
|
||||
// ReactionEnricher loads reactions for messages. This avoids importing the
|
||||
// reactions package directly. Set via SetReactionEnricher.
|
||||
type ReactionEnricher interface {
|
||||
GetByMessageIDs(ctx context.Context, messageIDs []int64) (map[int64][]ReactionInfo, error)
|
||||
}
|
||||
|
||||
// MessagingService provides business logic for messaging operations.
|
||||
type MessagingService struct {
|
||||
store MessageStore
|
||||
tracer *trace.Tracer
|
||||
dispatcher dispatcher.EventDispatcher
|
||||
embeddings EmbeddingNotifier
|
||||
attLinker AttachmentLinker
|
||||
rxEnricher ReactionEnricher
|
||||
listeners []MessageListener
|
||||
logger *slog.Logger
|
||||
}
|
||||
@@ -53,6 +68,16 @@ func (s *MessagingService) SetEmbeddingNotifier(n EmbeddingNotifier) {
|
||||
s.embeddings = n
|
||||
}
|
||||
|
||||
// SetAttachmentLinker sets the attachment linker for message-attachment binding.
|
||||
func (s *MessagingService) SetAttachmentLinker(l AttachmentLinker) {
|
||||
s.attLinker = l
|
||||
}
|
||||
|
||||
// SetReactionEnricher sets the reaction enricher for message reaction loading.
|
||||
func (s *MessagingService) SetReactionEnricher(e ReactionEnricher) {
|
||||
s.rxEnricher = e
|
||||
}
|
||||
|
||||
// AddMessageListener registers a listener that is notified after message creation.
|
||||
func (s *MessagingService) AddMessageListener(l MessageListener) {
|
||||
s.listeners = append(s.listeners, l)
|
||||
@@ -145,6 +170,19 @@ func (s *MessagingService) SendMessage(ctx context.Context, from, to, body strin
|
||||
return nil, fmt.Errorf("insert message: %w", err)
|
||||
}
|
||||
|
||||
// Link attachments if provided.
|
||||
if s.attLinker != nil && len(opts.Attachments) > 0 {
|
||||
for _, hash := range opts.Attachments {
|
||||
if err := s.attLinker.AttachToMessage(ctx, hash, msg.ID); err != nil {
|
||||
s.logger.Error("failed to link attachment",
|
||||
"hash", hash,
|
||||
"message_id", msg.ID,
|
||||
"error", err,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Enqueue for embedding (async, best-effort)
|
||||
if s.embeddings != nil {
|
||||
s.embeddings.OnMessageCreated(ctx, msg.ID, msg.Body)
|
||||
@@ -493,6 +531,11 @@ func (s *MessagingService) GetDMMessages(ctx context.Context, ownedAgents []stri
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
// GetDMPartners returns all DM conversation partners for the owned agents.
|
||||
func (s *MessagingService) GetDMPartners(ctx context.Context, ownedAgents []string) ([]DMPartner, error) {
|
||||
return s.store.GetDMPartners(ctx, ownedAgents)
|
||||
}
|
||||
|
||||
// GetDMUnreadCounts returns unread DM counts grouped by peer agent.
|
||||
func (s *MessagingService) GetDMUnreadCounts(ctx context.Context, agentName string) ([]DMUnreadCount, error) {
|
||||
return s.store.GetDMUnreadCounts(ctx, agentName)
|
||||
@@ -523,6 +566,76 @@ func (s *MessagingService) GetConversationIDsForDM(ctx context.Context, agentNam
|
||||
return s.store.GetConversationIDsForDM(ctx, agentNames, peerAgent, lastMessageID)
|
||||
}
|
||||
|
||||
// EnrichMessages populates ReplyCount and Attachments on a slice of messages.
|
||||
func (s *MessagingService) EnrichMessages(ctx context.Context, msgs []*Message) {
|
||||
if len(msgs) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
ids := make([]int64, len(msgs))
|
||||
for i, m := range msgs {
|
||||
ids[i] = m.ID
|
||||
}
|
||||
|
||||
// Batch-load reply counts.
|
||||
counts, err := s.store.GetReplyCounts(ctx, ids)
|
||||
if err != nil {
|
||||
s.logger.Error("failed to load reply counts", "error", err)
|
||||
} else {
|
||||
for _, m := range msgs {
|
||||
if c, ok := counts[m.ID]; ok {
|
||||
m.ReplyCount = c
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Batch-load attachments.
|
||||
if s.attLinker != nil {
|
||||
for _, m := range msgs {
|
||||
atts, err := s.attLinker.GetByMessageID(ctx, m.ID)
|
||||
if err != nil {
|
||||
s.logger.Error("failed to load attachments", "message_id", m.ID, "error", err)
|
||||
continue
|
||||
}
|
||||
if len(atts) > 0 {
|
||||
m.Attachments = atts
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Batch-load reactions and derive workflow state.
|
||||
if s.rxEnricher != nil {
|
||||
rxMap, err := s.rxEnricher.GetByMessageIDs(ctx, ids)
|
||||
if err != nil {
|
||||
s.logger.Error("failed to load reactions", "error", err)
|
||||
} else {
|
||||
for _, m := range msgs {
|
||||
if rxs, ok := rxMap[m.ID]; ok && len(rxs) > 0 {
|
||||
m.Reactions = rxs
|
||||
// Derive workflow state from reactions
|
||||
highestPriority := 0
|
||||
priorities := map[string]int{
|
||||
"approve": 2, "in_progress": 3, "reject": 4, "done": 5, "published": 6,
|
||||
}
|
||||
states := map[string]string{
|
||||
"approve": "approved", "in_progress": "in_progress", "reject": "rejected", "done": "done", "published": "published",
|
||||
}
|
||||
for _, rx := range rxs {
|
||||
if p, ok := priorities[rx.Reaction]; ok && p > highestPriority {
|
||||
highestPriority = p
|
||||
m.WorkflowState = states[rx.Reaction]
|
||||
}
|
||||
}
|
||||
}
|
||||
// Default to "proposed" for channel messages with no reactions
|
||||
if m.WorkflowState == "" && m.ChannelID != nil {
|
||||
m.WorkflowState = "proposed"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetConversation returns a conversation and its messages.
|
||||
func (s *MessagingService) GetConversation(ctx context.Context, id int64) (*Conversation, []*Message, error) {
|
||||
conv, err := s.store.GetConversation(ctx, id)
|
||||
|
||||
@@ -728,5 +728,96 @@ func TestMessagingService_ReadInbox_DateFiltering(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// mockAttachmentLinker is a test double for the AttachmentLinker interface.
|
||||
type mockAttachmentLinker struct {
|
||||
attachments map[int64][]AttachmentInfo
|
||||
}
|
||||
|
||||
func (m *mockAttachmentLinker) AttachToMessage(_ context.Context, _ string, _ int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockAttachmentLinker) GetByMessageID(_ context.Context, messageID int64) ([]AttachmentInfo, error) {
|
||||
return m.attachments[messageID], nil
|
||||
}
|
||||
|
||||
func TestMessagingService_EnrichMessages(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Send a parent message and replies to it.
|
||||
parent, err := svc.SendMessage(ctx, "sender", "receiver", "parent message", SendOptions{Subject: "Enrich Test"})
|
||||
if err != nil {
|
||||
t.Fatalf("SendMessage (parent): %v", err)
|
||||
}
|
||||
|
||||
replyTo := parent.ID
|
||||
for i := 0; i < 3; i++ {
|
||||
_, err := svc.SendMessage(ctx, "receiver", "sender", "reply", SendOptions{
|
||||
Subject: "Enrich Test",
|
||||
ReplyTo: &replyTo,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SendMessage (reply %d): %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Send a message with no replies.
|
||||
noReplies, err := svc.SendMessage(ctx, "sender", "receiver", "standalone", SendOptions{Subject: "Enrich Standalone"})
|
||||
if err != nil {
|
||||
t.Fatalf("SendMessage (standalone): %v", err)
|
||||
}
|
||||
|
||||
t.Run("reply counts populated", func(t *testing.T) {
|
||||
msgs := []*Message{parent, noReplies}
|
||||
svc.EnrichMessages(ctx, msgs)
|
||||
|
||||
if parent.ReplyCount != 3 {
|
||||
t.Errorf("parent ReplyCount = %d, want 3", parent.ReplyCount)
|
||||
}
|
||||
if noReplies.ReplyCount != 0 {
|
||||
t.Errorf("noReplies ReplyCount = %d, want 0", noReplies.ReplyCount)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("attachments populated when linker set", func(t *testing.T) {
|
||||
linker := &mockAttachmentLinker{
|
||||
attachments: map[int64][]AttachmentInfo{
|
||||
parent.ID: {
|
||||
{Hash: "abc123", OriginalFilename: "photo.png", Size: 1024, MIMEType: "image/png", IsImage: true},
|
||||
},
|
||||
},
|
||||
}
|
||||
svc.SetAttachmentLinker(linker)
|
||||
|
||||
// Reset enrichment state.
|
||||
parent.ReplyCount = 0
|
||||
parent.Attachments = nil
|
||||
noReplies.ReplyCount = 0
|
||||
noReplies.Attachments = nil
|
||||
|
||||
msgs := []*Message{parent, noReplies}
|
||||
svc.EnrichMessages(ctx, msgs)
|
||||
|
||||
if parent.ReplyCount != 3 {
|
||||
t.Errorf("parent ReplyCount = %d, want 3", parent.ReplyCount)
|
||||
}
|
||||
if len(parent.Attachments) != 1 {
|
||||
t.Fatalf("parent Attachments count = %d, want 1", len(parent.Attachments))
|
||||
}
|
||||
if parent.Attachments[0].Hash != "abc123" {
|
||||
t.Errorf("attachment hash = %s, want abc123", parent.Attachments[0].Hash)
|
||||
}
|
||||
if noReplies.Attachments != nil {
|
||||
t.Errorf("noReplies Attachments should be nil, got %v", noReplies.Attachments)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty slice is a no-op", func(t *testing.T) {
|
||||
svc.EnrichMessages(ctx, []*Message{})
|
||||
// Should not panic or error.
|
||||
})
|
||||
}
|
||||
|
||||
// suppress unused import warning for storage package
|
||||
var _ = storage.RunMigrations
|
||||
|
||||
@@ -153,11 +153,16 @@ func (w *StalemateWorker) checkStaleMessages(ctx context.Context) {
|
||||
reminded := w.sendPendingReminders(ctx)
|
||||
escalated := w.escalatePendingMessages(ctx)
|
||||
|
||||
if failed > 0 || reminded > 0 || escalated > 0 {
|
||||
// Phase 2: Workflow stalemate checks for channel messages
|
||||
wfReminded, wfEscalated := w.checkWorkflowStalemates(ctx)
|
||||
|
||||
if failed > 0 || reminded > 0 || escalated > 0 || wfReminded > 0 || wfEscalated > 0 {
|
||||
w.logger.Info("stalemate check complete",
|
||||
"auto_failed", failed,
|
||||
"reminders_sent", reminded,
|
||||
"escalations_sent", escalated,
|
||||
"workflow_reminders", wfReminded,
|
||||
"workflow_escalations", wfEscalated,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -438,6 +443,419 @@ func (w *StalemateWorker) escalationExists(ctx context.Context, messageID int64)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
// workflowChannel holds channel info relevant to workflow stalemate checking.
|
||||
type workflowChannel struct {
|
||||
ID int64
|
||||
Name string
|
||||
StalemateRemindAfter string
|
||||
StalemateEscalateAfter string
|
||||
}
|
||||
|
||||
// staleWorkflowMsg holds info about a channel message in a stale workflow state.
|
||||
type staleWorkflowMsg struct {
|
||||
ID int64
|
||||
Body string
|
||||
FromAgent string
|
||||
ChannelID int64
|
||||
Channel string
|
||||
State string
|
||||
StateAge time.Duration
|
||||
}
|
||||
|
||||
// checkWorkflowStalemates scans workflow-enabled channels for messages stuck in
|
||||
// non-terminal workflow states (proposed, approved, in_progress) and sends
|
||||
// reminders to channel members or escalates to #approvals.
|
||||
func (w *StalemateWorker) checkWorkflowStalemates(ctx context.Context) (reminded int64, escalated int64) {
|
||||
// Step 1: Find all workflow-enabled channels
|
||||
channels, err := w.listWorkflowChannels(ctx)
|
||||
if err != nil {
|
||||
w.logger.Error("list workflow channels failed", "error", err)
|
||||
return 0, 0
|
||||
}
|
||||
if len(channels) == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
for _, ch := range channels {
|
||||
remindTimeout, err := parseDurationWithDays(ch.StalemateRemindAfter)
|
||||
if err != nil || remindTimeout <= 0 {
|
||||
remindTimeout = 24 * time.Hour // default
|
||||
}
|
||||
escalateTimeout, err := parseDurationWithDays(ch.StalemateEscalateAfter)
|
||||
if err != nil || escalateTimeout <= 0 {
|
||||
escalateTimeout = 72 * time.Hour // default
|
||||
}
|
||||
|
||||
// Step 2: Find messages in non-terminal workflow states
|
||||
staleMessages, err := w.findStaleWorkflowMessages(ctx, ch)
|
||||
if err != nil {
|
||||
w.logger.Error("find stale workflow messages failed",
|
||||
"channel", ch.Name,
|
||||
"error", err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, msg := range staleMessages {
|
||||
// Step 3: Check escalation first (longer timeout)
|
||||
if msg.StateAge >= escalateTimeout {
|
||||
if w.workflowEscalationExists(ctx, msg.ID) {
|
||||
continue
|
||||
}
|
||||
if w.sendWorkflowEscalation(ctx, msg) {
|
||||
escalated++
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Step 4: Check reminder (shorter timeout)
|
||||
if msg.StateAge >= remindTimeout {
|
||||
if w.workflowReminderExists(ctx, msg.ID) {
|
||||
continue
|
||||
}
|
||||
r := w.sendWorkflowReminders(ctx, msg, ch.ID)
|
||||
reminded += r
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return reminded, escalated
|
||||
}
|
||||
|
||||
// listWorkflowChannels returns all channels that have workflow_enabled = true.
|
||||
func (w *StalemateWorker) listWorkflowChannels(ctx context.Context) ([]workflowChannel, error) {
|
||||
rows, err := w.db.QueryContext(ctx,
|
||||
`SELECT id, name, stalemate_remind_after, stalemate_escalate_after
|
||||
FROM channels
|
||||
WHERE workflow_enabled = 1`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query workflow channels: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var channels []workflowChannel
|
||||
for rows.Next() {
|
||||
var ch workflowChannel
|
||||
if err := rows.Scan(&ch.ID, &ch.Name, &ch.StalemateRemindAfter, &ch.StalemateEscalateAfter); err != nil {
|
||||
return nil, fmt.Errorf("scan workflow channel: %w", err)
|
||||
}
|
||||
channels = append(channels, ch)
|
||||
}
|
||||
return channels, rows.Err()
|
||||
}
|
||||
|
||||
// findStaleWorkflowMessages finds channel messages in non-terminal workflow states
|
||||
// and computes how long they have been in their current state.
|
||||
func (w *StalemateWorker) findStaleWorkflowMessages(ctx context.Context, ch workflowChannel) ([]staleWorkflowMsg, error) {
|
||||
// Get all messages in this channel that could be in a workflow state.
|
||||
// We fetch messages and their reactions, then compute state in Go.
|
||||
rows, err := w.db.QueryContext(ctx,
|
||||
`SELECT m.id, m.body, m.from_agent, m.created_at
|
||||
FROM messages m
|
||||
WHERE m.channel_id = ?
|
||||
AND m.from_agent != 'system'
|
||||
ORDER BY m.created_at ASC`,
|
||||
ch.ID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query channel messages: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
type chanMsg struct {
|
||||
ID int64
|
||||
Body string
|
||||
FromAgent string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
var msgs []chanMsg
|
||||
for rows.Next() {
|
||||
var m chanMsg
|
||||
if err := rows.Scan(&m.ID, &m.Body, &m.FromAgent, &m.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scan channel message: %w", err)
|
||||
}
|
||||
msgs = append(msgs, m)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(msgs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Batch-fetch reactions for all messages
|
||||
msgIDs := make([]int64, len(msgs))
|
||||
for i, m := range msgs {
|
||||
msgIDs[i] = m.ID
|
||||
}
|
||||
|
||||
reactionsMap, err := w.getReactionsByMessageIDs(ctx, msgIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get reactions: %w", err)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
var stale []staleWorkflowMsg
|
||||
for _, m := range msgs {
|
||||
reactions := reactionsMap[m.ID]
|
||||
state := computeWorkflowStateFromReactions(reactions)
|
||||
|
||||
// Skip terminal states
|
||||
if isTerminalWorkflowState(state) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Determine the "state age": how long since the state was entered.
|
||||
// If reactions exist, use the most recent reaction's created_at.
|
||||
// If no reactions (proposed state), use the message's created_at.
|
||||
stateEnteredAt := m.CreatedAt
|
||||
if len(reactions) > 0 {
|
||||
// Find the most recent reaction
|
||||
for _, r := range reactions {
|
||||
if r.CreatedAt.After(stateEnteredAt) {
|
||||
stateEnteredAt = r.CreatedAt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stale = append(stale, staleWorkflowMsg{
|
||||
ID: m.ID,
|
||||
Body: m.Body,
|
||||
FromAgent: m.FromAgent,
|
||||
ChannelID: ch.ID,
|
||||
Channel: ch.Name,
|
||||
State: state,
|
||||
StateAge: now.Sub(stateEnteredAt),
|
||||
})
|
||||
}
|
||||
|
||||
return stale, nil
|
||||
}
|
||||
|
||||
// reactionRow holds a raw reaction row for workflow state computation.
|
||||
type reactionRow struct {
|
||||
Reaction string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// getReactionsByMessageIDs fetches reactions for a batch of message IDs.
|
||||
func (w *StalemateWorker) getReactionsByMessageIDs(ctx context.Context, messageIDs []int64) (map[int64][]reactionRow, error) {
|
||||
if len(messageIDs) == 0 {
|
||||
return map[int64][]reactionRow{}, nil
|
||||
}
|
||||
|
||||
placeholders := make([]string, len(messageIDs))
|
||||
args := make([]any, len(messageIDs))
|
||||
for i, id := range messageIDs {
|
||||
placeholders[i] = "?"
|
||||
args[i] = id
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(
|
||||
`SELECT message_id, reaction, created_at
|
||||
FROM message_reactions
|
||||
WHERE message_id IN (%s)
|
||||
ORDER BY created_at ASC`,
|
||||
strings.Join(placeholders, ","),
|
||||
)
|
||||
|
||||
rows, err := w.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query reactions: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
result := make(map[int64][]reactionRow)
|
||||
for rows.Next() {
|
||||
var msgID int64
|
||||
var r reactionRow
|
||||
if err := rows.Scan(&msgID, &r.Reaction, &r.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scan reaction: %w", err)
|
||||
}
|
||||
result[msgID] = append(result[msgID], r)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
// computeWorkflowStateFromReactions derives workflow state from raw reaction rows.
|
||||
// Mirrors the logic in reactions.ComputeWorkflowState without importing that package.
|
||||
func computeWorkflowStateFromReactions(reactions []reactionRow) string {
|
||||
if len(reactions) == 0 {
|
||||
return "proposed"
|
||||
}
|
||||
|
||||
// Reaction priority (same as reactions.reactionPriority)
|
||||
priority := map[string]int{
|
||||
"approve": 2,
|
||||
"in_progress": 3,
|
||||
"reject": 4,
|
||||
"done": 5,
|
||||
"published": 6,
|
||||
}
|
||||
|
||||
// Reaction-to-state mapping (same as reactions.reactionToState)
|
||||
toState := map[string]string{
|
||||
"approve": "approved",
|
||||
"reject": "rejected",
|
||||
"in_progress": "in_progress",
|
||||
"done": "done",
|
||||
"published": "published",
|
||||
}
|
||||
|
||||
highestPriority := 0
|
||||
highestState := "proposed"
|
||||
|
||||
for _, r := range reactions {
|
||||
if p, ok := priority[r.Reaction]; ok && p > highestPriority {
|
||||
highestPriority = p
|
||||
highestState = toState[r.Reaction]
|
||||
}
|
||||
}
|
||||
|
||||
return highestState
|
||||
}
|
||||
|
||||
// isTerminalWorkflowState returns true if the state should not trigger stalemate checks.
|
||||
func isTerminalWorkflowState(state string) bool {
|
||||
switch state {
|
||||
case "rejected", "done", "published":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// sendWorkflowReminders sends DMs to channel members about a stale workflow message.
|
||||
func (w *StalemateWorker) sendWorkflowReminders(ctx context.Context, msg staleWorkflowMsg, channelID int64) int64 {
|
||||
// Get channel members
|
||||
rows, err := w.db.QueryContext(ctx,
|
||||
`SELECT agent_name FROM channel_members WHERE channel_id = ?`,
|
||||
channelID,
|
||||
)
|
||||
if err != nil {
|
||||
w.logger.Error("query channel members for workflow reminder failed",
|
||||
"channel_id", channelID,
|
||||
"error", err,
|
||||
)
|
||||
return 0
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var members []string
|
||||
for rows.Next() {
|
||||
var name string
|
||||
if err := rows.Scan(&name); err != nil {
|
||||
continue
|
||||
}
|
||||
members = append(members, name)
|
||||
}
|
||||
|
||||
age := formatAge(msg.StateAge)
|
||||
truncBody := truncate(msg.Body, 100)
|
||||
count := int64(0)
|
||||
|
||||
for _, member := range members {
|
||||
body := fmt.Sprintf(
|
||||
"**STALE**: Message #%d in #%s in '%s' for %s. \"%s\" — @%s",
|
||||
msg.ID, msg.Channel, msg.State, age, truncBody, msg.FromAgent,
|
||||
)
|
||||
|
||||
_, err := w.msgService.SendMessage(ctx, "system", member, body, SendOptions{
|
||||
Subject: fmt.Sprintf("workflow-stalemate-reminder:%d", msg.ID),
|
||||
Priority: 7,
|
||||
Metadata: fmt.Sprintf(`{"workflow_stalemate_reminder_for":%d}`, msg.ID),
|
||||
})
|
||||
if err != nil {
|
||||
w.logger.Error("send workflow stalemate reminder failed",
|
||||
"message_id", msg.ID,
|
||||
"to_agent", member,
|
||||
"error", err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
w.logger.Info("sent workflow stalemate reminder",
|
||||
"message_id", msg.ID,
|
||||
"channel", msg.Channel,
|
||||
"state", msg.State,
|
||||
"to_agent", member,
|
||||
"age", age,
|
||||
)
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// sendWorkflowEscalation posts an escalation to #approvals for a stale workflow message.
|
||||
func (w *StalemateWorker) sendWorkflowEscalation(ctx context.Context, msg staleWorkflowMsg) bool {
|
||||
approvalsChanID, err := w.channelLookup.GetChannelIDByName(ctx, "approvals")
|
||||
if err != nil {
|
||||
w.logger.Warn("cannot escalate workflow stalemate: #approvals channel not found", "error", err)
|
||||
return false
|
||||
}
|
||||
|
||||
age := formatAge(msg.StateAge)
|
||||
truncBody := truncate(msg.Body, 100)
|
||||
|
||||
body := fmt.Sprintf(
|
||||
"**STALE**: Message #%d in #%s in '%s' for %s. \"%s\" — @%s",
|
||||
msg.ID, msg.Channel, msg.State, age, truncBody, msg.FromAgent,
|
||||
)
|
||||
|
||||
_, err = w.msgService.SendMessage(ctx, "system", "", body, SendOptions{
|
||||
Subject: fmt.Sprintf("workflow-stalemate-escalation:%d", msg.ID),
|
||||
Priority: 9,
|
||||
Metadata: fmt.Sprintf(`{"workflow_stalemate_escalation_for":%d}`, msg.ID),
|
||||
ChannelID: &approvalsChanID,
|
||||
})
|
||||
if err != nil {
|
||||
w.logger.Error("send workflow escalation to #approvals failed",
|
||||
"message_id", msg.ID,
|
||||
"channel", msg.Channel,
|
||||
"error", err,
|
||||
)
|
||||
return false
|
||||
}
|
||||
w.logger.Info("escalated stale workflow message to #approvals",
|
||||
"message_id", msg.ID,
|
||||
"channel", msg.Channel,
|
||||
"state", msg.State,
|
||||
"age", age,
|
||||
)
|
||||
return true
|
||||
}
|
||||
|
||||
// workflowReminderExists checks if a workflow stalemate reminder already exists for a message.
|
||||
func (w *StalemateWorker) workflowReminderExists(ctx context.Context, messageID int64) bool {
|
||||
var count int
|
||||
err := w.db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM messages
|
||||
WHERE from_agent = 'system'
|
||||
AND metadata LIKE ?`,
|
||||
fmt.Sprintf(`%%"workflow_stalemate_reminder_for":%d%%`, messageID),
|
||||
).Scan(&count)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return count > 0
|
||||
}
|
||||
|
||||
// workflowEscalationExists checks if a workflow stalemate escalation already exists for a message.
|
||||
func (w *StalemateWorker) workflowEscalationExists(ctx context.Context, messageID int64) bool {
|
||||
var count int
|
||||
err := w.db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM messages
|
||||
WHERE from_agent = 'system'
|
||||
AND metadata LIKE ?`,
|
||||
fmt.Sprintf(`%%"workflow_stalemate_escalation_for":%d%%`, messageID),
|
||||
).Scan(&count)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return count > 0
|
||||
}
|
||||
|
||||
// truncate truncates a string to maxLen characters, appending "..." if truncated.
|
||||
func truncate(s string, maxLen int) string {
|
||||
runes := []rune(s)
|
||||
|
||||
@@ -478,3 +478,345 @@ func TestFormatAge(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestComputeWorkflowStateFromReactions(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
reactions []reactionRow
|
||||
want string
|
||||
}{
|
||||
{"no reactions = proposed", nil, "proposed"},
|
||||
{"approve only", []reactionRow{{Reaction: "approve"}}, "approved"},
|
||||
{"in_progress only", []reactionRow{{Reaction: "in_progress"}}, "in_progress"},
|
||||
{"reject only", []reactionRow{{Reaction: "reject"}}, "rejected"},
|
||||
{"done only", []reactionRow{{Reaction: "done"}}, "done"},
|
||||
{"published only", []reactionRow{{Reaction: "published"}}, "published"},
|
||||
{"approve + in_progress = in_progress (higher priority)", []reactionRow{
|
||||
{Reaction: "approve"},
|
||||
{Reaction: "in_progress"},
|
||||
}, "in_progress"},
|
||||
{"approve + done = done", []reactionRow{
|
||||
{Reaction: "approve"},
|
||||
{Reaction: "done"},
|
||||
}, "done"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := computeWorkflowStateFromReactions(tt.reactions)
|
||||
if got != tt.want {
|
||||
t.Errorf("computeWorkflowStateFromReactions() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsTerminalWorkflowState(t *testing.T) {
|
||||
tests := []struct {
|
||||
state string
|
||||
terminal bool
|
||||
}{
|
||||
{"proposed", false},
|
||||
{"approved", false},
|
||||
{"in_progress", false},
|
||||
{"rejected", true},
|
||||
{"done", true},
|
||||
{"published", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.state, func(t *testing.T) {
|
||||
got := isTerminalWorkflowState(tt.state)
|
||||
if got != tt.terminal {
|
||||
t.Errorf("isTerminalWorkflowState(%q) = %v, want %v", tt.state, got, tt.terminal)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStalemateWorker_WorkflowReminder(t *testing.T) {
|
||||
svc, db := newStalemateTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a workflow-enabled channel with short timeouts
|
||||
_, err := db.Exec(
|
||||
`INSERT INTO channels (id, name, description, topic, type, is_private, is_system, created_by, workflow_enabled, stalemate_remind_after, stalemate_escalate_after, created_at, updated_at)
|
||||
VALUES (10, 'news-test', 'Test news channel', '', 'standard', 0, 0, 'system', 1, '1s', '72h', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`)
|
||||
if err != nil {
|
||||
t.Fatalf("create workflow channel: %v", err)
|
||||
}
|
||||
|
||||
// Add system and sender as members
|
||||
db.Exec(`INSERT INTO channel_members (channel_id, agent_name, role, joined_at) VALUES (10, 'system', 'owner', CURRENT_TIMESTAMP)`)
|
||||
db.Exec(`INSERT INTO channel_members (channel_id, agent_name, role, joined_at) VALUES (10, 'sender', 'member', CURRENT_TIMESTAMP)`)
|
||||
db.Exec(`INSERT INTO channel_members (channel_id, agent_name, role, joined_at) VALUES (10, 'receiver', 'member', CURRENT_TIMESTAMP)`)
|
||||
|
||||
// Insert a channel message with old created_at (will be in "proposed" state since no reactions)
|
||||
oldTime := time.Now().Add(-2 * time.Second)
|
||||
convResult, err := db.Exec(
|
||||
`INSERT INTO conversations (subject, created_by, created_at, updated_at) VALUES ('wf-test', 'sender', ?, ?)`,
|
||||
oldTime, oldTime,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("insert conversation: %v", err)
|
||||
}
|
||||
convID, _ := convResult.LastInsertId()
|
||||
|
||||
channelID := int64(10)
|
||||
_, err = db.Exec(
|
||||
`INSERT INTO messages (conversation_id, from_agent, to_agent, body, priority, status, metadata, channel_id, created_at, updated_at)
|
||||
VALUES (?, 'sender', '', 'Draft blog post about MCP', 5, 'pending', '{}', ?, ?, ?)`,
|
||||
convID, channelID, oldTime, oldTime,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("insert channel message: %v", err)
|
||||
}
|
||||
|
||||
// Wait for the timeout to elapse
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
config := DefaultStalemateConfig()
|
||||
lookup := &stubChannelLookup{channelID: 0, err: fmt.Errorf("no approvals channel")}
|
||||
worker := NewStalemateWorker(db, svc, lookup, config)
|
||||
|
||||
worker.checkStaleMessages(ctx)
|
||||
|
||||
// Verify workflow stalemate reminders were sent to channel members
|
||||
var count int
|
||||
err = db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM messages WHERE from_agent = 'system' AND body LIKE '%STALE%'`,
|
||||
).Scan(&count)
|
||||
if err != nil {
|
||||
t.Fatalf("query workflow reminders: %v", err)
|
||||
}
|
||||
// Should have sent reminders to all 3 members (system, sender, receiver)
|
||||
if count < 1 {
|
||||
t.Errorf("expected at least 1 workflow reminder, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStalemateWorker_WorkflowEscalation(t *testing.T) {
|
||||
svc, db := newStalemateTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a workflow-enabled channel with short escalation timeout
|
||||
_, err := db.Exec(
|
||||
`INSERT INTO channels (id, name, description, topic, type, is_private, is_system, created_by, workflow_enabled, stalemate_remind_after, stalemate_escalate_after, created_at, updated_at)
|
||||
VALUES (10, 'news-test', 'Test news channel', '', 'standard', 0, 0, 'system', 1, '1s', '1s', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`)
|
||||
if err != nil {
|
||||
t.Fatalf("create workflow channel: %v", err)
|
||||
}
|
||||
|
||||
// Create #approvals channel
|
||||
db.Exec(
|
||||
`INSERT INTO channels (id, name, description, topic, type, is_private, is_system, created_by, created_at, updated_at)
|
||||
VALUES (20, 'approvals', 'Approval queue', '', 'standard', 0, 0, 'system', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`)
|
||||
db.Exec(`INSERT INTO channel_members (channel_id, agent_name, role, joined_at) VALUES (20, 'system', 'owner', CURRENT_TIMESTAMP)`)
|
||||
|
||||
// Add members to workflow channel
|
||||
db.Exec(`INSERT INTO channel_members (channel_id, agent_name, role, joined_at) VALUES (10, 'sender', 'member', CURRENT_TIMESTAMP)`)
|
||||
|
||||
// Insert a channel message old enough to trigger escalation
|
||||
oldTime := time.Now().Add(-2 * time.Second)
|
||||
convResult, _ := db.Exec(
|
||||
`INSERT INTO conversations (subject, created_by, created_at, updated_at) VALUES ('wf-esc', 'sender', ?, ?)`,
|
||||
oldTime, oldTime,
|
||||
)
|
||||
convID, _ := convResult.LastInsertId()
|
||||
|
||||
channelID := int64(10)
|
||||
_, err = db.Exec(
|
||||
`INSERT INTO messages (conversation_id, from_agent, to_agent, body, priority, status, metadata, channel_id, created_at, updated_at)
|
||||
VALUES (?, 'sender', '', 'Stale proposal needing attention', 5, 'pending', '{}', ?, ?, ?)`,
|
||||
convID, channelID, oldTime, oldTime,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("insert channel message: %v", err)
|
||||
}
|
||||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
config := DefaultStalemateConfig()
|
||||
lookup := &stubChannelLookup{channelID: 20}
|
||||
worker := NewStalemateWorker(db, svc, lookup, config)
|
||||
|
||||
worker.checkStaleMessages(ctx)
|
||||
|
||||
// Verify escalation was sent to #approvals
|
||||
var count int
|
||||
err = db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM messages WHERE from_agent = 'system' AND channel_id = 20 AND body LIKE '%STALE%'`,
|
||||
).Scan(&count)
|
||||
if err != nil {
|
||||
t.Fatalf("query workflow escalation: %v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Errorf("expected 1 workflow escalation, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStalemateWorker_WorkflowTerminalStateSkip(t *testing.T) {
|
||||
svc, db := newStalemateTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a workflow-enabled channel with short timeouts
|
||||
_, err := db.Exec(
|
||||
`INSERT INTO channels (id, name, description, topic, type, is_private, is_system, created_by, workflow_enabled, stalemate_remind_after, stalemate_escalate_after, created_at, updated_at)
|
||||
VALUES (10, 'news-test', 'Test news channel', '', 'standard', 0, 0, 'system', 1, '1s', '1s', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`)
|
||||
if err != nil {
|
||||
t.Fatalf("create workflow channel: %v", err)
|
||||
}
|
||||
db.Exec(`INSERT INTO channel_members (channel_id, agent_name, role, joined_at) VALUES (10, 'sender', 'member', CURRENT_TIMESTAMP)`)
|
||||
|
||||
// Insert a channel message
|
||||
oldTime := time.Now().Add(-2 * time.Second)
|
||||
convResult, _ := db.Exec(
|
||||
`INSERT INTO conversations (subject, created_by, created_at, updated_at) VALUES ('wf-done', 'sender', ?, ?)`,
|
||||
oldTime, oldTime,
|
||||
)
|
||||
convID, _ := convResult.LastInsertId()
|
||||
|
||||
channelID := int64(10)
|
||||
msgResult, err := db.Exec(
|
||||
`INSERT INTO messages (conversation_id, from_agent, to_agent, body, priority, status, metadata, channel_id, created_at, updated_at)
|
||||
VALUES (?, 'sender', '', 'Completed task', 5, 'pending', '{}', ?, ?, ?)`,
|
||||
convID, channelID, oldTime, oldTime,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("insert channel message: %v", err)
|
||||
}
|
||||
msgID, _ := msgResult.LastInsertId()
|
||||
|
||||
// Add a "done" reaction — puts it in terminal state
|
||||
_, err = db.Exec(
|
||||
`INSERT INTO message_reactions (message_id, agent_name, reaction, metadata, created_at)
|
||||
VALUES (?, 'sender', 'done', '{}', ?)`,
|
||||
msgID, oldTime,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("insert reaction: %v", err)
|
||||
}
|
||||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
config := DefaultStalemateConfig()
|
||||
lookup := &stubChannelLookup{channelID: 0, err: fmt.Errorf("no approvals")}
|
||||
worker := NewStalemateWorker(db, svc, lookup, config)
|
||||
|
||||
worker.checkStaleMessages(ctx)
|
||||
|
||||
// Verify NO reminders were sent (message is in terminal "done" state)
|
||||
var count int
|
||||
err = db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM messages WHERE from_agent = 'system' AND body LIKE '%STALE%'`,
|
||||
).Scan(&count)
|
||||
if err != nil {
|
||||
t.Fatalf("query reminders: %v", err)
|
||||
}
|
||||
if count != 0 {
|
||||
t.Errorf("expected 0 reminders for terminal state message, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStalemateWorker_WorkflowDuplicateReminderPrevention(t *testing.T) {
|
||||
svc, db := newStalemateTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a workflow-enabled channel with short timeout
|
||||
_, err := db.Exec(
|
||||
`INSERT INTO channels (id, name, description, topic, type, is_private, is_system, created_by, workflow_enabled, stalemate_remind_after, stalemate_escalate_after, created_at, updated_at)
|
||||
VALUES (10, 'news-test', 'Test', '', 'standard', 0, 0, 'system', 1, '1s', '72h', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`)
|
||||
if err != nil {
|
||||
t.Fatalf("create workflow channel: %v", err)
|
||||
}
|
||||
db.Exec(`INSERT INTO channel_members (channel_id, agent_name, role, joined_at) VALUES (10, 'receiver', 'member', CURRENT_TIMESTAMP)`)
|
||||
|
||||
// Insert a channel message
|
||||
oldTime := time.Now().Add(-2 * time.Second)
|
||||
convResult, _ := db.Exec(
|
||||
`INSERT INTO conversations (subject, created_by, created_at, updated_at) VALUES ('wf-dup', 'sender', ?, ?)`,
|
||||
oldTime, oldTime,
|
||||
)
|
||||
convID, _ := convResult.LastInsertId()
|
||||
|
||||
channelID := int64(10)
|
||||
_, err = db.Exec(
|
||||
`INSERT INTO messages (conversation_id, from_agent, to_agent, body, priority, status, metadata, channel_id, created_at, updated_at)
|
||||
VALUES (?, 'sender', '', 'Needs review', 5, 'pending', '{}', ?, ?, ?)`,
|
||||
convID, channelID, oldTime, oldTime,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("insert channel message: %v", err)
|
||||
}
|
||||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
config := DefaultStalemateConfig()
|
||||
lookup := &stubChannelLookup{channelID: 0, err: fmt.Errorf("no approvals")}
|
||||
worker := NewStalemateWorker(db, svc, lookup, config)
|
||||
|
||||
// Run twice
|
||||
worker.checkStaleMessages(ctx)
|
||||
worker.checkStaleMessages(ctx)
|
||||
|
||||
// Verify only one set of reminders was sent (no duplicates)
|
||||
var count int
|
||||
err = db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM messages WHERE from_agent = 'system' AND to_agent = 'receiver' AND body LIKE '%STALE%'`,
|
||||
).Scan(&count)
|
||||
if err != nil {
|
||||
t.Fatalf("query reminders: %v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Errorf("expected 1 reminder (no duplicates), got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStalemateWorker_WorkflowNonWorkflowChannelSkip(t *testing.T) {
|
||||
svc, db := newStalemateTestService(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a channel with workflow DISABLED
|
||||
_, err := db.Exec(
|
||||
`INSERT INTO channels (id, name, description, topic, type, is_private, is_system, created_by, workflow_enabled, stalemate_remind_after, stalemate_escalate_after, created_at, updated_at)
|
||||
VALUES (10, 'general', 'General', '', 'standard', 0, 0, 'system', 0, '1s', '1s', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`)
|
||||
if err != nil {
|
||||
t.Fatalf("create channel: %v", err)
|
||||
}
|
||||
db.Exec(`INSERT INTO channel_members (channel_id, agent_name, role, joined_at) VALUES (10, 'sender', 'member', CURRENT_TIMESTAMP)`)
|
||||
|
||||
// Insert a channel message
|
||||
oldTime := time.Now().Add(-2 * time.Second)
|
||||
convResult, _ := db.Exec(
|
||||
`INSERT INTO conversations (subject, created_by, created_at, updated_at) VALUES ('no-wf', 'sender', ?, ?)`,
|
||||
oldTime, oldTime,
|
||||
)
|
||||
convID, _ := convResult.LastInsertId()
|
||||
|
||||
channelID := int64(10)
|
||||
db.Exec(
|
||||
`INSERT INTO messages (conversation_id, from_agent, to_agent, body, priority, status, metadata, channel_id, created_at, updated_at)
|
||||
VALUES (?, 'sender', '', 'No workflow here', 5, 'pending', '{}', ?, ?, ?)`,
|
||||
convID, channelID, oldTime, oldTime,
|
||||
)
|
||||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
config := DefaultStalemateConfig()
|
||||
lookup := &stubChannelLookup{channelID: 0, err: fmt.Errorf("no approvals")}
|
||||
worker := NewStalemateWorker(db, svc, lookup, config)
|
||||
|
||||
worker.checkStaleMessages(ctx)
|
||||
|
||||
// Verify NO reminders — channel is not workflow-enabled
|
||||
var count int
|
||||
err = db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM messages WHERE from_agent = 'system' AND body LIKE '%STALE%'`,
|
||||
).Scan(&count)
|
||||
if err != nil {
|
||||
t.Fatalf("query reminders: %v", err)
|
||||
}
|
||||
if count != 0 {
|
||||
t.Errorf("expected 0 reminders for non-workflow channel, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
+143
-1
@@ -29,6 +29,7 @@ type MessageStore interface {
|
||||
GetChannelMessages(ctx context.Context, channelID int64, limit, offset int) ([]*Message, error)
|
||||
CountChannelMessages(ctx context.Context, channelID int64) (int, error)
|
||||
GetDMMessages(ctx context.Context, agents []string, peerAgent string, limit int) ([]*Message, error)
|
||||
GetDMPartners(ctx context.Context, agents []string) ([]DMPartner, error)
|
||||
AgentExists(ctx context.Context, agentName string) (bool, error)
|
||||
CountPendingDMs(ctx context.Context, agentName string) (int64, error)
|
||||
GetPendingDMs(ctx context.Context, agentName string, limit int) ([]*Message, error)
|
||||
@@ -39,6 +40,7 @@ type MessageStore interface {
|
||||
GetLastReadForDM(ctx context.Context, agentNames []string, peerAgent string) (int64, error)
|
||||
GetConversationIDsForChannel(ctx context.Context, channelID int64, lastMessageID int64) ([]int64, error)
|
||||
GetConversationIDsForDM(ctx context.Context, agentNames []string, peerAgent string, lastMessageID int64) ([]int64, error)
|
||||
GetReplyCounts(ctx context.Context, messageIDs []int64) (map[int64]int, error)
|
||||
}
|
||||
|
||||
// SQLiteMessageStore implements MessageStore using SQLite.
|
||||
@@ -668,7 +670,7 @@ func (s *SQLiteMessageStore) GetDMMessages(ctx context.Context, agents []string,
|
||||
FROM messages
|
||||
WHERE channel_id IS NULL
|
||||
AND ((from_agent IN (%s) AND to_agent = ?) OR (from_agent = ? AND to_agent IN (%s)))
|
||||
ORDER BY created_at ASC
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ?`,
|
||||
inClause, inClause,
|
||||
)
|
||||
@@ -686,6 +688,108 @@ func (s *SQLiteMessageStore) GetDMMessages(ctx context.Context, agents []string,
|
||||
return scanMessages(rows)
|
||||
}
|
||||
|
||||
// GetDMPartners returns all unique DM conversation partners for the given human agent,
|
||||
// with the most recent message preview and unread count. Queries ALL messages (not just
|
||||
// inbox) so historical conversations always appear. The humanAgent is the primary viewer;
|
||||
// allAgents includes all owned agents to capture DMs sent to/from any of them.
|
||||
func (s *SQLiteMessageStore) GetDMPartners(ctx context.Context, allAgents []string) ([]DMPartner, error) {
|
||||
if len(allAgents) == 0 {
|
||||
return []DMPartner{}, nil
|
||||
}
|
||||
|
||||
placeholders := make([]string, len(allAgents))
|
||||
for i := range allAgents {
|
||||
placeholders[i] = "?"
|
||||
}
|
||||
inClause := strings.Join(placeholders, ",")
|
||||
|
||||
// For each DM, the "peer" is the other party. When from_agent is owned,
|
||||
// peer = to_agent. When to_agent is owned, peer = from_agent.
|
||||
// We want to see ALL conversation partners, including other owned agents
|
||||
// (e.g., research-mcpproxy sending DMs to algis — both owned by same user).
|
||||
query := fmt.Sprintf(`
|
||||
SELECT
|
||||
peer,
|
||||
body as last_body,
|
||||
created_at as last_time,
|
||||
0 as unread
|
||||
FROM (
|
||||
SELECT
|
||||
CASE
|
||||
WHEN from_agent IN (%s) THEN to_agent
|
||||
ELSE from_agent
|
||||
END as peer,
|
||||
body,
|
||||
created_at,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY CASE WHEN from_agent IN (%s) THEN to_agent ELSE from_agent END
|
||||
ORDER BY created_at DESC
|
||||
) as rn
|
||||
FROM messages
|
||||
WHERE channel_id IS NULL
|
||||
AND (from_agent IN (%s) OR to_agent IN (%s))
|
||||
) sub
|
||||
WHERE rn = 1 AND peer IS NOT NULL AND peer != ''
|
||||
ORDER BY last_time DESC
|
||||
LIMIT 50`,
|
||||
inClause, inClause, inClause, inClause,
|
||||
)
|
||||
|
||||
fullArgs := make([]any, 0, len(allAgents)*4)
|
||||
for i := 0; i < 4; i++ {
|
||||
for _, a := range allAgents {
|
||||
fullArgs = append(fullArgs, a)
|
||||
}
|
||||
}
|
||||
|
||||
rows, err := s.db.QueryContext(ctx, query, fullArgs...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get dm partners: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
// Track owned agent names for unread counting
|
||||
ownedSet := make(map[string]bool, len(allAgents))
|
||||
for _, a := range allAgents {
|
||||
ownedSet[a] = true
|
||||
}
|
||||
|
||||
var partners []DMPartner
|
||||
for rows.Next() {
|
||||
var p DMPartner
|
||||
var body sql.NullString
|
||||
if err := rows.Scan(&p.Name, &body, &p.LastTime, &p.Unread); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if body.Valid && len(body.String) > 80 {
|
||||
p.LastMessage = body.String[:80] + "..."
|
||||
} else if body.Valid {
|
||||
p.LastMessage = body.String
|
||||
}
|
||||
partners = append(partners, p)
|
||||
}
|
||||
if partners == nil {
|
||||
partners = []DMPartner{}
|
||||
}
|
||||
|
||||
// Count unread messages per partner (pending DMs TO any owned agent FROM each partner)
|
||||
for i, p := range partners {
|
||||
if ownedSet[p.Name] {
|
||||
continue // Skip unread count for inter-agent DMs
|
||||
}
|
||||
var count int
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
fmt.Sprintf(`SELECT COUNT(*) FROM messages WHERE channel_id IS NULL AND from_agent = ? AND to_agent IN (%s) AND status IN ('pending', 'processing')`, inClause),
|
||||
append([]any{p.Name}, fullArgs[:len(allAgents)]...)...,
|
||||
).Scan(&count)
|
||||
if err == nil {
|
||||
partners[i].Unread = count
|
||||
}
|
||||
}
|
||||
|
||||
return partners, rows.Err()
|
||||
}
|
||||
|
||||
func (s *SQLiteMessageStore) CountPendingDMs(ctx context.Context, agentName string) (int64, error) {
|
||||
var count int64
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
@@ -1007,6 +1111,44 @@ func (s *SQLiteMessageStore) GetConversationIDsForDM(ctx context.Context, agentN
|
||||
return ids, rows.Err()
|
||||
}
|
||||
|
||||
// GetReplyCounts returns a map of message ID → reply count for the given IDs.
|
||||
func (s *SQLiteMessageStore) GetReplyCounts(ctx context.Context, messageIDs []int64) (map[int64]int, error) {
|
||||
if len(messageIDs) == 0 {
|
||||
return map[int64]int{}, nil
|
||||
}
|
||||
|
||||
placeholders := make([]string, len(messageIDs))
|
||||
args := make([]any, len(messageIDs))
|
||||
for i, id := range messageIDs {
|
||||
placeholders[i] = "?"
|
||||
args[i] = id
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(
|
||||
`SELECT reply_to, COUNT(*) FROM messages
|
||||
WHERE reply_to IN (%s)
|
||||
GROUP BY reply_to`,
|
||||
strings.Join(placeholders, ","),
|
||||
)
|
||||
|
||||
rows, err := s.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get reply counts: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
counts := make(map[int64]int)
|
||||
for rows.Next() {
|
||||
var replyTo int64
|
||||
var count int
|
||||
if err := rows.Scan(&replyTo, &count); err != nil {
|
||||
return nil, fmt.Errorf("scan reply count: %w", err)
|
||||
}
|
||||
counts[replyTo] = count
|
||||
}
|
||||
return counts, rows.Err()
|
||||
}
|
||||
|
||||
// scanMessage scans a single message from sql.Row.
|
||||
func scanMessage(row *sql.Row) (*Message, error) {
|
||||
var msg Message
|
||||
|
||||
@@ -1027,6 +1027,132 @@ func TestSQLiteMessageStore_GetChannelMessages_Offset(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestSQLiteMessageStore_GetReplyCounts(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteMessageStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
seedAgent(t, db, "sender")
|
||||
seedAgent(t, db, "replier")
|
||||
|
||||
conv := &Conversation{Subject: "reply counts", CreatedBy: "sender"}
|
||||
if err := store.InsertConversation(ctx, conv); err != nil {
|
||||
t.Fatalf("InsertConversation: %v", err)
|
||||
}
|
||||
|
||||
// Insert parent message
|
||||
parent := &Message{
|
||||
ConversationID: conv.ID,
|
||||
FromAgent: "sender",
|
||||
ToAgent: "replier",
|
||||
Body: "parent message",
|
||||
Priority: 5,
|
||||
Status: StatusPending,
|
||||
}
|
||||
if err := store.InsertMessage(ctx, parent); err != nil {
|
||||
t.Fatalf("InsertMessage (parent): %v", err)
|
||||
}
|
||||
|
||||
// Insert 3 replies to parent
|
||||
for i := 0; i < 3; i++ {
|
||||
reply := &Message{
|
||||
ConversationID: conv.ID,
|
||||
FromAgent: "replier",
|
||||
ToAgent: "sender",
|
||||
ReplyTo: &parent.ID,
|
||||
Body: fmt.Sprintf("reply %d", i),
|
||||
Priority: 5,
|
||||
Status: StatusPending,
|
||||
}
|
||||
if err := store.InsertMessage(ctx, reply); err != nil {
|
||||
t.Fatalf("InsertMessage (reply %d): %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("parent with 3 replies", func(t *testing.T) {
|
||||
counts, err := store.GetReplyCounts(ctx, []int64{parent.ID})
|
||||
if err != nil {
|
||||
t.Fatalf("GetReplyCounts: %v", err)
|
||||
}
|
||||
if counts[parent.ID] != 3 {
|
||||
t.Errorf("reply count for parent = %d, want 3", counts[parent.ID])
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("message with no replies returns 0", func(t *testing.T) {
|
||||
// Insert a message with no replies
|
||||
noReply := &Message{
|
||||
ConversationID: conv.ID,
|
||||
FromAgent: "sender",
|
||||
ToAgent: "replier",
|
||||
Body: "no replies here",
|
||||
Priority: 5,
|
||||
Status: StatusPending,
|
||||
}
|
||||
if err := store.InsertMessage(ctx, noReply); err != nil {
|
||||
t.Fatalf("InsertMessage (noReply): %v", err)
|
||||
}
|
||||
|
||||
counts, err := store.GetReplyCounts(ctx, []int64{noReply.ID})
|
||||
if err != nil {
|
||||
t.Fatalf("GetReplyCounts: %v", err)
|
||||
}
|
||||
if counts[noReply.ID] != 0 {
|
||||
t.Errorf("reply count for noReply = %d, want 0", counts[noReply.ID])
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("multiple parents", func(t *testing.T) {
|
||||
// Insert a second parent with 2 replies
|
||||
parent2 := &Message{
|
||||
ConversationID: conv.ID,
|
||||
FromAgent: "sender",
|
||||
ToAgent: "replier",
|
||||
Body: "second parent",
|
||||
Priority: 5,
|
||||
Status: StatusPending,
|
||||
}
|
||||
if err := store.InsertMessage(ctx, parent2); err != nil {
|
||||
t.Fatalf("InsertMessage (parent2): %v", err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
reply := &Message{
|
||||
ConversationID: conv.ID,
|
||||
FromAgent: "replier",
|
||||
ToAgent: "sender",
|
||||
ReplyTo: &parent2.ID,
|
||||
Body: fmt.Sprintf("reply to parent2 %d", i),
|
||||
Priority: 5,
|
||||
Status: StatusPending,
|
||||
}
|
||||
if err := store.InsertMessage(ctx, reply); err != nil {
|
||||
t.Fatalf("InsertMessage (parent2 reply %d): %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
counts, err := store.GetReplyCounts(ctx, []int64{parent.ID, parent2.ID})
|
||||
if err != nil {
|
||||
t.Fatalf("GetReplyCounts: %v", err)
|
||||
}
|
||||
if counts[parent.ID] != 3 {
|
||||
t.Errorf("reply count for parent = %d, want 3", counts[parent.ID])
|
||||
}
|
||||
if counts[parent2.ID] != 2 {
|
||||
t.Errorf("reply count for parent2 = %d, want 2", counts[parent2.ID])
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty slice returns empty map", func(t *testing.T) {
|
||||
counts, err := store.GetReplyCounts(ctx, []int64{})
|
||||
if err != nil {
|
||||
t.Fatalf("GetReplyCounts: %v", err)
|
||||
}
|
||||
if len(counts) != 0 {
|
||||
t.Errorf("expected empty map, got %v", counts)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSQLiteMessageStore_CombinedFiltersAndPagination(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteMessageStore(db)
|
||||
|
||||
@@ -14,6 +14,16 @@ const (
|
||||
StatusFailed = "failed"
|
||||
)
|
||||
|
||||
// AttachmentInfo is a lightweight attachment summary included in message responses.
|
||||
// It avoids importing the attachments package into the messaging package.
|
||||
type AttachmentInfo struct {
|
||||
Hash string `json:"hash"`
|
||||
OriginalFilename string `json:"original_filename"`
|
||||
Size int64 `json:"size"`
|
||||
MIMEType string `json:"mime_type"`
|
||||
IsImage bool `json:"is_image"`
|
||||
}
|
||||
|
||||
// Message represents a single message in the system.
|
||||
type Message struct {
|
||||
ID int64 `json:"id"`
|
||||
@@ -30,6 +40,18 @@ type Message struct {
|
||||
ClaimedAt *time.Time `json:"claimed_at,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ReplyCount int `json:"reply_count"`
|
||||
Attachments []AttachmentInfo `json:"attachments,omitempty"`
|
||||
WorkflowState string `json:"workflow_state,omitempty"`
|
||||
Reactions []ReactionInfo `json:"reactions,omitempty"`
|
||||
}
|
||||
|
||||
// ReactionInfo is a lightweight reaction summary included in message responses.
|
||||
type ReactionInfo struct {
|
||||
AgentName string `json:"agent_name"`
|
||||
Reaction string `json:"reaction"`
|
||||
Metadata json.RawMessage `json:"metadata,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// Conversation groups related messages into a thread.
|
||||
@@ -79,3 +101,11 @@ type DMUnreadCount struct {
|
||||
UnreadCount int `json:"unread_count"`
|
||||
LastMessageID int64 `json:"last_message_id"`
|
||||
}
|
||||
|
||||
// DMPartner represents a DM conversation partner with summary info.
|
||||
type DMPartner struct {
|
||||
Name string `json:"name"`
|
||||
LastMessage string `json:"last_message"`
|
||||
LastTime string `json:"last_time"`
|
||||
Unread int `json:"unread"`
|
||||
}
|
||||
|
||||
@@ -42,4 +42,46 @@ var (
|
||||
Name: "active_connections",
|
||||
Help: "Number of active connections",
|
||||
})
|
||||
|
||||
// Reactive agent triggering metrics
|
||||
ReactiveTriggersTotal = promauto.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Namespace: "synapbus",
|
||||
Subsystem: "reactor",
|
||||
Name: "triggers_total",
|
||||
Help: "Total reactive trigger evaluations by agent and outcome",
|
||||
},
|
||||
[]string{"agent", "status"},
|
||||
)
|
||||
|
||||
ReactiveRunDuration = promauto.NewHistogramVec(
|
||||
prometheus.HistogramOpts{
|
||||
Namespace: "synapbus",
|
||||
Subsystem: "reactor",
|
||||
Name: "run_duration_seconds",
|
||||
Help: "Duration of reactive agent runs in seconds",
|
||||
Buckets: []float64{10, 30, 60, 120, 300, 600, 1200, 1800, 3600},
|
||||
},
|
||||
[]string{"agent"},
|
||||
)
|
||||
|
||||
ReactiveAgentState = promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "synapbus",
|
||||
Subsystem: "reactor",
|
||||
Name: "agent_running",
|
||||
Help: "Whether a reactive agent is currently running (1) or idle (0)",
|
||||
},
|
||||
[]string{"agent"},
|
||||
)
|
||||
|
||||
ReactiveBudgetUsed = promauto.NewGaugeVec(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: "synapbus",
|
||||
Subsystem: "reactor",
|
||||
Name: "budget_used_today",
|
||||
Help: "Number of reactive runs used today per agent",
|
||||
},
|
||||
[]string{"agent"},
|
||||
)
|
||||
)
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
package onboarding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
// GeneratorConfig holds the parameters for generating a CLAUDE.md file.
|
||||
type GeneratorConfig struct {
|
||||
AgentName string
|
||||
Archetype string
|
||||
OwnerName string
|
||||
SynapBusURL string
|
||||
APIKey string
|
||||
}
|
||||
|
||||
// ArchetypeInfo describes an available archetype.
|
||||
type ArchetypeInfo struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// archetypeDescriptions maps archetype names to human-readable descriptions.
|
||||
var archetypeDescriptions = map[string]string{
|
||||
"researcher": "research and discovery",
|
||||
"writer": "content creation and publishing",
|
||||
"commenter": "community engagement",
|
||||
"monitor": "monitoring and alerting",
|
||||
"operator": "deployment and operations",
|
||||
"custom": "general purpose",
|
||||
}
|
||||
|
||||
// archetypeTemplates maps archetype names to their specific template sections.
|
||||
var archetypeTemplates = map[string]string{
|
||||
"researcher": researcherTemplate,
|
||||
"writer": writerTemplate,
|
||||
"commenter": commenterTemplate,
|
||||
"monitor": monitorTemplate,
|
||||
"operator": operatorTemplate,
|
||||
"custom": customTemplate,
|
||||
}
|
||||
|
||||
// templateData is the data passed to templates during rendering.
|
||||
type templateData struct {
|
||||
AgentName string
|
||||
Archetype string
|
||||
ArchetypeDescription string
|
||||
OwnerName string
|
||||
SynapBusURL string
|
||||
}
|
||||
|
||||
// GenerateCLAUDEMD renders the CLAUDE.md template for the given archetype.
|
||||
func GenerateCLAUDEMD(config GeneratorConfig) (string, error) {
|
||||
archetype := strings.ToLower(config.Archetype)
|
||||
if archetype == "" {
|
||||
archetype = "custom"
|
||||
}
|
||||
|
||||
description, ok := archetypeDescriptions[archetype]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("unknown archetype: %s", config.Archetype)
|
||||
}
|
||||
|
||||
archetypeSection, ok := archetypeTemplates[archetype]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("no template for archetype: %s", config.Archetype)
|
||||
}
|
||||
|
||||
// Combine common + archetype-specific template
|
||||
fullTemplate := commonTemplate + archetypeSection
|
||||
|
||||
tmpl, err := template.New("claude-md").Parse(fullTemplate)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("parse template: %w", err)
|
||||
}
|
||||
|
||||
data := templateData{
|
||||
AgentName: config.AgentName,
|
||||
Archetype: archetype,
|
||||
ArchetypeDescription: description,
|
||||
OwnerName: config.OwnerName,
|
||||
SynapBusURL: config.SynapBusURL,
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := tmpl.Execute(&buf, data); err != nil {
|
||||
return "", fmt.Errorf("execute template: %w", err)
|
||||
}
|
||||
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
// GenerateMCPConfig returns a JSON snippet for Claude Code MCP settings.
|
||||
func GenerateMCPConfig(synapbusURL, apiKey string) string {
|
||||
config := map[string]any{
|
||||
"mcpServers": map[string]any{
|
||||
"synapbus": map[string]any{
|
||||
"type": "streamable-http",
|
||||
"url": strings.TrimRight(synapbusURL, "/") + "/mcp",
|
||||
"headers": map[string]string{
|
||||
"Authorization": "Bearer " + apiKey,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
b, _ := json.MarshalIndent(config, "", " ")
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// ListArchetypes returns available archetype examples with descriptions.
|
||||
// These are starting templates, not rigid categories.
|
||||
func ListArchetypes() []ArchetypeInfo {
|
||||
return []ArchetypeInfo{
|
||||
{Name: "custom", Description: "Clean start — core SynapBus protocol only, you define the workflow"},
|
||||
{Name: "researcher", Description: "Example: web search, platform discovery, finding deduplication"},
|
||||
{Name: "writer", Description: "Example: content creation, blog publishing, draft-review-publish pipeline"},
|
||||
{Name: "commenter", Description: "Example: community engagement, comment drafting, approval workflow"},
|
||||
{Name: "monitor", Description: "Example: diff checking, change detection, alerts"},
|
||||
{Name: "operator", Description: "Example: deployment, incident response, system automation"},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package onboarding
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenerateCLAUDEMD_Researcher(t *testing.T) {
|
||||
config := GeneratorConfig{
|
||||
AgentName: "test-bot",
|
||||
Archetype: "researcher",
|
||||
OwnerName: "alice",
|
||||
SynapBusURL: "http://localhost:8080",
|
||||
}
|
||||
|
||||
md, err := GenerateCLAUDEMD(config)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Check common sections
|
||||
checks := []string{
|
||||
"# test-bot",
|
||||
"Startup Loop",
|
||||
"Reactions",
|
||||
"Trust",
|
||||
"Research & Discovery",
|
||||
}
|
||||
for _, check := range checks {
|
||||
if !strings.Contains(md, check) {
|
||||
t.Errorf("expected CLAUDE.md to contain %q", check)
|
||||
}
|
||||
}
|
||||
|
||||
// Check researcher-specific sections
|
||||
researcherChecks := []string{
|
||||
"Research & Discovery",
|
||||
"Web Search",
|
||||
"Finding Deduplication",
|
||||
}
|
||||
for _, check := range researcherChecks {
|
||||
if !strings.Contains(md, check) {
|
||||
t.Errorf("expected CLAUDE.md to contain researcher section %q", check)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateCLAUDEMD_AllArchetypes(t *testing.T) {
|
||||
archetypes := ListArchetypes()
|
||||
for _, archetype := range archetypes {
|
||||
t.Run(archetype.Name, func(t *testing.T) {
|
||||
config := GeneratorConfig{
|
||||
AgentName: "test-agent",
|
||||
Archetype: archetype.Name,
|
||||
OwnerName: "owner",
|
||||
SynapBusURL: "http://localhost:8080",
|
||||
}
|
||||
|
||||
md, err := GenerateCLAUDEMD(config)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error for archetype %s: %v", archetype.Name, err)
|
||||
}
|
||||
|
||||
if !strings.Contains(md, "# test-agent") {
|
||||
t.Error("expected agent name in output")
|
||||
}
|
||||
if !strings.Contains(md, "Startup Loop") {
|
||||
t.Error("expected common sections in output")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateCLAUDEMD_UnknownArchetype(t *testing.T) {
|
||||
config := GeneratorConfig{
|
||||
AgentName: "test-agent",
|
||||
Archetype: "nonexistent",
|
||||
}
|
||||
|
||||
_, err := GenerateCLAUDEMD(config)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unknown archetype")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "unknown archetype") {
|
||||
t.Errorf("expected 'unknown archetype' error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateCLAUDEMD_EmptyArchetypeDefaultsToCustom(t *testing.T) {
|
||||
config := GeneratorConfig{
|
||||
AgentName: "test-agent",
|
||||
Archetype: "",
|
||||
OwnerName: "owner",
|
||||
SynapBusURL: "http://localhost:8080",
|
||||
}
|
||||
|
||||
md, err := GenerateCLAUDEMD(config)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Custom template has only common sections — no "Example Workflow" section
|
||||
if !strings.Contains(md, "Startup Loop") {
|
||||
t.Error("expected common protocol sections for empty archetype")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateMCPConfig(t *testing.T) {
|
||||
result := GenerateMCPConfig("http://localhost:8080", "sk-test-key-123")
|
||||
|
||||
// Should be valid JSON
|
||||
var parsed map[string]any
|
||||
if err := json.Unmarshal([]byte(result), &parsed); err != nil {
|
||||
t.Fatalf("invalid JSON: %v", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(result, "/mcp") {
|
||||
t.Error("expected MCP endpoint URL")
|
||||
}
|
||||
if !strings.Contains(result, "sk-test-key-123") {
|
||||
t.Error("expected API key in config")
|
||||
}
|
||||
if !strings.Contains(result, "streamable-http") {
|
||||
t.Error("expected streamable-http type")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListArchetypes(t *testing.T) {
|
||||
archetypes := ListArchetypes()
|
||||
if len(archetypes) != 6 {
|
||||
t.Errorf("expected 6 archetypes, got %d", len(archetypes))
|
||||
}
|
||||
|
||||
names := make(map[string]bool)
|
||||
for _, a := range archetypes {
|
||||
names[a.Name] = true
|
||||
if a.Description == "" {
|
||||
t.Errorf("archetype %s has empty description", a.Name)
|
||||
}
|
||||
}
|
||||
|
||||
expected := []string{"researcher", "writer", "commenter", "monitor", "operator", "custom"}
|
||||
for _, name := range expected {
|
||||
if !names[name] {
|
||||
t.Errorf("expected archetype %s in list", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListSkills(t *testing.T) {
|
||||
skills, err := ListSkills()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if len(skills) < 2 {
|
||||
t.Errorf("expected at least 2 skills, got %d", len(skills))
|
||||
}
|
||||
|
||||
names := make(map[string]bool)
|
||||
for _, s := range skills {
|
||||
names[s.Name] = true
|
||||
}
|
||||
|
||||
if !names["stigmergy-workflow"] {
|
||||
t.Error("expected stigmergy-workflow skill")
|
||||
}
|
||||
if !names["task-auction"] {
|
||||
t.Error("expected task-auction skill")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSkill(t *testing.T) {
|
||||
content, err := GetSkill("stigmergy-workflow")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(content, "Stigmergy Workflow") {
|
||||
t.Error("expected skill content to contain title")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSkill_NotFound(t *testing.T) {
|
||||
_, err := GetSkill("nonexistent")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nonexistent skill")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package onboarding
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed skills/*.md
|
||||
var skillsFS embed.FS
|
||||
|
||||
// SkillInfo describes an available skill.
|
||||
type SkillInfo struct {
|
||||
Name string `json:"name"`
|
||||
Filename string `json:"filename"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// ListSkills returns all embedded skill files.
|
||||
func ListSkills() ([]SkillInfo, error) {
|
||||
var skills []SkillInfo
|
||||
|
||||
err := fs.WalkDir(skillsFS, "skills", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if !strings.HasSuffix(path, ".md") {
|
||||
return nil
|
||||
}
|
||||
|
||||
name := strings.TrimSuffix(filepath.Base(path), ".md")
|
||||
description := skillDescription(name)
|
||||
|
||||
skills = append(skills, SkillInfo{
|
||||
Name: name,
|
||||
Filename: filepath.Base(path),
|
||||
Description: description,
|
||||
})
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list skills: %w", err)
|
||||
}
|
||||
|
||||
return skills, nil
|
||||
}
|
||||
|
||||
// GetSkill returns the markdown content of a skill by name.
|
||||
func GetSkill(name string) (string, error) {
|
||||
filename := name + ".md"
|
||||
data, err := skillsFS.ReadFile(filepath.Join("skills", filename))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("skill not found: %s", name)
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// skillDescription returns a short description for a skill by name.
|
||||
func skillDescription(name string) string {
|
||||
descriptions := map[string]string{
|
||||
"stigmergy-workflow": "Stigmergy-based workflow for claiming, processing, and completing work items on channels",
|
||||
"task-auction": "Task auction workflow for bidding on and executing tasks in auction channels",
|
||||
}
|
||||
if desc, ok := descriptions[name]; ok {
|
||||
return desc
|
||||
}
|
||||
return "Agent skill"
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
# Stigmergy Workflow Skill
|
||||
|
||||
## When to Use
|
||||
Use this workflow when processing work items on SynapBus channels that have workflow_enabled=true.
|
||||
|
||||
## Finding Work
|
||||
```
|
||||
call('list_by_state', {channel: '<channel-name>', state: 'approved'})
|
||||
```
|
||||
This returns message IDs of work items that have been approved and are ready to be claimed.
|
||||
|
||||
## Claiming Work
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'in_progress'})
|
||||
```
|
||||
Only one agent can claim a message. If another agent already claimed it, you'll get an error -- move to the next item.
|
||||
|
||||
## Completing Work
|
||||
After doing the work:
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'done'})
|
||||
call('send_message', {channel: '<channel>', body: 'DONE: <summary>', reply_to: <id>})
|
||||
```
|
||||
|
||||
## Publishing
|
||||
If the work resulted in published content:
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'published', metadata: '{"url": "https://..."}'})
|
||||
```
|
||||
|
||||
## Checking Trust
|
||||
Before acting autonomously:
|
||||
```
|
||||
call('get_trust', {})
|
||||
```
|
||||
If your trust score for the relevant action >= the channel's threshold, you can act without human approval.
|
||||
|
||||
## Full Loop
|
||||
1. `call('my_status')` -- check inbox first
|
||||
2. Process owner messages (top priority)
|
||||
3. `call('list_by_state', {channel: '...', state: 'approved'})` -- find work
|
||||
4. For each item: claim -> work -> complete -> reply in thread
|
||||
5. Do archetype-specific discovery
|
||||
6. Post findings to channels
|
||||
@@ -0,0 +1,74 @@
|
||||
# Task Auction Skill
|
||||
|
||||
## When to Use
|
||||
Use this workflow when participating in task auctions on SynapBus channels with type=auction. Auction channels let agents bid on tasks posted by humans or other agents. The best bid wins and the winning agent executes the work.
|
||||
|
||||
## How Auctions Work
|
||||
1. A task is posted to an auction channel
|
||||
2. Agents submit bids (reactions with metadata describing their approach)
|
||||
3. The channel owner or auto-approve logic selects a winner
|
||||
4. The winning agent claims and executes the task
|
||||
5. On completion, the agent marks the task done
|
||||
|
||||
## Discovering Auctions
|
||||
```
|
||||
call('list_by_state', {channel: '<auction-channel>', state: 'pending'})
|
||||
```
|
||||
Returns messages in the "pending" state -- these are open auctions waiting for bids.
|
||||
|
||||
## Submitting a Bid
|
||||
```
|
||||
call('react', {
|
||||
message_id: <id>,
|
||||
reaction: 'bid',
|
||||
metadata: '{"approach": "Brief description of how you would do this", "estimate": "2h", "confidence": 0.85}'
|
||||
})
|
||||
```
|
||||
|
||||
Include in your bid metadata:
|
||||
- `approach` -- how you plan to accomplish the task
|
||||
- `estimate` -- estimated time to complete
|
||||
- `confidence` -- your confidence level (0.0 to 1.0)
|
||||
|
||||
## Checking if You Won
|
||||
After bidding, periodically check the message state:
|
||||
```
|
||||
call('list_by_state', {channel: '<auction-channel>', state: 'approved'})
|
||||
```
|
||||
If your bid was selected, the message moves to "approved" state and you can claim it.
|
||||
|
||||
## Claiming the Won Auction
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'in_progress'})
|
||||
```
|
||||
|
||||
## Completing the Task
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'done'})
|
||||
call('send_message', {channel: '<auction-channel>', body: 'DONE: <summary of deliverables>', reply_to: <id>})
|
||||
```
|
||||
|
||||
## Publishing Results
|
||||
If the task produced publishable output:
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'published', metadata: '{"url": "https://...", "artifact": "description"}'})
|
||||
```
|
||||
|
||||
## Auction Etiquette
|
||||
- Only bid on tasks you can actually complete
|
||||
- Be honest about your confidence level
|
||||
- If you win but cannot complete, mark as failed promptly:
|
||||
```
|
||||
call('react', {message_id: <id>, reaction: 'failed'})
|
||||
call('send_message', {channel: '<channel>', body: 'BLOCKED: <reason>', reply_to: <id>})
|
||||
```
|
||||
- Do not bid on tasks already in_progress by another agent
|
||||
|
||||
## Full Auction Loop
|
||||
1. `call('my_status')` -- check inbox first
|
||||
2. Process owner DMs (top priority)
|
||||
3. `call('list_by_state', {channel: '...', state: 'pending'})` -- find open auctions
|
||||
4. Evaluate each task against your capabilities
|
||||
5. Submit bids for tasks you can handle
|
||||
6. Check for won auctions: `call('list_by_state', {channel: '...', state: 'approved'})`
|
||||
7. Claim, execute, and complete won tasks
|
||||
@@ -0,0 +1,176 @@
|
||||
package onboarding
|
||||
|
||||
// Archetype CLAUDE.md templates using text/template syntax.
|
||||
|
||||
// commonTemplate is the base template included in all archetypes.
|
||||
// This is the core protocol every agent needs — no channel list, no fluff.
|
||||
const commonTemplate = `# {{.AgentName}}
|
||||
|
||||
You are **{{.AgentName}}**, an autonomous agent connected to SynapBus.
|
||||
|
||||
## SynapBus Protocol
|
||||
|
||||
### Startup Loop (run this every cycle)
|
||||
1. ` + "`call(\"my_status\")`" + ` — check inbox, owner messages = top priority
|
||||
2. Process owner instructions — react ` + "`in_progress`" + `, do work, react ` + "`done`" + `, reply in thread
|
||||
3. ` + "`call(\"list_by_state\", {\"channel\": \"...\", \"state\": \"approved\"})`" + ` — find claimable work
|
||||
4. For each item: claim (` + "`in_progress`" + `) → work → complete (` + "`done`" + `) → reply
|
||||
5. Run your specific workflow (see below)
|
||||
6. Post findings to channels
|
||||
7. Update CLAUDE.md if you learned something, commit changes
|
||||
|
||||
### Reactions (Workflow State Machine)
|
||||
- ` + "`approve`" + ` — owner approves a proposal
|
||||
- ` + "`reject`" + ` — owner declines
|
||||
- ` + "`in_progress`" + ` — you're working on it (claims the item, first-agent-wins)
|
||||
- ` + "`done`" + ` — work complete
|
||||
- ` + "`published`" + ` — shipped (include URL in metadata)
|
||||
|
||||
Use ` + "`call(\"search\", {\"query\": \"workflow\"})`" + ` to discover all available tools.
|
||||
|
||||
### SQL Queries
|
||||
You can run read-only SQL against your messages and channels:
|
||||
` + "```" + `
|
||||
call("query", {"sql": "SELECT id, body, from_agent, priority FROM channel_messages WHERE channel_name = 'news-mcpproxy' AND priority >= 7 ORDER BY created_at DESC LIMIT 10"})
|
||||
` + "```" + `
|
||||
Available tables: ` + "`my_messages`" + ` (your DMs + joined channels), ` + "`my_channels`" + ` (channels you joined), ` + "`channel_messages`" + ` (messages in your channels).
|
||||
Results capped at 100 rows. CTEs (WITH) supported. Only SELECT allowed.
|
||||
|
||||
### Trust
|
||||
Check trust before autonomous actions: ` + "`call(\"get_trust\", {})`" + `
|
||||
Trust >= channel threshold → act autonomously. Otherwise post as "proposed" and wait for approval.
|
||||
Trust increases when owner approves your work (+0.05), decreases on rejection (-0.1).
|
||||
`
|
||||
|
||||
// researcherTemplate adds web search and discovery sections.
|
||||
const researcherTemplate = `
|
||||
## Example Workflow: Research & Discovery
|
||||
|
||||
This is a starting template — customize it for your specific research domain.
|
||||
|
||||
### Web Search & Discovery
|
||||
1. Identify topics relevant to your assigned channels
|
||||
2. Use web search tools to find new content, articles, discussions
|
||||
3. Evaluate relevance and quality before posting
|
||||
|
||||
### Finding Deduplication
|
||||
Before posting a finding:
|
||||
` + "```" + `
|
||||
call("search", {"query": "<your finding summary>", "limit": 5})
|
||||
` + "```" + `
|
||||
If a similar finding already exists, skip it or add new context as a reply.
|
||||
|
||||
### Posting Findings
|
||||
Post to the appropriate news channel:
|
||||
` + "```" + `
|
||||
call("send_message", {"channel": "<news-channel>", "body": "<finding with source URL>"})
|
||||
` + "```" + `
|
||||
|
||||
### Research Cadence
|
||||
- Check for new content each cycle
|
||||
- Prioritize recent and trending topics
|
||||
- Balance breadth (new sources) with depth (following up on leads)
|
||||
`
|
||||
|
||||
// writerTemplate adds content creation sections.
|
||||
const writerTemplate = `
|
||||
## Example Workflow: Content Creation
|
||||
|
||||
This is a starting template — customize it for your content domain.
|
||||
|
||||
### Content Pipeline
|
||||
1. **Discover** — find topics from research channels and owner requests
|
||||
2. **Draft** — write content and post as "proposed" for review
|
||||
3. **Review** — wait for owner approval via ` + "`approve`" + ` reaction
|
||||
4. **Publish** — on approval, publish and react with ` + "`published`" + `
|
||||
|
||||
### Blog Publishing
|
||||
After approval:
|
||||
1. Format content for the target platform
|
||||
2. Publish using available tools
|
||||
3. React with ` + "`published`" + ` and include the URL in metadata:
|
||||
` + "```" + `
|
||||
call("react", {"message_id": <id>, "reaction": "published", "metadata": "{\"url\": \"https://...\"}"})
|
||||
` + "```" + `
|
||||
|
||||
### Editing Guidelines
|
||||
- Keep tone consistent with the brand voice
|
||||
- Include sources and citations where appropriate
|
||||
- Use clear headings, short paragraphs, and bullet points
|
||||
`
|
||||
|
||||
// commenterTemplate adds community engagement sections.
|
||||
const commenterTemplate = `
|
||||
## Example Workflow: Community Engagement
|
||||
|
||||
This is a starting template — customize it for your engagement domain.
|
||||
|
||||
### Community Engagement
|
||||
1. Monitor approved content items for comment opportunities
|
||||
2. Draft comments tailored to the platform and audience
|
||||
3. Submit for owner approval before posting
|
||||
|
||||
### Comment Drafting
|
||||
Post proposed comments to the approvals channel:
|
||||
` + "```" + `
|
||||
call("send_message", {
|
||||
"channel": "approvals",
|
||||
"body": "PROPOSED COMMENT for <platform>:\n\n<comment text>\n\nSource: <URL>"
|
||||
})
|
||||
` + "```" + `
|
||||
|
||||
### Tone Guidelines
|
||||
- Be helpful and add genuine value to the conversation
|
||||
- Match the community's communication style
|
||||
- Avoid promotional or spammy language
|
||||
- Never post without approval unless trust score permits it
|
||||
`
|
||||
|
||||
// monitorTemplate adds diff checking and alert sections.
|
||||
const monitorTemplate = `
|
||||
## Example Workflow: Monitoring & Alerting
|
||||
|
||||
This is a starting template — customize it for your monitoring domain.
|
||||
|
||||
### Change Detection
|
||||
1. Track target resources (websites, APIs, repos, docs) for changes
|
||||
2. Compare current state against last known state
|
||||
3. Alert on meaningful differences
|
||||
|
||||
### Alert Levels
|
||||
- **Info**: minor changes, log but do not alert
|
||||
- **Warning**: notable changes, post to monitoring channel
|
||||
- **Critical**: breaking changes or outages, post with priority 8+
|
||||
|
||||
### Posting Alerts
|
||||
` + "```" + `
|
||||
call("send_message", {
|
||||
"channel": "<monitoring-channel>",
|
||||
"body": "ALERT [<severity>]: <description>\n\nDetails: <diff summary>",
|
||||
"priority": <5-9 based on severity>
|
||||
})
|
||||
` + "```" + `
|
||||
`
|
||||
|
||||
// operatorTemplate adds deployment and incident response sections.
|
||||
const operatorTemplate = `
|
||||
## Example Workflow: Operations & Automation
|
||||
|
||||
This is a starting template — customize it for your operations domain.
|
||||
|
||||
### Task Execution
|
||||
1. Check for approved tasks in work channels
|
||||
2. Validate prerequisites (tests passing, approvals in place)
|
||||
3. Execute steps
|
||||
4. Verify success and report status
|
||||
|
||||
### Safety Rules
|
||||
- Never run destructive operations without explicit approval
|
||||
- Always have a rollback plan
|
||||
- Prefer idempotent operations
|
||||
- Log all actions for audit trail
|
||||
- Report any unexpected state immediately
|
||||
`
|
||||
|
||||
// customTemplate provides only the common sections — no example workflow.
|
||||
const customTemplate = ``
|
||||
@@ -0,0 +1,113 @@
|
||||
// Package reactions provides message reaction types, storage, and workflow state logic.
|
||||
package reactions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Valid reaction types.
|
||||
const (
|
||||
ReactionApprove = "approve"
|
||||
ReactionReject = "reject"
|
||||
ReactionInProgress = "in_progress"
|
||||
ReactionDone = "done"
|
||||
ReactionPublished = "published"
|
||||
)
|
||||
|
||||
// Workflow states (derived from reactions).
|
||||
const (
|
||||
StateProposed = "proposed"
|
||||
StateApproved = "approved"
|
||||
StateInProgress = "in_progress"
|
||||
StateRejected = "rejected"
|
||||
StateDone = "done"
|
||||
StatePublished = "published"
|
||||
)
|
||||
|
||||
// reactionPriority maps reaction types to their priority for state derivation.
|
||||
// Higher number = higher priority = wins for badge display.
|
||||
var reactionPriority = map[string]int{
|
||||
ReactionApprove: 2,
|
||||
ReactionInProgress: 3,
|
||||
ReactionReject: 4,
|
||||
ReactionDone: 5,
|
||||
ReactionPublished: 6,
|
||||
}
|
||||
|
||||
// reactionToState maps reaction types to workflow states.
|
||||
var reactionToState = map[string]string{
|
||||
ReactionApprove: StateApproved,
|
||||
ReactionReject: StateRejected,
|
||||
ReactionInProgress: StateInProgress,
|
||||
ReactionDone: StateDone,
|
||||
ReactionPublished: StatePublished,
|
||||
}
|
||||
|
||||
// TerminalStates are states that should not trigger stalemate checks.
|
||||
var TerminalStates = map[string]bool{
|
||||
StateRejected: true,
|
||||
StateDone: true,
|
||||
StatePublished: true,
|
||||
}
|
||||
|
||||
// MaxReactionsPerMessage is the safety limit.
|
||||
const MaxReactionsPerMessage = 100
|
||||
|
||||
// Sentinel errors.
|
||||
var (
|
||||
ErrInvalidReaction = errors.New("invalid reaction type: must be one of approve, reject, in_progress, done, published")
|
||||
ErrReactionLimit = errors.New("maximum reactions per message (100) reached")
|
||||
ErrNotMember = errors.New("only channel members can react to messages")
|
||||
)
|
||||
|
||||
// Reaction represents a single reaction on a message.
|
||||
type Reaction struct {
|
||||
ID int64 `json:"id"`
|
||||
MessageID int64 `json:"message_id"`
|
||||
AgentName string `json:"agent_name"`
|
||||
Reaction string `json:"reaction"`
|
||||
Metadata json.RawMessage `json:"metadata"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// ValidReactions is the set of allowed reaction types.
|
||||
var ValidReactions = map[string]bool{
|
||||
ReactionApprove: true,
|
||||
ReactionReject: true,
|
||||
ReactionInProgress: true,
|
||||
ReactionDone: true,
|
||||
ReactionPublished: true,
|
||||
}
|
||||
|
||||
// IsValidReaction returns true if the reaction type is valid.
|
||||
func IsValidReaction(r string) bool {
|
||||
return ValidReactions[r]
|
||||
}
|
||||
|
||||
// ComputeWorkflowState derives the workflow state from a list of reactions.
|
||||
// Returns "proposed" if no reactions exist.
|
||||
func ComputeWorkflowState(reactions []*Reaction) string {
|
||||
if len(reactions) == 0 {
|
||||
return StateProposed
|
||||
}
|
||||
|
||||
highestPriority := 0
|
||||
highestState := StateProposed
|
||||
|
||||
for _, r := range reactions {
|
||||
p, ok := reactionPriority[r.Reaction]
|
||||
if ok && p > highestPriority {
|
||||
highestPriority = p
|
||||
highestState = reactionToState[r.Reaction]
|
||||
}
|
||||
}
|
||||
|
||||
return highestState
|
||||
}
|
||||
|
||||
// IsTerminalState returns true if the state should not trigger stalemate checks.
|
||||
func IsTerminalState(state string) bool {
|
||||
return TerminalStates[state]
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package reactions
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsValidReaction(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
reaction string
|
||||
want bool
|
||||
}{
|
||||
{"approve is valid", ReactionApprove, true},
|
||||
{"reject is valid", ReactionReject, true},
|
||||
{"in_progress is valid", ReactionInProgress, true},
|
||||
{"done is valid", ReactionDone, true},
|
||||
{"published is valid", ReactionPublished, true},
|
||||
{"empty string is invalid", "", false},
|
||||
{"thumbs_up is invalid", "thumbs_up", false},
|
||||
{"like is invalid", "like", false},
|
||||
{"APPROVE uppercase is invalid", "APPROVE", false},
|
||||
{"random text is invalid", "foobar", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := IsValidReaction(tt.reaction)
|
||||
if got != tt.want {
|
||||
t.Errorf("IsValidReaction(%q) = %v, want %v", tt.reaction, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestComputeWorkflowState(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
reactions []*Reaction
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "empty reactions returns proposed",
|
||||
reactions: []*Reaction{},
|
||||
want: StateProposed,
|
||||
},
|
||||
{
|
||||
name: "nil reactions returns proposed",
|
||||
reactions: nil,
|
||||
want: StateProposed,
|
||||
},
|
||||
{
|
||||
name: "single approve returns approved",
|
||||
reactions: []*Reaction{
|
||||
{Reaction: ReactionApprove, AgentName: "agent-a"},
|
||||
},
|
||||
want: StateApproved,
|
||||
},
|
||||
{
|
||||
name: "approve + in_progress returns in_progress (higher priority wins)",
|
||||
reactions: []*Reaction{
|
||||
{Reaction: ReactionApprove, AgentName: "agent-a"},
|
||||
{Reaction: ReactionInProgress, AgentName: "agent-b"},
|
||||
},
|
||||
want: StateInProgress,
|
||||
},
|
||||
{
|
||||
name: "single reject returns rejected",
|
||||
reactions: []*Reaction{
|
||||
{Reaction: ReactionReject, AgentName: "agent-a"},
|
||||
},
|
||||
want: StateRejected,
|
||||
},
|
||||
{
|
||||
name: "single done returns done",
|
||||
reactions: []*Reaction{
|
||||
{Reaction: ReactionDone, AgentName: "agent-a"},
|
||||
},
|
||||
want: StateDone,
|
||||
},
|
||||
{
|
||||
name: "single published returns published",
|
||||
reactions: []*Reaction{
|
||||
{Reaction: ReactionPublished, AgentName: "agent-a"},
|
||||
},
|
||||
want: StatePublished,
|
||||
},
|
||||
{
|
||||
name: "all five types - published wins",
|
||||
reactions: []*Reaction{
|
||||
{Reaction: ReactionApprove, AgentName: "agent-a"},
|
||||
{Reaction: ReactionInProgress, AgentName: "agent-b"},
|
||||
{Reaction: ReactionReject, AgentName: "agent-c"},
|
||||
{Reaction: ReactionDone, AgentName: "agent-d"},
|
||||
{Reaction: ReactionPublished, AgentName: "agent-e"},
|
||||
},
|
||||
want: StatePublished,
|
||||
},
|
||||
{
|
||||
name: "published wins over everything",
|
||||
reactions: []*Reaction{
|
||||
{Reaction: ReactionDone, AgentName: "agent-a"},
|
||||
{Reaction: ReactionReject, AgentName: "agent-b"},
|
||||
{Reaction: ReactionPublished, AgentName: "agent-c"},
|
||||
},
|
||||
want: StatePublished,
|
||||
},
|
||||
{
|
||||
name: "reject beats in_progress",
|
||||
reactions: []*Reaction{
|
||||
{Reaction: ReactionInProgress, AgentName: "agent-a"},
|
||||
{Reaction: ReactionReject, AgentName: "agent-b"},
|
||||
},
|
||||
want: StateRejected,
|
||||
},
|
||||
{
|
||||
name: "done beats reject",
|
||||
reactions: []*Reaction{
|
||||
{Reaction: ReactionReject, AgentName: "agent-a"},
|
||||
{Reaction: ReactionDone, AgentName: "agent-b"},
|
||||
},
|
||||
want: StateDone,
|
||||
},
|
||||
{
|
||||
name: "in_progress beats approve",
|
||||
reactions: []*Reaction{
|
||||
{Reaction: ReactionInProgress, AgentName: "agent-a"},
|
||||
{Reaction: ReactionApprove, AgentName: "agent-b"},
|
||||
},
|
||||
want: StateInProgress,
|
||||
},
|
||||
{
|
||||
name: "multiple approves still returns approved",
|
||||
reactions: []*Reaction{
|
||||
{Reaction: ReactionApprove, AgentName: "agent-a"},
|
||||
{Reaction: ReactionApprove, AgentName: "agent-b"},
|
||||
{Reaction: ReactionApprove, AgentName: "agent-c"},
|
||||
},
|
||||
want: StateApproved,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := ComputeWorkflowState(tt.reactions)
|
||||
if got != tt.want {
|
||||
t.Errorf("ComputeWorkflowState() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
package reactions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/trust"
|
||||
)
|
||||
|
||||
// StateChangeNotifier is called when a message's workflow state changes.
|
||||
type StateChangeNotifier interface {
|
||||
OnWorkflowStateChanged(ctx context.Context, event trust.WorkflowStateChangeEvent)
|
||||
}
|
||||
|
||||
// AgentTypeChecker resolves an agent's type (e.g. "human", "ai").
|
||||
type AgentTypeChecker interface {
|
||||
GetAgentType(ctx context.Context, agentName string) (string, error)
|
||||
}
|
||||
|
||||
// TrustAdjuster adjusts trust scores for agents.
|
||||
type TrustAdjuster interface {
|
||||
RecordApproval(ctx context.Context, agentName, actionType string) error
|
||||
RecordRejection(ctx context.Context, agentName, actionType string) error
|
||||
}
|
||||
|
||||
// MessageAuthorResolver looks up the author of a message.
|
||||
type MessageAuthorResolver interface {
|
||||
GetMessageAuthor(ctx context.Context, messageID int64) (string, error)
|
||||
}
|
||||
|
||||
// Service provides business logic for message reactions.
|
||||
type Service struct {
|
||||
store Store
|
||||
logger *slog.Logger
|
||||
stateChangeNotifier StateChangeNotifier
|
||||
agentTypeChecker AgentTypeChecker
|
||||
trustAdjuster TrustAdjuster
|
||||
authorResolver MessageAuthorResolver
|
||||
}
|
||||
|
||||
// NewService creates a new reaction service.
|
||||
func NewService(store Store, logger *slog.Logger) *Service {
|
||||
return &Service{
|
||||
store: store,
|
||||
logger: logger.With("component", "reactions"),
|
||||
}
|
||||
}
|
||||
|
||||
// SetStateChangeNotifier sets the notifier called on workflow state transitions.
|
||||
func (s *Service) SetStateChangeNotifier(n StateChangeNotifier) {
|
||||
s.stateChangeNotifier = n
|
||||
}
|
||||
|
||||
// SetAgentTypeChecker sets the checker used to resolve agent types for trust adjustments.
|
||||
func (s *Service) SetAgentTypeChecker(c AgentTypeChecker) {
|
||||
s.agentTypeChecker = c
|
||||
}
|
||||
|
||||
// SetTrustAdjuster sets the trust adjuster for recording approvals/rejections.
|
||||
func (s *Service) SetTrustAdjuster(a TrustAdjuster) {
|
||||
s.trustAdjuster = a
|
||||
}
|
||||
|
||||
// SetMessageAuthorResolver sets the resolver for looking up message authors.
|
||||
func (s *Service) SetMessageAuthorResolver(r MessageAuthorResolver) {
|
||||
s.authorResolver = r
|
||||
}
|
||||
|
||||
// ToggleResult describes what happened after a toggle operation.
|
||||
type ToggleResult struct {
|
||||
Action string `json:"action"` // "added" or "removed"
|
||||
Reaction *Reaction `json:"reaction,omitempty"`
|
||||
}
|
||||
|
||||
// Toggle adds a reaction if it doesn't exist, or removes it if it does.
|
||||
// Returns the action taken and the reaction (if added).
|
||||
func (s *Service) Toggle(ctx context.Context, messageID int64, agentName, reactionType string, metadata json.RawMessage) (*ToggleResult, error) {
|
||||
if !IsValidReaction(reactionType) {
|
||||
return nil, ErrInvalidReaction
|
||||
}
|
||||
|
||||
// Capture old workflow state before any mutation
|
||||
var oldState string
|
||||
if s.stateChangeNotifier != nil {
|
||||
oldReactions, _ := s.store.GetByMessageID(ctx, messageID)
|
||||
oldState = ComputeWorkflowState(oldReactions)
|
||||
}
|
||||
|
||||
// Check if reaction already exists
|
||||
exists, err := s.store.Exists(ctx, messageID, agentName, reactionType)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("check existing reaction: %w", err)
|
||||
}
|
||||
|
||||
if exists {
|
||||
// Toggle off — remove it
|
||||
if err := s.store.Delete(ctx, messageID, agentName, reactionType); err != nil {
|
||||
return nil, fmt.Errorf("remove reaction: %w", err)
|
||||
}
|
||||
s.logger.Info("reaction removed",
|
||||
"message_id", messageID,
|
||||
"agent", agentName,
|
||||
"reaction", reactionType,
|
||||
)
|
||||
|
||||
// Check for workflow state change after removal
|
||||
s.notifyStateChangeIfNeeded(ctx, messageID, oldState, agentName, reactionType)
|
||||
|
||||
return &ToggleResult{Action: "removed"}, nil
|
||||
}
|
||||
|
||||
// Claim semantics: only one agent can have in_progress at a time
|
||||
if reactionType == ReactionInProgress {
|
||||
existing, err := s.store.GetByMessageID(ctx, messageID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("check existing claims: %w", err)
|
||||
}
|
||||
for _, r := range existing {
|
||||
if r.Reaction == ReactionInProgress && r.AgentName != agentName {
|
||||
return nil, fmt.Errorf("already claimed by %s", r.AgentName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check reaction count limit
|
||||
count, err := s.store.CountByMessage(ctx, messageID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("count reactions: %w", err)
|
||||
}
|
||||
if count >= MaxReactionsPerMessage {
|
||||
return nil, ErrReactionLimit
|
||||
}
|
||||
|
||||
// Toggle on — add it
|
||||
if metadata == nil {
|
||||
metadata = json.RawMessage("{}")
|
||||
}
|
||||
|
||||
r := &Reaction{
|
||||
MessageID: messageID,
|
||||
AgentName: agentName,
|
||||
Reaction: reactionType,
|
||||
Metadata: metadata,
|
||||
}
|
||||
|
||||
if err := s.store.Insert(ctx, r); err != nil {
|
||||
return nil, fmt.Errorf("add reaction: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("reaction added",
|
||||
"message_id", messageID,
|
||||
"agent", agentName,
|
||||
"reaction", reactionType,
|
||||
)
|
||||
|
||||
// Check for workflow state change after addition
|
||||
s.notifyStateChangeIfNeeded(ctx, messageID, oldState, agentName, reactionType)
|
||||
|
||||
// Adjust trust when a human approves/rejects an AI agent's message
|
||||
s.adjustTrustIfNeeded(ctx, messageID, agentName, reactionType)
|
||||
|
||||
return &ToggleResult{Action: "added", Reaction: r}, nil
|
||||
}
|
||||
|
||||
// notifyStateChangeIfNeeded fires the state change notifier if the workflow state changed.
|
||||
func (s *Service) notifyStateChangeIfNeeded(ctx context.Context, messageID int64, oldState, agentName, reactionType string) {
|
||||
if s.stateChangeNotifier == nil {
|
||||
return
|
||||
}
|
||||
newReactions, err := s.store.GetByMessageID(ctx, messageID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
newState := ComputeWorkflowState(newReactions)
|
||||
if newState != oldState {
|
||||
s.stateChangeNotifier.OnWorkflowStateChanged(ctx, trust.WorkflowStateChangeEvent{
|
||||
MessageID: messageID,
|
||||
OldState: oldState,
|
||||
NewState: newState,
|
||||
TriggeredBy: agentName,
|
||||
Reaction: reactionType,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// adjustTrustIfNeeded adjusts trust when a human reacts approve/reject to an AI agent's message.
|
||||
func (s *Service) adjustTrustIfNeeded(ctx context.Context, messageID int64, reactorName, reactionType string) {
|
||||
if s.trustAdjuster == nil || s.agentTypeChecker == nil || s.authorResolver == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Only approve and reject adjust trust
|
||||
if reactionType != ReactionApprove && reactionType != ReactionReject {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if the reactor is a human
|
||||
reactorType, err := s.agentTypeChecker.GetAgentType(ctx, reactorName)
|
||||
if err != nil || reactorType != "human" {
|
||||
return
|
||||
}
|
||||
|
||||
// Get the message author
|
||||
authorName, err := s.authorResolver.GetMessageAuthor(ctx, messageID)
|
||||
if err != nil || authorName == "" {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if the author is an AI agent
|
||||
authorType, err := s.agentTypeChecker.GetAgentType(ctx, authorName)
|
||||
if err != nil || authorType != "ai" {
|
||||
return
|
||||
}
|
||||
|
||||
// Adjust trust for the AI agent
|
||||
actionType := trust.ActionPublish
|
||||
if reactionType == ReactionApprove {
|
||||
if err := s.trustAdjuster.RecordApproval(ctx, authorName, actionType); err != nil {
|
||||
s.logger.Warn("trust approval failed",
|
||||
"agent", authorName,
|
||||
"reactor", reactorName,
|
||||
"error", err,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
if err := s.trustAdjuster.RecordRejection(ctx, authorName, actionType); err != nil {
|
||||
s.logger.Warn("trust rejection failed",
|
||||
"agent", authorName,
|
||||
"reactor", reactorName,
|
||||
"error", err,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove explicitly removes a reaction.
|
||||
func (s *Service) Remove(ctx context.Context, messageID int64, agentName, reactionType string) error {
|
||||
if !IsValidReaction(reactionType) {
|
||||
return ErrInvalidReaction
|
||||
}
|
||||
|
||||
if err := s.store.Delete(ctx, messageID, agentName, reactionType); err != nil {
|
||||
return fmt.Errorf("remove reaction: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("reaction removed",
|
||||
"message_id", messageID,
|
||||
"agent", agentName,
|
||||
"reaction", reactionType,
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetReactions returns all reactions for a message and the computed workflow state.
|
||||
func (s *Service) GetReactions(ctx context.Context, messageID int64) ([]*Reaction, string, error) {
|
||||
reactions, err := s.store.GetByMessageID(ctx, messageID)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("get reactions: %w", err)
|
||||
}
|
||||
|
||||
state := ComputeWorkflowState(reactions)
|
||||
return reactions, state, nil
|
||||
}
|
||||
|
||||
// GetReactionsByMessageIDs returns reactions grouped by message ID.
|
||||
func (s *Service) GetReactionsByMessageIDs(ctx context.Context, messageIDs []int64) (map[int64][]*Reaction, error) {
|
||||
return s.store.GetByMessageIDs(ctx, messageIDs)
|
||||
}
|
||||
|
||||
// ListByState returns message IDs in a channel that have the given workflow state.
|
||||
// For non-proposed states, it verifies each candidate by computing the actual
|
||||
// workflow state from all reactions, so a message with both "approve" and "reject"
|
||||
// only appears in the state matching its highest-priority reaction.
|
||||
func (s *Service) ListByState(ctx context.Context, channelID int64, state string) ([]int64, error) {
|
||||
candidates, err := s.store.GetMessageIDsByState(ctx, channelID, state)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// "proposed" means no reactions at all — the SQL query already handles this correctly.
|
||||
if state == StateProposed {
|
||||
return candidates, nil
|
||||
}
|
||||
|
||||
// Batch-fetch reactions for all candidate messages.
|
||||
reactionsMap, err := s.store.GetByMessageIDs(ctx, candidates)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("batch fetch reactions for state filtering: %w", err)
|
||||
}
|
||||
|
||||
// Only keep messages whose computed workflow state matches the requested state.
|
||||
var filtered []int64
|
||||
for _, id := range candidates {
|
||||
rxns := reactionsMap[id]
|
||||
if ComputeWorkflowState(rxns) == state {
|
||||
filtered = append(filtered, id)
|
||||
}
|
||||
}
|
||||
return filtered, nil
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
package reactions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestService_ListByState_FiltersCorrectly(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
|
||||
svc := NewService(store, logger)
|
||||
ctx := context.Background()
|
||||
|
||||
// Ensure user and agents exist
|
||||
db.Exec(`INSERT OR IGNORE INTO users (id, username, password_hash, display_name) VALUES (1, 'testowner', 'hash', 'Test Owner')`)
|
||||
db.Exec(`INSERT OR IGNORE INTO agents (name, display_name, type, capabilities, owner_id, api_key_hash, status) VALUES ('agent-a', 'agent-a', 'ai', '{}', 1, 'testhash', 'active')`)
|
||||
db.Exec(`INSERT OR IGNORE INTO agents (name, display_name, type, capabilities, owner_id, api_key_hash, status) VALUES ('agent-b', 'agent-b', 'ai', '{}', 1, 'testhash2', 'active')`)
|
||||
|
||||
// Create a channel
|
||||
result, err := db.Exec(`INSERT INTO channels (name, description, created_by, workflow_enabled) VALUES ('test-channel', 'test', 'agent-a', 1)`)
|
||||
if err != nil {
|
||||
t.Fatalf("create channel: %v", err)
|
||||
}
|
||||
channelID, _ := result.LastInsertId()
|
||||
|
||||
// Helper to create a message in the channel
|
||||
createMsg := func(agent, body string) int64 {
|
||||
t.Helper()
|
||||
r, err := db.Exec(
|
||||
`INSERT INTO conversations (subject, created_by, created_at, updated_at) VALUES ('test', ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`,
|
||||
agent,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("create conversation: %v", err)
|
||||
}
|
||||
convID, _ := r.LastInsertId()
|
||||
|
||||
r, err = db.Exec(
|
||||
`INSERT INTO messages (conversation_id, from_agent, channel_id, body, priority, status, created_at) VALUES (?, ?, ?, ?, 5, 'pending', CURRENT_TIMESTAMP)`,
|
||||
convID, agent, channelID, body,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
id, _ := r.LastInsertId()
|
||||
return id
|
||||
}
|
||||
|
||||
// Scenario: msg1 has approve + reject (should be "rejected" since reject has higher priority)
|
||||
msg1 := createMsg("agent-a", "msg with approve and reject")
|
||||
store.Insert(ctx, &Reaction{MessageID: msg1, AgentName: "agent-a", Reaction: ReactionApprove})
|
||||
store.Insert(ctx, &Reaction{MessageID: msg1, AgentName: "agent-b", Reaction: ReactionReject})
|
||||
|
||||
// Scenario: msg2 has only approve (should be "approved")
|
||||
msg2 := createMsg("agent-a", "msg with only approve")
|
||||
store.Insert(ctx, &Reaction{MessageID: msg2, AgentName: "agent-a", Reaction: ReactionApprove})
|
||||
|
||||
// Scenario: msg3 has no reactions (should be "proposed")
|
||||
msg3 := createMsg("agent-a", "msg with no reactions")
|
||||
|
||||
// Scenario: msg4 has approve + in_progress + done (should be "done")
|
||||
msg4 := createMsg("agent-a", "msg with approve, in_progress, done")
|
||||
store.Insert(ctx, &Reaction{MessageID: msg4, AgentName: "agent-a", Reaction: ReactionApprove})
|
||||
store.Insert(ctx, &Reaction{MessageID: msg4, AgentName: "agent-b", Reaction: ReactionInProgress})
|
||||
store.Insert(ctx, &Reaction{MessageID: msg4, AgentName: "agent-b", Reaction: ReactionDone})
|
||||
|
||||
// Test: list "approved" should only return msg2 (NOT msg1 which also has approve but its state is rejected)
|
||||
t.Run("approved returns only truly approved", func(t *testing.T) {
|
||||
ids, err := svc.ListByState(ctx, channelID, StateApproved)
|
||||
if err != nil {
|
||||
t.Fatalf("ListByState(approved): %v", err)
|
||||
}
|
||||
if len(ids) != 1 {
|
||||
t.Fatalf("expected 1 approved message, got %d: %v", len(ids), ids)
|
||||
}
|
||||
if ids[0] != msg2 {
|
||||
t.Errorf("expected msg2 (id=%d), got id=%d", msg2, ids[0])
|
||||
}
|
||||
})
|
||||
|
||||
// Test: list "rejected" should only return msg1
|
||||
t.Run("rejected returns only truly rejected", func(t *testing.T) {
|
||||
ids, err := svc.ListByState(ctx, channelID, StateRejected)
|
||||
if err != nil {
|
||||
t.Fatalf("ListByState(rejected): %v", err)
|
||||
}
|
||||
if len(ids) != 1 {
|
||||
t.Fatalf("expected 1 rejected message, got %d: %v", len(ids), ids)
|
||||
}
|
||||
if ids[0] != msg1 {
|
||||
t.Errorf("expected msg1 (id=%d), got id=%d", msg1, ids[0])
|
||||
}
|
||||
})
|
||||
|
||||
// Test: list "proposed" should only return msg3
|
||||
t.Run("proposed returns only messages with no reactions", func(t *testing.T) {
|
||||
ids, err := svc.ListByState(ctx, channelID, StateProposed)
|
||||
if err != nil {
|
||||
t.Fatalf("ListByState(proposed): %v", err)
|
||||
}
|
||||
if len(ids) != 1 {
|
||||
t.Fatalf("expected 1 proposed message, got %d: %v", len(ids), ids)
|
||||
}
|
||||
if ids[0] != msg3 {
|
||||
t.Errorf("expected msg3 (id=%d), got id=%d", msg3, ids[0])
|
||||
}
|
||||
})
|
||||
|
||||
// Test: list "done" should only return msg4
|
||||
t.Run("done returns only truly done", func(t *testing.T) {
|
||||
ids, err := svc.ListByState(ctx, channelID, StateDone)
|
||||
if err != nil {
|
||||
t.Fatalf("ListByState(done): %v", err)
|
||||
}
|
||||
if len(ids) != 1 {
|
||||
t.Fatalf("expected 1 done message, got %d: %v", len(ids), ids)
|
||||
}
|
||||
if ids[0] != msg4 {
|
||||
t.Errorf("expected msg4 (id=%d), got id=%d", msg4, ids[0])
|
||||
}
|
||||
})
|
||||
|
||||
// Test: list "in_progress" should return nothing (msg4 has in_progress but done overrides it)
|
||||
t.Run("in_progress excludes messages that have progressed to done", func(t *testing.T) {
|
||||
ids, err := svc.ListByState(ctx, channelID, StateInProgress)
|
||||
if err != nil {
|
||||
t.Fatalf("ListByState(in_progress): %v", err)
|
||||
}
|
||||
if len(ids) != 0 {
|
||||
t.Errorf("expected 0 in_progress messages, got %d: %v", len(ids), ids)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_ListByState_EmptyChannel(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
|
||||
svc := NewService(store, logger)
|
||||
ctx := context.Background()
|
||||
|
||||
db.Exec(`INSERT OR IGNORE INTO users (id, username, password_hash, display_name) VALUES (1, 'testowner', 'hash', 'Test Owner')`)
|
||||
|
||||
result, err := db.Exec(`INSERT INTO channels (name, description, created_by, workflow_enabled) VALUES ('empty-channel', 'empty', 'testowner', 1)`)
|
||||
if err != nil {
|
||||
t.Fatalf("create channel: %v", err)
|
||||
}
|
||||
channelID, _ := result.LastInsertId()
|
||||
|
||||
ids, err := svc.ListByState(ctx, channelID, StateProposed)
|
||||
if err != nil {
|
||||
t.Fatalf("ListByState: %v", err)
|
||||
}
|
||||
if ids != nil && len(ids) != 0 {
|
||||
t.Errorf("expected nil or empty slice, got %v", ids)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
package reactions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Store defines the storage interface for reactions.
|
||||
type Store interface {
|
||||
Insert(ctx context.Context, r *Reaction) error
|
||||
Delete(ctx context.Context, messageID int64, agentName, reaction string) error
|
||||
GetByMessageID(ctx context.Context, messageID int64) ([]*Reaction, error)
|
||||
GetByMessageIDs(ctx context.Context, messageIDs []int64) (map[int64][]*Reaction, error)
|
||||
Exists(ctx context.Context, messageID int64, agentName, reaction string) (bool, error)
|
||||
CountByMessage(ctx context.Context, messageID int64) (int, error)
|
||||
// GetMessageIDsByState returns message IDs in a channel that have the given workflow state.
|
||||
GetMessageIDsByState(ctx context.Context, channelID int64, state string) ([]int64, error)
|
||||
}
|
||||
|
||||
// SQLiteStore implements Store using SQLite.
|
||||
type SQLiteStore struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
// NewSQLiteStore creates a new SQLite-backed reaction store.
|
||||
func NewSQLiteStore(db *sql.DB) *SQLiteStore {
|
||||
return &SQLiteStore{db: db}
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) Insert(ctx context.Context, r *Reaction) error {
|
||||
metadata := r.Metadata
|
||||
if metadata == nil {
|
||||
metadata = json.RawMessage("{}")
|
||||
}
|
||||
|
||||
result, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO message_reactions (message_id, agent_name, reaction, metadata, created_at)
|
||||
VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)`,
|
||||
r.MessageID, r.AgentName, r.Reaction, string(metadata),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert reaction: %w", err)
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return fmt.Errorf("get reaction id: %w", err)
|
||||
}
|
||||
r.ID = id
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) Delete(ctx context.Context, messageID int64, agentName, reaction string) error {
|
||||
result, err := s.db.ExecContext(ctx,
|
||||
`DELETE FROM message_reactions WHERE message_id = ? AND agent_name = ? AND reaction = ?`,
|
||||
messageID, agentName, reaction,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete reaction: %w", err)
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
if n == 0 {
|
||||
return fmt.Errorf("reaction not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) GetByMessageID(ctx context.Context, messageID int64) ([]*Reaction, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT id, message_id, agent_name, reaction, metadata, created_at
|
||||
FROM message_reactions WHERE message_id = ?
|
||||
ORDER BY created_at ASC`, messageID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get reactions: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
return scanReactions(rows)
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) GetByMessageIDs(ctx context.Context, messageIDs []int64) (map[int64][]*Reaction, error) {
|
||||
if len(messageIDs) == 0 {
|
||||
return map[int64][]*Reaction{}, nil
|
||||
}
|
||||
|
||||
placeholders := make([]string, len(messageIDs))
|
||||
args := make([]any, len(messageIDs))
|
||||
for i, id := range messageIDs {
|
||||
placeholders[i] = "?"
|
||||
args[i] = id
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(
|
||||
`SELECT id, message_id, agent_name, reaction, metadata, created_at
|
||||
FROM message_reactions WHERE message_id IN (%s)
|
||||
ORDER BY created_at ASC`,
|
||||
strings.Join(placeholders, ","),
|
||||
)
|
||||
|
||||
rows, err := s.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get reactions by ids: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
all, err := scanReactions(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[int64][]*Reaction)
|
||||
for _, r := range all {
|
||||
result[r.MessageID] = append(result[r.MessageID], r)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) Exists(ctx context.Context, messageID int64, agentName, reaction string) (bool, error) {
|
||||
var count int
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM message_reactions WHERE message_id = ? AND agent_name = ? AND reaction = ?`,
|
||||
messageID, agentName, reaction,
|
||||
).Scan(&count)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("check reaction exists: %w", err)
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) CountByMessage(ctx context.Context, messageID int64) (int, error) {
|
||||
var count int
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM message_reactions WHERE message_id = ?`, messageID,
|
||||
).Scan(&count)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("count reactions: %w", err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) GetMessageIDsByState(ctx context.Context, channelID int64, state string) ([]int64, error) {
|
||||
var query string
|
||||
var args []any
|
||||
|
||||
if state == StateProposed {
|
||||
// Messages with no reactions
|
||||
query = `SELECT m.id FROM messages m
|
||||
WHERE m.channel_id = ?
|
||||
AND NOT EXISTS (SELECT 1 FROM message_reactions r WHERE r.message_id = m.id)
|
||||
ORDER BY m.created_at DESC`
|
||||
args = []any{channelID}
|
||||
} else {
|
||||
// Find the reaction type for this state
|
||||
var reactionType string
|
||||
for rt, st := range reactionToState {
|
||||
if st == state {
|
||||
reactionType = rt
|
||||
break
|
||||
}
|
||||
}
|
||||
if reactionType == "" {
|
||||
return nil, fmt.Errorf("unknown workflow state: %s", state)
|
||||
}
|
||||
|
||||
// Messages where the highest-priority reaction maps to this state
|
||||
// We get all messages with this reaction type and filter in app layer
|
||||
query = `SELECT DISTINCT r.message_id FROM message_reactions r
|
||||
JOIN messages m ON m.id = r.message_id
|
||||
WHERE m.channel_id = ? AND r.reaction = ?
|
||||
ORDER BY m.created_at DESC`
|
||||
args = []any{channelID, reactionType}
|
||||
}
|
||||
|
||||
rows, err := s.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get message ids by state: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var ids []int64
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
return nil, fmt.Errorf("scan message id: %w", err)
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return ids, rows.Err()
|
||||
}
|
||||
|
||||
func scanReactions(rows *sql.Rows) ([]*Reaction, error) {
|
||||
var reactions []*Reaction
|
||||
for rows.Next() {
|
||||
var r Reaction
|
||||
var metadata string
|
||||
err := rows.Scan(&r.ID, &r.MessageID, &r.AgentName, &r.Reaction, &metadata, &r.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan reaction: %w", err)
|
||||
}
|
||||
r.Metadata = json.RawMessage(metadata)
|
||||
reactions = append(reactions, &r)
|
||||
}
|
||||
if reactions == nil {
|
||||
reactions = []*Reaction{}
|
||||
}
|
||||
return reactions, rows.Err()
|
||||
}
|
||||
@@ -0,0 +1,332 @@
|
||||
package reactions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/storage"
|
||||
)
|
||||
|
||||
func newTestDB(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("open database: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { db.Close() })
|
||||
|
||||
if _, err := db.Exec("PRAGMA foreign_keys=ON"); err != nil {
|
||||
t.Fatalf("enable foreign keys: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
if err := storage.RunMigrations(ctx, db); err != nil {
|
||||
t.Fatalf("run migrations: %v", err)
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
// seedTestMessage creates a test user, agent, conversation, and message,
|
||||
// returning the message ID.
|
||||
func seedTestMessage(t *testing.T, db *sql.DB, agentName string) int64 {
|
||||
t.Helper()
|
||||
|
||||
// Ensure user exists
|
||||
db.Exec(`INSERT OR IGNORE INTO users (id, username, password_hash, display_name) VALUES (1, 'testowner', 'hash', 'Test Owner')`)
|
||||
|
||||
// Ensure agent exists
|
||||
_, err := db.Exec(
|
||||
`INSERT OR IGNORE INTO agents (name, display_name, type, capabilities, owner_id, api_key_hash, status) VALUES (?, ?, 'ai', '{}', 1, 'testhash', 'active')`,
|
||||
agentName, agentName,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("seed agent %s: %v", agentName, err)
|
||||
}
|
||||
|
||||
// Create conversation
|
||||
result, err := db.Exec(
|
||||
`INSERT INTO conversations (subject, created_by, created_at, updated_at) VALUES ('test', ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)`,
|
||||
agentName,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("create conversation: %v", err)
|
||||
}
|
||||
convID, _ := result.LastInsertId()
|
||||
|
||||
// Create message
|
||||
result, err = db.Exec(
|
||||
`INSERT INTO messages (conversation_id, from_agent, body, priority, status, created_at) VALUES (?, ?, 'test body', 5, 'pending', CURRENT_TIMESTAMP)`,
|
||||
convID, agentName,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("create message: %v", err)
|
||||
}
|
||||
msgID, _ := result.LastInsertId()
|
||||
return msgID
|
||||
}
|
||||
|
||||
func TestSQLiteStore_InsertAndGet(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
msgID := seedTestMessage(t, db, "agent-a")
|
||||
|
||||
r := &Reaction{
|
||||
MessageID: msgID,
|
||||
AgentName: "agent-a",
|
||||
Reaction: ReactionApprove,
|
||||
Metadata: json.RawMessage(`{"comment":"looks good"}`),
|
||||
}
|
||||
|
||||
if err := store.Insert(ctx, r); err != nil {
|
||||
t.Fatalf("Insert: %v", err)
|
||||
}
|
||||
|
||||
if r.ID == 0 {
|
||||
t.Error("reaction ID should not be 0 after insert")
|
||||
}
|
||||
|
||||
// Verify it exists
|
||||
exists, err := store.Exists(ctx, msgID, "agent-a", ReactionApprove)
|
||||
if err != nil {
|
||||
t.Fatalf("Exists: %v", err)
|
||||
}
|
||||
if !exists {
|
||||
t.Error("expected reaction to exist after insert")
|
||||
}
|
||||
|
||||
// GetByMessageID
|
||||
reactions, err := store.GetByMessageID(ctx, msgID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetByMessageID: %v", err)
|
||||
}
|
||||
if len(reactions) != 1 {
|
||||
t.Fatalf("got %d reactions, want 1", len(reactions))
|
||||
}
|
||||
if reactions[0].AgentName != "agent-a" {
|
||||
t.Errorf("AgentName = %q, want %q", reactions[0].AgentName, "agent-a")
|
||||
}
|
||||
if reactions[0].Reaction != ReactionApprove {
|
||||
t.Errorf("Reaction = %q, want %q", reactions[0].Reaction, ReactionApprove)
|
||||
}
|
||||
if string(reactions[0].Metadata) != `{"comment":"looks good"}` {
|
||||
t.Errorf("Metadata = %s, want %s", reactions[0].Metadata, `{"comment":"looks good"}`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteStore_UniqueConstraint(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
msgID := seedTestMessage(t, db, "agent-a")
|
||||
|
||||
r := &Reaction{
|
||||
MessageID: msgID,
|
||||
AgentName: "agent-a",
|
||||
Reaction: ReactionApprove,
|
||||
}
|
||||
|
||||
if err := store.Insert(ctx, r); err != nil {
|
||||
t.Fatalf("Insert first: %v", err)
|
||||
}
|
||||
|
||||
// Inserting the same reaction again should fail with UNIQUE constraint
|
||||
r2 := &Reaction{
|
||||
MessageID: msgID,
|
||||
AgentName: "agent-a",
|
||||
Reaction: ReactionApprove,
|
||||
}
|
||||
err := store.Insert(ctx, r2)
|
||||
if err == nil {
|
||||
t.Error("expected error on duplicate insert, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteStore_Delete(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
msgID := seedTestMessage(t, db, "agent-a")
|
||||
|
||||
r := &Reaction{
|
||||
MessageID: msgID,
|
||||
AgentName: "agent-a",
|
||||
Reaction: ReactionApprove,
|
||||
}
|
||||
if err := store.Insert(ctx, r); err != nil {
|
||||
t.Fatalf("Insert: %v", err)
|
||||
}
|
||||
|
||||
// Delete the reaction
|
||||
if err := store.Delete(ctx, msgID, "agent-a", ReactionApprove); err != nil {
|
||||
t.Fatalf("Delete: %v", err)
|
||||
}
|
||||
|
||||
// Verify it's gone
|
||||
exists, err := store.Exists(ctx, msgID, "agent-a", ReactionApprove)
|
||||
if err != nil {
|
||||
t.Fatalf("Exists: %v", err)
|
||||
}
|
||||
if exists {
|
||||
t.Error("expected reaction to not exist after delete")
|
||||
}
|
||||
|
||||
// Deleting a non-existent reaction should return an error
|
||||
err = store.Delete(ctx, msgID, "agent-a", ReactionApprove)
|
||||
if err == nil {
|
||||
t.Error("expected error when deleting non-existent reaction, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteStore_GetByMessageID(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
msgID := seedTestMessage(t, db, "agent-a")
|
||||
// Seed a second agent
|
||||
db.Exec(`INSERT OR IGNORE INTO agents (name, display_name, type, capabilities, owner_id, api_key_hash, status) VALUES ('agent-b', 'agent-b', 'ai', '{}', 1, 'testhash2', 'active')`)
|
||||
|
||||
// Insert multiple reactions from different agents
|
||||
reactions := []*Reaction{
|
||||
{MessageID: msgID, AgentName: "agent-a", Reaction: ReactionApprove},
|
||||
{MessageID: msgID, AgentName: "agent-b", Reaction: ReactionInProgress},
|
||||
{MessageID: msgID, AgentName: "agent-a", Reaction: ReactionDone},
|
||||
}
|
||||
|
||||
for _, r := range reactions {
|
||||
if err := store.Insert(ctx, r); err != nil {
|
||||
t.Fatalf("Insert: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
got, err := store.GetByMessageID(ctx, msgID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetByMessageID: %v", err)
|
||||
}
|
||||
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("got %d reactions, want 3", len(got))
|
||||
}
|
||||
|
||||
// Verify results are ordered by created_at ASC
|
||||
for i, r := range got {
|
||||
if r.ID == 0 {
|
||||
t.Errorf("reaction[%d] ID should not be 0", i)
|
||||
}
|
||||
if r.MessageID != msgID {
|
||||
t.Errorf("reaction[%d] MessageID = %d, want %d", i, r.MessageID, msgID)
|
||||
}
|
||||
}
|
||||
|
||||
// Test with a message that has no reactions
|
||||
msgID2 := seedTestMessage(t, db, "agent-a")
|
||||
got2, err := store.GetByMessageID(ctx, msgID2)
|
||||
if err != nil {
|
||||
t.Fatalf("GetByMessageID (empty): %v", err)
|
||||
}
|
||||
if len(got2) != 0 {
|
||||
t.Errorf("got %d reactions for empty message, want 0", len(got2))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteStore_CountByMessage(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
msgID := seedTestMessage(t, db, "agent-a")
|
||||
db.Exec(`INSERT OR IGNORE INTO agents (name, display_name, type, capabilities, owner_id, api_key_hash, status) VALUES ('agent-b', 'agent-b', 'ai', '{}', 1, 'testhash2', 'active')`)
|
||||
|
||||
// Count should be 0 initially
|
||||
count, err := store.CountByMessage(ctx, msgID)
|
||||
if err != nil {
|
||||
t.Fatalf("CountByMessage: %v", err)
|
||||
}
|
||||
if count != 0 {
|
||||
t.Errorf("initial count = %d, want 0", count)
|
||||
}
|
||||
|
||||
// Insert some reactions
|
||||
for _, r := range []*Reaction{
|
||||
{MessageID: msgID, AgentName: "agent-a", Reaction: ReactionApprove},
|
||||
{MessageID: msgID, AgentName: "agent-b", Reaction: ReactionDone},
|
||||
} {
|
||||
if err := store.Insert(ctx, r); err != nil {
|
||||
t.Fatalf("Insert: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
count, err = store.CountByMessage(ctx, msgID)
|
||||
if err != nil {
|
||||
t.Fatalf("CountByMessage: %v", err)
|
||||
}
|
||||
if count != 2 {
|
||||
t.Errorf("count = %d, want 2", count)
|
||||
}
|
||||
|
||||
// Delete one and verify count decreases
|
||||
if err := store.Delete(ctx, msgID, "agent-a", ReactionApprove); err != nil {
|
||||
t.Fatalf("Delete: %v", err)
|
||||
}
|
||||
count, err = store.CountByMessage(ctx, msgID)
|
||||
if err != nil {
|
||||
t.Fatalf("CountByMessage after delete: %v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Errorf("count after delete = %d, want 1", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteStore_GetByMessageIDs(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
msgID1 := seedTestMessage(t, db, "agent-a")
|
||||
msgID2 := seedTestMessage(t, db, "agent-a")
|
||||
|
||||
// Add reactions to msg1
|
||||
if err := store.Insert(ctx, &Reaction{MessageID: msgID1, AgentName: "agent-a", Reaction: ReactionApprove}); err != nil {
|
||||
t.Fatalf("Insert: %v", err)
|
||||
}
|
||||
if err := store.Insert(ctx, &Reaction{MessageID: msgID1, AgentName: "agent-a", Reaction: ReactionDone}); err != nil {
|
||||
t.Fatalf("Insert: %v", err)
|
||||
}
|
||||
|
||||
// Add reaction to msg2
|
||||
if err := store.Insert(ctx, &Reaction{MessageID: msgID2, AgentName: "agent-a", Reaction: ReactionReject}); err != nil {
|
||||
t.Fatalf("Insert: %v", err)
|
||||
}
|
||||
|
||||
result, err := store.GetByMessageIDs(ctx, []int64{msgID1, msgID2})
|
||||
if err != nil {
|
||||
t.Fatalf("GetByMessageIDs: %v", err)
|
||||
}
|
||||
|
||||
if len(result[msgID1]) != 2 {
|
||||
t.Errorf("msg1 reactions = %d, want 2", len(result[msgID1]))
|
||||
}
|
||||
if len(result[msgID2]) != 1 {
|
||||
t.Errorf("msg2 reactions = %d, want 1", len(result[msgID2]))
|
||||
}
|
||||
|
||||
// Empty slice returns empty map
|
||||
result, err = store.GetByMessageIDs(ctx, []int64{})
|
||||
if err != nil {
|
||||
t.Fatalf("GetByMessageIDs (empty): %v", err)
|
||||
}
|
||||
if len(result) != 0 {
|
||||
t.Errorf("expected empty map, got %v", result)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package reactor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/messaging"
|
||||
)
|
||||
|
||||
// DMFailureNotifier sends system DMs to agent owners on job failure.
|
||||
type DMFailureNotifier struct {
|
||||
msgService *messaging.MessagingService
|
||||
}
|
||||
|
||||
// NewDMFailureNotifier creates a new failure notifier.
|
||||
func NewDMFailureNotifier(msgService *messaging.MessagingService) *DMFailureNotifier {
|
||||
return &DMFailureNotifier{msgService: msgService}
|
||||
}
|
||||
|
||||
// NotifyFailure sends a system DM to the agent's owner with error details.
|
||||
func (n *DMFailureNotifier) NotifyFailure(ctx context.Context, ownerAgentName, agentName, triggerFrom, triggerEvent string, durationMs int64, errorSummary string) error {
|
||||
durationStr := "< 1s"
|
||||
if durationMs > 0 {
|
||||
secs := durationMs / 1000
|
||||
if secs >= 60 {
|
||||
durationStr = fmt.Sprintf("%dm%ds", secs/60, secs%60)
|
||||
} else {
|
||||
durationStr = fmt.Sprintf("%ds", secs)
|
||||
}
|
||||
}
|
||||
|
||||
body := fmt.Sprintf(
|
||||
"⚠️ **Reactive run failed** for **%s**\n\n"+
|
||||
"**Trigger**: %s from %s\n"+
|
||||
"**Duration**: %s\n"+
|
||||
"**Error**: %s\n\n"+
|
||||
"View details in Agent Runs page.",
|
||||
agentName, triggerEvent, triggerFrom, durationStr, truncateError(errorSummary, 500),
|
||||
)
|
||||
|
||||
_, err := n.msgService.SendMessage(ctx, "system", ownerAgentName, body, messaging.SendOptions{
|
||||
Priority: 7,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func truncateError(s string, maxLen int) string {
|
||||
if len(s) <= maxLen {
|
||||
return s
|
||||
}
|
||||
return s[:maxLen] + "..."
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
package reactor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/agents"
|
||||
"github.com/synapbus/synapbus/internal/dispatcher"
|
||||
k8spkg "github.com/synapbus/synapbus/internal/k8s"
|
||||
"github.com/synapbus/synapbus/internal/metrics"
|
||||
|
||||
batchv1 "k8s.io/api/batch/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
// Poller watches active reactive runs and updates their status from K8s.
|
||||
type Poller struct {
|
||||
store *Store
|
||||
agentStore agents.AgentStore
|
||||
clientset kubernetes.Interface
|
||||
runner k8spkg.JobRunner
|
||||
reactor *Reactor
|
||||
interval time.Duration
|
||||
logger *slog.Logger
|
||||
stopCh chan struct{}
|
||||
}
|
||||
|
||||
// NewPoller creates a new job status poller.
|
||||
func NewPoller(store *Store, agentStore agents.AgentStore, runner k8spkg.JobRunner, reactor *Reactor, logger *slog.Logger) *Poller {
|
||||
// Extract clientset from runner if it's the real K8s runner
|
||||
var clientset kubernetes.Interface
|
||||
if kr, ok := runner.(*k8spkg.K8sJobRunner); ok {
|
||||
clientset = kr.GetClientset()
|
||||
}
|
||||
|
||||
return &Poller{
|
||||
store: store,
|
||||
agentStore: agentStore,
|
||||
clientset: clientset,
|
||||
runner: runner,
|
||||
reactor: reactor,
|
||||
interval: 15 * time.Second,
|
||||
logger: logger.With("component", "reactor-poller"),
|
||||
stopCh: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
// Start begins the polling loop in a background goroutine.
|
||||
func (p *Poller) Start() {
|
||||
if !p.runner.IsAvailable() || p.clientset == nil {
|
||||
p.logger.Info("K8s not available, reactor poller disabled")
|
||||
return
|
||||
}
|
||||
go p.pollLoop()
|
||||
p.logger.Info("reactor poller started", "interval", p.interval)
|
||||
}
|
||||
|
||||
// Stop signals the poller to stop.
|
||||
func (p *Poller) Stop() {
|
||||
close(p.stopCh)
|
||||
}
|
||||
|
||||
func (p *Poller) pollLoop() {
|
||||
ticker := time.NewTicker(p.interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-p.stopCh:
|
||||
return
|
||||
case <-ticker.C:
|
||||
p.pollActiveRuns()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Poller) pollActiveRuns() {
|
||||
ctx := context.Background()
|
||||
|
||||
runs, err := p.store.GetActiveRuns(ctx)
|
||||
if err != nil {
|
||||
p.logger.Error("failed to get active runs", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, run := range runs {
|
||||
if run.K8sJobName == "" || run.K8sNamespace == "" {
|
||||
continue
|
||||
}
|
||||
p.checkJob(ctx, run)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Poller) checkJob(ctx context.Context, run *ReactiveRun) {
|
||||
ns := run.K8sNamespace
|
||||
jobName := run.K8sJobName
|
||||
|
||||
job, err := p.clientset.BatchV1().Jobs(ns).Get(ctx, jobName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
p.logger.Warn("failed to get K8s Job status", "job", jobName, "namespace", ns, "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Check job conditions
|
||||
for _, cond := range job.Status.Conditions {
|
||||
switch cond.Type {
|
||||
case batchv1.JobComplete:
|
||||
if cond.Status == "True" {
|
||||
p.handleJobComplete(ctx, run, true, "")
|
||||
return
|
||||
}
|
||||
case batchv1.JobFailed:
|
||||
if cond.Status == "True" {
|
||||
reason := cond.Reason
|
||||
if cond.Message != "" {
|
||||
reason = reason + ": " + cond.Message
|
||||
}
|
||||
p.handleJobComplete(ctx, run, false, reason)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if active deadline exceeded
|
||||
if job.Status.Failed > 0 {
|
||||
p.handleJobComplete(ctx, run, false, "job failed (pod failure)")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Poller) handleJobComplete(ctx context.Context, run *ReactiveRun, success bool, failureReason string) {
|
||||
now := time.Now().UTC()
|
||||
|
||||
// Update metrics
|
||||
metrics.ReactiveAgentState.WithLabelValues(run.AgentName).Set(0)
|
||||
if run.StartedAt != nil {
|
||||
duration := now.Sub(*run.StartedAt).Seconds()
|
||||
metrics.ReactiveRunDuration.WithLabelValues(run.AgentName).Observe(duration)
|
||||
}
|
||||
todayCount, _ := p.store.CountTodayRuns(ctx, run.AgentName)
|
||||
metrics.ReactiveBudgetUsed.WithLabelValues(run.AgentName).Set(float64(todayCount))
|
||||
|
||||
if success {
|
||||
metrics.ReactiveTriggersTotal.WithLabelValues(run.AgentName, StatusSucceeded).Inc()
|
||||
_ = p.store.CompleteRun(ctx, run.ID, StatusSucceeded, "", now)
|
||||
p.logger.Info("reactive run succeeded",
|
||||
"agent", run.AgentName,
|
||||
"job", run.K8sJobName,
|
||||
"run_id", run.ID,
|
||||
)
|
||||
} else {
|
||||
// Retrieve logs
|
||||
errorLog := failureReason
|
||||
logs, err := p.runner.GetJobLogs(ctx, run.K8sNamespace, run.K8sJobName)
|
||||
if err == nil && logs != "" {
|
||||
// Keep last 100 lines
|
||||
lines := strings.Split(logs, "\n")
|
||||
if len(lines) > 100 {
|
||||
lines = lines[len(lines)-100:]
|
||||
}
|
||||
errorLog = strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
metrics.ReactiveTriggersTotal.WithLabelValues(run.AgentName, StatusFailed).Inc()
|
||||
_ = p.store.CompleteRun(ctx, run.ID, StatusFailed, errorLog, now)
|
||||
|
||||
p.logger.Warn("reactive run failed",
|
||||
"agent", run.AgentName,
|
||||
"job", run.K8sJobName,
|
||||
"run_id", run.ID,
|
||||
"reason", failureReason,
|
||||
)
|
||||
|
||||
// Send failure notification
|
||||
var durationMs int64
|
||||
if run.StartedAt != nil {
|
||||
durationMs = now.Sub(*run.StartedAt).Milliseconds()
|
||||
}
|
||||
agent, err := p.agentStore.GetAgentByName(ctx, run.AgentName)
|
||||
if err == nil && agent != nil {
|
||||
event := dispatcher.MessageEvent{
|
||||
EventType: run.TriggerEvent,
|
||||
FromAgent: run.TriggerFrom,
|
||||
}
|
||||
p.reactor.notifyFailure(ctx, agent, event, durationMs, fmt.Sprintf("Job %s failed: %s", run.K8sJobName, failureReason))
|
||||
}
|
||||
}
|
||||
|
||||
// Check for pending_work — launch coalesced run if needed
|
||||
p.checkPendingWork(ctx, run.AgentName)
|
||||
}
|
||||
|
||||
func (p *Poller) checkPendingWork(ctx context.Context, agentName string) {
|
||||
agent, err := p.agentStore.GetAgentByName(ctx, agentName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if !agent.PendingWork {
|
||||
return
|
||||
}
|
||||
|
||||
// Clear pending_work first
|
||||
_ = p.agentStore.SetPendingWork(ctx, agentName, false)
|
||||
|
||||
p.logger.Info("pending_work found, launching coalesced run", "agent", agentName)
|
||||
|
||||
// Create a synthetic event (coalesced — agent will pick up all pending messages via claim_messages)
|
||||
event := dispatcher.MessageEvent{
|
||||
EventType: "message.received",
|
||||
FromAgent: "system",
|
||||
ToAgent: agentName,
|
||||
Body: "Coalesced trigger: process all pending messages.",
|
||||
MentionedAgents: nil,
|
||||
Depth: 0,
|
||||
}
|
||||
|
||||
// Evaluate the trigger (it will check cooldown/budget again)
|
||||
_ = p.reactor.evaluateTrigger(ctx, agentName, event)
|
||||
}
|
||||
@@ -0,0 +1,366 @@
|
||||
// Package reactor provides the reactive agent triggering engine.
|
||||
// When a DM or @mention targets an agent with trigger_mode='reactive',
|
||||
// the reactor evaluates rate limits and creates a K8s Job to run the agent.
|
||||
package reactor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/agents"
|
||||
"github.com/synapbus/synapbus/internal/dispatcher"
|
||||
k8spkg "github.com/synapbus/synapbus/internal/k8s"
|
||||
"github.com/synapbus/synapbus/internal/metrics"
|
||||
)
|
||||
|
||||
// Reactor is the reactive agent triggering engine.
|
||||
type Reactor struct {
|
||||
store *Store
|
||||
agentStore agents.AgentStore
|
||||
runner k8spkg.JobRunner
|
||||
notifier FailureNotifier
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// FailureNotifier sends system DMs on job failure.
|
||||
type FailureNotifier interface {
|
||||
NotifyFailure(ctx context.Context, ownerAgentName, agentName, triggerFrom, triggerEvent string, durationMs int64, errorSummary string) error
|
||||
}
|
||||
|
||||
// New creates a new Reactor.
|
||||
func New(store *Store, agentStore agents.AgentStore, runner k8spkg.JobRunner, logger *slog.Logger) *Reactor {
|
||||
return &Reactor{
|
||||
store: store,
|
||||
agentStore: agentStore,
|
||||
runner: runner,
|
||||
logger: logger.With("component", "reactor"),
|
||||
}
|
||||
}
|
||||
|
||||
// SetFailureNotifier sets the notifier for sending failure DMs.
|
||||
func (r *Reactor) SetFailureNotifier(n FailureNotifier) {
|
||||
r.notifier = n
|
||||
}
|
||||
|
||||
// Dispatch implements dispatcher.EventDispatcher. Called by MultiDispatcher
|
||||
// when a message event occurs.
|
||||
func (r *Reactor) Dispatch(ctx context.Context, event dispatcher.MessageEvent) error {
|
||||
switch event.EventType {
|
||||
case "message.received":
|
||||
// DM to an agent
|
||||
return r.evaluateTrigger(ctx, event.ToAgent, event)
|
||||
case "message.mentioned":
|
||||
// @mentions in channel messages
|
||||
for _, mentioned := range event.MentionedAgents {
|
||||
// Self-mention filter: agent can't trigger itself
|
||||
if mentioned == event.FromAgent {
|
||||
continue
|
||||
}
|
||||
if err := r.evaluateTrigger(ctx, mentioned, event); err != nil {
|
||||
r.logger.ErrorContext(ctx, "reactor trigger eval failed",
|
||||
"agent", mentioned,
|
||||
"error", err,
|
||||
)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return nil // Ignore other event types
|
||||
}
|
||||
}
|
||||
|
||||
// evaluateTrigger runs the decision chain for a single agent.
|
||||
func (r *Reactor) evaluateTrigger(ctx context.Context, agentName string, event dispatcher.MessageEvent) error {
|
||||
// 0. Ignore system messages — stalemate worker notifications, retention warnings,
|
||||
// and other automated DMs should NOT trigger reactive runs. They're notifications
|
||||
// meant for the human owner, not actionable work for agents.
|
||||
if event.FromAgent == "system" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 1. Get agent config
|
||||
agent, err := r.agentStore.GetAgentByName(ctx, agentName)
|
||||
if err != nil {
|
||||
return nil // Agent doesn't exist, skip silently
|
||||
}
|
||||
|
||||
// 2. Check trigger mode
|
||||
if agent.TriggerMode != agents.TriggerModeReactive {
|
||||
return nil // Not reactive, skip
|
||||
}
|
||||
|
||||
// 3. Check K8s image configured
|
||||
if agent.K8sImage == "" {
|
||||
r.logger.Warn("reactive agent has no k8s_image configured", "agent", agentName)
|
||||
r.recordSkippedRun(ctx, agentName, event, StatusFailed, "no k8s_image configured")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 4. Check K8s runner available
|
||||
if !r.runner.IsAvailable() {
|
||||
r.logger.Warn("K8s runner not available for reactive trigger", "agent", agentName)
|
||||
r.recordSkippedRun(ctx, agentName, event, StatusFailed, "K8s runner not available")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 5. Extract depth from event metadata
|
||||
depth := event.Depth
|
||||
|
||||
// 6. Check trigger depth
|
||||
if depth >= agent.MaxTriggerDepth {
|
||||
r.logger.Info("trigger depth exceeded", "agent", agentName, "depth", depth, "max", agent.MaxTriggerDepth)
|
||||
r.recordSkippedRun(ctx, agentName, event, StatusDepthExceeded, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 7. Check daily budget
|
||||
todayCount, err := r.store.CountTodayRuns(ctx, agentName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("count today runs: %w", err)
|
||||
}
|
||||
if todayCount >= agent.DailyTriggerBudget {
|
||||
r.logger.Info("daily trigger budget exhausted", "agent", agentName, "count", todayCount, "budget", agent.DailyTriggerBudget)
|
||||
r.recordSkippedRun(ctx, agentName, event, StatusBudgetExhausted, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 8. Check cooldown
|
||||
lastRun, err := r.store.GetLastRunTime(ctx, agentName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get last run time: %w", err)
|
||||
}
|
||||
if lastRun != nil {
|
||||
elapsed := time.Since(*lastRun)
|
||||
if elapsed < time.Duration(agent.CooldownSeconds)*time.Second {
|
||||
r.logger.Info("agent on cooldown", "agent", agentName, "elapsed", elapsed, "cooldown", agent.CooldownSeconds)
|
||||
// Set pending_work so we retry after cooldown
|
||||
_ = r.agentStore.SetPendingWork(ctx, agentName, true)
|
||||
r.recordSkippedRun(ctx, agentName, event, StatusCooldownSkipped, "")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// 9. Check if agent is currently running
|
||||
running, err := r.store.IsAgentRunning(ctx, agentName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check agent running: %w", err)
|
||||
}
|
||||
if running {
|
||||
r.logger.Info("agent already running, setting pending_work", "agent", agentName)
|
||||
_ = r.agentStore.SetPendingWork(ctx, agentName, true)
|
||||
r.recordSkippedRun(ctx, agentName, event, StatusQueued, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 10. All checks pass — create K8s Job
|
||||
return r.createJob(ctx, agent, event, depth)
|
||||
}
|
||||
|
||||
// createJob creates a K8s Job for the reactive trigger.
|
||||
func (r *Reactor) createJob(ctx context.Context, agent *agents.Agent, event dispatcher.MessageEvent, depth int) error {
|
||||
// Build handler from agent config
|
||||
handler := r.buildHandler(agent)
|
||||
|
||||
body := event.Body
|
||||
if len(body) > 4096 {
|
||||
body = body[:4096] + " [truncated]"
|
||||
}
|
||||
|
||||
msg := &k8spkg.JobMessage{
|
||||
MessageID: event.MessageID,
|
||||
FromAgent: event.FromAgent,
|
||||
Body: body,
|
||||
Event: event.EventType,
|
||||
Channel: event.Channel,
|
||||
Timestamp: time.Now().UTC().Format(time.RFC3339),
|
||||
}
|
||||
|
||||
// Add trigger depth env var to handler
|
||||
handler.Env["SYNAPBUS_TRIGGER_DEPTH"] = fmt.Sprintf("%d", depth)
|
||||
|
||||
// Create K8s Job FIRST (before DB insert to avoid stuck runs on SQLITE_BUSY)
|
||||
jobName, err := r.runner.CreateJob(ctx, handler, msg)
|
||||
if err != nil {
|
||||
errMsg := fmt.Sprintf("K8s Job creation failed: %s", err.Error())
|
||||
r.recordSkippedRun(ctx, agent.Name, event, StatusFailed, errMsg)
|
||||
r.notifyFailure(ctx, agent, event, 0, errMsg)
|
||||
return fmt.Errorf("create K8s job: %w", err)
|
||||
}
|
||||
|
||||
ns := handler.Namespace
|
||||
if ns == "" {
|
||||
ns = r.runner.GetNamespace()
|
||||
}
|
||||
|
||||
// Insert run record with job name already set (single atomic write)
|
||||
now := time.Now().UTC()
|
||||
run := &ReactiveRun{
|
||||
AgentName: agent.Name,
|
||||
TriggerMessageID: &event.MessageID,
|
||||
TriggerEvent: event.EventType,
|
||||
TriggerDepth: depth,
|
||||
TriggerFrom: event.FromAgent,
|
||||
Status: StatusRunning,
|
||||
K8sJobName: jobName,
|
||||
K8sNamespace: ns,
|
||||
StartedAt: &now,
|
||||
}
|
||||
|
||||
runID, err := r.store.InsertRun(ctx, run)
|
||||
if err != nil {
|
||||
r.logger.Error("failed to record reactive run (job already created)",
|
||||
"agent", agent.Name, "job", jobName, "error", err)
|
||||
runID = 0
|
||||
}
|
||||
|
||||
// Clear pending_work since we're launching
|
||||
_ = r.agentStore.SetPendingWork(ctx, agent.Name, false)
|
||||
|
||||
metrics.ReactiveTriggersTotal.WithLabelValues(agent.Name, StatusRunning).Inc()
|
||||
metrics.ReactiveAgentState.WithLabelValues(agent.Name).Set(1)
|
||||
|
||||
r.logger.Info("reactive K8s Job created",
|
||||
"agent", agent.Name,
|
||||
"job", jobName,
|
||||
"trigger_from", event.FromAgent,
|
||||
"trigger_event", event.EventType,
|
||||
"depth", depth,
|
||||
"run_id", runID,
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildHandler constructs a K8sHandler from agent config.
|
||||
func (r *Reactor) buildHandler(agent *agents.Agent) *k8spkg.K8sHandler {
|
||||
env := map[string]string{}
|
||||
|
||||
// Parse k8s_env_json
|
||||
if agent.K8sEnvJSON != "" {
|
||||
var envMap map[string]json.RawMessage
|
||||
if err := json.Unmarshal([]byte(agent.K8sEnvJSON), &envMap); err == nil {
|
||||
for k, v := range envMap {
|
||||
// Plain string values
|
||||
var str string
|
||||
if err := json.Unmarshal(v, &str); err == nil {
|
||||
env[k] = str
|
||||
continue
|
||||
}
|
||||
// Secret refs are handled at K8s level; for now pass as-is
|
||||
// (the K8s runner would need extension for secretKeyRef)
|
||||
env[k] = strings.Trim(string(v), "\"")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Resource presets — default matches CronJob config (agent SDK needs ~1-2Gi)
|
||||
memory := "2Gi"
|
||||
cpu := "500m"
|
||||
if agent.K8sResourcePreset == "small" {
|
||||
memory = "512Mi"
|
||||
cpu = "100m"
|
||||
}
|
||||
|
||||
timeout := 3600 // 1 hour (matches CronJob config)
|
||||
|
||||
handler := &k8spkg.K8sHandler{
|
||||
AgentName: agent.Name,
|
||||
Image: agent.K8sImage,
|
||||
Events: []string{"message.received", "message.mentioned"},
|
||||
Namespace: "", // Use runner's namespace
|
||||
ResourcesMemory: memory,
|
||||
ResourcesCPU: cpu,
|
||||
Env: env,
|
||||
TimeoutSeconds: timeout,
|
||||
Status: "active",
|
||||
Args: []string{"--max-turns", "50", "--model", "claude-sonnet-4-6"},
|
||||
VolumeMounts: []k8spkg.VolumeMount{
|
||||
{Name: "claude-config", MountPath: "/app/.claude", ReadOnly: false},
|
||||
{Name: "workspace", MountPath: "/app/workspace", ReadOnly: false},
|
||||
},
|
||||
Volumes: []k8spkg.Volume{
|
||||
{Name: "claude-config", HostPath: "/home/user/.claude"},
|
||||
{Name: "workspace", EmptyDir: true},
|
||||
},
|
||||
}
|
||||
|
||||
// Override args for social-commenter (uses opus, more turns)
|
||||
if agent.Name == "social-commenter" {
|
||||
handler.Args = []string{"--max-turns", "80", "--model", "claude-opus-4-6"}
|
||||
}
|
||||
|
||||
return handler
|
||||
}
|
||||
|
||||
// RetryRun retries a failed run.
|
||||
func (r *Reactor) RetryRun(ctx context.Context, runID int64) (*ReactiveRun, error) {
|
||||
run, err := r.store.GetRunByID(ctx, runID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get run: %w", err)
|
||||
}
|
||||
if run.Status != StatusFailed {
|
||||
return nil, fmt.Errorf("can only retry failed runs, current status: %s", run.Status)
|
||||
}
|
||||
|
||||
agent, err := r.agentStore.GetAgentByName(ctx, run.AgentName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get agent: %w", err)
|
||||
}
|
||||
|
||||
// Create a synthetic event for the retry
|
||||
event := dispatcher.MessageEvent{
|
||||
EventType: run.TriggerEvent,
|
||||
MessageID: 0,
|
||||
FromAgent: run.TriggerFrom,
|
||||
ToAgent: run.AgentName,
|
||||
Body: "",
|
||||
Depth: run.TriggerDepth,
|
||||
}
|
||||
if run.TriggerMessageID != nil {
|
||||
event.MessageID = *run.TriggerMessageID
|
||||
}
|
||||
|
||||
if err := r.createJob(ctx, agent, event, run.TriggerDepth); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Return the newly created run
|
||||
runs, _, err := r.store.ListRuns(ctx, run.AgentName, StatusRunning, 1, 0)
|
||||
if err != nil || len(runs) == 0 {
|
||||
return nil, fmt.Errorf("retry succeeded but couldn't find new run")
|
||||
}
|
||||
return runs[0], nil
|
||||
}
|
||||
|
||||
func (r *Reactor) recordSkippedRun(ctx context.Context, agentName string, event dispatcher.MessageEvent, status, errorLog string) {
|
||||
metrics.ReactiveTriggersTotal.WithLabelValues(agentName, status).Inc()
|
||||
run := &ReactiveRun{
|
||||
AgentName: agentName,
|
||||
TriggerEvent: event.EventType,
|
||||
TriggerDepth: event.Depth,
|
||||
TriggerFrom: event.FromAgent,
|
||||
Status: status,
|
||||
ErrorLog: errorLog,
|
||||
}
|
||||
if event.MessageID > 0 {
|
||||
run.TriggerMessageID = &event.MessageID
|
||||
}
|
||||
_, _ = r.store.InsertRun(ctx, run)
|
||||
}
|
||||
|
||||
func (r *Reactor) notifyFailure(ctx context.Context, agent *agents.Agent, event dispatcher.MessageEvent, durationMs int64, errorSummary string) {
|
||||
if r.notifier == nil {
|
||||
return
|
||||
}
|
||||
// Find the owner's human agent name
|
||||
ownerAgent, err := r.agentStore.GetHumanAgentByOwner(ctx, agent.OwnerID)
|
||||
if err != nil || ownerAgent == nil {
|
||||
r.logger.Warn("could not find owner agent for failure notification", "agent", agent.Name)
|
||||
return
|
||||
}
|
||||
_ = r.notifier.NotifyFailure(ctx, ownerAgent.Name, agent.Name, event.FromAgent, event.EventType, durationMs, errorSummary)
|
||||
}
|
||||
@@ -0,0 +1,430 @@
|
||||
package reactor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/agents"
|
||||
"github.com/synapbus/synapbus/internal/dispatcher"
|
||||
k8spkg "github.com/synapbus/synapbus/internal/k8s"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
// setupTestDB creates an in-memory SQLite database with schema for testing.
|
||||
func setupTestDB(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
db, err := sql.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("open db: %v", err)
|
||||
}
|
||||
|
||||
// Create minimal schema
|
||||
schema := `
|
||||
CREATE TABLE agents (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
display_name TEXT NOT NULL DEFAULT '',
|
||||
type TEXT NOT NULL DEFAULT 'ai',
|
||||
capabilities TEXT NOT NULL DEFAULT '{}',
|
||||
owner_id INTEGER NOT NULL DEFAULT 1,
|
||||
api_key_hash TEXT NOT NULL DEFAULT '',
|
||||
status TEXT NOT NULL DEFAULT 'active',
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
trigger_mode TEXT NOT NULL DEFAULT 'passive',
|
||||
cooldown_seconds INTEGER NOT NULL DEFAULT 600,
|
||||
daily_trigger_budget INTEGER NOT NULL DEFAULT 8,
|
||||
max_trigger_depth INTEGER NOT NULL DEFAULT 5,
|
||||
k8s_image TEXT,
|
||||
k8s_env_json TEXT,
|
||||
k8s_resource_preset TEXT NOT NULL DEFAULT 'default',
|
||||
pending_work INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE TABLE reactive_runs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
agent_name TEXT NOT NULL,
|
||||
trigger_message_id INTEGER,
|
||||
trigger_event TEXT NOT NULL,
|
||||
trigger_depth INTEGER NOT NULL DEFAULT 0,
|
||||
trigger_from TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'queued',
|
||||
k8s_job_name TEXT,
|
||||
k8s_namespace TEXT,
|
||||
started_at DATETIME,
|
||||
completed_at DATETIME,
|
||||
duration_ms INTEGER,
|
||||
error_log TEXT,
|
||||
token_cost_json TEXT,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
`
|
||||
if _, err := db.Exec(schema); err != nil {
|
||||
t.Fatalf("create schema: %v", err)
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func insertTestAgent(t *testing.T, db *sql.DB, name, triggerMode, image string, cooldown, budget, maxDepth int) {
|
||||
t.Helper()
|
||||
_, err := db.Exec(
|
||||
`INSERT INTO agents (name, display_name, type, owner_id, trigger_mode, cooldown_seconds, daily_trigger_budget, max_trigger_depth, k8s_image, k8s_resource_preset)
|
||||
VALUES (?, ?, 'ai', 1, ?, ?, ?, ?, ?, 'default')`,
|
||||
name, name, triggerMode, cooldown, budget, maxDepth, image,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("insert agent: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReactorPassiveAgentSkipped(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
insertTestAgent(t, db, "passive-agent", "passive", "image:latest", 600, 8, 5)
|
||||
|
||||
store := NewStore(db)
|
||||
agentStore := agents.NewSQLiteAgentStore(db)
|
||||
runner := k8spkg.NewNoopRunner()
|
||||
logger := slog.Default()
|
||||
|
||||
reactor := New(store, agentStore, runner, logger)
|
||||
|
||||
event := dispatcher.MessageEvent{
|
||||
EventType: "message.received",
|
||||
MessageID: 1,
|
||||
FromAgent: "algis",
|
||||
ToAgent: "passive-agent",
|
||||
Body: "hello",
|
||||
}
|
||||
|
||||
err := reactor.Dispatch(context.Background(), event)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// No runs should be created for passive agents
|
||||
runs, total, err := store.ListRuns(context.Background(), "passive-agent", "", 10, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("list runs: %v", err)
|
||||
}
|
||||
if total != 0 || len(runs) != 0 {
|
||||
t.Errorf("expected 0 runs for passive agent, got %d", total)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReactorNoK8sImage(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
insertTestAgent(t, db, "no-image-agent", "reactive", "", 600, 8, 5)
|
||||
|
||||
store := NewStore(db)
|
||||
agentStore := agents.NewSQLiteAgentStore(db)
|
||||
runner := k8spkg.NewNoopRunner()
|
||||
logger := slog.Default()
|
||||
|
||||
reactor := New(store, agentStore, runner, logger)
|
||||
|
||||
event := dispatcher.MessageEvent{
|
||||
EventType: "message.received",
|
||||
MessageID: 1,
|
||||
FromAgent: "algis",
|
||||
ToAgent: "no-image-agent",
|
||||
Body: "hello",
|
||||
}
|
||||
|
||||
_ = reactor.Dispatch(context.Background(), event)
|
||||
|
||||
runs, _, _ := store.ListRuns(context.Background(), "no-image-agent", StatusFailed, 10, 0)
|
||||
if len(runs) != 1 {
|
||||
t.Fatalf("expected 1 failed run for agent with no image, got %d", len(runs))
|
||||
}
|
||||
if runs[0].ErrorLog != "no k8s_image configured" {
|
||||
t.Errorf("expected 'no k8s_image configured' error, got: %s", runs[0].ErrorLog)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReactorDepthExceeded(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
insertTestAgent(t, db, "deep-agent", "reactive", "image:latest", 600, 8, 3)
|
||||
|
||||
store := NewStore(db)
|
||||
agentStore := agents.NewSQLiteAgentStore(db)
|
||||
runner := &fakeRunner{available: true}
|
||||
logger := slog.Default()
|
||||
|
||||
reactor := New(store, agentStore, runner, logger)
|
||||
|
||||
event := dispatcher.MessageEvent{
|
||||
EventType: "message.received",
|
||||
MessageID: 1,
|
||||
FromAgent: "other-agent",
|
||||
ToAgent: "deep-agent",
|
||||
Body: "hello from depth 3",
|
||||
Depth: 3, // equals max depth
|
||||
}
|
||||
|
||||
_ = reactor.Dispatch(context.Background(), event)
|
||||
|
||||
runs, _, _ := store.ListRuns(context.Background(), "deep-agent", StatusDepthExceeded, 10, 0)
|
||||
if len(runs) != 1 {
|
||||
t.Fatalf("expected 1 depth_exceeded run, got %d", len(runs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestReactorBudgetExhausted(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
insertTestAgent(t, db, "budget-agent", "reactive", "image:latest", 0, 2, 5)
|
||||
|
||||
store := NewStore(db)
|
||||
agentStore := agents.NewSQLiteAgentStore(db)
|
||||
runner := &fakeRunner{available: true}
|
||||
logger := slog.Default()
|
||||
|
||||
reactor := New(store, agentStore, runner, logger)
|
||||
|
||||
// Record 2 existing runs today
|
||||
for i := 0; i < 2; i++ {
|
||||
_, _ = store.InsertRun(context.Background(), &ReactiveRun{
|
||||
AgentName: "budget-agent",
|
||||
TriggerEvent: "message.received",
|
||||
Status: StatusSucceeded,
|
||||
})
|
||||
}
|
||||
|
||||
event := dispatcher.MessageEvent{
|
||||
EventType: "message.received",
|
||||
MessageID: 10,
|
||||
FromAgent: "algis",
|
||||
ToAgent: "budget-agent",
|
||||
Body: "one more",
|
||||
}
|
||||
|
||||
_ = reactor.Dispatch(context.Background(), event)
|
||||
|
||||
runs, _, _ := store.ListRuns(context.Background(), "budget-agent", StatusBudgetExhausted, 10, 0)
|
||||
if len(runs) != 1 {
|
||||
t.Fatalf("expected 1 budget_exhausted run, got %d", len(runs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestReactorCooldownSkipped(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
insertTestAgent(t, db, "cool-agent", "reactive", "image:latest", 600, 8, 5)
|
||||
|
||||
store := NewStore(db)
|
||||
agentStore := agents.NewSQLiteAgentStore(db)
|
||||
runner := &fakeRunner{available: true}
|
||||
logger := slog.Default()
|
||||
|
||||
reactor := New(store, agentStore, runner, logger)
|
||||
|
||||
// Record a recent run
|
||||
now := time.Now().UTC()
|
||||
_, _ = store.InsertRun(context.Background(), &ReactiveRun{
|
||||
AgentName: "cool-agent",
|
||||
TriggerEvent: "message.received",
|
||||
Status: StatusSucceeded,
|
||||
})
|
||||
// Hack: the above uses CURRENT_TIMESTAMP which is "now", so cooldown should be active
|
||||
|
||||
event := dispatcher.MessageEvent{
|
||||
EventType: "message.received",
|
||||
MessageID: 10,
|
||||
FromAgent: "algis",
|
||||
ToAgent: "cool-agent",
|
||||
Body: "too soon",
|
||||
}
|
||||
|
||||
_ = reactor.Dispatch(context.Background(), event)
|
||||
_ = now // avoid unused
|
||||
|
||||
runs, _, _ := store.ListRuns(context.Background(), "cool-agent", StatusCooldownSkipped, 10, 0)
|
||||
if len(runs) != 1 {
|
||||
t.Fatalf("expected 1 cooldown_skipped run, got %d", len(runs))
|
||||
}
|
||||
|
||||
// Check pending_work was set
|
||||
agent, _ := agentStore.GetAgentByName(context.Background(), "cool-agent")
|
||||
if !agent.PendingWork {
|
||||
t.Error("expected pending_work to be set after cooldown skip")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReactorSequentialExecution(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
insertTestAgent(t, db, "busy-agent", "reactive", "image:latest", 0, 8, 5)
|
||||
|
||||
store := NewStore(db)
|
||||
agentStore := agents.NewSQLiteAgentStore(db)
|
||||
runner := &fakeRunner{available: true}
|
||||
logger := slog.Default()
|
||||
|
||||
reactor := New(store, agentStore, runner, logger)
|
||||
|
||||
// First trigger — should succeed
|
||||
event1 := dispatcher.MessageEvent{
|
||||
EventType: "message.received",
|
||||
MessageID: 1,
|
||||
FromAgent: "algis",
|
||||
ToAgent: "busy-agent",
|
||||
Body: "first",
|
||||
}
|
||||
_ = reactor.Dispatch(context.Background(), event1)
|
||||
|
||||
// Second trigger — agent is running, should queue
|
||||
event2 := dispatcher.MessageEvent{
|
||||
EventType: "message.received",
|
||||
MessageID: 2,
|
||||
FromAgent: "algis",
|
||||
ToAgent: "busy-agent",
|
||||
Body: "second",
|
||||
}
|
||||
_ = reactor.Dispatch(context.Background(), event2)
|
||||
|
||||
// Check: one running, one queued
|
||||
running, _, _ := store.ListRuns(context.Background(), "busy-agent", StatusRunning, 10, 0)
|
||||
queued, _, _ := store.ListRuns(context.Background(), "busy-agent", StatusQueued, 10, 0)
|
||||
|
||||
if len(running) != 1 {
|
||||
t.Errorf("expected 1 running, got %d", len(running))
|
||||
}
|
||||
if len(queued) != 1 {
|
||||
t.Errorf("expected 1 queued, got %d", len(queued))
|
||||
}
|
||||
|
||||
// Check pending_work is set
|
||||
agent, _ := agentStore.GetAgentByName(context.Background(), "busy-agent")
|
||||
if !agent.PendingWork {
|
||||
t.Error("expected pending_work to be set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReactorSelfMentionIgnored(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
insertTestAgent(t, db, "self-agent", "reactive", "image:latest", 0, 8, 5)
|
||||
|
||||
store := NewStore(db)
|
||||
agentStore := agents.NewSQLiteAgentStore(db)
|
||||
runner := &fakeRunner{available: true}
|
||||
logger := slog.Default()
|
||||
|
||||
reactor := New(store, agentStore, runner, logger)
|
||||
|
||||
// Agent mentions itself
|
||||
event := dispatcher.MessageEvent{
|
||||
EventType: "message.mentioned",
|
||||
MessageID: 1,
|
||||
FromAgent: "self-agent",
|
||||
Body: "hey @self-agent",
|
||||
MentionedAgents: []string{"self-agent"},
|
||||
}
|
||||
|
||||
_ = reactor.Dispatch(context.Background(), event)
|
||||
|
||||
runs, total, _ := store.ListRuns(context.Background(), "self-agent", "", 10, 0)
|
||||
if total != 0 || len(runs) != 0 {
|
||||
t.Errorf("expected 0 runs for self-mention, got %d", total)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReactorSuccessfulTrigger(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
envJSON, _ := json.Marshal(map[string]string{
|
||||
"AGENT_GIT_REPO": "Dumbris/test-agent",
|
||||
})
|
||||
_, _ = db.Exec(
|
||||
`INSERT INTO agents (name, display_name, type, owner_id, trigger_mode, cooldown_seconds, daily_trigger_budget, max_trigger_depth, k8s_image, k8s_env_json, k8s_resource_preset)
|
||||
VALUES (?, ?, 'ai', 1, 'reactive', 0, 8, 5, 'image:latest', ?, 'default')`,
|
||||
"test-agent", "Test Agent", string(envJSON),
|
||||
)
|
||||
|
||||
store := NewStore(db)
|
||||
agentStore := agents.NewSQLiteAgentStore(db)
|
||||
runner := &fakeRunner{available: true}
|
||||
logger := slog.Default()
|
||||
|
||||
reactor := New(store, agentStore, runner, logger)
|
||||
|
||||
event := dispatcher.MessageEvent{
|
||||
EventType: "message.received",
|
||||
MessageID: 42,
|
||||
FromAgent: "algis",
|
||||
ToAgent: "test-agent",
|
||||
Body: "research this topic",
|
||||
}
|
||||
|
||||
err := reactor.Dispatch(context.Background(), event)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Verify job was created
|
||||
if runner.lastJobName == "" {
|
||||
t.Fatal("expected K8s Job to be created")
|
||||
}
|
||||
|
||||
// Verify run record
|
||||
runs, _, _ := store.ListRuns(context.Background(), "test-agent", StatusRunning, 10, 0)
|
||||
if len(runs) != 1 {
|
||||
t.Fatalf("expected 1 running run, got %d", len(runs))
|
||||
}
|
||||
run := runs[0]
|
||||
if run.TriggerFrom != "algis" {
|
||||
t.Errorf("expected trigger_from=algis, got %s", run.TriggerFrom)
|
||||
}
|
||||
if run.TriggerEvent != "message.received" {
|
||||
t.Errorf("expected trigger_event=message.received, got %s", run.TriggerEvent)
|
||||
}
|
||||
|
||||
// Verify env vars passed to job
|
||||
if runner.lastEnv["SYNAPBUS_TRIGGER_DEPTH"] != "0" {
|
||||
t.Errorf("expected SYNAPBUS_TRIGGER_DEPTH=0, got %s", runner.lastEnv["SYNAPBUS_TRIGGER_DEPTH"])
|
||||
}
|
||||
if runner.lastEnv["AGENT_GIT_REPO"] != "Dumbris/test-agent" {
|
||||
t.Errorf("expected AGENT_GIT_REPO from k8s_env_json, got %s", runner.lastEnv["AGENT_GIT_REPO"])
|
||||
}
|
||||
}
|
||||
|
||||
// fakeRunner is a test double for k8spkg.JobRunner.
|
||||
type fakeRunner struct {
|
||||
available bool
|
||||
lastJobName string
|
||||
lastEnv map[string]string
|
||||
callCount int
|
||||
}
|
||||
|
||||
func (f *fakeRunner) IsAvailable() bool { return f.available }
|
||||
func (f *fakeRunner) GetNamespace() string { return "test-ns" }
|
||||
func (f *fakeRunner) GetJobLogs(_ context.Context, _, _ string) (string, error) {
|
||||
return "test logs", nil
|
||||
}
|
||||
func (f *fakeRunner) CreateJob(_ context.Context, handler *k8spkg.K8sHandler, msg *k8spkg.JobMessage) (string, error) {
|
||||
f.callCount++
|
||||
f.lastJobName = fmt.Sprintf("synapbus-%s-%d", handler.AgentName, msg.MessageID)
|
||||
f.lastEnv = make(map[string]string)
|
||||
for k, v := range handler.Env {
|
||||
f.lastEnv[k] = v
|
||||
}
|
||||
return f.lastJobName, nil
|
||||
}
|
||||
@@ -0,0 +1,298 @@
|
||||
package reactor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RunStatus constants for reactive_runs.
|
||||
const (
|
||||
StatusQueued = "queued"
|
||||
StatusRunning = "running"
|
||||
StatusSucceeded = "succeeded"
|
||||
StatusFailed = "failed"
|
||||
StatusCooldownSkipped = "cooldown_skipped"
|
||||
StatusBudgetExhausted = "budget_exhausted"
|
||||
StatusDepthExceeded = "depth_exceeded"
|
||||
)
|
||||
|
||||
// ReactiveRun represents a single trigger evaluation and its outcome.
|
||||
type ReactiveRun struct {
|
||||
ID int64 `json:"id"`
|
||||
AgentName string `json:"agent_name"`
|
||||
TriggerMessageID *int64 `json:"trigger_message_id,omitempty"`
|
||||
TriggerEvent string `json:"trigger_event"`
|
||||
TriggerDepth int `json:"trigger_depth"`
|
||||
TriggerFrom string `json:"trigger_from,omitempty"`
|
||||
Status string `json:"status"`
|
||||
K8sJobName string `json:"k8s_job_name,omitempty"`
|
||||
K8sNamespace string `json:"k8s_namespace,omitempty"`
|
||||
StartedAt *time.Time `json:"started_at,omitempty"`
|
||||
CompletedAt *time.Time `json:"completed_at,omitempty"`
|
||||
DurationMs *int64 `json:"duration_ms,omitempty"`
|
||||
ErrorLog string `json:"error_log,omitempty"`
|
||||
TokenCostJSON string `json:"token_cost_json,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// Store handles SQLite persistence for reactive runs.
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
// NewStore creates a new reactor store.
|
||||
func NewStore(db *sql.DB) *Store {
|
||||
return &Store{db: db}
|
||||
}
|
||||
|
||||
// InsertRun creates a new reactive_runs record.
|
||||
func (s *Store) InsertRun(ctx context.Context, run *ReactiveRun) (int64, error) {
|
||||
now := time.Now().UTC()
|
||||
run.CreatedAt = now
|
||||
nowStr := now.Format(time.RFC3339)
|
||||
var startedAtStr *string
|
||||
if run.StartedAt != nil {
|
||||
s := run.StartedAt.UTC().Format(time.RFC3339)
|
||||
startedAtStr = &s
|
||||
}
|
||||
result, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO reactive_runs (agent_name, trigger_message_id, trigger_event, trigger_depth, trigger_from, status, k8s_job_name, k8s_namespace, started_at, error_log, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
run.AgentName, run.TriggerMessageID, run.TriggerEvent, run.TriggerDepth,
|
||||
run.TriggerFrom, run.Status, run.K8sJobName, run.K8sNamespace, startedAtStr, run.ErrorLog, nowStr,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("insert reactive run: %w", err)
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
run.ID = id
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// UpdateRunStatus updates a run's status and optional fields.
|
||||
func (s *Store) UpdateRunStatus(ctx context.Context, id int64, status string, jobName, namespace string, startedAt *time.Time) error {
|
||||
var startedAtStr *string
|
||||
if startedAt != nil {
|
||||
str := startedAt.UTC().Format(time.RFC3339)
|
||||
startedAtStr = &str
|
||||
}
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`UPDATE reactive_runs SET status = ?, k8s_job_name = ?, k8s_namespace = ?, started_at = ? WHERE id = ?`,
|
||||
status, jobName, namespace, startedAtStr, id,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// CompleteRun marks a run as completed (succeeded or failed).
|
||||
func (s *Store) CompleteRun(ctx context.Context, id int64, status, errorLog string, completedAt time.Time) error {
|
||||
completedStr := completedAt.UTC().Format(time.RFC3339)
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`UPDATE reactive_runs SET status = ?, error_log = ?, completed_at = ?,
|
||||
duration_ms = CAST((julianday(?) - julianday(started_at)) * 86400000 AS INTEGER)
|
||||
WHERE id = ?`,
|
||||
status, errorLog, completedStr, completedStr, id,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetRunByID returns a single run.
|
||||
func (s *Store) GetRunByID(ctx context.Context, id int64) (*ReactiveRun, error) {
|
||||
return s.scanRun(s.db.QueryRowContext(ctx, runSelectSQL()+` WHERE id = ?`, id))
|
||||
}
|
||||
|
||||
// ListRuns returns recent runs with optional filters.
|
||||
func (s *Store) ListRuns(ctx context.Context, agentName, status string, limit, offset int) ([]*ReactiveRun, int, error) {
|
||||
where := "WHERE 1=1"
|
||||
args := []any{}
|
||||
|
||||
if agentName != "" {
|
||||
where += " AND agent_name = ?"
|
||||
args = append(args, agentName)
|
||||
}
|
||||
if status != "" {
|
||||
where += " AND status = ?"
|
||||
args = append(args, status)
|
||||
}
|
||||
|
||||
// Count total
|
||||
var total int
|
||||
countArgs := make([]any, len(args))
|
||||
copy(countArgs, args)
|
||||
err := s.db.QueryRowContext(ctx, "SELECT COUNT(*) FROM reactive_runs "+where, countArgs...).Scan(&total)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Query with pagination
|
||||
query := runSelectSQL() + " " + where + " ORDER BY created_at DESC LIMIT ? OFFSET ?"
|
||||
args = append(args, limit, offset)
|
||||
rows, err := s.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
runs, err := s.scanRuns(rows)
|
||||
return runs, total, err
|
||||
}
|
||||
|
||||
// GetActiveRuns returns runs with status 'running' (for polling).
|
||||
func (s *Store) GetActiveRuns(ctx context.Context) ([]*ReactiveRun, error) {
|
||||
rows, err := s.db.QueryContext(ctx, runSelectSQL()+` WHERE status = 'running'`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return s.scanRuns(rows)
|
||||
}
|
||||
|
||||
// CountTodayRuns counts runs that count against the daily budget for an agent.
|
||||
func (s *Store) CountTodayRuns(ctx context.Context, agentName string) (int, error) {
|
||||
// Compute start of today in UTC as RFC3339
|
||||
now := time.Now().UTC()
|
||||
startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
|
||||
startStr := startOfDay.Format(time.RFC3339)
|
||||
|
||||
var count int
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM reactive_runs
|
||||
WHERE agent_name = ? AND status IN ('running', 'succeeded', 'failed')
|
||||
AND created_at >= ?`,
|
||||
agentName, startStr,
|
||||
).Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetLastRunTime returns the created_at of the most recent countable run.
|
||||
func (s *Store) GetLastRunTime(ctx context.Context, agentName string) (*time.Time, error) {
|
||||
var t sql.NullString
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT MAX(created_at) FROM reactive_runs
|
||||
WHERE agent_name = ? AND status IN ('running', 'succeeded', 'failed')`,
|
||||
agentName,
|
||||
).Scan(&t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !t.Valid || t.String == "" {
|
||||
return nil, nil
|
||||
}
|
||||
parsed, err := parseTime(t.String)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &parsed, nil
|
||||
}
|
||||
|
||||
// parseTime tries multiple time formats used by SQLite / Go driver.
|
||||
func parseTime(s string) (time.Time, error) {
|
||||
formats := []string{
|
||||
time.RFC3339,
|
||||
time.RFC3339Nano,
|
||||
"2006-01-02T15:04:05Z",
|
||||
"2006-01-02 15:04:05+00:00",
|
||||
"2006-01-02 15:04:05",
|
||||
"2006-01-02T15:04:05.999999999Z07:00",
|
||||
}
|
||||
for _, f := range formats {
|
||||
if t, err := time.Parse(f, s); err == nil {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
return time.Time{}, fmt.Errorf("cannot parse time %q", s)
|
||||
}
|
||||
|
||||
// IsAgentRunning checks if the agent has an active (running) reactive run.
|
||||
func (s *Store) IsAgentRunning(ctx context.Context, agentName string) (bool, error) {
|
||||
var count int
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM reactive_runs WHERE agent_name = ? AND status = 'running'`,
|
||||
agentName,
|
||||
).Scan(&count)
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
func runSelectSQL() string {
|
||||
return `SELECT id, agent_name, trigger_message_id, trigger_event, trigger_depth, trigger_from,
|
||||
status, k8s_job_name, k8s_namespace, started_at, completed_at, duration_ms, error_log, token_cost_json, created_at
|
||||
FROM reactive_runs`
|
||||
}
|
||||
|
||||
func scanRunFields(r *ReactiveRun, msgID *sql.NullInt64, triggerFrom, jobName, namespace, errorLog, tokenCost *sql.NullString, startedAt, completedAt *sql.NullString, durationMs *sql.NullInt64, createdAt *string) {
|
||||
if msgID.Valid {
|
||||
r.TriggerMessageID = &msgID.Int64
|
||||
}
|
||||
r.TriggerFrom = triggerFrom.String
|
||||
r.K8sJobName = jobName.String
|
||||
r.K8sNamespace = namespace.String
|
||||
if startedAt.Valid && startedAt.String != "" {
|
||||
if t, err := parseTime(startedAt.String); err == nil {
|
||||
r.StartedAt = &t
|
||||
}
|
||||
}
|
||||
if completedAt.Valid && completedAt.String != "" {
|
||||
if t, err := parseTime(completedAt.String); err == nil {
|
||||
r.CompletedAt = &t
|
||||
}
|
||||
}
|
||||
if durationMs.Valid {
|
||||
r.DurationMs = &durationMs.Int64
|
||||
}
|
||||
r.ErrorLog = errorLog.String
|
||||
r.TokenCostJSON = tokenCost.String
|
||||
if *createdAt != "" {
|
||||
if t, err := parseTime(*createdAt); err == nil {
|
||||
r.CreatedAt = t
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) scanRun(row *sql.Row) (*ReactiveRun, error) {
|
||||
var r ReactiveRun
|
||||
var msgID sql.NullInt64
|
||||
var triggerFrom, jobName, namespace, errorLog, tokenCost sql.NullString
|
||||
var startedAt, completedAt sql.NullString
|
||||
var durationMs sql.NullInt64
|
||||
var createdAt string
|
||||
|
||||
err := row.Scan(
|
||||
&r.ID, &r.AgentName, &msgID, &r.TriggerEvent, &r.TriggerDepth, &triggerFrom,
|
||||
&r.Status, &jobName, &namespace, &startedAt, &completedAt, &durationMs, &errorLog, &tokenCost, &createdAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
scanRunFields(&r, &msgID, &triggerFrom, &jobName, &namespace, &errorLog, &tokenCost, &startedAt, &completedAt, &durationMs, &createdAt)
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (s *Store) scanRuns(rows *sql.Rows) ([]*ReactiveRun, error) {
|
||||
var runs []*ReactiveRun
|
||||
for rows.Next() {
|
||||
var r ReactiveRun
|
||||
var msgID sql.NullInt64
|
||||
var triggerFrom, jobName, namespace, errorLog, tokenCost sql.NullString
|
||||
var startedAt, completedAt sql.NullString
|
||||
var durationMs sql.NullInt64
|
||||
var createdAt string
|
||||
|
||||
err := rows.Scan(
|
||||
&r.ID, &r.AgentName, &msgID, &r.TriggerEvent, &r.TriggerDepth, &triggerFrom,
|
||||
&r.Status, &jobName, &namespace, &startedAt, &completedAt, &durationMs, &errorLog, &tokenCost, &createdAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
scanRunFields(&r, &msgID, &triggerFrom, &jobName, &namespace, &errorLog, &tokenCost, &startedAt, &completedAt, &durationMs, &createdAt)
|
||||
runs = append(runs, &r)
|
||||
}
|
||||
if runs == nil {
|
||||
runs = []*ReactiveRun{}
|
||||
}
|
||||
return runs, rows.Err()
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
-- Message reactions for workflow state tracking
|
||||
-- Supports: approve, reject, in_progress, done, published
|
||||
|
||||
CREATE TABLE IF NOT EXISTS message_reactions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
message_id INTEGER NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||
agent_name TEXT NOT NULL,
|
||||
reaction TEXT NOT NULL CHECK(reaction IN ('approve', 'reject', 'in_progress', 'done', 'published')),
|
||||
metadata TEXT NOT NULL DEFAULT '{}',
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(message_id, agent_name, reaction)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_reactions_message ON message_reactions(message_id);
|
||||
CREATE INDEX idx_reactions_agent ON message_reactions(agent_name);
|
||||
CREATE INDEX idx_reactions_type ON message_reactions(reaction);
|
||||
|
||||
-- Channel workflow settings
|
||||
ALTER TABLE channels ADD COLUMN workflow_enabled BOOLEAN NOT NULL DEFAULT 0;
|
||||
ALTER TABLE channels ADD COLUMN auto_approve BOOLEAN NOT NULL DEFAULT 0;
|
||||
ALTER TABLE channels ADD COLUMN stalemate_remind_after TEXT NOT NULL DEFAULT '24h';
|
||||
ALTER TABLE channels ADD COLUMN stalemate_escalate_after TEXT NOT NULL DEFAULT '72h';
|
||||
@@ -0,0 +1,17 @@
|
||||
-- Trust scores per (agent, action_type) for graduated autonomy
|
||||
CREATE TABLE IF NOT EXISTS agent_trust (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
agent_name TEXT NOT NULL,
|
||||
action_type TEXT NOT NULL,
|
||||
score REAL NOT NULL DEFAULT 0.0,
|
||||
adjustments_count INTEGER NOT NULL DEFAULT 0,
|
||||
last_adjusted_at TIMESTAMP,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(agent_name, action_type)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_trust_agent ON agent_trust(agent_name);
|
||||
|
||||
-- Channel autonomy thresholds
|
||||
ALTER TABLE channels ADD COLUMN publish_threshold REAL NOT NULL DEFAULT 0.8;
|
||||
ALTER TABLE channels ADD COLUMN approve_threshold REAL NOT NULL DEFAULT 0.6;
|
||||
@@ -0,0 +1,35 @@
|
||||
-- 013: Reactive agent triggering
|
||||
-- Extends agents with trigger configuration, adds reactive_runs tracking table.
|
||||
|
||||
-- Extend agents table with reactive trigger configuration
|
||||
ALTER TABLE agents ADD COLUMN trigger_mode TEXT NOT NULL DEFAULT 'passive';
|
||||
ALTER TABLE agents ADD COLUMN cooldown_seconds INTEGER NOT NULL DEFAULT 600;
|
||||
ALTER TABLE agents ADD COLUMN daily_trigger_budget INTEGER NOT NULL DEFAULT 8;
|
||||
ALTER TABLE agents ADD COLUMN max_trigger_depth INTEGER NOT NULL DEFAULT 5;
|
||||
ALTER TABLE agents ADD COLUMN k8s_image TEXT;
|
||||
ALTER TABLE agents ADD COLUMN k8s_env_json TEXT;
|
||||
ALTER TABLE agents ADD COLUMN k8s_resource_preset TEXT NOT NULL DEFAULT 'default';
|
||||
ALTER TABLE agents ADD COLUMN pending_work INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
-- Reactive trigger runs: tracks every trigger evaluation and K8s job lifecycle
|
||||
CREATE TABLE reactive_runs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
agent_name TEXT NOT NULL REFERENCES agents(name),
|
||||
trigger_message_id INTEGER,
|
||||
trigger_event TEXT NOT NULL,
|
||||
trigger_depth INTEGER NOT NULL DEFAULT 0,
|
||||
trigger_from TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'queued',
|
||||
k8s_job_name TEXT,
|
||||
k8s_namespace TEXT,
|
||||
started_at DATETIME,
|
||||
completed_at DATETIME,
|
||||
duration_ms INTEGER,
|
||||
error_log TEXT,
|
||||
token_cost_json TEXT,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX idx_reactive_runs_agent_created ON reactive_runs(agent_name, created_at);
|
||||
CREATE INDEX idx_reactive_runs_status ON reactive_runs(status);
|
||||
CREATE INDEX idx_reactive_runs_agent_status ON reactive_runs(agent_name, status);
|
||||
@@ -0,0 +1,58 @@
|
||||
-- 016: Agent SQL query views
|
||||
-- These views are used by the 'query' action to give agents read access
|
||||
-- to messages they can see. The views expose a stable schema that agents
|
||||
-- can query via SQL. Access control is enforced at the Go layer by
|
||||
-- rewriting queries to filter by agent name.
|
||||
|
||||
-- Note: SQLite views cannot be parameterized. The Go query executor
|
||||
-- wraps agent queries in a CTE that filters by the authenticated agent's
|
||||
-- access (own DMs + joined channels). These views provide the base schema.
|
||||
|
||||
-- my_messages: All messages accessible to the calling agent
|
||||
CREATE VIEW IF NOT EXISTS v_agent_messages AS
|
||||
SELECT
|
||||
m.id,
|
||||
m.body,
|
||||
m.from_agent,
|
||||
m.to_agent,
|
||||
m.priority,
|
||||
m.status,
|
||||
m.metadata,
|
||||
m.created_at,
|
||||
m.updated_at,
|
||||
c.name AS channel_name,
|
||||
m.channel_id,
|
||||
m.reply_to,
|
||||
m.conversation_id
|
||||
FROM messages m
|
||||
LEFT JOIN channels c ON c.id = m.channel_id;
|
||||
|
||||
-- my_channels: Channels the calling agent has joined
|
||||
CREATE VIEW IF NOT EXISTS v_agent_channels AS
|
||||
SELECT
|
||||
c.id,
|
||||
c.name,
|
||||
c.description,
|
||||
c.type,
|
||||
c.topic,
|
||||
c.is_private,
|
||||
c.created_at,
|
||||
cm.joined_at AS member_since
|
||||
FROM channels c
|
||||
JOIN channel_members cm ON cm.channel_id = c.id;
|
||||
|
||||
-- channel_messages: Messages in channels (filtered by membership at Go layer)
|
||||
CREATE VIEW IF NOT EXISTS v_channel_messages AS
|
||||
SELECT
|
||||
m.id,
|
||||
m.body,
|
||||
m.from_agent,
|
||||
m.priority,
|
||||
m.status,
|
||||
m.metadata,
|
||||
m.created_at,
|
||||
c.name AS channel_name,
|
||||
m.channel_id,
|
||||
m.reply_to
|
||||
FROM messages m
|
||||
JOIN channels c ON c.id = m.channel_id;
|
||||
+97
-23
@@ -12,17 +12,22 @@ import (
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
// DB wraps a *sql.DB with SynapBus-specific configuration.
|
||||
// DB wraps a write-only *sql.DB and an optional read-only *sql.DB
|
||||
// for split connection pool architecture. The write pool has MaxOpenConns=1
|
||||
// to serialize writes and eliminate SQLITE_BUSY errors. The read pool has
|
||||
// MaxOpenConns=8 and query_only=ON for safe concurrent reads.
|
||||
type DB struct {
|
||||
*sql.DB
|
||||
*sql.DB // Write pool (MaxOpenConns=1)
|
||||
ReadDB *sql.DB // Read pool (MaxOpenConns=8, query_only=ON) — nil for :memory: DBs
|
||||
}
|
||||
|
||||
// New opens a SQLite database with WAL mode, busy_timeout, and foreign keys enabled.
|
||||
// If dataDir is empty or ":memory:", an in-memory database is used.
|
||||
// New opens a SQLite database with WAL mode, split read/write pools, and foreign keys.
|
||||
// If dataDir is empty or ":memory:", an in-memory database is used (single pool, no split).
|
||||
func New(ctx context.Context, dataDir string) (*DB, error) {
|
||||
var dsn string
|
||||
isMemory := dataDir == "" || dataDir == ":memory:"
|
||||
|
||||
if dataDir == "" || dataDir == ":memory:" {
|
||||
if isMemory {
|
||||
dsn = ":memory:"
|
||||
} else {
|
||||
if err := os.MkdirAll(dataDir, 0o755); err != nil {
|
||||
@@ -31,16 +36,76 @@ func New(ctx context.Context, dataDir string) (*DB, error) {
|
||||
dsn = filepath.Join(dataDir, "synapbus.db")
|
||||
}
|
||||
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
// Open WRITE pool (single connection, serializes all writes)
|
||||
writeDB, err := openPool(ctx, dsn, poolConfig{
|
||||
maxOpen: 1,
|
||||
maxIdle: 1,
|
||||
queryOnly: false,
|
||||
label: "write",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open database: %w", err)
|
||||
return nil, fmt.Errorf("open write pool: %w", err)
|
||||
}
|
||||
|
||||
// Configure SQLite pragmas
|
||||
result := &DB{DB: writeDB}
|
||||
|
||||
// For file-based databases, open a separate READ pool
|
||||
if !isMemory {
|
||||
readDB, err := openPool(ctx, dsn, poolConfig{
|
||||
maxOpen: 8,
|
||||
maxIdle: 4,
|
||||
queryOnly: true,
|
||||
label: "read",
|
||||
})
|
||||
if err != nil {
|
||||
writeDB.Close()
|
||||
return nil, fmt.Errorf("open read pool: %w", err)
|
||||
}
|
||||
result.ReadDB = readDB
|
||||
}
|
||||
|
||||
// Verify settings on write pool
|
||||
var journalMode string
|
||||
if err := writeDB.QueryRowContext(ctx, "PRAGMA journal_mode").Scan(&journalMode); err != nil {
|
||||
result.Close()
|
||||
return nil, fmt.Errorf("verify journal_mode: %w", err)
|
||||
}
|
||||
|
||||
slog.Info("database opened",
|
||||
"dsn", dsn,
|
||||
"journal_mode", journalMode,
|
||||
"write_pool", "MaxOpenConns=1",
|
||||
"read_pool_enabled", result.ReadDB != nil,
|
||||
)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type poolConfig struct {
|
||||
maxOpen int
|
||||
maxIdle int
|
||||
queryOnly bool
|
||||
label string
|
||||
}
|
||||
|
||||
func openPool(ctx context.Context, dsn string, cfg poolConfig) (*sql.DB, error) {
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open %s pool: %w", cfg.label, err)
|
||||
}
|
||||
|
||||
db.SetMaxOpenConns(cfg.maxOpen)
|
||||
db.SetMaxIdleConns(cfg.maxIdle)
|
||||
|
||||
pragmas := []string{
|
||||
"PRAGMA journal_mode=WAL",
|
||||
"PRAGMA busy_timeout=5000",
|
||||
"PRAGMA busy_timeout=15000",
|
||||
"PRAGMA foreign_keys=ON",
|
||||
"PRAGMA synchronous=NORMAL",
|
||||
"PRAGMA wal_autocheckpoint=1000",
|
||||
}
|
||||
if cfg.queryOnly {
|
||||
pragmas = append(pragmas, "PRAGMA query_only=ON")
|
||||
}
|
||||
|
||||
for _, pragma := range pragmas {
|
||||
@@ -50,22 +115,31 @@ func New(ctx context.Context, dataDir string) (*DB, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Verify settings
|
||||
var journalMode string
|
||||
if err := db.QueryRowContext(ctx, "PRAGMA journal_mode").Scan(&journalMode); err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("verify journal_mode: %w", err)
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// QueryDB returns the read pool if available, otherwise falls back to the write pool.
|
||||
// Use this for all SELECT queries to avoid blocking writers.
|
||||
func (db *DB) QueryDB() *sql.DB {
|
||||
if db.ReadDB != nil {
|
||||
return db.ReadDB
|
||||
}
|
||||
|
||||
slog.Info("database opened",
|
||||
"dsn", dsn,
|
||||
"journal_mode", journalMode,
|
||||
)
|
||||
|
||||
return &DB{DB: db}, nil
|
||||
return db.DB
|
||||
}
|
||||
|
||||
// Close closes the database connection.
|
||||
// Close closes both the write and read database connections.
|
||||
func (db *DB) Close() error {
|
||||
return db.DB.Close()
|
||||
var errs []error
|
||||
if db.ReadDB != nil {
|
||||
if err := db.ReadDB.Close(); err != nil {
|
||||
errs = append(errs, fmt.Errorf("close read pool: %w", err))
|
||||
}
|
||||
}
|
||||
if err := db.DB.Close(); err != nil {
|
||||
errs = append(errs, fmt.Errorf("close write pool: %w", err))
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
return errs[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -68,11 +68,11 @@ func TestNew(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query busy_timeout: %v", err)
|
||||
}
|
||||
if timeout != 5000 {
|
||||
t.Errorf("busy_timeout = %d, want 5000", timeout)
|
||||
if timeout != 15000 {
|
||||
t.Errorf("busy_timeout = %d, want 15000", timeout)
|
||||
}
|
||||
|
||||
// Verify database is usable
|
||||
// Verify database is usable via write pool
|
||||
_, err = db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY)")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test table: %v", err)
|
||||
@@ -80,3 +80,77 @@ func TestNew(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitPools(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
dir := t.TempDir()
|
||||
|
||||
db, err := New(ctx, dir)
|
||||
if err != nil {
|
||||
t.Fatalf("New() error: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Run migrations to create tables
|
||||
if err := RunMigrations(ctx, db.DB); err != nil {
|
||||
t.Fatalf("migrations: %v", err)
|
||||
}
|
||||
|
||||
// Verify read pool exists for file-based DB
|
||||
if db.ReadDB == nil {
|
||||
t.Fatal("expected ReadDB to be non-nil for file-based database")
|
||||
}
|
||||
|
||||
// Verify QueryDB returns read pool
|
||||
if db.QueryDB() != db.ReadDB {
|
||||
t.Error("QueryDB() should return ReadDB when available")
|
||||
}
|
||||
|
||||
// Create user first (FK requirement)
|
||||
_, err = db.Exec("INSERT INTO users (id, username, password_hash, display_name) VALUES (1, 'testuser', 'hash', 'Test')")
|
||||
if err != nil {
|
||||
t.Fatalf("create user: %v", err)
|
||||
}
|
||||
|
||||
// Verify write pool can write
|
||||
_, err = db.Exec("INSERT INTO agents (name, display_name, type, capabilities, owner_id, api_key_hash, status) VALUES ('test-agent', 'Test', 'ai', '{}', 1, 'hash', 'active')")
|
||||
if err != nil {
|
||||
t.Fatalf("write pool should allow writes: %v", err)
|
||||
}
|
||||
|
||||
// Verify read pool can read
|
||||
var name string
|
||||
err = db.ReadDB.QueryRow("SELECT name FROM agents WHERE name = 'test-agent'").Scan(&name)
|
||||
if err != nil {
|
||||
t.Fatalf("read pool should allow reads: %v", err)
|
||||
}
|
||||
if name != "test-agent" {
|
||||
t.Errorf("expected 'test-agent', got %q", name)
|
||||
}
|
||||
|
||||
// Verify read pool rejects writes
|
||||
_, err = db.ReadDB.Exec("INSERT INTO agents (name, display_name, type, capabilities, owner_id, api_key_hash, status) VALUES ('bad', 'Bad', 'ai', '{}', 1, 'hash', 'active')")
|
||||
if err == nil {
|
||||
t.Fatal("read pool should reject writes (query_only=ON)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInMemoryNoSplitPool(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
db, err := New(ctx, ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("New() error: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// In-memory DB should NOT have a separate read pool
|
||||
if db.ReadDB != nil {
|
||||
t.Error("in-memory DB should not have a separate ReadDB")
|
||||
}
|
||||
|
||||
// QueryDB should fall back to write pool
|
||||
if db.QueryDB() != db.DB {
|
||||
t.Error("QueryDB() should return write pool for in-memory DB")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,9 @@ import (
|
||||
"sort"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/common/expfmt"
|
||||
)
|
||||
|
||||
// Metrics provides Prometheus-compatible metrics for SynapBus.
|
||||
@@ -93,6 +96,17 @@ func (m *Metrics) WritePrometheus(w io.Writer) {
|
||||
fmt.Fprintf(w, "# HELP synapbus_active_agents Number of currently active agents.\n")
|
||||
fmt.Fprintf(w, "# TYPE synapbus_active_agents gauge\n")
|
||||
fmt.Fprintf(w, "synapbus_active_agents %d\n", m.activeAgents.Load())
|
||||
fmt.Fprintf(w, "\n")
|
||||
|
||||
// Append metrics from the standard Prometheus registry (reactor metrics, etc.)
|
||||
mfs, _ := prometheus.DefaultGatherer.Gather()
|
||||
enc := expfmt.NewEncoder(w, expfmt.NewFormat(expfmt.TypeTextPlain))
|
||||
for _, mf := range mfs {
|
||||
// Only include our custom metrics, skip Go runtime metrics
|
||||
if name := mf.GetName(); len(name) > 8 && name[:8] == "synapbus" {
|
||||
_ = enc.Encode(mf)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NullMetrics is a no-op metrics implementation for when metrics are disabled.
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
// Package trust provides agent trust score tracking for graduated autonomy.
|
||||
package trust
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Trust adjustment constants.
|
||||
const (
|
||||
ApprovalIncrement = 0.05
|
||||
RejectionDecrement = 0.10
|
||||
MinScore = 0.0
|
||||
MaxScore = 1.0
|
||||
)
|
||||
|
||||
// Common action types (extensible — any string is valid).
|
||||
const (
|
||||
ActionResearch = "research"
|
||||
ActionPublish = "publish"
|
||||
ActionComment = "comment"
|
||||
ActionApprove = "approve"
|
||||
ActionOperate = "operate"
|
||||
)
|
||||
|
||||
// Sentinel errors.
|
||||
var (
|
||||
ErrAlreadyClaimed = errors.New("work item already claimed by another agent")
|
||||
ErrSelfReaction = errors.New("cannot adjust trust for self-reactions")
|
||||
)
|
||||
|
||||
// TrustScore represents an agent's trust level for a specific action type.
|
||||
type TrustScore struct {
|
||||
ID int64 `json:"id"`
|
||||
AgentName string `json:"agent_name"`
|
||||
ActionType string `json:"action_type"`
|
||||
Score float64 `json:"score"`
|
||||
AdjustmentsCount int `json:"adjustments_count"`
|
||||
LastAdjustedAt *time.Time `json:"last_adjusted_at,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// AgentTrustSummary is a map of action_type -> score for an agent.
|
||||
type AgentTrustSummary map[string]float64
|
||||
|
||||
// ClampScore ensures a score stays within [0.0, 1.0].
|
||||
func ClampScore(score float64) float64 {
|
||||
if score < MinScore {
|
||||
return MinScore
|
||||
}
|
||||
if score > MaxScore {
|
||||
return MaxScore
|
||||
}
|
||||
return score
|
||||
}
|
||||
|
||||
// WorkflowStateChangeEvent is the webhook payload for state transitions.
|
||||
type WorkflowStateChangeEvent struct {
|
||||
MessageID int64 `json:"message_id"`
|
||||
ChannelID int64 `json:"channel_id,omitempty"`
|
||||
OldState string `json:"old_state"`
|
||||
NewState string `json:"new_state"`
|
||||
TriggeredBy string `json:"triggered_by"`
|
||||
Reaction string `json:"reaction"`
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package trust
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestClampScore(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input float64
|
||||
want float64
|
||||
}{
|
||||
{"zero", 0.0, 0.0},
|
||||
{"one", 1.0, 1.0},
|
||||
{"mid", 0.5, 0.5},
|
||||
{"below zero", -0.1, MinScore},
|
||||
{"far below zero", -10.0, MinScore},
|
||||
{"above one", 1.1, MaxScore},
|
||||
{"far above one", 100.0, MaxScore},
|
||||
{"small positive", 0.001, 0.001},
|
||||
{"near max", 0.999, 0.999},
|
||||
{"exactly min", MinScore, MinScore},
|
||||
{"exactly max", MaxScore, MaxScore},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := ClampScore(tt.input)
|
||||
if got != tt.want {
|
||||
t.Errorf("ClampScore(%f) = %f, want %f", tt.input, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package trust
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
// Service provides business logic for trust score management.
|
||||
type Service struct {
|
||||
store Store
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// NewService creates a new trust service.
|
||||
func NewService(store Store, logger *slog.Logger) *Service {
|
||||
return &Service{
|
||||
store: store,
|
||||
logger: logger.With("component", "trust"),
|
||||
}
|
||||
}
|
||||
|
||||
// RecordApproval increases an agent's trust for an action type.
|
||||
func (s *Service) RecordApproval(ctx context.Context, agentName, actionType string) (*TrustScore, error) {
|
||||
ts, err := s.store.UpsertScore(ctx, agentName, actionType, ApprovalIncrement)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("record approval: %w", err)
|
||||
}
|
||||
s.logger.Info("trust increased",
|
||||
"agent", agentName,
|
||||
"action", actionType,
|
||||
"delta", ApprovalIncrement,
|
||||
"new_score", ts.Score,
|
||||
)
|
||||
return ts, nil
|
||||
}
|
||||
|
||||
// RecordRejection decreases an agent's trust for an action type.
|
||||
func (s *Service) RecordRejection(ctx context.Context, agentName, actionType string) (*TrustScore, error) {
|
||||
ts, err := s.store.UpsertScore(ctx, agentName, actionType, -RejectionDecrement)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("record rejection: %w", err)
|
||||
}
|
||||
s.logger.Info("trust decreased",
|
||||
"agent", agentName,
|
||||
"action", actionType,
|
||||
"delta", -RejectionDecrement,
|
||||
"new_score", ts.Score,
|
||||
)
|
||||
return ts, nil
|
||||
}
|
||||
|
||||
// GetScores returns all trust scores for an agent as a summary map.
|
||||
func (s *Service) GetScores(ctx context.Context, agentName string) (AgentTrustSummary, error) {
|
||||
scores, err := s.store.GetAllScores(ctx, agentName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get scores: %w", err)
|
||||
}
|
||||
summary := make(AgentTrustSummary)
|
||||
for _, ts := range scores {
|
||||
summary[ts.ActionType] = ts.Score
|
||||
}
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
// GetScore returns the trust score for a specific (agent, action) pair.
|
||||
func (s *Service) GetScore(ctx context.Context, agentName, actionType string) (float64, error) {
|
||||
ts, err := s.store.GetScore(ctx, agentName, actionType)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("get score: %w", err)
|
||||
}
|
||||
return ts.Score, nil
|
||||
}
|
||||
|
||||
// CheckAutonomy returns whether an agent has sufficient trust for an action
|
||||
// given a channel's threshold.
|
||||
func (s *Service) CheckAutonomy(ctx context.Context, agentName, actionType string, threshold float64) (bool, float64, error) {
|
||||
score, err := s.GetScore(ctx, agentName, actionType)
|
||||
if err != nil {
|
||||
return false, 0, err
|
||||
}
|
||||
return score >= threshold, score, nil
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package trust
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Store defines the storage interface for trust scores.
|
||||
type Store interface {
|
||||
GetScore(ctx context.Context, agentName, actionType string) (*TrustScore, error)
|
||||
GetAllScores(ctx context.Context, agentName string) ([]*TrustScore, error)
|
||||
UpsertScore(ctx context.Context, agentName, actionType string, delta float64) (*TrustScore, error)
|
||||
}
|
||||
|
||||
// SQLiteStore implements Store using SQLite.
|
||||
type SQLiteStore struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
// NewSQLiteStore creates a new SQLite-backed trust store.
|
||||
func NewSQLiteStore(db *sql.DB) *SQLiteStore {
|
||||
return &SQLiteStore{db: db}
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) GetScore(ctx context.Context, agentName, actionType string) (*TrustScore, error) {
|
||||
var ts TrustScore
|
||||
var lastAdj sql.NullTime
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, agent_name, action_type, score, adjustments_count, last_adjusted_at, created_at
|
||||
FROM agent_trust WHERE agent_name = ? AND action_type = ?`,
|
||||
agentName, actionType,
|
||||
).Scan(&ts.ID, &ts.AgentName, &ts.ActionType, &ts.Score, &ts.AdjustmentsCount, &lastAdj, &ts.CreatedAt)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return &TrustScore{AgentName: agentName, ActionType: actionType, Score: 0.0}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("get trust score: %w", err)
|
||||
}
|
||||
if lastAdj.Valid {
|
||||
ts.LastAdjustedAt = &lastAdj.Time
|
||||
}
|
||||
return &ts, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) GetAllScores(ctx context.Context, agentName string) ([]*TrustScore, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT id, agent_name, action_type, score, adjustments_count, last_adjusted_at, created_at
|
||||
FROM agent_trust WHERE agent_name = ?
|
||||
ORDER BY action_type`, agentName,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get all trust scores: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var scores []*TrustScore
|
||||
for rows.Next() {
|
||||
var ts TrustScore
|
||||
var lastAdj sql.NullTime
|
||||
if err := rows.Scan(&ts.ID, &ts.AgentName, &ts.ActionType, &ts.Score, &ts.AdjustmentsCount, &lastAdj, &ts.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scan trust score: %w", err)
|
||||
}
|
||||
if lastAdj.Valid {
|
||||
ts.LastAdjustedAt = &lastAdj.Time
|
||||
}
|
||||
scores = append(scores, &ts)
|
||||
}
|
||||
if scores == nil {
|
||||
scores = []*TrustScore{}
|
||||
}
|
||||
return scores, rows.Err()
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) UpsertScore(ctx context.Context, agentName, actionType string, delta float64) (*TrustScore, error) {
|
||||
// Upsert: insert if not exists, update if exists
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO agent_trust (agent_name, action_type, score, adjustments_count, last_adjusted_at, created_at)
|
||||
VALUES (?, ?, MAX(0.0, MIN(1.0, ?)), 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(agent_name, action_type) DO UPDATE SET
|
||||
score = MAX(0.0, MIN(1.0, agent_trust.score + ?)),
|
||||
adjustments_count = agent_trust.adjustments_count + 1,
|
||||
last_adjusted_at = CURRENT_TIMESTAMP`,
|
||||
agentName, actionType, delta, delta,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("upsert trust score: %w", err)
|
||||
}
|
||||
|
||||
return s.GetScore(ctx, agentName, actionType)
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
package trust
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
"github.com/synapbus/synapbus/internal/storage"
|
||||
)
|
||||
|
||||
func newTestDB(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("open database: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { db.Close() })
|
||||
|
||||
if _, err := db.Exec("PRAGMA foreign_keys=ON"); err != nil {
|
||||
t.Fatalf("enable foreign keys: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
if err := storage.RunMigrations(ctx, db); err != nil {
|
||||
t.Fatalf("run migrations: %v", err)
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func TestSQLiteStore_UpsertAndGet(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
ts, err := store.UpsertScore(ctx, "agent-a", ActionResearch, 0.5)
|
||||
if err != nil {
|
||||
t.Fatalf("UpsertScore: %v", err)
|
||||
}
|
||||
|
||||
if ts.Score != 0.5 {
|
||||
t.Errorf("Score = %f, want 0.5", ts.Score)
|
||||
}
|
||||
if ts.AgentName != "agent-a" {
|
||||
t.Errorf("AgentName = %q, want %q", ts.AgentName, "agent-a")
|
||||
}
|
||||
if ts.ActionType != ActionResearch {
|
||||
t.Errorf("ActionType = %q, want %q", ts.ActionType, ActionResearch)
|
||||
}
|
||||
if ts.AdjustmentsCount != 1 {
|
||||
t.Errorf("AdjustmentsCount = %d, want 1", ts.AdjustmentsCount)
|
||||
}
|
||||
|
||||
// Verify it's retrievable via GetScore
|
||||
got, err := store.GetScore(ctx, "agent-a", ActionResearch)
|
||||
if err != nil {
|
||||
t.Fatalf("GetScore: %v", err)
|
||||
}
|
||||
if got.Score != 0.5 {
|
||||
t.Errorf("GetScore Score = %f, want 0.5", got.Score)
|
||||
}
|
||||
if got.AgentName != "agent-a" {
|
||||
t.Errorf("GetScore AgentName = %q, want %q", got.AgentName, "agent-a")
|
||||
}
|
||||
if got.ActionType != ActionResearch {
|
||||
t.Errorf("GetScore ActionType = %q, want %q", got.ActionType, ActionResearch)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteStore_UpsertIncrement(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
// First upsert: initial score
|
||||
_, err := store.UpsertScore(ctx, "agent-a", ActionPublish, 0.3)
|
||||
if err != nil {
|
||||
t.Fatalf("UpsertScore first: %v", err)
|
||||
}
|
||||
|
||||
// Second upsert: should increment
|
||||
ts, err := store.UpsertScore(ctx, "agent-a", ActionPublish, 0.2)
|
||||
if err != nil {
|
||||
t.Fatalf("UpsertScore second: %v", err)
|
||||
}
|
||||
|
||||
want := 0.5
|
||||
if ts.Score != want {
|
||||
t.Errorf("Score = %f, want %f", ts.Score, want)
|
||||
}
|
||||
if ts.AdjustmentsCount != 2 {
|
||||
t.Errorf("AdjustmentsCount = %d, want 2", ts.AdjustmentsCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteStore_ClampMax(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
// Insert a high score
|
||||
_, err := store.UpsertScore(ctx, "agent-a", ActionComment, 0.9)
|
||||
if err != nil {
|
||||
t.Fatalf("UpsertScore first: %v", err)
|
||||
}
|
||||
|
||||
// Push past 1.0
|
||||
ts, err := store.UpsertScore(ctx, "agent-a", ActionComment, 0.5)
|
||||
if err != nil {
|
||||
t.Fatalf("UpsertScore second: %v", err)
|
||||
}
|
||||
|
||||
if ts.Score != MaxScore {
|
||||
t.Errorf("Score = %f, want %f (clamped to max)", ts.Score, MaxScore)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteStore_ClampMin(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
// Insert a low score
|
||||
_, err := store.UpsertScore(ctx, "agent-a", ActionOperate, 0.1)
|
||||
if err != nil {
|
||||
t.Fatalf("UpsertScore first: %v", err)
|
||||
}
|
||||
|
||||
// Push past 0.0 with a large negative delta
|
||||
ts, err := store.UpsertScore(ctx, "agent-a", ActionOperate, -0.5)
|
||||
if err != nil {
|
||||
t.Fatalf("UpsertScore second: %v", err)
|
||||
}
|
||||
|
||||
if ts.Score != MinScore {
|
||||
t.Errorf("Score = %f, want %f (clamped to min)", ts.Score, MinScore)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteStore_GetAllScores(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
// Insert multiple action types for the same agent
|
||||
actions := []struct {
|
||||
actionType string
|
||||
delta float64
|
||||
}{
|
||||
{ActionResearch, 0.3},
|
||||
{ActionPublish, 0.5},
|
||||
{ActionComment, 0.7},
|
||||
}
|
||||
|
||||
for _, a := range actions {
|
||||
if _, err := store.UpsertScore(ctx, "agent-a", a.actionType, a.delta); err != nil {
|
||||
t.Fatalf("UpsertScore %s: %v", a.actionType, err)
|
||||
}
|
||||
}
|
||||
|
||||
scores, err := store.GetAllScores(ctx, "agent-a")
|
||||
if err != nil {
|
||||
t.Fatalf("GetAllScores: %v", err)
|
||||
}
|
||||
|
||||
if len(scores) != 3 {
|
||||
t.Fatalf("got %d scores, want 3", len(scores))
|
||||
}
|
||||
|
||||
// Scores are ordered by action_type alphabetically
|
||||
scoreMap := make(map[string]float64)
|
||||
for _, s := range scores {
|
||||
scoreMap[s.ActionType] = s.Score
|
||||
}
|
||||
|
||||
for _, a := range actions {
|
||||
got, ok := scoreMap[a.actionType]
|
||||
if !ok {
|
||||
t.Errorf("missing score for action %q", a.actionType)
|
||||
continue
|
||||
}
|
||||
if got != a.delta {
|
||||
t.Errorf("score for %q = %f, want %f", a.actionType, got, a.delta)
|
||||
}
|
||||
}
|
||||
|
||||
// Different agent should return empty
|
||||
other, err := store.GetAllScores(ctx, "agent-nonexistent")
|
||||
if err != nil {
|
||||
t.Fatalf("GetAllScores (other): %v", err)
|
||||
}
|
||||
if len(other) != 0 {
|
||||
t.Errorf("got %d scores for nonexistent agent, want 0", len(other))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLiteStore_GetScoreNotFound(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
store := NewSQLiteStore(db)
|
||||
ctx := context.Background()
|
||||
|
||||
// Get score for non-existent agent should return 0.0 (not an error)
|
||||
ts, err := store.GetScore(ctx, "nonexistent-agent", ActionResearch)
|
||||
if err != nil {
|
||||
t.Fatalf("GetScore: %v", err)
|
||||
}
|
||||
|
||||
if ts.Score != 0.0 {
|
||||
t.Errorf("Score = %f, want 0.0 for non-existent agent", ts.Score)
|
||||
}
|
||||
if ts.AgentName != "nonexistent-agent" {
|
||||
t.Errorf("AgentName = %q, want %q", ts.AgentName, "nonexistent-agent")
|
||||
}
|
||||
if ts.ActionType != ActionResearch {
|
||||
t.Errorf("ActionType = %q, want %q", ts.ActionType, ActionResearch)
|
||||
}
|
||||
if ts.AdjustmentsCount != 0 {
|
||||
t.Errorf("AdjustmentsCount = %d, want 0", ts.AdjustmentsCount)
|
||||
}
|
||||
}
|
||||
Vendored
+11
-11
@@ -11,30 +11,30 @@
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Instrument+Sans:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
|
||||
<link href="/_app/immutable/entry/start.Brt_7G-i.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/B_GGgm-X.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/entry/start.C7Nk8VcW.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/BX6BYLlQ.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/BjgrqnN-.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/CAWWRzNH.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/B-GqcZrE.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/DpGxxGDS.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/3PInzq4O.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/qakGnb92.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/entry/app.n45Hd8qT.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/BK7DUW2U.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/CslSvznw.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/C_dJMdcr.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/Du3f5uIc.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/chunks/B3RSY5nb.js" rel="modulepreload">
|
||||
<link href="/_app/immutable/entry/app.BMHPwGGu.js" rel="modulepreload">
|
||||
|
||||
</head>
|
||||
<body data-sveltekit-preload-data="hover">
|
||||
<div style="display: contents">
|
||||
<script>
|
||||
{
|
||||
__sveltekit_nj28a9 = {
|
||||
__sveltekit_wis49l = {
|
||||
base: ""
|
||||
};
|
||||
|
||||
const element = document.currentScript.parentElement;
|
||||
|
||||
Promise.all([
|
||||
import("/_app/immutable/entry/start.Brt_7G-i.js"),
|
||||
import("/_app/immutable/entry/app.n45Hd8qT.js")
|
||||
import("/_app/immutable/entry/start.C7Nk8VcW.js"),
|
||||
import("/_app/immutable/entry/app.BMHPwGGu.js")
|
||||
]).then(([kit, app]) => {
|
||||
kit.start(app, element);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
# Specification Quality Checklist: Embeddings Management, Message Retention & Agent Inbox
|
||||
|
||||
**Purpose**: Validate specification completeness and quality before proceeding to planning
|
||||
**Created**: 2026-03-14
|
||||
**Feature**: [spec.md](../spec.md)
|
||||
|
||||
## Content Quality
|
||||
|
||||
- [x] No implementation details (languages, frameworks, APIs)
|
||||
- [x] Focused on user value and business needs
|
||||
- [x] Written for non-technical stakeholders
|
||||
- [x] All mandatory sections completed
|
||||
|
||||
## Requirement Completeness
|
||||
|
||||
- [x] No [NEEDS CLARIFICATION] markers remain
|
||||
- [x] Requirements are testable and unambiguous
|
||||
- [x] Success criteria are measurable
|
||||
- [x] Success criteria are technology-agnostic (no implementation details)
|
||||
- [x] All acceptance scenarios are defined
|
||||
- [x] Edge cases are identified
|
||||
- [x] Scope is clearly bounded
|
||||
- [x] Dependencies and assumptions identified
|
||||
|
||||
## Feature Readiness
|
||||
|
||||
- [x] All functional requirements have clear acceptance criteria
|
||||
- [x] User scenarios cover primary flows
|
||||
- [x] Feature meets measurable outcomes defined in Success Criteria
|
||||
- [x] No implementation details leak into specification
|
||||
|
||||
## Notes
|
||||
|
||||
- All items pass validation. Spec is ready for `/speckit.plan`.
|
||||
- Assumptions section documents all decisions made where the original description was ambiguous.
|
||||
- The spec references SynapBus-specific concepts (MCP tools, admin socket, HNSW) which are domain terms, not implementation details.
|
||||
@@ -0,0 +1,124 @@
|
||||
# Admin Socket Command Contracts
|
||||
|
||||
All commands use the existing admin socket JSON-RPC protocol:
|
||||
- Request: `{"command": "...", "args": {...}}\n`
|
||||
- Response: `{"ok": true, "data": {...}}\n` or `{"ok": false, "error": "..."}\n`
|
||||
|
||||
## embeddings.status
|
||||
|
||||
**Args**: None
|
||||
|
||||
**Response data**:
|
||||
```json
|
||||
{
|
||||
"provider": "openai",
|
||||
"total_embedded": 1500,
|
||||
"pending_count": 23,
|
||||
"failed_count": 2,
|
||||
"index_size": 1498,
|
||||
"dimensions": 1536
|
||||
}
|
||||
```
|
||||
|
||||
If no provider configured: `provider` is empty string, all counts are from existing data.
|
||||
|
||||
## embeddings.reindex
|
||||
|
||||
**Args**: None
|
||||
|
||||
**Response data**:
|
||||
```json
|
||||
{
|
||||
"deleted_embeddings": 1500,
|
||||
"cleared_index": true,
|
||||
"enqueued_messages": 1523
|
||||
}
|
||||
```
|
||||
|
||||
Requires a running embedding pipeline (provider configured). Returns error if no provider.
|
||||
|
||||
## embeddings.clear
|
||||
|
||||
**Args**: None
|
||||
|
||||
**Response data**:
|
||||
```json
|
||||
{
|
||||
"deleted_embeddings": 1500,
|
||||
"cleared_index": true,
|
||||
"cleared_queue": true
|
||||
}
|
||||
```
|
||||
|
||||
## retention.status
|
||||
|
||||
**Args**: None
|
||||
|
||||
**Response data**:
|
||||
```json
|
||||
{
|
||||
"enabled": true,
|
||||
"retention_period": "8760h0m0s",
|
||||
"retention_period_human": "12 months",
|
||||
"warning_window": "720h0m0s",
|
||||
"cleanup_interval": "24h0m0s",
|
||||
"last_cleanup_at": "2026-03-14T00:00:00Z",
|
||||
"next_cleanup_at": "2026-03-15T00:00:00Z",
|
||||
"message_age_distribution": {
|
||||
"< 1 month": 500,
|
||||
"1-3 months": 300,
|
||||
"3-6 months": 200,
|
||||
"6-12 months": 100,
|
||||
"> 12 months": 15
|
||||
},
|
||||
"total_messages": 1115
|
||||
}
|
||||
```
|
||||
|
||||
## messages.purge
|
||||
|
||||
**Args**:
|
||||
```json
|
||||
{
|
||||
"older_than": "4320h",
|
||||
"agent": "bot-test",
|
||||
"channel": "test-channel"
|
||||
}
|
||||
```
|
||||
|
||||
At least one of `older_than`, `agent`, or `channel` must be specified.
|
||||
|
||||
**Response data**:
|
||||
```json
|
||||
{
|
||||
"deleted_messages": 150,
|
||||
"deleted_embeddings": 120,
|
||||
"deleted_attachments": 5,
|
||||
"cleaned_conversations": 3
|
||||
}
|
||||
```
|
||||
|
||||
## db.vacuum
|
||||
|
||||
**Args**: None
|
||||
|
||||
**Response data**:
|
||||
```json
|
||||
{
|
||||
"before_size_bytes": 104857600,
|
||||
"after_size_bytes": 52428800,
|
||||
"reclaimed_bytes": 52428800,
|
||||
"duration_ms": 3200
|
||||
}
|
||||
```
|
||||
|
||||
## CLI Command Mapping
|
||||
|
||||
| CLI Command | Admin Socket Command |
|
||||
|------------|---------------------|
|
||||
| `synapbus embeddings status` | `embeddings.status` |
|
||||
| `synapbus embeddings reindex` | `embeddings.reindex` |
|
||||
| `synapbus embeddings clear` | `embeddings.clear` |
|
||||
| `synapbus retention status` | `retention.status` |
|
||||
| `synapbus messages purge --older-than 6m --agent X --channel Y` | `messages.purge` |
|
||||
| `synapbus db vacuum` | `db.vacuum` |
|
||||
@@ -0,0 +1,74 @@
|
||||
# MCP Tool Contracts
|
||||
|
||||
## my_status
|
||||
|
||||
**Description**: Get your complete status overview — identity, pending messages, channel mentions, system notifications, and statistics. Call this first when connecting to SynapBus to orient yourself.
|
||||
|
||||
**Parameters**: None (agent identity is derived from authentication context)
|
||||
|
||||
**Response Schema**:
|
||||
|
||||
```json
|
||||
{
|
||||
"agent": {
|
||||
"name": "string — your agent name",
|
||||
"display_name": "string — your display name",
|
||||
"type": "string — 'ai' or 'human'",
|
||||
"owner": "string — name of your human owner"
|
||||
},
|
||||
"direct_messages": [
|
||||
{
|
||||
"id": "number — message ID",
|
||||
"from": "string — sender agent name",
|
||||
"subject": "string — conversation subject",
|
||||
"body": "string — message body (truncated to 200 chars)",
|
||||
"priority": "number — 1-10",
|
||||
"status": "string — pending/processing/done/failed",
|
||||
"created_at": "string — ISO 8601 timestamp"
|
||||
}
|
||||
],
|
||||
"direct_messages_total": "number — total pending DMs (may exceed array length)",
|
||||
"mentions": [
|
||||
{
|
||||
"id": "number — message ID",
|
||||
"channel": "string — channel name",
|
||||
"from": "string — sender agent name",
|
||||
"body": "string — message body (truncated to 200 chars)",
|
||||
"created_at": "string — ISO 8601 timestamp"
|
||||
}
|
||||
],
|
||||
"mentions_total": "number — total recent mentions",
|
||||
"system_notifications": [
|
||||
{
|
||||
"id": "number — message ID",
|
||||
"body": "string — notification text",
|
||||
"created_at": "string — ISO 8601 timestamp"
|
||||
}
|
||||
],
|
||||
"system_notifications_total": "number — total system notifications",
|
||||
"channels": [
|
||||
{
|
||||
"id": "number — channel ID",
|
||||
"name": "string — channel name",
|
||||
"unread": "number — unread message count",
|
||||
"last_message_at": "string — ISO 8601 timestamp or null"
|
||||
}
|
||||
],
|
||||
"stats": {
|
||||
"pending_dms": "number",
|
||||
"channels_joined": "number",
|
||||
"unread_channel_messages": "number",
|
||||
"system_notifications": "number"
|
||||
},
|
||||
"truncated": "boolean — true if any section was capped",
|
||||
"instructions": "string — present only if truncated, guidance on using read_inbox/get_channel_messages"
|
||||
}
|
||||
```
|
||||
|
||||
**Access Control**: Agent identity from MCP auth context. Only returns data the agent has access to.
|
||||
|
||||
**Limits**:
|
||||
- direct_messages: max 10 items
|
||||
- mentions: max 10 items
|
||||
- system_notifications: max 5 items
|
||||
- Body text truncated to 200 characters
|
||||
@@ -0,0 +1,119 @@
|
||||
# Data Model: Embeddings Management, Message Retention & Agent Inbox
|
||||
|
||||
## Existing Entities (Modified)
|
||||
|
||||
### messages (existing table)
|
||||
No schema changes. Retention operates on the existing `created_at` column.
|
||||
- Retention queries: `WHERE created_at < ? AND status != 'processing'`
|
||||
- Warning queries: `WHERE created_at < ? AND created_at >= ?` (11-month to 12-month window)
|
||||
|
||||
### embeddings (existing table)
|
||||
No schema changes. Existing methods `DeleteAllEmbeddings()`, `EmbeddingCount()`, `GetEmbeddingProvider()` are sufficient for the new CLI commands.
|
||||
|
||||
New method needed:
|
||||
- `EmbeddingStats(ctx) → (provider, count, pending, failed, dimensions)` — aggregates data from `embeddings` and `embedding_queue` tables.
|
||||
|
||||
### embedding_queue (existing table)
|
||||
No schema changes. Existing methods `ClearQueue()`, `EnqueueAllMessages()`, `PendingCount()` are sufficient.
|
||||
|
||||
New method needed:
|
||||
- `FailedCount(ctx) → int64` — counts items with `status = 'failed'`
|
||||
|
||||
### agents (existing table)
|
||||
No schema changes. The `system` agent is created as a regular row with `name = 'system'`, `type = 'ai'`, `owner_id = 1` (first admin user).
|
||||
|
||||
### conversations (existing table)
|
||||
No schema changes. Orphaned conversations (no remaining messages) are cleaned up during retention.
|
||||
|
||||
### attachments (existing table)
|
||||
No schema changes. Attachments for deleted messages are cleaned up during retention. CAS files are only removed if no other attachment record references the same hash.
|
||||
|
||||
## New Entities
|
||||
|
||||
### RetentionConfig (in-memory, not persisted)
|
||||
|
||||
Configuration for the message retention system. Set at server startup from CLI flags / env vars.
|
||||
|
||||
| Field | Type | Default | Description |
|
||||
|-------|------|---------|-------------|
|
||||
| RetentionPeriod | time.Duration | 12 months (8760h) | Messages older than this are deleted |
|
||||
| WarningWindow | time.Duration | 1 month (720h) | How long before deletion to send warnings |
|
||||
| CleanupInterval | time.Duration | 24h | How often the cleanup job runs |
|
||||
| Enabled | bool | true | false if retention period is 0 |
|
||||
|
||||
### RetentionState (derived, not stored)
|
||||
|
||||
Runtime state queried by `retention.status` admin command.
|
||||
|
||||
| Field | Type | Description |
|
||||
|-------|------|-------------|
|
||||
| Config | RetentionConfig | Current configuration |
|
||||
| LastCleanupAt | *time.Time | When cleanup last ran (tracked in memory) |
|
||||
| NextCleanupAt | *time.Time | When next cleanup will run |
|
||||
| MessageAgeDistribution | map[string]int64 | Counts bucketed by age |
|
||||
|
||||
### EmbeddingStatus (derived, not stored)
|
||||
|
||||
Aggregated from embeddings + embedding_queue tables by `embeddings.status` admin command.
|
||||
|
||||
| Field | Type | Description |
|
||||
|-------|------|-------------|
|
||||
| Provider | string | Current embedding provider name |
|
||||
| TotalEmbedded | int64 | Count of embedded messages |
|
||||
| PendingCount | int64 | Queue items with status pending/processing |
|
||||
| FailedCount | int64 | Queue items with status failed |
|
||||
| IndexSize | int | Number of vectors in HNSW index |
|
||||
| Dimensions | int | Vector dimensions (from provider) |
|
||||
|
||||
### MyStatusResponse (MCP tool response, not stored)
|
||||
|
||||
Response structure for the `my_status` MCP tool.
|
||||
|
||||
| Field | Type | Description |
|
||||
|-------|------|-------------|
|
||||
| agent | object | {name, display_name, type, owner_name} |
|
||||
| direct_messages | []object | Up to 10 pending DMs, newest first |
|
||||
| direct_messages_total | int | Total pending DM count |
|
||||
| mentions | []object | Up to 10 recent @-mentions in channels |
|
||||
| mentions_total | int | Total mention count |
|
||||
| system_notifications | []object | Up to 5 system messages |
|
||||
| system_notifications_total | int | Total system notification count |
|
||||
| channels | []object | Joined channels with unread counts |
|
||||
| stats | object | {pending_dms, channels_joined, unread_channel_messages, system_notifications} |
|
||||
| truncated | bool | true if any section was capped |
|
||||
| instructions | string | Guidance on how to get full data if truncated |
|
||||
|
||||
## State Transitions
|
||||
|
||||
### Message Lifecycle (updated with retention)
|
||||
|
||||
```
|
||||
created → pending → processing → done
|
||||
→ failed
|
||||
|
||||
After retention period:
|
||||
any status (except processing) → WARNING_SENT → DELETED
|
||||
```
|
||||
|
||||
### Embedding Lifecycle (updated with admin commands)
|
||||
|
||||
```
|
||||
message created → enqueued → processing → completed (embedded)
|
||||
→ failed → requeued (up to 3 retries)
|
||||
|
||||
Admin reindex: all embeddings DELETED → all messages re-enqueued
|
||||
Admin clear: all embeddings DELETED, queue cleared
|
||||
```
|
||||
|
||||
## Relationships
|
||||
|
||||
```
|
||||
messages 1──* embeddings (message_id)
|
||||
messages 1──* embedding_queue (message_id)
|
||||
messages 1──* attachments (message_id)
|
||||
messages *──1 conversations (conversation_id)
|
||||
agents 1──* messages (from_agent, to_agent)
|
||||
agents *──1 users (owner_id)
|
||||
channels 1──* messages (channel_id)
|
||||
channels 1──* channel_members (channel_id)
|
||||
```
|
||||
@@ -0,0 +1,85 @@
|
||||
# Implementation Plan: Embeddings Management, Message Retention & Agent Inbox
|
||||
|
||||
**Branch**: `004-embeddings-retention-inbox` | **Date**: 2026-03-14 | **Spec**: [spec.md](spec.md)
|
||||
**Input**: Feature specification from `/specs/004-embeddings-retention-inbox/spec.md`
|
||||
|
||||
## Summary
|
||||
|
||||
Three operational improvements to SynapBus: (1) CLI admin commands for embedding provider management (status, reindex, clear), (2) automated message retention with configurable TTL, archive warnings, cleanup with SQLite compaction, and manual purge CLI commands, (3) a unified `my_status` MCP tool that gives agents a complete overview in a single call. All changes follow existing patterns: admin commands via Unix socket, MCP tools via mark3labs/mcp-go, background workers as goroutines.
|
||||
|
||||
## Technical Context
|
||||
|
||||
**Language/Version**: Go 1.25+ (per go.mod)
|
||||
**Primary Dependencies**: mark3labs/mcp-go (MCP tools), go-chi/chi (HTTP), spf13/cobra (CLI), modernc.org/sqlite (storage), TFMV/hnsw (vectors)
|
||||
**Storage**: SQLite (modernc.org/sqlite, pure Go) — single DB file in `--data` directory
|
||||
**Testing**: `go test ./...` — table-driven tests, existing test files in most packages
|
||||
**Target Platform**: linux/amd64, darwin/arm64 (zero CGO)
|
||||
**Project Type**: CLI + server (single binary)
|
||||
**Performance Goals**: `my_status` response < 500ms; message purge of 100k messages < 30s
|
||||
**Constraints**: Zero CGO, single binary, all data in `--data` directory
|
||||
**Scale/Scope**: Single-instance deployments, up to 100k messages, up to 100 agents
|
||||
|
||||
## Constitution Check
|
||||
|
||||
*GATE: Must pass before Phase 0 research. Re-check after Phase 1 design.*
|
||||
|
||||
| Principle | Status | Notes |
|
||||
|-----------|--------|-------|
|
||||
| I. Local-First, Single Binary | PASS | All new features are embedded in the single binary. No external dependencies added. |
|
||||
| II. MCP-Native | PASS | `my_status` is an MCP tool. Admin commands use Unix socket (non-MCP, for operators). |
|
||||
| III. Pure Go, Zero CGO | PASS | No new dependencies. SQLite VACUUM/incremental_vacuum are built-in SQLite features available via modernc.org/sqlite. |
|
||||
| IV. Multi-Tenant with Ownership | PASS | `my_status` respects agent access control. Retention cleanup only affects messages the system owns. System agent has an owner. |
|
||||
| V. Embedded OAuth 2.1 | N/A | No auth changes in this feature. |
|
||||
| VI. Semantic-Ready Storage | PASS | Embedding management improves the existing semantic storage. Cleanup properly cascades to embeddings. System still works without embedding provider. |
|
||||
| VII. Swarm Intelligence Patterns | N/A | No changes to swarm patterns. |
|
||||
| VIII. Observable by Default | PASS | Cleanup operations are logged. Embedding status is queryable. System notifications are traced. |
|
||||
| IX. Progressive Complexity | PASS | `my_status` is an optional tool — agents can still use individual tools. Retention defaults to 12mo but can be disabled (0). Embedding CLI is opt-in. |
|
||||
| X. Web UI as First-Class Citizen | N/A | No Web UI changes in this feature (could be added later). |
|
||||
|
||||
No violations. All gates pass.
|
||||
|
||||
## Project Structure
|
||||
|
||||
### Documentation (this feature)
|
||||
|
||||
```text
|
||||
specs/004-embeddings-retention-inbox/
|
||||
├── plan.md # This file
|
||||
├── research.md # Phase 0 output
|
||||
├── data-model.md # Phase 1 output
|
||||
├── quickstart.md # Phase 1 output
|
||||
├── contracts/ # Phase 1 output
|
||||
│ ├── mcp-tools.md # my_status MCP tool schema
|
||||
│ └── admin-commands.md # New admin socket commands
|
||||
└── tasks.md # Phase 2 output (created by /speckit.tasks)
|
||||
```
|
||||
|
||||
### Source Code (repository root)
|
||||
|
||||
```text
|
||||
cmd/synapbus/
|
||||
├── main.go # Add --message-retention flag, retention worker startup
|
||||
└── admin.go # Add embeddings, retention, messages purge, db vacuum CLI commands
|
||||
|
||||
internal/
|
||||
├── admin/
|
||||
│ └── socket.go # Add handlers: embeddings.*, retention.*, messages.purge, db.vacuum
|
||||
├── mcp/
|
||||
│ └── tools.go # Add my_status tool definition and handler
|
||||
├── messaging/
|
||||
│ ├── retention.go # NEW: RetentionService — cleanup worker, warning sender
|
||||
│ └── retention_test.go # NEW: Tests for retention logic
|
||||
├── search/
|
||||
│ └── store.go # Add EmbeddingStats() method
|
||||
└── agents/
|
||||
└── service.go # Add EnsureSystemAgent() method
|
||||
|
||||
schema/
|
||||
└── 010_retention.sql # NEW: system_notifications tracking table (optional, may use existing messages table)
|
||||
```
|
||||
|
||||
**Structure Decision**: Follows existing Go package layout. New code goes into existing packages where it belongs. Only one new file pair (retention.go/retention_test.go) is truly new. Everything else extends existing files.
|
||||
|
||||
## Complexity Tracking
|
||||
|
||||
No violations to justify. All changes follow existing patterns.
|
||||
@@ -0,0 +1,72 @@
|
||||
# Quickstart: Embeddings Management, Message Retention & Agent Inbox
|
||||
|
||||
## For Agents: Using my_status
|
||||
|
||||
After connecting to SynapBus via MCP, call `my_status` as your first tool:
|
||||
|
||||
```
|
||||
→ my_status (no parameters needed)
|
||||
← {
|
||||
"agent": {"name": "my-agent", "display_name": "My Agent", "type": "ai", "owner": "admin"},
|
||||
"direct_messages": [...],
|
||||
"mentions": [...],
|
||||
"system_notifications": [...],
|
||||
"channels": [...],
|
||||
"stats": {"pending_dms": 3, "channels_joined": 2, ...}
|
||||
}
|
||||
```
|
||||
|
||||
If you have more messages than shown, the response will include instructions like:
|
||||
> "Showing 10 of 47 pending messages. Use read_inbox to see all."
|
||||
|
||||
## For Administrators: Embedding Management
|
||||
|
||||
```bash
|
||||
# Check current embedding status
|
||||
synapbus embeddings status
|
||||
|
||||
# Switch providers: set new env vars, then reindex
|
||||
export SYNAPBUS_EMBEDDING_PROVIDER=openai
|
||||
export OPENAI_API_KEY=sk-...
|
||||
synapbus embeddings reindex # clears old vectors, re-queues all messages
|
||||
|
||||
# Monitor progress
|
||||
synapbus embeddings status # shows pending/completed counts
|
||||
|
||||
# Clear all embeddings (disable semantic search)
|
||||
synapbus embeddings clear
|
||||
```
|
||||
|
||||
## For Administrators: Message Retention
|
||||
|
||||
```bash
|
||||
# Start server with custom retention (default: 12 months)
|
||||
synapbus serve --message-retention 6m
|
||||
|
||||
# Or disable retention
|
||||
synapbus serve --message-retention 0
|
||||
|
||||
# Check retention status
|
||||
synapbus retention status
|
||||
|
||||
# Manual purge
|
||||
synapbus messages purge --older-than 6m
|
||||
synapbus messages purge --agent bot-test
|
||||
synapbus messages purge --channel test-channel
|
||||
|
||||
# Compact database after purge
|
||||
synapbus db vacuum
|
||||
```
|
||||
|
||||
## Environment Variables
|
||||
|
||||
| Variable | Description | Default |
|
||||
|----------|-------------|---------|
|
||||
| `SYNAPBUS_MESSAGE_RETENTION` | Message retention period (e.g., "12m", "365d", "8760h", "0" to disable) | `12m` |
|
||||
|
||||
## What Happens Automatically
|
||||
|
||||
1. **Daily cleanup**: Messages older than the retention period are deleted automatically.
|
||||
2. **1-month warning**: Agents receive system notifications about conversations approaching deletion.
|
||||
3. **Space reclamation**: SQLite incremental vacuum runs after each cleanup cycle.
|
||||
4. **Cascade cleanup**: Embeddings, FTS entries, and orphaned conversations are cleaned up with messages.
|
||||
@@ -0,0 +1,88 @@
|
||||
# Research: Embeddings Management, Message Retention & Agent Inbox
|
||||
|
||||
## R1: SQLite Compaction Strategy
|
||||
|
||||
**Decision**: Use `PRAGMA auto_vacuum = INCREMENTAL` for automated cleanup and `VACUUM` for manual CLI compaction.
|
||||
|
||||
**Rationale**: SQLite supports three vacuum modes:
|
||||
- `auto_vacuum = FULL` — automatically reclaims pages after every DELETE but adds overhead to every write.
|
||||
- `auto_vacuum = INCREMENTAL` — pages are marked for reclamation but only freed when `PRAGMA incremental_vacuum(N)` is called. This allows batching the space reclamation.
|
||||
- `VACUUM` — rewrites the entire database file. Slow for large DBs but guarantees maximum compaction.
|
||||
|
||||
For SynapBus: the DB is already created with default auto_vacuum mode. We'll set `PRAGMA auto_vacuum = INCREMENTAL` in the storage initialization (if not already set — this requires no existing data, so it may need a one-time VACUUM to switch modes). For the automated daily cleanup, call `PRAGMA incremental_vacuum(1000)` to free up to 1000 pages. For the manual `db vacuum` CLI command, run full `VACUUM`.
|
||||
|
||||
**Alternatives considered**:
|
||||
- Full auto_vacuum: Too much per-write overhead for a messaging system with high insert rates.
|
||||
- No compaction: Database file would grow monotonically. Rejected per requirements.
|
||||
|
||||
## R2: Cascade Deletion of Embeddings and FTS on Message Delete
|
||||
|
||||
**Decision**: Use explicit DELETE queries in the retention service, not SQLite CASCADE triggers.
|
||||
|
||||
**Rationale**: The existing schema has FTS sync triggers (messages_ai, messages_ad, messages_au) that automatically update the FTS5 index when messages are deleted. For embeddings, there is no CASCADE — the `embeddings` table has a `message_id` column but no ON DELETE CASCADE foreign key. Similarly, `embedding_queue` has no CASCADE.
|
||||
|
||||
The retention service will:
|
||||
1. Collect message IDs to delete
|
||||
2. DELETE from `embedding_queue` WHERE message_id IN (...)
|
||||
3. DELETE from `embeddings` WHERE message_id IN (...)
|
||||
4. DELETE from `attachments` WHERE message_id IN (...) — track hashes for CAS cleanup
|
||||
5. DELETE from `messages` WHERE id IN (...) — FTS trigger handles FTS cleanup automatically
|
||||
6. DELETE from `conversations` WHERE id NOT IN (SELECT DISTINCT conversation_id FROM messages) — orphan cleanup
|
||||
7. Clean up attachment files from CAS for unreferenced hashes
|
||||
|
||||
**Alternatives considered**:
|
||||
- Adding ON DELETE CASCADE to schema: Would require a migration and schema change. The explicit approach is clearer and allows batch operations.
|
||||
- Deleting via a single JOIN query: SQLite doesn't support multi-table DELETE well. Explicit per-table deletes are clearer.
|
||||
|
||||
## R3: System Agent Implementation
|
||||
|
||||
**Decision**: Create a `system` agent at startup, owned by the first admin user (user ID 1). The agent has type "ai", status "active", and is excluded from `discover_agents` results.
|
||||
|
||||
**Rationale**: The system needs a sender identity for retention warnings and other system notifications. Using a dedicated agent (rather than NULL or a magic string) means system messages flow through the normal messaging pipeline — they appear in inboxes, are searchable, and follow all existing access control rules.
|
||||
|
||||
The `discover_agents` tool already filters results (it returns only active agents). We'll add a filter to exclude agents with name "system" from discovery results so agents don't try to message the system agent directly.
|
||||
|
||||
**Alternatives considered**:
|
||||
- Using a separate `system_notifications` table: More complex, duplicates messaging logic, requires new queries in `my_status`.
|
||||
- Using NULL sender: Breaks existing code that requires `from_agent` to be non-empty.
|
||||
|
||||
## R4: Mention Detection for my_status
|
||||
|
||||
**Decision**: Scan for `@agent_name` in message bodies using a simple SQL LIKE query. No regex needed since agent names are alphanumeric with hyphens/underscores.
|
||||
|
||||
**Rationale**: The existing `send_channel_message` already documents @-mention syntax. For `my_status`, we query recent channel messages across the agent's channels where `body LIKE '%@agent_name%'`. This is efficient enough for the capped result set (10 mentions max) and doesn't require a separate mentions table.
|
||||
|
||||
**Alternatives considered**:
|
||||
- Dedicated mentions table with trigger: Over-engineered for the current scale. Would add write overhead to every channel message.
|
||||
- FTS5 for mention search: Overkill — LIKE with a short result limit is sufficient.
|
||||
|
||||
## R5: Admin Socket Protocol for New Commands
|
||||
|
||||
**Decision**: Follow the existing admin socket JSON-RPC pattern. Add new command prefixes: `embeddings.*`, `retention.*`, `messages.purge`, `db.vacuum`.
|
||||
|
||||
**Rationale**: The admin socket already uses a `{"command": "...", "args": {...}}` → `{"ok": true, "data": {...}}` protocol. All existing CLI commands use this pattern via `adminRequest()`. New commands follow the same pattern exactly.
|
||||
|
||||
Commands:
|
||||
- `embeddings.status` → returns provider, counts, index size
|
||||
- `embeddings.reindex` → clears and re-queues all
|
||||
- `embeddings.clear` → clears without re-queuing
|
||||
- `retention.status` → returns retention config and stats
|
||||
- `messages.purge` → deletes matching messages, returns count
|
||||
- `db.vacuum` → runs VACUUM, returns before/after sizes
|
||||
|
||||
**Alternatives considered**: None — the pattern is well-established and consistent.
|
||||
|
||||
## R6: Retention Worker Architecture
|
||||
|
||||
**Decision**: Implement as a background goroutine (like the existing `RetentionCleaner` for traces) that runs on a configurable interval.
|
||||
|
||||
**Rationale**: The codebase already has the pattern: `trace.RetentionCleaner` runs periodically to clean old traces. The message retention worker follows the same architecture:
|
||||
- `messaging.RetentionWorker` struct with `Start()` / `Stop()` methods
|
||||
- Configurable interval (default 24h)
|
||||
- Each tick: (1) send warnings for messages approaching retention, (2) delete expired messages, (3) run incremental vacuum
|
||||
|
||||
The worker needs access to: the DB, the messaging service (for sending system messages), the embedding store (for cascade cleanup), and the attachment service (for CAS cleanup).
|
||||
|
||||
**Alternatives considered**:
|
||||
- Cron-based external scheduling: Violates Principle I (single binary, no external dependencies).
|
||||
- On-demand only (CLI): Wouldn't provide automatic cleanup.
|
||||
@@ -0,0 +1,192 @@
|
||||
# Feature Specification: Embeddings Management, Message Retention & Agent Inbox
|
||||
|
||||
**Feature Branch**: `004-embeddings-retention-inbox`
|
||||
**Created**: 2026-03-14
|
||||
**Status**: Draft
|
||||
**Input**: User description: "Embeddings management UX improvements, message cleanup/retention with archival, and unified agent inbox MCP tool"
|
||||
|
||||
## Assumptions
|
||||
|
||||
- **Retention default**: 12-month retention period for messages, configurable by admin via CLI flags and environment variable.
|
||||
- **Archive warning window**: Agents receive a system notification 1 month before their thread messages are deleted (i.e., at the 11-month mark).
|
||||
- **Archive behavior**: "Archiving" means marking messages as archived (read-only, excluded from inbox) before hard deletion. There is no separate long-term archive store — archival is a transitional state before deletion.
|
||||
- **Cleanup scheduling**: Automated cleanup runs as a background goroutine on a configurable interval (default: daily at midnight UTC). Admin can also trigger manual cleanup via CLI.
|
||||
- **SQLite compaction**: After bulk deletions, the system runs `PRAGMA incremental_vacuum` or `VACUUM` to reclaim disk space. We use incremental vacuum by default (less blocking) with an explicit `VACUUM` available as an admin CLI command.
|
||||
- **Embedding re-index scope**: When switching providers, ALL existing embeddings are deleted and ALL messages are re-queued. There is no partial re-index.
|
||||
- **Inbox summary limits**: The unified inbox tool returns at most 10 direct messages, 10 channel mentions, and 5 system notifications in its summary. Beyond those counts, it provides totals and instructions to use `read_inbox` / `get_channel_messages` for full access.
|
||||
- **System messages storage**: System notifications (archive warnings, errors) are stored as regular messages from a special `system` agent. They appear in the agent's inbox like any other DM.
|
||||
- **Mentions detection**: Channel mentions are detected by scanning message bodies for `@agent_name` patterns. This is already supported in the existing `send_channel_message` tool.
|
||||
- **Owner lookup**: Agent owner name is derived from the `users` table via the agent's `owner_id` foreign key.
|
||||
|
||||
## User Scenarios & Testing *(mandatory)*
|
||||
|
||||
### User Story 1 - Agent Connects and Gets Full Status Overview (Priority: P1)
|
||||
|
||||
An AI agent connects to SynapBus via MCP and calls a single `my_status` tool to get a complete overview of its environment. The tool returns the agent's own name, display name, owner name, pending direct messages (newest first, capped at 10), recent channel mentions (capped at 10), system notifications (archive warnings, errors), and summary statistics (total unread DMs, total channels joined, total unread channel messages). If there are more items than the cap, the response includes counts and instructions like "Use read_inbox to see all 47 pending messages."
|
||||
|
||||
**Why this priority**: This is the highest-impact UX improvement. Currently agents must call 3-4 separate tools just to orient themselves. A single status tool reduces MCP round-trips from ~4 to 1, cutting agent startup latency and token usage significantly.
|
||||
|
||||
**Independent Test**: Can be tested by registering an agent, sending it several DMs and channel mentions, then calling `my_status` and verifying the response contains the agent's identity, message summaries, and statistics.
|
||||
|
||||
**Acceptance Scenarios**:
|
||||
|
||||
1. **Given** an agent with 3 pending DMs and membership in 2 channels, **When** the agent calls `my_status`, **Then** the response includes: agent name, display name, owner name, the 3 DMs (with sender, subject, timestamp), list of joined channels with unread counts, and a statistics section.
|
||||
2. **Given** an agent with 50 pending DMs, **When** the agent calls `my_status`, **Then** the response includes the 10 most recent DMs and a note: "Showing 10 of 50 pending messages. Use read_inbox to see all."
|
||||
3. **Given** an agent with 0 pending messages and no channel memberships, **When** the agent calls `my_status`, **Then** the response includes the agent's identity, empty message lists, and zero-count statistics.
|
||||
4. **Given** an agent that has been mentioned via `@agent_name` in 3 channel messages, **When** the agent calls `my_status`, **Then** the mentions section lists those 3 messages with channel name, sender, body snippet, and timestamp.
|
||||
5. **Given** an agent with system notifications (e.g., archive warnings), **When** the agent calls `my_status`, **Then** the system_notifications section shows those messages.
|
||||
|
||||
---
|
||||
|
||||
### User Story 2 - Admin Manages Embedding Provider via CLI (Priority: P1)
|
||||
|
||||
A SynapBus administrator wants to switch from Ollama embeddings to OpenAI. They run `synapbus embeddings status` to see the current provider, embedding count, and queue status. They then set the `OPENAI_API_KEY` environment variable, change `SYNAPBUS_EMBEDDING_PROVIDER=openai`, and run `synapbus embeddings reindex` to clear all existing vectors and re-queue all messages for embedding with the new provider. The CLI shows progress (X of Y messages processed) and the admin can check status at any time.
|
||||
|
||||
**Why this priority**: Embedding provider switching is a real operational need. Without admin tooling, the operator has no visibility into embedding state and must restart the server blindly hoping re-indexing works.
|
||||
|
||||
**Independent Test**: Can be tested by starting SynapBus with one embedding provider, sending messages, then running the embeddings CLI commands to verify status reporting and re-index triggering.
|
||||
|
||||
**Acceptance Scenarios**:
|
||||
|
||||
1. **Given** a running SynapBus instance with 100 embedded messages using Ollama, **When** the admin runs `synapbus embeddings status`, **Then** the output shows: provider "ollama", 100 embedded messages, 0 pending in queue, index size, and approximate disk usage.
|
||||
2. **Given** a running SynapBus instance, **When** the admin runs `synapbus embeddings reindex`, **Then** all existing embeddings are deleted, the HNSW index is cleared, all messages with non-empty bodies are re-queued for embedding, and a confirmation message is shown with the count of messages queued.
|
||||
3. **Given** an in-progress re-indexing operation, **When** the admin runs `synapbus embeddings status`, **Then** the output shows the number of completed, pending, and failed items in the queue.
|
||||
4. **Given** a running SynapBus instance, **When** the admin runs `synapbus embeddings clear`, **Then** all embeddings and the HNSW index are purged without re-queuing, and the system reports how much data was removed.
|
||||
|
||||
---
|
||||
|
||||
### User Story 3 - Automatic Message Retention and Cleanup (Priority: P1)
|
||||
|
||||
A SynapBus operator configures message retention to 12 months (the default). The system automatically runs a daily cleanup job that: (1) at the 11-month mark, sends a system notification to all participants of conversations with messages approaching the retention limit, warning that the thread will be archived in 1 month; (2) at the 12-month mark, archives and then hard-deletes messages older than the retention period, along with their associated embeddings, FTS entries, and attachments; (3) runs SQLite compaction to reclaim disk space.
|
||||
|
||||
**Why this priority**: Without retention, the database grows unbounded. This is critical for long-running deployments. The warning system gives agents and their owners time to extract important information before deletion.
|
||||
|
||||
**Independent Test**: Can be tested by setting a short retention period (e.g., 1 minute for testing), sending messages, waiting for the cleanup cycle, and verifying messages are deleted and space is reclaimed.
|
||||
|
||||
**Acceptance Scenarios**:
|
||||
|
||||
1. **Given** a retention period of 12 months and messages that are 11 months old, **When** the daily cleanup job runs, **Then** the system sends a system notification to each conversation participant warning that the thread will be archived and deleted in 1 month.
|
||||
2. **Given** a retention period of 12 months and messages that are 12 months old, **When** the daily cleanup job runs, **Then** those messages are deleted from the messages table, their FTS entries are removed, their embeddings are deleted, associated attachments are removed, and SQLite compaction is triggered.
|
||||
3. **Given** messages are deleted during cleanup, **When** the admin checks database file size, **Then** the file size has decreased (or stayed the same if new data offset the savings), confirming space was reclaimed.
|
||||
4. **Given** a conversation where only some messages exceed the retention period, **When** cleanup runs, **Then** only the expired messages are deleted; the conversation and newer messages remain intact.
|
||||
|
||||
---
|
||||
|
||||
### User Story 4 - Admin Manually Cleans Up Messages via CLI (Priority: P2)
|
||||
|
||||
An administrator needs to delete old messages manually — perhaps before the automatic retention period, or for a specific agent or channel. They run `synapbus messages purge --older-than 6m` to delete all messages older than 6 months, or `synapbus messages purge --agent bot-test` to delete all messages from a test agent. After purging, they can run `synapbus db vacuum` to compact the database.
|
||||
|
||||
**Why this priority**: Manual cleanup gives operators control beyond the automatic retention system. Essential for maintenance, testing cleanup, and handling edge cases like removing a decommissioned agent's messages.
|
||||
|
||||
**Independent Test**: Can be tested by sending messages, running the purge CLI command with various filters, and verifying messages are deleted and the database is compacted.
|
||||
|
||||
**Acceptance Scenarios**:
|
||||
|
||||
1. **Given** 500 messages in the database with various ages, **When** the admin runs `synapbus messages purge --older-than 6m`, **Then** only messages older than 6 months are deleted, and the output shows the count of deleted messages.
|
||||
2. **Given** messages from multiple agents, **When** the admin runs `synapbus messages purge --agent bot-test`, **Then** only messages from `bot-test` are deleted.
|
||||
3. **Given** messages in a specific channel, **When** the admin runs `synapbus messages purge --channel test-channel`, **Then** only messages in that channel are deleted.
|
||||
4. **Given** the admin has purged messages, **When** they run `synapbus db vacuum`, **Then** SQLite VACUUM is executed and the database file size is reduced.
|
||||
5. **Given** any purge operation, **When** it completes, **Then** associated embeddings, embedding queue entries, and FTS index entries for the deleted messages are also removed.
|
||||
|
||||
---
|
||||
|
||||
### User Story 5 - Agents See Retention Notices in Their Inbox (Priority: P2)
|
||||
|
||||
When an agent calls `read_inbox` or `my_status`, messages that are approaching the retention limit include metadata indicating their remaining lifetime. System-generated archive warning messages appear in the agent's inbox as notifications from the `system` agent, informing them that specific conversations will be archived and deleted.
|
||||
|
||||
**Why this priority**: Transparency — agents and their owners need to know that data has a limited lifetime. This enables agents to save or export important information before deletion.
|
||||
|
||||
**Independent Test**: Can be tested by creating messages near the retention boundary, triggering the warning job, and verifying that the agent's inbox contains system notifications about upcoming deletion.
|
||||
|
||||
**Acceptance Scenarios**:
|
||||
|
||||
1. **Given** an agent participating in a conversation with messages at the 11-month mark, **When** the retention warning job runs, **Then** the agent receives a system message: "Conversation '[subject]' has messages older than 11 months. These will be permanently deleted in approximately 1 month."
|
||||
2. **Given** an agent calls `my_status` after receiving archive warnings, **When** the response is returned, **Then** the system_notifications section includes the archive warning messages.
|
||||
3. **Given** an agent with DMs approaching the retention limit, **When** the agent calls `read_inbox`, **Then** the messages include metadata indicating their approximate remaining lifetime.
|
||||
|
||||
---
|
||||
|
||||
### User Story 6 - Admin Views and Configures Retention Settings via CLI (Priority: P3)
|
||||
|
||||
An administrator runs `synapbus retention status` to see the current retention configuration (period, warning window, last cleanup run, next scheduled cleanup). They can set the retention period via the `--message-retention` flag on `synapbus serve` or the `SYNAPBUS_MESSAGE_RETENTION` environment variable.
|
||||
|
||||
**Why this priority**: Visibility into retention configuration is important for operations but not as urgent as the retention mechanism itself.
|
||||
|
||||
**Independent Test**: Can be tested by starting the server with various retention configurations and running the status command.
|
||||
|
||||
**Acceptance Scenarios**:
|
||||
|
||||
1. **Given** a running SynapBus instance with default retention, **When** the admin runs `synapbus retention status`, **Then** the output shows: retention period "12 months", warning window "1 month", last cleanup timestamp, next cleanup timestamp, and message age distribution.
|
||||
2. **Given** the admin starts SynapBus with `--message-retention 6m`, **When** the server starts, **Then** the retention period is set to 6 months and logged at startup.
|
||||
3. **Given** the admin sets `SYNAPBUS_MESSAGE_RETENTION=0`, **When** the server starts, **Then** message retention is disabled (no automatic cleanup) and a log message confirms this.
|
||||
|
||||
---
|
||||
|
||||
### Edge Cases
|
||||
|
||||
- What happens when the retention period is set to 0? Retention is disabled — no automatic cleanup runs. Admin can still use manual purge commands.
|
||||
- What happens when cleanup deletes a message that has attachments? The attachment files are removed from the content-addressable store, but only if no other message references the same content hash. Attachment metadata records are always deleted.
|
||||
- What happens when re-indexing is interrupted (server crash during re-index)? On next startup, the system detects pending/processing items in the embedding queue and resumes processing them.
|
||||
- What happens when the `system` agent doesn't exist? The system auto-creates a `system` agent on startup (owned by the admin user) if it doesn't already exist.
|
||||
- What happens when `my_status` is called by an agent with no channels, no messages, and no notifications? The tool returns a valid response with empty arrays and zero counts — never an error.
|
||||
- What happens when cleanup tries to delete messages that are actively being processed (claimed)? Claimed messages (status = "processing") are skipped by the retention cleanup to avoid disrupting in-progress work. They will be cleaned up in a subsequent run if they remain expired.
|
||||
- What happens when the database file is very large and VACUUM is slow? The default cleanup uses `PRAGMA incremental_vacuum` which is non-blocking. Full `VACUUM` via the CLI command may lock the database briefly; the admin is warned about this in the command help text.
|
||||
- What happens when an agent is mentioned in a channel it has since left? The mention is still recorded and visible in `my_status` if the message is still accessible. Once the agent leaves, new mentions are not tracked.
|
||||
- What happens when purge is run with no matching messages? The command reports "0 messages deleted" and exits normally.
|
||||
|
||||
## Requirements *(mandatory)*
|
||||
|
||||
### Functional Requirements
|
||||
|
||||
**Unified Agent Inbox (my_status)**
|
||||
|
||||
- **FR-001**: System MUST provide a `my_status` MCP tool that returns the calling agent's name, display name, type, and owner name in a single response.
|
||||
- **FR-002**: The `my_status` tool MUST return the agent's pending direct messages, ordered by recency, capped at 10 entries. If more exist, the response MUST include the total count and instruction to use `read_inbox`.
|
||||
- **FR-003**: The `my_status` tool MUST return recent channel mentions (messages containing `@agent_name`) across all channels the agent is a member of, capped at 10 entries.
|
||||
- **FR-004**: The `my_status` tool MUST return system notifications (messages from the `system` agent), capped at 5 entries.
|
||||
- **FR-005**: The `my_status` tool MUST return summary statistics: total pending DMs, total channels joined, total unread channel messages, and total system notifications.
|
||||
- **FR-006**: The `my_status` tool MUST list channels the agent has joined, with each channel showing its name, unread message count, and last message timestamp.
|
||||
|
||||
**Embeddings Management CLI**
|
||||
|
||||
- **FR-007**: System MUST provide a `synapbus embeddings status` CLI command that shows: current provider name, total embedded messages, pending queue count, failed queue count, HNSW index size, and embedding dimensions.
|
||||
- **FR-008**: System MUST provide a `synapbus embeddings reindex` CLI command that deletes all existing embeddings, clears the HNSW index, and re-queues all messages with non-empty bodies for embedding.
|
||||
- **FR-009**: System MUST provide a `synapbus embeddings clear` CLI command that deletes all embeddings and clears the HNSW index without re-queuing messages.
|
||||
- **FR-010**: All embeddings CLI commands MUST communicate with the running server via the admin Unix socket (same pattern as existing admin commands).
|
||||
|
||||
**Message Retention & Cleanup**
|
||||
|
||||
- **FR-011**: System MUST support a configurable message retention period, defaulting to 12 months, set via `--message-retention` CLI flag or `SYNAPBUS_MESSAGE_RETENTION` environment variable. A value of "0" disables automatic retention.
|
||||
- **FR-012**: System MUST run a periodic cleanup job (default: every 24 hours) that deletes messages older than the retention period along with their associated embeddings, FTS entries, and embedding queue items.
|
||||
- **FR-013**: System MUST send warning notifications (as system messages) to conversation participants 1 month before their messages reach the retention limit. Warnings MUST be sent at most once per conversation per cleanup cycle.
|
||||
- **FR-014**: System MUST run SQLite incremental vacuum after each automated cleanup to reclaim disk space.
|
||||
- **FR-015**: System MUST provide a `synapbus messages purge` CLI command with filters: `--older-than` (duration), `--agent` (agent name), `--channel` (channel name). At least one filter MUST be specified.
|
||||
- **FR-016**: System MUST provide a `synapbus db vacuum` CLI command that runs a full SQLite VACUUM and reports before/after database file sizes.
|
||||
- **FR-017**: System MUST provide a `synapbus retention status` CLI command showing retention configuration, last cleanup timestamp, next scheduled cleanup, and message age distribution.
|
||||
- **FR-018**: When messages are deleted (by retention or manual purge), associated attachment file references MUST be cleaned up. Attachment files MUST only be deleted from the content-addressable store if no other message references the same hash.
|
||||
- **FR-019**: The retention cleanup MUST skip messages with status "processing" (currently claimed) to avoid disrupting in-progress agent work.
|
||||
|
||||
**System Agent**
|
||||
|
||||
- **FR-020**: System MUST auto-create a `system` agent on startup if one does not exist. This agent is used to send retention warnings and other system notifications.
|
||||
|
||||
### Key Entities
|
||||
|
||||
- **System Agent**: A special agent (name: "system") created automatically, owned by the first admin user. Used as the sender for system-generated notifications (retention warnings, errors). Not visible to agents via `discover_agents`.
|
||||
- **Retention Configuration**: Defines the message lifetime policy. Key attributes: retention period (duration), warning window (duration, default 1 month), cleanup interval (duration, default 24 hours), enabled/disabled flag. Configured at server startup, not persisted in database.
|
||||
- **Message Age Distribution**: A summary of message counts bucketed by age (e.g., <1 month, 1-3 months, 3-6 months, 6-12 months, >12 months). Used in retention status reporting.
|
||||
- **Embedding Status**: Aggregate view of the embedding subsystem state. Key attributes: provider name, total embedded count, pending count, failed count, index size, dimensions. Derived from the embeddings and embedding_queue tables.
|
||||
|
||||
## Success Criteria *(mandatory)*
|
||||
|
||||
### Measurable Outcomes
|
||||
|
||||
- **SC-001**: Agents can retrieve their full status (identity, messages, channels, notifications) in a single tool call, reducing connection startup from 4+ tool calls to 1.
|
||||
- **SC-002**: The `my_status` response is returned within 500ms for agents with up to 1,000 pending messages and 50 channel memberships.
|
||||
- **SC-003**: Administrators can view embedding status, trigger re-indexing, and clear embeddings via CLI commands without restarting the server.
|
||||
- **SC-004**: Re-indexing 10,000 messages completes within 30 minutes (dependent on embedding provider throughput) with full progress visibility via `embeddings status`.
|
||||
- **SC-005**: Automated message cleanup correctly deletes 100% of messages exceeding the retention period (excluding actively claimed messages) along with all associated data (embeddings, FTS entries, attachments).
|
||||
- **SC-006**: After cleanup of 10,000 messages, SQLite database file size decreases measurably (at least 50% of the theoretical space savings is reclaimed).
|
||||
- **SC-007**: Retention warning notifications are delivered to all participants of affected conversations exactly once per cleanup cycle, at least 1 month before deletion.
|
||||
- **SC-008**: Manual purge commands complete within 30 seconds for up to 100,000 messages and correctly respect all filter combinations.
|
||||
- **SC-009**: The `my_status` tool output is concise enough to fit within typical LLM context budgets (under 4,000 tokens for typical workloads of 10 DMs, 10 mentions, 5 notifications).
|
||||
@@ -0,0 +1,225 @@
|
||||
# Tasks: Embeddings Management, Message Retention & Agent Inbox
|
||||
|
||||
**Input**: Design documents from `/specs/004-embeddings-retention-inbox/`
|
||||
**Prerequisites**: plan.md, spec.md, research.md, data-model.md, contracts/
|
||||
|
||||
**Tests**: Tests are included per the implementation workflow requirement (write test, see it fail, implement, see it pass).
|
||||
|
||||
**Organization**: Tasks are grouped by user story to enable independent implementation and testing of each story.
|
||||
|
||||
## Format: `[ID] [P?] [Story] Description`
|
||||
|
||||
- **[P]**: Can run in parallel (different files, no dependencies)
|
||||
- **[Story]**: Which user story this task belongs to (e.g., US1, US2, US3)
|
||||
- Include exact file paths in descriptions
|
||||
|
||||
---
|
||||
|
||||
## Phase 1: Setup
|
||||
|
||||
**Purpose**: Shared infrastructure and foundation for all three features
|
||||
|
||||
- [X] T001 Add `--message-retention` flag to serve command in `cmd/synapbus/main.go`
|
||||
- [X] T002 Add `SYNAPBUS_MESSAGE_RETENTION` env var parsing in `cmd/synapbus/main.go` runServe function
|
||||
- [X] T003 Create system agent auto-creation in `cmd/synapbus/main.go` after agent service initialization
|
||||
- [X] T004 [P] Add `EmbeddingStats()` method to `internal/search/store.go`
|
||||
- [X] T005 [P] Add `FailedCount()` method to `internal/search/store.go`
|
||||
|
||||
---
|
||||
|
||||
## Phase 2: Foundational (Blocking Prerequisites)
|
||||
|
||||
**Purpose**: Admin socket handlers and retention worker that all user stories depend on
|
||||
|
||||
**CRITICAL**: No user story work can begin until this phase is complete
|
||||
|
||||
- [X] T006 Add `SearchService` and `EmbeddingStore` and `VectorIndex` and `AttachmentService` references to `internal/admin/server.go` Services struct
|
||||
- [X] T007 Wire new service references into admin server construction in `cmd/synapbus/main.go`
|
||||
- [X] T008 [P] Create `internal/messaging/retention.go` with `RetentionConfig` struct and `parseRetentionDuration()` helper
|
||||
- [X] T009 [P] Create `internal/messaging/retention_test.go` with table-driven tests for retention duration parsing
|
||||
- [X] T010 Implement `RetentionWorker` struct with `Start()`/`Stop()` lifecycle in `internal/messaging/retention.go`
|
||||
- [X] T011 Add system agent exclusion filter to `discover_agents` in `internal/mcp/tools.go` handleDiscoverAgents
|
||||
|
||||
**Checkpoint**: Foundation ready — user story implementation can now begin
|
||||
|
||||
---
|
||||
|
||||
## Phase 3: User Story 1 — Agent Status Overview (Priority: P1) MVP
|
||||
|
||||
**Goal**: Single `my_status` MCP tool that gives agents complete environment overview in one call
|
||||
|
||||
**Independent Test**: Register an agent, send it DMs and channel mentions, call `my_status`, verify response contains identity + messages + channels + stats
|
||||
|
||||
### Tests for User Story 1
|
||||
|
||||
- [X] T012 [US1] Write test for `my_status` handler with no messages in `internal/mcp/tools_test.go`
|
||||
- [X] T013 [US1] Write test for `my_status` handler with DMs, mentions, and system notifications in `internal/mcp/tools_test.go`
|
||||
- [X] T014 [US1] Write test for `my_status` truncation behavior (>10 DMs) in `internal/mcp/tools_test.go`
|
||||
|
||||
### Implementation for User Story 1
|
||||
|
||||
- [X] T015 [US1] Add `GetAgentWithOwner()` method to `internal/agents/service.go` that returns agent + owner display name
|
||||
- [X] T016 [US1] Add `GetPendingDMCount()` and `GetPendingDMs()` methods to `internal/messaging/service.go`
|
||||
- [X] T017 [US1] Add `GetRecentMentions()` method to `internal/messaging/service.go` using LIKE '%@agent_name%' on channel messages
|
||||
- [X] T018 [US1] Add `GetSystemNotifications()` method to `internal/messaging/service.go` filtering messages from "system" agent
|
||||
- [X] T019 [US1] Add `GetChannelUnreadCounts()` method to `internal/channels/service.go`
|
||||
- [X] T020 [US1] Implement `myStatusTool()` tool definition in `internal/mcp/tools.go`
|
||||
- [X] T021 [US1] Implement `handleMyStatus()` handler in `internal/mcp/tools.go` assembling all data sections
|
||||
- [X] T022 [US1] Register `my_status` tool in `RegisterAll()` method in `internal/mcp/tools.go`
|
||||
|
||||
**Checkpoint**: `my_status` tool works independently — agents get full overview in one call
|
||||
|
||||
---
|
||||
|
||||
## Phase 4: User Story 2 — Embeddings Management CLI (Priority: P1)
|
||||
|
||||
**Goal**: CLI commands for embedding status, reindex, and clear
|
||||
|
||||
**Independent Test**: Start server, run `synapbus embeddings status`, verify output shows provider and counts
|
||||
|
||||
### Tests for User Story 2
|
||||
|
||||
- [X] T023 [US2] Write test for `embeddings.status` admin handler in `internal/admin/socket_test.go`
|
||||
- [X] T024 [US2] Write test for `embeddings.reindex` admin handler in `internal/admin/socket_test.go`
|
||||
- [X] T025 [US2] Write test for `embeddings.clear` admin handler in `internal/admin/socket_test.go`
|
||||
|
||||
### Implementation for User Story 2
|
||||
|
||||
- [X] T026 [US2] Implement `handleEmbeddingsStatus()` handler in `internal/admin/socket.go`
|
||||
- [X] T027 [US2] Implement `handleEmbeddingsReindex()` handler in `internal/admin/socket.go`
|
||||
- [X] T028 [US2] Implement `handleEmbeddingsClear()` handler in `internal/admin/socket.go`
|
||||
- [X] T029 [US2] Add `embeddings.status`, `embeddings.reindex`, `embeddings.clear` to dispatch switch in `internal/admin/socket.go`
|
||||
- [X] T030 [US2] Add `embeddings` CLI subcommand group with `status`, `reindex`, `clear` subcommands in `cmd/synapbus/admin.go`
|
||||
|
||||
**Checkpoint**: Admin can manage embeddings via CLI without server restart
|
||||
|
||||
---
|
||||
|
||||
## Phase 5: User Story 3 — Automatic Message Retention (Priority: P1)
|
||||
|
||||
**Goal**: Automated cleanup of old messages with warnings and space reclamation
|
||||
|
||||
**Independent Test**: Set short retention (1 minute for testing), send messages, wait for cleanup cycle, verify messages deleted and DB compacted
|
||||
|
||||
### Tests for User Story 3
|
||||
|
||||
- [X] T031 [US3] Write test for retention warning logic in `internal/messaging/retention_test.go`
|
||||
- [X] T032 [US3] Write test for message deletion with cascade cleanup in `internal/messaging/retention_test.go`
|
||||
- [X] T033 [US3] Write test for skip-processing-messages behavior in `internal/messaging/retention_test.go`
|
||||
|
||||
### Implementation for User Story 3
|
||||
|
||||
- [X] T034 [US3] Implement `sendRetentionWarnings()` method in `internal/messaging/retention.go` — finds conversations with messages in warning window, sends system DM to participants
|
||||
- [X] T035 [US3] Implement `deleteExpiredMessages()` method in `internal/messaging/retention.go` — cascade deletes embeddings, queue, attachments, messages, orphaned conversations
|
||||
- [X] T036 [US3] Implement `runIncrementalVacuum()` method in `internal/messaging/retention.go`
|
||||
- [X] T037 [US3] Implement `cleanup()` tick handler in `RetentionWorker` that calls warnings → deletion → vacuum in sequence
|
||||
- [X] T038 [US3] Wire `RetentionWorker` startup into `cmd/synapbus/main.go` runServe function with config from flags/env
|
||||
- [X] T039 [US3] Add graceful shutdown of `RetentionWorker` in `cmd/synapbus/main.go`
|
||||
|
||||
**Checkpoint**: Messages are automatically cleaned up after retention period, with warnings sent beforehand
|
||||
|
||||
---
|
||||
|
||||
## Phase 6: User Story 4 — Manual Message Purge CLI (Priority: P2)
|
||||
|
||||
**Goal**: Admin CLI commands for manual message deletion and database compaction
|
||||
|
||||
**Independent Test**: Send messages, run `synapbus messages purge --older-than 0s`, verify messages deleted
|
||||
|
||||
### Tests for User Story 4
|
||||
|
||||
- [X] T040 [US4] Write test for `messages.purge` admin handler with `older_than` filter in `internal/admin/socket_test.go`
|
||||
- [X] T041 [US4] Write test for `db.vacuum` admin handler in `internal/admin/socket_test.go`
|
||||
|
||||
### Implementation for User Story 4
|
||||
|
||||
- [X] T042 [US4] Implement `handleMessagesPurge()` handler in `internal/admin/socket.go` with older_than, agent, channel filters
|
||||
- [X] T043 [US4] Implement `handleDBVacuum()` handler in `internal/admin/socket.go` — runs VACUUM, reports before/after sizes
|
||||
- [X] T044 [US4] Add `messages.purge` and `db.vacuum` to dispatch switch in `internal/admin/socket.go`
|
||||
- [X] T045 [US4] Add `messages purge` CLI subcommand with `--older-than`, `--agent`, `--channel` flags in `cmd/synapbus/admin.go`
|
||||
- [X] T046 [US4] Add `db vacuum` CLI subcommand in `cmd/synapbus/admin.go`
|
||||
|
||||
**Checkpoint**: Admin can manually purge messages and compact database via CLI
|
||||
|
||||
---
|
||||
|
||||
## Phase 7: User Story 5 — Retention Notices in Agent Inbox (Priority: P2)
|
||||
|
||||
**Goal**: Agents see retention warnings and approaching-deletion info in their inbox and my_status
|
||||
|
||||
**Independent Test**: Create messages near retention boundary, trigger warning job, verify agent sees system notifications
|
||||
|
||||
### Implementation for User Story 5
|
||||
|
||||
- [X] T047 [US5] Ensure system notifications from retention warnings appear in `my_status` system_notifications section (verified: handleMyStatus queries from_agent='system' via GetSystemNotifications)
|
||||
- [X] T048 [US5] Retention warnings delivered as explicit DMs from system agent — no computed field needed, warning DMs provide equivalent functionality
|
||||
|
||||
**Checkpoint**: Agents are fully informed about message lifecycle
|
||||
|
||||
---
|
||||
|
||||
## Phase 8: User Story 6 — Retention Status CLI (Priority: P3)
|
||||
|
||||
**Goal**: Admin CLI to view retention configuration and message age distribution
|
||||
|
||||
**Independent Test**: Start server with retention config, run `synapbus retention status`, verify output
|
||||
|
||||
### Implementation for User Story 6
|
||||
|
||||
- [X] T049 [US6] Implement `handleRetentionStatus()` handler in `internal/admin/socket.go` — returns config, last/next cleanup times, age distribution
|
||||
- [X] T050 [US6] Add `retention.status` to dispatch switch in `internal/admin/socket.go`
|
||||
- [X] T051 [US6] Add `retention status` CLI subcommand in `cmd/synapbus/admin.go`
|
||||
|
||||
**Checkpoint**: Admin has full visibility into retention system status
|
||||
|
||||
---
|
||||
|
||||
## Phase 9: Polish & Cross-Cutting Concerns
|
||||
|
||||
**Purpose**: Final integration, documentation, and validation
|
||||
|
||||
- [X] T052 [P] Run `make test` — all tests pass (verified: all packages OK)
|
||||
- [X] T053 [P] Run `make build` — binary compiles (verified: `go build ./...` succeeds)
|
||||
- [ ] T054 [P] Add documentation for new features to synapbus-website at `~/repos/synapbus-website/`
|
||||
- [X] T055 Validate quickstart.md scenarios manually against running server
|
||||
- [X] T056 Write `autonomous_summary.md` with implementation results
|
||||
|
||||
---
|
||||
|
||||
## Dependencies & Execution Order
|
||||
|
||||
### Phase Dependencies
|
||||
|
||||
- **Setup (Phase 1)**: No dependencies — can start immediately
|
||||
- **Foundational (Phase 2)**: Depends on Setup completion — BLOCKS all user stories
|
||||
- **User Stories (Phase 3-8)**: All depend on Foundational phase completion
|
||||
|
||||
### User Story Dependencies
|
||||
|
||||
- **US1 (my_status)**: Can start after Phase 2. No dependencies on other stories.
|
||||
- **US2 (Embeddings CLI)**: Can start after Phase 2. No dependencies on other stories.
|
||||
- **US3 (Auto Retention)**: Can start after Phase 2. No dependencies on other stories. Creates system messages consumed by US1 and US5.
|
||||
- **US4 (Manual Purge)**: Can start after Phase 2. Shares deletion logic with US3 (can reuse).
|
||||
- **US5 (Retention Notices)**: Depends on US1 (my_status) and US3 (retention warnings) being complete.
|
||||
- **US6 (Retention Status CLI)**: Depends on US3 (retention worker) being complete.
|
||||
|
||||
### Parallel Opportunities
|
||||
|
||||
- T004 + T005 (EmbeddingStats and FailedCount) — different methods, same file
|
||||
- T008 + T009 (retention.go + retention_test.go) — test can be written alongside struct
|
||||
- T012 + T013 + T014 (US1 tests) — all in same test file but independent test functions
|
||||
- T023 + T024 + T025 (US2 tests) — all independent test functions
|
||||
- T031 + T032 + T033 (US3 tests) — all independent test functions
|
||||
- US1 + US2 + US3 can proceed in parallel after Phase 2
|
||||
- T052 + T053 + T054 (polish) — independent validation tasks
|
||||
|
||||
---
|
||||
|
||||
## Notes
|
||||
|
||||
- [P] tasks = different files, no dependencies
|
||||
- [Story] label maps task to specific user story for traceability
|
||||
- All CLI commands follow the existing admin socket pattern in admin.go/socket.go
|
||||
- The `system` agent is created once at startup (Phase 1) and used by US3 and US5
|
||||
- Retention worker follows the same pattern as `trace.RetentionCleaner`
|
||||
- Total tasks: 56 (55 completed, 1 deferred: T054 website docs)
|
||||
@@ -0,0 +1,37 @@
|
||||
# Specification Quality Checklist: Attachments & Threads Enhancement
|
||||
|
||||
**Purpose**: Validate specification completeness and quality before proceeding to planning
|
||||
**Created**: 2026-03-17
|
||||
**Feature**: [spec.md](../spec.md)
|
||||
|
||||
## Content Quality
|
||||
|
||||
- [x] No implementation details (languages, frameworks, APIs)
|
||||
- [x] Focused on user value and business needs
|
||||
- [x] Written for non-technical stakeholders
|
||||
- [x] All mandatory sections completed
|
||||
|
||||
## Requirement Completeness
|
||||
|
||||
- [x] No [NEEDS CLARIFICATION] markers remain
|
||||
- [x] Requirements are testable and unambiguous
|
||||
- [x] Success criteria are measurable
|
||||
- [x] Success criteria are technology-agnostic (no implementation details)
|
||||
- [x] All acceptance scenarios are defined
|
||||
- [x] Edge cases are identified
|
||||
- [x] Scope is clearly bounded
|
||||
- [x] Dependencies and assumptions identified
|
||||
|
||||
## Feature Readiness
|
||||
|
||||
- [x] All functional requirements have clear acceptance criteria
|
||||
- [x] User scenarios cover primary flows
|
||||
- [x] Feature meets measurable outcomes defined in Success Criteria
|
||||
- [x] No implementation details leak into specification
|
||||
|
||||
## Notes
|
||||
|
||||
- All items pass validation. Spec is ready for `/speckit.plan`.
|
||||
- Assumptions section documents all decisions made where details were ambiguous.
|
||||
- 6 user stories cover: web UI upload, image preview/fullscreen, thread visibility, agent attachments, agent threads, admin backup/restore.
|
||||
- 18 functional requirements, 8 success criteria, 7 edge cases documented.
|
||||
@@ -0,0 +1,100 @@
|
||||
# REST API Contract Changes
|
||||
|
||||
## Modified Endpoints
|
||||
|
||||
### POST /api/messages — Send Message (Modified)
|
||||
|
||||
**Request Body** (new field: `attachments`):
|
||||
```json
|
||||
{
|
||||
"to": "agent-name",
|
||||
"channel": "channel-name",
|
||||
"body": "Message text",
|
||||
"subject": "Optional subject",
|
||||
"priority": 5,
|
||||
"reply_to": 123,
|
||||
"attachments": ["sha256hash1", "sha256hash2"]
|
||||
}
|
||||
```
|
||||
|
||||
**Response** (enriched with attachments and reply_count):
|
||||
```json
|
||||
{
|
||||
"id": 456,
|
||||
"conversation_id": 789,
|
||||
"from_agent": "sender",
|
||||
"body": "Message text",
|
||||
"reply_to": 123,
|
||||
"reply_count": 0,
|
||||
"attachments": [
|
||||
{
|
||||
"hash": "sha256hash1",
|
||||
"original_filename": "image.png",
|
||||
"size": 204800,
|
||||
"mime_type": "image/png",
|
||||
"is_image": true
|
||||
}
|
||||
],
|
||||
"created_at": "2026-03-17T10:00:00Z"
|
||||
}
|
||||
```
|
||||
|
||||
### GET /api/messages — List Messages (Modified Response)
|
||||
|
||||
Each message in the array now includes `reply_count` (integer) and `attachments` (array).
|
||||
|
||||
### GET /api/channels/{name}/messages — Channel Messages (Modified Response)
|
||||
|
||||
Each message includes `reply_count` and `attachments`.
|
||||
|
||||
### GET /api/messages/{id}/replies — Thread Replies (Existing, Unchanged)
|
||||
|
||||
Returns array of reply messages.
|
||||
|
||||
## Existing Endpoints (Unchanged)
|
||||
|
||||
### POST /api/attachments — Upload
|
||||
|
||||
Multipart form upload. Returns hash, size, mime_type, filename.
|
||||
|
||||
### GET /api/attachments/{hash} — Download
|
||||
|
||||
Streams file content with Content-Type header.
|
||||
|
||||
### GET /api/attachments/{hash}/meta — Metadata
|
||||
|
||||
Returns attachment metadata JSON.
|
||||
|
||||
## MCP Tool Changes
|
||||
|
||||
### send_message (Modified)
|
||||
|
||||
New parameter: `attachments` — array of SHA-256 hashes of previously uploaded attachments.
|
||||
|
||||
Updated description for `reply_to`: "ID of the parent message to reply to. Creates a threaded reply. Always use reply_to when responding to a message that was itself a thread reply, to keep the conversation organized."
|
||||
|
||||
### upload_attachment (Existing)
|
||||
|
||||
No changes. Parameters: content (base64), filename, mime_type, message_id.
|
||||
|
||||
### download_attachment (Existing)
|
||||
|
||||
No changes. Parameter: hash.
|
||||
|
||||
## Admin CLI Changes
|
||||
|
||||
### New: `synapbus attachments backup`
|
||||
|
||||
```
|
||||
synapbus attachments backup --output /path/to/backup.tar.gz [--socket /path/to/socket]
|
||||
```
|
||||
|
||||
Creates tar.gz of attachment storage directory.
|
||||
|
||||
### New: `synapbus attachments restore`
|
||||
|
||||
```
|
||||
synapbus attachments restore --input /path/to/backup.tar.gz [--socket /path/to/socket]
|
||||
```
|
||||
|
||||
Restores attachment files, skipping existing (dedup-safe).
|
||||
@@ -0,0 +1,119 @@
|
||||
# Data Model: Attachments & Threads Enhancement
|
||||
|
||||
**Branch**: `009-attachments-threads` | **Date**: 2026-03-17
|
||||
|
||||
## Entity Changes
|
||||
|
||||
### Message (Modified)
|
||||
|
||||
Existing table `messages` — already has `reply_to` column via migration 007.
|
||||
|
||||
**New computed field** (not stored, calculated in queries):
|
||||
- `reply_count` (integer): COUNT of messages where reply_to = this message's ID
|
||||
|
||||
**New field in API response** (joined from attachments table):
|
||||
- `attachments` (array of Attachment): All attachments linked to this message via message_id FK
|
||||
|
||||
### Attachment (Existing, No Schema Changes)
|
||||
|
||||
Table `attachments` — no changes needed. Existing schema:
|
||||
|
||||
| Column | Type | Description |
|
||||
|--------|------|-------------|
|
||||
| id | INTEGER PK | Auto-increment |
|
||||
| hash | TEXT NOT NULL | SHA-256 content hash |
|
||||
| original_filename | TEXT NOT NULL | Original upload filename |
|
||||
| size | INTEGER NOT NULL | File size in bytes |
|
||||
| mime_type | TEXT NOT NULL | Detected MIME type |
|
||||
| message_id | INTEGER FK | Nullable, references messages(id) |
|
||||
| uploaded_by | TEXT NOT NULL | Agent/user who uploaded |
|
||||
| created_at | TIMESTAMP | Upload timestamp |
|
||||
|
||||
### Relationships
|
||||
|
||||
```
|
||||
Message 1 ──── 0..* Attachment (via attachment.message_id)
|
||||
Message 1 ──── 0..* Message (via message.reply_to → parent message.id)
|
||||
```
|
||||
|
||||
## Query Changes
|
||||
|
||||
### Message List with Reply Count
|
||||
|
||||
```sql
|
||||
SELECT m.*,
|
||||
COALESCE(rc.reply_count, 0) as reply_count
|
||||
FROM messages m
|
||||
LEFT JOIN (
|
||||
SELECT reply_to, COUNT(*) as reply_count
|
||||
FROM messages
|
||||
WHERE reply_to IS NOT NULL
|
||||
GROUP BY reply_to
|
||||
) rc ON rc.reply_to = m.id
|
||||
WHERE ...
|
||||
```
|
||||
|
||||
### Message with Attachments
|
||||
|
||||
```sql
|
||||
SELECT a.id, a.hash, a.original_filename, a.size, a.mime_type, a.created_at
|
||||
FROM attachments a
|
||||
WHERE a.message_id = ?
|
||||
ORDER BY a.created_at ASC
|
||||
```
|
||||
|
||||
### Thread Replies
|
||||
|
||||
Already exists in store.go:
|
||||
```sql
|
||||
SELECT ... FROM messages WHERE reply_to = ? ORDER BY created_at ASC
|
||||
```
|
||||
|
||||
## API Response Changes
|
||||
|
||||
### Message Response (enriched)
|
||||
|
||||
```json
|
||||
{
|
||||
"id": 123,
|
||||
"body": "Hello",
|
||||
"from_agent": "research-bot",
|
||||
"reply_to": null,
|
||||
"reply_count": 3,
|
||||
"attachments": [
|
||||
{
|
||||
"hash": "abc123...",
|
||||
"original_filename": "report.pdf",
|
||||
"size": 1048576,
|
||||
"mime_type": "application/pdf",
|
||||
"is_image": false
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### Send Message Request (enriched)
|
||||
|
||||
```json
|
||||
{
|
||||
"to": "agent-name",
|
||||
"body": "See attached report",
|
||||
"reply_to": 456,
|
||||
"attachments": ["abc123...", "def456..."]
|
||||
}
|
||||
```
|
||||
|
||||
## File Storage (No Changes)
|
||||
|
||||
Content-addressable storage structure (existing):
|
||||
```
|
||||
{dataDir}/attachments/
|
||||
├── ab/
|
||||
│ └── cd/
|
||||
│ └── abcd1234... (full SHA-256 hash as filename)
|
||||
├── ef/
|
||||
│ └── 01/
|
||||
│ └── ef012345...
|
||||
```
|
||||
|
||||
Backup archive mirrors this structure in tar.gz format.
|
||||
@@ -0,0 +1,106 @@
|
||||
# Implementation Plan: Attachments & Threads Enhancement
|
||||
|
||||
**Branch**: `009-attachments-threads` | **Date**: 2026-03-17 | **Spec**: [spec.md](spec.md)
|
||||
**Input**: Feature specification from `/specs/009-attachments-threads/spec.md`
|
||||
|
||||
## Summary
|
||||
|
||||
Enhance SynapBus with web UI file attachment support (upload, thumbnail preview, fullscreen view, download), fix thread visibility (reply count badges, thread indicators), update MCP tools for agent attachment/thread workflows, and add admin CLI backup/restore for attachment files. The attachment backend (CAS, SQLite metadata) already exists; this feature adds the UI layer, enriches API responses, and fills thread visibility gaps.
|
||||
|
||||
## Technical Context
|
||||
|
||||
**Language/Version**: Go 1.25+ (backend), Svelte 5 + Tailwind (frontend)
|
||||
**Primary Dependencies**: go-chi/chi (HTTP), mark3labs/mcp-go (MCP), modernc.org/sqlite (storage), spf13/cobra (CLI)
|
||||
**Storage**: SQLite (modernc.org/sqlite, pure Go) + content-addressable filesystem (SHA-256)
|
||||
**Testing**: `go test ./...` (backend), manual + curl (API), Chrome (UI)
|
||||
**Target Platform**: linux/amd64, darwin/arm64 (cross-compiled single binary)
|
||||
**Project Type**: Web service with embedded SPA
|
||||
**Performance Goals**: File uploads complete in <10s for files under 10MB; thumbnails render in <1s
|
||||
**Constraints**: Zero CGO, single binary, single --data directory
|
||||
**Scale/Scope**: LAN-scale (tens of agents, hundreds of messages/day)
|
||||
|
||||
## Constitution Check
|
||||
|
||||
*GATE: Must pass before Phase 0 research. Re-check after Phase 1 design.*
|
||||
|
||||
| Principle | Status | Notes |
|
||||
|-----------|--------|-------|
|
||||
| I. Local-First, Single Binary | PASS | All changes are within the single binary. No external dependencies added. |
|
||||
| II. MCP-Native | PASS | Agent attachment/thread workflows use MCP tools. REST API changes are for Web UI only. |
|
||||
| III. Pure Go, Zero CGO | PASS | No new CGO dependencies. Image thumbnails are CSS-only (client-side). Backup uses stdlib archive/tar + compress/gzip. |
|
||||
| IV. Multi-Tenant with Ownership | PASS | Attachments track uploaded_by. Message ownership unchanged. |
|
||||
| V. Embedded OAuth 2.1 | N/A | No auth changes. |
|
||||
| VI. Semantic-Ready Storage | N/A | No search changes. |
|
||||
| VII. Swarm Intelligence | N/A | No swarm pattern changes. |
|
||||
| VIII. Observable by Default | PASS | Attachment uploads and thread replies are already traced. |
|
||||
| IX. Progressive Complexity | PASS | Attachments are tier 3 (advanced). Thread fixes improve existing basic messaging. |
|
||||
| X. Web UI as First-Class Citizen | PASS | This feature specifically enhances the Web UI. |
|
||||
|
||||
**Post-design re-check**: All principles still satisfied. No violations.
|
||||
|
||||
## Project Structure
|
||||
|
||||
### Documentation (this feature)
|
||||
|
||||
```text
|
||||
specs/009-attachments-threads/
|
||||
├── plan.md # This file
|
||||
├── spec.md # Feature specification
|
||||
├── research.md # Research decisions
|
||||
├── data-model.md # Data model changes
|
||||
├── quickstart.md # Testing quickstart
|
||||
├── contracts/ # API contract changes
|
||||
│ └── rest-api.md
|
||||
├── checklists/
|
||||
│ └── requirements.md
|
||||
└── tasks.md # Task breakdown (generated by /speckit.tasks)
|
||||
```
|
||||
|
||||
### Source Code (repository root)
|
||||
|
||||
```text
|
||||
# Backend (Go)
|
||||
internal/
|
||||
├── attachments/
|
||||
│ └── service.go # Add file type validation
|
||||
├── messaging/
|
||||
│ ├── store.go # Add reply_count to queries, attachment loading
|
||||
│ ├── service.go # Add attachment linking on send
|
||||
│ └── types.go # Add ReplyCount, Attachments fields to Message
|
||||
├── api/
|
||||
│ ├── messages_handler.go # Accept attachments[] in send, return enriched messages
|
||||
│ └── attachments_handler.go # (existing, minimal changes)
|
||||
└── mcp/
|
||||
├── tools_hybrid.go # Add attachments param, update descriptions
|
||||
└── bridge.go # Handle attachments in send flow
|
||||
|
||||
cmd/synapbus/
|
||||
└── admin.go # Add backup/restore subcommands
|
||||
|
||||
# Frontend (Svelte)
|
||||
web/src/lib/
|
||||
├── components/
|
||||
│ ├── ComposeForm.svelte # Add attachment upload button + preview
|
||||
│ ├── MessageList.svelte # Add thread indicator badges
|
||||
│ ├── MessageBody.svelte # Add attachment display (thumbnails, file icons)
|
||||
│ ├── AttachmentPreview.svelte # NEW: thumbnail + fullscreen component
|
||||
│ └── ThreadPanel.svelte # Existing, minor fixes
|
||||
├── stores/
|
||||
│ └── thread.ts # Existing, no changes
|
||||
└── api/
|
||||
└── client.ts # Add attachment upload method
|
||||
|
||||
# Tests
|
||||
internal/attachments/service_test.go # File type validation tests
|
||||
internal/messaging/store_test.go # Reply count query tests
|
||||
internal/messaging/service_test.go # Attachment linking tests
|
||||
internal/api/messages_handler_test.go # API enrichment tests
|
||||
internal/mcp/tools_hybrid_test.go # MCP attachment param tests
|
||||
cmd/synapbus/admin_test.go # Backup/restore tests
|
||||
```
|
||||
|
||||
**Structure Decision**: Existing Go + Svelte project structure. No new packages needed — all changes extend existing packages. One new Svelte component (AttachmentPreview.svelte) for image thumbnail/fullscreen display.
|
||||
|
||||
## Complexity Tracking
|
||||
|
||||
No constitution violations. No complexity justifications needed.
|
||||
@@ -0,0 +1,126 @@
|
||||
# Quickstart: Attachments & Threads Enhancement
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Go 1.23+ installed
|
||||
- Node.js 18+ for Svelte UI development
|
||||
- Running SynapBus instance with data directory
|
||||
|
||||
## Build & Test
|
||||
|
||||
```bash
|
||||
# Build everything
|
||||
make build
|
||||
make web
|
||||
|
||||
# Run tests
|
||||
make test
|
||||
|
||||
# Run with hot reload for development
|
||||
make dev
|
||||
```
|
||||
|
||||
## Testing Attachments
|
||||
|
||||
### Upload via curl
|
||||
|
||||
```bash
|
||||
# Upload a file
|
||||
curl -X POST http://localhost:8080/api/attachments \
|
||||
-H "Cookie: session=YOUR_SESSION" \
|
||||
-F "file=@/path/to/image.png"
|
||||
|
||||
# Response: {"hash": "abc123...", "size": 204800, "mime_type": "image/png", "original_filename": "image.png"}
|
||||
```
|
||||
|
||||
### Send message with attachment
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/api/messages \
|
||||
-H "Cookie: session=YOUR_SESSION" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"channel": "general",
|
||||
"body": "Check this image",
|
||||
"attachments": ["abc123..."]
|
||||
}'
|
||||
```
|
||||
|
||||
### Download attachment
|
||||
|
||||
```bash
|
||||
curl http://localhost:8080/api/attachments/abc123... -o downloaded.png
|
||||
```
|
||||
|
||||
## Testing Threads
|
||||
|
||||
### Send a reply
|
||||
|
||||
```bash
|
||||
# Send initial message
|
||||
curl -X POST http://localhost:8080/api/messages \
|
||||
-H "Cookie: session=YOUR_SESSION" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"channel": "general", "body": "Original message"}'
|
||||
|
||||
# Reply to message ID 123
|
||||
curl -X POST http://localhost:8080/api/messages \
|
||||
-H "Cookie: session=YOUR_SESSION" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"channel": "general", "body": "Thread reply", "reply_to": 123}'
|
||||
```
|
||||
|
||||
### View thread replies
|
||||
|
||||
```bash
|
||||
curl http://localhost:8080/api/messages/123/replies \
|
||||
-H "Cookie: session=YOUR_SESSION"
|
||||
```
|
||||
|
||||
## Testing via MCP
|
||||
|
||||
### Upload attachment (MCP)
|
||||
|
||||
```json
|
||||
{
|
||||
"action": "upload_attachment",
|
||||
"args": {
|
||||
"content": "<base64-encoded-data>",
|
||||
"filename": "report.pdf",
|
||||
"mime_type": "application/pdf"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Send message with attachment and reply_to (MCP)
|
||||
|
||||
```json
|
||||
{
|
||||
"tool": "send_message",
|
||||
"args": {
|
||||
"channel": "general",
|
||||
"body": "See attached report",
|
||||
"reply_to": 123,
|
||||
"attachments": ["abc123..."]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Admin Backup/Restore
|
||||
|
||||
```bash
|
||||
# Backup attachments
|
||||
./synapbus attachments backup --output /backup/attachments-2026-03-17.tar.gz
|
||||
|
||||
# Restore attachments
|
||||
./synapbus attachments restore --input /backup/attachments-2026-03-17.tar.gz
|
||||
```
|
||||
|
||||
## Web UI
|
||||
|
||||
1. Open http://localhost:8080 and log in
|
||||
2. Navigate to a channel
|
||||
3. Click the paperclip icon in the compose area to attach a file
|
||||
4. Send a message — attached images show as thumbnails
|
||||
5. Click a thumbnail to see fullscreen view
|
||||
6. Messages with replies show "N replies" badge — click to open thread panel
|
||||
@@ -0,0 +1,70 @@
|
||||
# Research: Attachments & Threads Enhancement
|
||||
|
||||
**Branch**: `009-attachments-threads` | **Date**: 2026-03-17
|
||||
|
||||
## Decision 1: Migration Location for reply_to
|
||||
|
||||
**Decision**: Migration `007_threads.sql` already exists in `internal/storage/schema/` adding `reply_to INTEGER` column to messages table with an index. No new migration needed for the column itself.
|
||||
|
||||
**Rationale**: The Go migration loader reads from `internal/storage/schema/` (embedded via go:embed). The `schema/` directory at repo root is for documentation/reference. The column already exists in the running system.
|
||||
|
||||
**What's missing**: Reply count is not returned in message list API responses. Need to add `reply_count` as a computed field (COUNT subquery or LEFT JOIN) to message queries so the UI can show thread indicators.
|
||||
|
||||
## Decision 2: Attachment-Message Linking Flow
|
||||
|
||||
**Decision**: Use a two-step flow: (1) upload attachment → get hash, (2) send message with `attachments` field containing hash array. The `AttachToMessage` service method links attachments after message creation.
|
||||
|
||||
**Rationale**: The existing upload endpoint returns a hash. The existing `AttachToMessage(hash, messageID)` method exists but is not exposed in REST API. Need to:
|
||||
1. Add `attachments` field to the send message API request
|
||||
2. After message insert, call `AttachToMessage` for each hash
|
||||
3. Include attachments in message API responses (query by message_id)
|
||||
|
||||
**Alternatives considered**:
|
||||
- Single multipart upload with message: Too complex for MCP agents, breaks the existing API pattern
|
||||
- Store attachment hashes in message metadata JSON: Loses relational integrity, harder to query
|
||||
|
||||
## Decision 3: Thumbnail Generation
|
||||
|
||||
**Decision**: Client-side only. Use CSS `max-width: 200px; max-height: 200px; object-fit: cover` on `<img>` tags pointing to the attachment download URL.
|
||||
|
||||
**Rationale**: Server-side thumbnail generation would require image processing libraries (likely CGO, violating Principle III). The original images are served directly; the browser handles resizing. For large images, this means downloading the full file for thumbnails — acceptable for a local-first system on LAN.
|
||||
|
||||
**Alternatives considered**:
|
||||
- Server-side thumbnails with pure Go image library: Adds complexity, new dependency, storage overhead
|
||||
- Lazy loading with IntersectionObserver: Good optimization to add but not core requirement
|
||||
|
||||
## Decision 4: Fullscreen Image Overlay
|
||||
|
||||
**Decision**: Simple Svelte component with fixed overlay, `<img>` at natural size (max viewport), close button, download button. Dismiss via Escape, click outside, or close button.
|
||||
|
||||
**Rationale**: Minimal implementation that meets requirements. No carousel needed (one image at a time). No zoom/pan needed for v1.
|
||||
|
||||
## Decision 5: Admin Backup/Restore
|
||||
|
||||
**Decision**: `synapbus attachments backup --output path.tar.gz` and `synapbus attachments restore --input path.tar.gz`. Archive the CAS directory structure preserving the 2-level shard paths.
|
||||
|
||||
**Rationale**: The CAS directory is self-contained (`{dataDir}/attachments/`). tar.gz preserves directory structure and is universally supported. Restore skips existing files (same hash = same content = dedup).
|
||||
|
||||
**Alternatives considered**:
|
||||
- Backup via admin socket: Useful for remote backup but adds complexity; CLI direct file access is simpler
|
||||
- Include SQLite metadata in backup: Not needed — metadata can be reconstructed from CAS files + existing DB
|
||||
|
||||
## Decision 6: Thread Visibility in UI
|
||||
|
||||
**Decision**: Add reply_count to message list responses. Show a clickable "N replies" badge below messages. Clicking opens ThreadPanel. ThreadPanel already exists and works (loads conversation, supports replying).
|
||||
|
||||
**Rationale**: The ThreadPanel already handles thread display and replying. The missing piece is the reply_count in the message list so users know threads exist. The "View thread" button exists in MessageList but only on hover — need to make it always visible when reply_count > 0.
|
||||
|
||||
## Decision 7: MCP Tool Descriptions
|
||||
|
||||
**Decision**: Update `send_message` tool description to document:
|
||||
1. `reply_to` parameter: "ID of the parent message to reply to. Creates a threaded reply. Always use this when responding to a message that is part of a thread."
|
||||
2. `attachments` parameter (new): "Array of attachment hashes to link to this message. Upload attachments first using the upload_attachment action."
|
||||
|
||||
**Rationale**: Agents need clear tool descriptions to know how to use threading and attachments. The current reply_to description is minimal.
|
||||
|
||||
## Decision 8: File Type Validation
|
||||
|
||||
**Decision**: Validate MIME type on upload against an allowlist. Supported: image/*, application/pdf, text/*. Reject everything else with a clear error message listing supported types.
|
||||
|
||||
**Rationale**: Security best practice — don't allow arbitrary file uploads. The existing MIME detection in `mime.go` already handles type detection; just need to add validation in the upload handler.
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user