|
| 1 | +package daemon |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "log/slog" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "sort" |
| 10 | + "sync" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/aoagents/agent-orchestrator/backend/internal/cdc" |
| 14 | + "github.com/aoagents/agent-orchestrator/backend/internal/domain" |
| 15 | + "github.com/aoagents/agent-orchestrator/backend/internal/ports" |
| 16 | +) |
| 17 | + |
| 18 | +// milestoneStore claims one-time onboarding milestones durably. The CDC |
| 19 | +// subscriber runs in a single poller-driven goroutine, but the marker is |
| 20 | +// persisted so a milestone already reached in a prior daemon run is never |
| 21 | +// re-emitted after a restart. Keyed by an opaque name (e.g. "first_pr_raised" |
| 22 | +// or "pr_merged:<url>"). It is the funnel's once-per-install gate, the CDC |
| 23 | +// analogue of the store-derived first-ness checks in the session service. |
| 24 | +type milestoneStore struct { |
| 25 | + mu sync.Mutex |
| 26 | + path string |
| 27 | + seen map[string]struct{} |
| 28 | +} |
| 29 | + |
| 30 | +func newMilestoneStore(dataDir string) *milestoneStore { |
| 31 | + s := &milestoneStore{ |
| 32 | + path: filepath.Join(dataDir, "telemetry_milestones.json"), |
| 33 | + seen: map[string]struct{}{}, |
| 34 | + } |
| 35 | + if data, err := os.ReadFile(s.path); err == nil { |
| 36 | + var names []string |
| 37 | + if json.Unmarshal(data, &names) == nil { |
| 38 | + for _, n := range names { |
| 39 | + s.seen[n] = struct{}{} |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + return s |
| 44 | +} |
| 45 | + |
| 46 | +// claimed reports whether name was already recorded, without claiming it. |
| 47 | +func (s *milestoneStore) claimed(name string) bool { |
| 48 | + s.mu.Lock() |
| 49 | + defer s.mu.Unlock() |
| 50 | + _, ok := s.seen[name] |
| 51 | + return ok |
| 52 | +} |
| 53 | + |
| 54 | +// claim records name and returns true only the first time it is seen. |
| 55 | +func (s *milestoneStore) claim(name string) bool { |
| 56 | + s.mu.Lock() |
| 57 | + defer s.mu.Unlock() |
| 58 | + if _, ok := s.seen[name]; ok { |
| 59 | + return false |
| 60 | + } |
| 61 | + s.seen[name] = struct{}{} |
| 62 | + names := make([]string, 0, len(s.seen)) |
| 63 | + for n := range s.seen { |
| 64 | + names = append(names, n) |
| 65 | + } |
| 66 | + sort.Strings(names) |
| 67 | + if data, err := json.Marshal(names); err == nil { |
| 68 | + _ = os.WriteFile(s.path, data, 0o600) |
| 69 | + } |
| 70 | + return true |
| 71 | +} |
| 72 | + |
| 73 | +// prCDCPayload is the shape the pr_created/pr_updated triggers write into |
| 74 | +// change_log (migration 0006). Only the fields the funnel needs are decoded. |
| 75 | +type prCDCPayload struct { |
| 76 | + URL string `json:"url"` |
| 77 | + Session string `json:"session"` |
| 78 | + State string `json:"state"` |
| 79 | + CI string `json:"ci"` |
| 80 | + Review string `json:"review"` |
| 81 | + Mergeability string `json:"mergeability"` |
| 82 | +} |
| 83 | + |
| 84 | +// startOnboardingCDC subscribes to the CDC broadcaster and turns PR row changes |
| 85 | +// into funnel telemetry: pr_created -> pr_raised (activation), pr_updated with |
| 86 | +// state=merged -> pr_merged (success), each paired with a once-per-install |
| 87 | +// onboarding milestone. The broadcaster only pushes live events, so this is a |
| 88 | +// best-effort live signal; the milestone marker keeps first-* events exactly |
| 89 | +// once across restarts. The subscriber callback must not block. |
| 90 | +func startOnboardingCDC(bcast *cdc.Broadcaster, sink ports.EventSink, milestones *milestoneStore, log *slog.Logger) func() { |
| 91 | + if bcast == nil || sink == nil || milestones == nil { |
| 92 | + return func() {} |
| 93 | + } |
| 94 | + return bcast.Subscribe(func(ev cdc.Event) { |
| 95 | + switch ev.Type { |
| 96 | + case cdc.EventPRCreated: |
| 97 | + emitPRRaised(sink, milestones, ev, log) |
| 98 | + case cdc.EventPRUpdated: |
| 99 | + emitPRMerged(sink, milestones, ev, log) |
| 100 | + emitPRReviewed(sink, milestones, ev, log) |
| 101 | + case cdc.EventPRReviewThreadResolved: |
| 102 | + emitPRRevised(sink, milestones, ev) |
| 103 | + } |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +func decodePRPayload(ev cdc.Event, log *slog.Logger) (prCDCPayload, bool) { |
| 108 | + var p prCDCPayload |
| 109 | + if err := json.Unmarshal(ev.Payload, &p); err != nil { |
| 110 | + if log != nil { |
| 111 | + log.Warn("onboarding cdc: decode pr payload", "type", ev.Type, "seq", ev.Seq, "err", err) |
| 112 | + } |
| 113 | + return prCDCPayload{}, false |
| 114 | + } |
| 115 | + return p, true |
| 116 | +} |
| 117 | + |
| 118 | +func emitPRRaised(sink ports.EventSink, milestones *milestoneStore, ev cdc.Event, log *slog.Logger) { |
| 119 | + p, ok := decodePRPayload(ev, log) |
| 120 | + if !ok { |
| 121 | + return |
| 122 | + } |
| 123 | + payload := map[string]any{"state": p.State, "ci": p.CI, "review": p.Review, "mergeability": p.Mergeability} |
| 124 | + emitCDCTelemetry(sink, "ao.session.pr_raised", ev, payload) |
| 125 | + if milestones.claim("first_pr_raised") { |
| 126 | + emitCDCTelemetry(sink, "ao.onboarding.first_pr_raised", ev, map[string]any{"state": p.State}) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func emitPRMerged(sink ports.EventSink, milestones *milestoneStore, ev cdc.Event, log *slog.Logger) { |
| 131 | + p, ok := decodePRPayload(ev, log) |
| 132 | + if !ok || p.State != string(domain.PRStateMerged) { |
| 133 | + return |
| 134 | + } |
| 135 | + // pr_updated fires on any tracked-field change, and a merged PR can still |
| 136 | + // emit later updates (CI/review). Dedup the merge fact per PR URL so |
| 137 | + // pr_merged is one event per PR. |
| 138 | + if p.URL != "" && !milestones.claim("pr_merged:"+p.URL) { |
| 139 | + return |
| 140 | + } |
| 141 | + emitCDCTelemetry(sink, "ao.session.pr_merged", ev, map[string]any{"state": p.State}) |
| 142 | + if milestones.claim("first_pr_merged") { |
| 143 | + emitCDCTelemetry(sink, "ao.onboarding.first_pr_merged", ev, map[string]any{}) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func emitPRReviewed(sink ports.EventSink, milestones *milestoneStore, ev cdc.Event, log *slog.Logger) { |
| 148 | + p, ok := decodePRPayload(ev, log) |
| 149 | + if !ok { |
| 150 | + return |
| 151 | + } |
| 152 | + if p.Review != string(domain.ReviewApproved) && p.Review != string(domain.ReviewChangesRequest) { |
| 153 | + return |
| 154 | + } |
| 155 | + // pr_updated fires on any tracked-field change; dedup per (PR, decision) so |
| 156 | + // each distinct human verdict is one pr_reviewed event. |
| 157 | + if p.URL != "" && !milestones.claim("pr_reviewed:"+p.URL+":"+p.Review) { |
| 158 | + return |
| 159 | + } |
| 160 | + emitCDCTelemetry(sink, "ao.session.pr_reviewed", ev, map[string]any{"decision": p.Review}) |
| 161 | + if milestones.claim("first_pr_reviewed") { |
| 162 | + emitCDCTelemetry(sink, "ao.onboarding.first_pr_reviewed", ev, map[string]any{"decision": p.Review}) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// prThreadPayload is the pr_review_thread_resolved trigger shape (migration |
| 167 | +// 0004): a resolved review thread is the cleanest "agent addressed feedback" |
| 168 | +// signal available without tracking review history. |
| 169 | +type prThreadPayload struct { |
| 170 | + PR string `json:"pr"` |
| 171 | + Thread string `json:"thread"` |
| 172 | +} |
| 173 | + |
| 174 | +func emitPRRevised(sink ports.EventSink, milestones *milestoneStore, ev cdc.Event) { |
| 175 | + var p prThreadPayload |
| 176 | + if json.Unmarshal(ev.Payload, &p) != nil { |
| 177 | + return |
| 178 | + } |
| 179 | + // One revision signal per resolved thread. |
| 180 | + if p.Thread != "" && !milestones.claim("pr_revised:"+p.PR+":"+p.Thread) { |
| 181 | + return |
| 182 | + } |
| 183 | + emitCDCTelemetry(sink, "ao.session.pr_revised", ev, map[string]any{}) |
| 184 | + if milestones.claim("first_pr_revised") { |
| 185 | + emitCDCTelemetry(sink, "ao.onboarding.first_pr_revised", ev, map[string]any{}) |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +func emitCDCTelemetry(sink ports.EventSink, name string, ev cdc.Event, payload map[string]any) { |
| 190 | + out := ports.TelemetryEvent{ |
| 191 | + Name: name, |
| 192 | + Source: "cdc", |
| 193 | + OccurredAt: time.Now().UTC(), |
| 194 | + Level: ports.TelemetryLevelInfo, |
| 195 | + Payload: payload, |
| 196 | + } |
| 197 | + if ev.ProjectID != "" { |
| 198 | + projectID := domain.ProjectID(ev.ProjectID) |
| 199 | + out.ProjectID = &projectID |
| 200 | + } |
| 201 | + if ev.SessionID != "" { |
| 202 | + sessionID := domain.SessionID(ev.SessionID) |
| 203 | + out.SessionID = &sessionID |
| 204 | + } |
| 205 | + sink.Emit(context.Background(), out) |
| 206 | +} |
0 commit comments