Fleet topology epidemiology: strain plague map, interrupt fixes, tests.
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
This commit is contained in:
74
server/internal/api/epidemiology_bridge.go
Normal file
74
server/internal/api/epidemiology_bridge.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"crypto-miner-server/internal/epidemiology"
|
||||
)
|
||||
|
||||
func (h *WSHub) epidemiologyTracker() *epidemiology.Tracker {
|
||||
if h == nil {
|
||||
return nil
|
||||
}
|
||||
return h.epidemiology
|
||||
}
|
||||
|
||||
// SetEpidemiologyReporter wires AI + Seer broadcast callbacks for mining interrupts.
|
||||
func (h *WSHub) SetEpidemiologyReporter(r epidemiology.Reporter) {
|
||||
if h == nil || h.epidemiology == nil {
|
||||
return
|
||||
}
|
||||
h.epidemiology.SetReporter(r)
|
||||
}
|
||||
|
||||
func (h *WSHub) observeEpidemiologyFromStats(agentID string, in epidemiology.StatsInput) {
|
||||
tr := h.epidemiologyTracker()
|
||||
if tr == nil {
|
||||
return
|
||||
}
|
||||
tr.ObserveStats(agentID, in)
|
||||
}
|
||||
|
||||
func (h *WSHub) attachEpidemiologyFix(resp map[string]interface{}, agentID string) {
|
||||
tr := h.epidemiologyTracker()
|
||||
if tr == nil {
|
||||
return
|
||||
}
|
||||
fix, ok := tr.ConsumeFix(agentID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
resp["epidemiology_fix"] = fix
|
||||
}
|
||||
|
||||
func (h *WSHub) broadcastSeerEvent(ev epidemiology.SeerEvent) {
|
||||
h.broadcastDashboard(Message{Type: "seer_event", Payload: mustMarshal(ev)})
|
||||
}
|
||||
|
||||
func (h *WSHub) recordMiningInterruptAI(agentID, reasoning string) {
|
||||
if h.aiHandler == nil {
|
||||
return
|
||||
}
|
||||
entry := AIActivityEntry{
|
||||
AgentID: agentID,
|
||||
LastAction: "mining_interrupt",
|
||||
LastReasoning: truncateStr(reasoning, 240),
|
||||
LastReportAt: time.Now(),
|
||||
}
|
||||
h.aiHandler.recordActivity(entry)
|
||||
}
|
||||
|
||||
// WireDefaultEpidemiologyReporter connects mining-interrupt telemetry to AI + Seer streams.
|
||||
func (h *WSHub) WireDefaultEpidemiologyReporter() {
|
||||
if h == nil {
|
||||
return
|
||||
}
|
||||
h.SetEpidemiologyReporter(epidemiology.Reporter{
|
||||
OnAI: func(agentID, action, reasoning string) {
|
||||
h.recordMiningInterruptAI(agentID, reasoning)
|
||||
},
|
||||
OnSeer: func(ev epidemiology.SeerEvent) {
|
||||
h.broadcastSeerEvent(ev)
|
||||
},
|
||||
})
|
||||
}
|
||||
61
server/internal/api/epidemiology_test.go
Normal file
61
server/internal/api/epidemiology_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"crypto-miner-server/internal/epidemiology"
|
||||
)
|
||||
|
||||
func TestEpidemiologyAuthFixOneShot(t *testing.T) {
|
||||
hub := NewWSHub(nil)
|
||||
hub.WireDefaultEpidemiologyReporter()
|
||||
|
||||
hub.observeEpidemiologyFromStats("agent-fix", epidemiology.StatsInput{
|
||||
ChainExhausted: true,
|
||||
SpreadStrain: "#aabbcc",
|
||||
FailedMethods: []epidemiology.MethodFailure{
|
||||
{Method: "exe_subprocess", Reason: "blocked"},
|
||||
},
|
||||
})
|
||||
|
||||
resp := map[string]interface{}{}
|
||||
hub.attachEpidemiologyFix(resp, "agent-fix")
|
||||
if _, ok := resp["epidemiology_fix"]; !ok {
|
||||
t.Fatal("expected epidemiology_fix on auth pass")
|
||||
}
|
||||
raw, _ := json.Marshal(resp["epidemiology_fix"])
|
||||
var fix epidemiology.AgentFix
|
||||
if err := json.Unmarshal(raw, &fix); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fix.AgentID != "agent-fix" || !fix.RestartChain {
|
||||
t.Fatalf("fix=%+v", fix)
|
||||
}
|
||||
|
||||
resp2 := map[string]interface{}{}
|
||||
hub.attachEpidemiologyFix(resp2, "agent-fix")
|
||||
if _, ok := resp2["epidemiology_fix"]; ok {
|
||||
t.Fatal("fix should be consumed after first auth pass")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEpidemiologySeerEventBroadcast(t *testing.T) {
|
||||
hub := NewWSHub(nil)
|
||||
var seerKind string
|
||||
hub.SetEpidemiologyReporter(epidemiology.Reporter{
|
||||
OnSeer: func(ev epidemiology.SeerEvent) {
|
||||
seerKind = ev.Kind
|
||||
hub.broadcastSeerEvent(ev)
|
||||
},
|
||||
})
|
||||
hub.observeEpidemiologyFromStats("seer-agent", epidemiology.StatsInput{
|
||||
ChainExhausted: true,
|
||||
FailedMethods: []epidemiology.MethodFailure{
|
||||
{Method: "container", Reason: "no runtime"},
|
||||
},
|
||||
})
|
||||
if seerKind != "mining_interrupt" {
|
||||
t.Fatalf("seer kind=%q", seerKind)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user