package routers // Opt-in, expiring DEV interest: GETs only, no modem commands or recovery jobs. import ( "context" "encoding/json" "errors" "time" ) type DiagnosticEvent struct { Sequence uint64 DeviceID, Event, At string Data map[string]any } func (c *Collector) DiagnosticInterest() { c.devUntil.Store(time.Now().Add(3 * time.Second).UnixNano()) } func (c *Collector) DiagnosticTail(after uint64) []DiagnosticEvent { c.mu.Lock() defer c.mu.Unlock() out := []DiagnosticEvent{} for _, row := range c.devRows { if row.Sequence > after { out = append(out, row) } } return out } func (c *Collector) devRecord(id, event string, data map[string]any) { raw, e := json.Marshal(data) if e != nil { return } if len(raw) > 6500 { data = map[string]any{"state": "record_omitted", "reason": "size_bound", "bytes": len(raw)} } c.mu.Lock() defer c.mu.Unlock() c.devSequence++ c.devRows = append(c.devRows, DiagnosticEvent{c.devSequence, id, event, time.Now().UTC().Format(time.RFC3339Nano), data}) if len(c.devRows) > 128 { c.devRows = c.devRows[len(c.devRows)-128:] } } func diagnosticFields(source map[string]any, keys ...string) map[string]any { out := map[string]any{} for _, k := range keys { if v, ok := source[k]; ok { out[k] = v } } return out } func (c *Collector) pollDiagnostic(ctx context.Context) { if time.Now().UnixNano() > c.devUntil.Load() { return } for id, r := range c.state.Records { if r.Access == nil || r.Access.ClientAPI == nil || r.Rejected { continue } if !r.Device.Attached { continue } a, e := c.apiClient(Candidate{Address: r.Device.Address, Interface: r.Device.Interface, LocalIP: r.Device.LocalIP}, *r.Access.ClientAPI) if e != nil || a.identity != id { continue } call, cancel := context.WithTimeout(ctx, 650*time.Millisecond) raw, _, e := a.readResource(call, "status", "", "", 0) cancel() if e != nil { c.devRecord(id, "router_sample", map[string]any{"state": "read_failed"}) continue } projected, e := projectEvidence("status", raw, r.Access.ClientAPI.Token) var safe struct { Data map[string]any `json:"data"` } if e != nil || json.Unmarshal(projected, &safe) != nil { c.devRecord(id, "router_sample", map[string]any{"state": "unsupported_response"}) continue } d := safe.Data if c.devCursors == nil { c.devCursors = map[string]string{} c.devBoots = map[string]string{} } if boot, ok := d["boot_id"].(string); ok && c.devBoots[id] != boot { c.devBoots[id] = boot delete(c.devCursors, id) } summary := diagnosticFields(d, "boot_id", "uptime", "revision", "clock_quality", "firmware_version", "desired_policy", "persisted_policy", "observed", "effective_wan", "recovery", "traffic", "vpn") summary["state"] = "read" summary["sample_interval_s"] = 1 summary["slot_map"] = r.Device.SlotMap c.devRecord(id, "router_sample", summary) for _, kind := range []string{"wan", "modems"} { rows, _ := d[kind].([]any) for i, row := range rows { if i >= 8 { break } v, _ := row.(map[string]any) if v == nil { continue } c.devRecord(id, "router_"+kind, map[string]any{"boot_id": d["boot_id"], "router_uptime": d["uptime"], "sample": v}) } } // Preserve vendor-origin event times; repeated tail reads are deduplicated. if time.Since(c.devEventsAt[id]) < 5*time.Second { continue } c.devEventsAt[id] = time.Now() call, cancel = context.WithTimeout(ctx, 650*time.Millisecond) raw, _, e = a.readResource(call, "events", "", c.devCursors[id], 50) cancel() if e != nil { var apiErr *clientAPIError if errors.As(e, &apiErr) && (apiErr.Status == 400 || apiErr.Status == 409 || apiErr.Status == 410 || apiErr.Status == 422) && c.devCursors[id] != "" { delete(c.devCursors, id) c.devRecord(id, "router_event_cursor_reset", map[string]any{"state": "cursor_rejected", "http_status": apiErr.Status}) } c.devRecord(id, "router_events_unavailable", map[string]any{"state": "read_failed"}) continue } projected, e = projectEvidence("events", raw, r.Access.ClientAPI.Token) if e != nil || json.Unmarshal(projected, &safe) != nil { continue } if cursor, ok := safe.Data["cursor"].(string); ok && len(cursor) <= 512 { c.devCursors[id] = cursor } rows, _ := safe.Data["events"].([]any) if len(rows) >= 50 && c.devCursors[id] != "" { // Catch up one bounded page per poll, without blocking other work. c.devEventsAt[id] = time.Now().Add(-4 * time.Second) } c.devRecord(id, "router_event_page", map[string]any{"events": len(rows), "cursor_present": c.devCursors[id] != "", "page_full": len(rows) >= 50, "router_boot_id": d["boot_id"]}) for _, row := range rows { v, _ := row.(map[string]any) if v == nil { continue } value := diagnosticFields(v, "sequence", "source", "boot", "boot_id", "clock_quality", "epoch", "at", "at_utc", "observed_utc", "uptime", "topic", "category", "code", "recorded_code", "action", "recorded_action", "state", "reason", "operation_id", "initiator_kind", "actor_inferred", "causal_link_proven", "slot") keyBytes, _ := json.Marshal(value) key := id + string(keyBytes) if c.devSeen[key] { continue } if len(c.devSeen) >= 256 { c.devSeen = map[string]bool{} } c.devSeen[key] = true c.devRecord(id, "router_event", value) } } }