From 11761cdf855e3a8416e136cd5f1200489bda1c50 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Sun, 15 Nov 2026 12:39:06 -0500 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=8C=B1=20v6:=20converge=20kick=5Fobse?= =?UTF-8?q?rver.go=20with=20v5's=20fan-out=20observers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit v6's two kick-callback consumers, the mention responder and the Linear agent, now register with crux.AddLegacyKickObserver on the fan-out registry (#18303) instead of chaining through SetKickObserver. The adapter delivers exactly the events the single slot delivered (kick-delivered, kick-log-archived, kick-dropped) with the same detail, in order, on each observer's own goroutine. A runtime without the registry (the sidecar client) falls back to SetKickObserver, as before. SetKickObserver is single-slot again, so kick_observer.go and its test are byte-identical to v5. The v6-only archive-source helpers (#7483) move to kick_observer_source.go, and the v6-only deferred-mention tests to kick_deferred_test.go. A new test pins that both observers fire on the same kick with the same events, order and archive detail. Signed-off-by: Andy Anderson --- src/cmd/hive/mention_runtime.go | 4 +- src/crux/agent/kick_deferred_test.go | 190 ++++++++++++++++ src/crux/agent/kick_observer.go | 52 +---- src/crux/agent/kick_observer_source.go | 42 ++++ src/crux/agent/kick_observer_source_test.go | 99 +++++++++ src/crux/agent/kick_observer_test.go | 229 -------------------- src/crux/kick_observer_legacy.go | 38 ++++ src/crux/kick_observer_legacy_test.go | 42 ++++ src/pkg/dashboard/api_linear_agent.go | 4 +- 9 files changed, 421 insertions(+), 279 deletions(-) create mode 100644 src/crux/agent/kick_deferred_test.go create mode 100644 src/crux/agent/kick_observer_source.go create mode 100644 src/crux/agent/kick_observer_source_test.go create mode 100644 src/crux/kick_observer_legacy.go create mode 100644 src/crux/kick_observer_legacy_test.go diff --git a/src/cmd/hive/mention_runtime.go b/src/cmd/hive/mention_runtime.go index 76de0bf788..8a7bcfe72f 100644 --- a/src/cmd/hive/mention_runtime.go +++ b/src/cmd/hive/mention_runtime.go @@ -3,6 +3,7 @@ package main import ( "context" + "github.com/hivecommons/hive/crux" "github.com/hivecommons/hive/pkg/config" "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/mention" @@ -91,7 +92,8 @@ func (b *boot) syncMentionRuntime() { responder.SetPerThreadMaxFunc(func() int { return b.cfg.GitHub.Mentions.PerThreadMaxEffective() }) - b.agentMgr.SetKickObserver(responder.HandleAgentEvent) + // Fan-out, beside the Linear agent's observer (hivecommons/hive#7483). + crux.AddLegacyKickObserver(b.agentMgr, responder.HandleAgentEvent) b.logger.Info("GitHub mention trigger poller started", "interval", b.cfg.GitHub.Mentions.PollIntervalEffective()) } diff --git a/src/crux/agent/kick_deferred_test.go b/src/crux/agent/kick_deferred_test.go new file mode 100644 index 0000000000..8594d0e402 --- /dev/null +++ b/src/crux/agent/kick_deferred_test.go @@ -0,0 +1,190 @@ +package agent + +import ( + "fmt" + "testing" + "time" +) + +// Deferred startup kicks and the kick-dropped notification (v6-only; moved +// out of kick_observer_test.go so that file stays identical to v5's). + +func TestDeferredStartupMentionsQueueFIFOWithoutDrop(t *testing.T) { + m := &Manager{agents: map[string]*AgentProcess{}, logger: discardLogger()} + agent := &AgentProcess{Name: "scanner", startupLaunchQueued: true} + m.agents[agent.Name] = agent + got := make(chan [3]string, 4) + m.SetKickObserver(func(agentName, event, detail string) { + got <- [3]string{agentName, event, detail} + }) + + m.mu.Lock() + for _, k := range []deferredKick{ + {message: "first", source: "mention:first"}, + {message: "second", source: "mention:second"}, + {message: "first again", source: "mention:first"}, + } { + if !m.deferStartupKickLocked(agent, k.message, k.source) { + t.Fatalf("deferred startup kick %q was not accepted", k.source) + } + } + m.mu.Unlock() + + select { + case ev := <-got: + t.Fatalf("queued mentions must not be dropped, got %v", ev) + case <-time.After(50 * time.Millisecond): + } + want := []deferredKick{ + {message: "first again", source: "mention:first"}, + {message: "second", source: "mention:second"}, + } + if len(agent.deferredMentionKicks) != len(want) { + t.Fatalf("deferredMentionKicks = %+v, want %+v", agent.deferredMentionKicks, want) + } + for i := range want { + if agent.deferredMentionKicks[i] != want[i] { + t.Fatalf("deferredMentionKicks[%d] = %+v, want %+v", i, agent.deferredMentionKicks[i], want[i]) + } + } + if agent.pendingStartupKick != "" { + t.Fatalf("mention kick leaked into the coalesced startup slot: %q", agent.pendingStartupKick) + } +} + +func TestDeferredStartupCadenceKickDoesNotEvictMention(t *testing.T) { + m := &Manager{agents: map[string]*AgentProcess{}, logger: discardLogger()} + agent := &AgentProcess{Name: "reviewer", startupLaunchQueued: true} + m.agents[agent.Name] = agent + got := make(chan [3]string, 4) + m.SetKickObserver(func(agentName, event, detail string) { + got <- [3]string{agentName, event, detail} + }) + + m.mu.Lock() + if !m.deferStartupKickLocked(agent, "please review", "mention:IC_1") { + t.Fatal("mention kick was not deferred") + } + if !m.deferStartupKickLocked(agent, "cadence 1", GovernorCadenceKickSource) { + t.Fatal("cadence kick was not deferred") + } + if !m.deferStartupKickLocked(agent, "cadence 2", GovernorCadenceKickSource) { + t.Fatal("second cadence kick was not deferred") + } + m.mu.Unlock() + + select { + case ev := <-got: + t.Fatalf("cadence kick must not drop the deferred mention, got %v", ev) + case <-time.After(50 * time.Millisecond): + } + if len(agent.deferredMentionKicks) != 1 || agent.deferredMentionKicks[0].source != "mention:IC_1" { + t.Fatalf("deferredMentionKicks = %+v, want the mention kept", agent.deferredMentionKicks) + } + if agent.pendingStartupKick != "cadence 2" || agent.pendingStartupSource != GovernorCadenceKickSource { + t.Fatalf("cadence kicks did not coalesce: (%q,%q)", agent.pendingStartupKick, agent.pendingStartupSource) + } + + m.mu.Lock() + msg, trigger, fromQueue := m.nextDeferredMentionKickLocked(agent, "one-shot prompt", "send-kick") + m.mu.Unlock() + if fromQueue || msg != "one-shot prompt" || trigger != "send-kick" { + t.Fatalf("one-shot kick must be delivered as sent, got (%q,%q,%v)", msg, trigger, fromQueue) + } + if len(agent.deferredMentionKicks) != 1 { + t.Fatalf("one-shot kick consumed the queued mention: %+v", agent.deferredMentionKicks) + } + + m.mu.Lock() + msg, trigger, fromQueue = m.nextDeferredMentionKickLocked(agent, "cadence 3", GovernorCadenceKickSource) + m.mu.Unlock() + if !fromQueue || msg != "please review" || trigger != "mention:IC_1" { + t.Fatalf("next kick = (%q,%q,%v), want queued mention", msg, trigger, fromQueue) + } + removeDeferredMentionKickLocked(agent, trigger) + if len(agent.deferredMentionKicks) != 0 { + t.Fatalf("delivered mention not removed: %+v", agent.deferredMentionKicks) + } + + m.mu.Lock() + msg, trigger, fromQueue = m.nextDeferredMentionKickLocked(agent, "cadence 4", GovernorCadenceKickSource) + m.mu.Unlock() + if fromQueue || msg != "cadence 4" || trigger != GovernorCadenceKickSource { + t.Fatalf("empty queue must pass the kick through, got (%q,%q,%v)", msg, trigger, fromQueue) + } +} + +func TestDeferredStartupMentionQueueOverflowNotifiesDropped(t *testing.T) { + m := &Manager{agents: map[string]*AgentProcess{}, logger: discardLogger()} + agent := &AgentProcess{Name: "scanner", startupLaunchQueued: true} + m.agents[agent.Name] = agent + got := make(chan [3]string, 4) + m.SetKickObserver(func(agentName, event, detail string) { + got <- [3]string{agentName, event, detail} + }) + + m.mu.Lock() + for i := 0; i <= maxDeferredMentionKicks; i++ { + src := fmt.Sprintf("mention:%d", i) + if !m.deferStartupKickLocked(agent, "msg "+src, src) { + t.Fatalf("deferred startup kick %q was not accepted", src) + } + } + m.mu.Unlock() + + select { + case ev := <-got: + want := [3]string{"scanner", "kick-dropped", "mention:0"} + if ev != want { + t.Fatalf("event = %v, want %v", ev, want) + } + case <-time.After(2 * time.Second): + t.Fatal("overflow did not notify dropped mention source") + } + if len(agent.deferredMentionKicks) != maxDeferredMentionKicks { + t.Fatalf("queue length = %d, want %d", len(agent.deferredMentionKicks), maxDeferredMentionKicks) + } + if agent.deferredMentionKicks[0].source != "mention:1" { + t.Fatalf("oldest queued = %q, want mention:1", agent.deferredMentionKicks[0].source) + } +} + +func TestStopDropsDeferredMentionKicks(t *testing.T) { + m := &Manager{agents: map[string]*AgentProcess{}, logger: discardLogger()} + agent := &AgentProcess{Name: "scanner", State: StateStopped, startupLaunchQueued: true} + m.agents[agent.Name] = agent + got := make(chan [3]string, 4) + m.SetKickObserver(func(agentName, event, detail string) { + got <- [3]string{agentName, event, detail} + }) + + m.mu.Lock() + for _, src := range []string{"mention:a", "action:b"} { + if !m.deferStartupKickLocked(agent, "msg "+src, src) { + t.Fatalf("deferred startup kick %q was not accepted", src) + } + } + m.mu.Unlock() + + if err := m.Stop("scanner"); err != nil { + t.Fatalf("Stop: %v", err) + } + if len(agent.deferredMentionKicks) != 0 { + t.Fatalf("Stop left queued mentions: %+v", agent.deferredMentionKicks) + } + dropped := map[string]bool{} + for i := 0; i < 2; i++ { + select { + case ev := <-got: + if ev[0] != "scanner" || ev[1] != "kick-dropped" { + t.Fatalf("event = %v, want kick-dropped", ev) + } + dropped[ev[2]] = true + case <-time.After(2 * time.Second): + t.Fatalf("Stop did not report every dropped mention, got %v", dropped) + } + } + if !dropped["mention:a"] || !dropped["action:b"] { + t.Fatalf("dropped sources = %v, want mention:a and action:b", dropped) + } +} diff --git a/src/crux/agent/kick_observer.go b/src/crux/agent/kick_observer.go index b1ba10496a..cd0c3cdb3a 100644 --- a/src/crux/agent/kick_observer.go +++ b/src/crux/agent/kick_observer.go @@ -1,7 +1,6 @@ package agent import ( - "strings" "sync" "time" @@ -22,61 +21,18 @@ const kickObserverBuffer = 256 // Compile-time proof the in-process runtime offers fan-out observers. var _ crux.KickObserverRegistry = (*Manager)(nil) -// kickObserverSourcePrefix separates the archive reason from the kick source -// in a kick-log-archived detail, so the mention responder can tell which -// kicks it summoned (hivecommons/hive#7483). -const kickObserverSourcePrefix = " source=" - -func kickObserverArchiveDetail(reason, source string) string { - reason = strings.TrimSpace(reason) - source = strings.TrimSpace(source) - if source == "" { - return reason - } - if reason == "" { - return kickObserverSourcePrefix + source - } - return reason + kickObserverSourcePrefix + source -} - -// KickObserverDetailSource returns the kick source carried in an archive -// detail, or "" when it has none. -func KickObserverDetailSource(detail string) string { - if _, source, ok := strings.Cut(detail, kickObserverSourcePrefix); ok { - return strings.TrimSpace(source) - } - return "" -} - -// KickObserverDetailReason returns the archive reason without the source. -func KickObserverDetailReason(detail string) string { - reason, _, _ := strings.Cut(detail, kickObserverSourcePrefix) - return strings.TrimSpace(reason) -} - // SetKickObserver installs (or with nil, removes) the single-slot kick // lifecycle observer. Safe to leave unset: notifications are then no-ops. // -// This is the compatibility path: it receives only kick-delivered and -// kick-log-archived, each on its own goroutine, and is independent of the -// fan-out observers (AddKickObserver). On this line it has two consumers, -// the mention responder and the Linear agent, so a second call chains after -// the installed observer instead of replacing it (hivecommons/hive#7483); -// nil clears every chained observer. +// This is the compatibility path (the Linear agent is its consumer): it +// receives only kick-delivered and kick-log-archived, each on its own +// goroutine, exactly as before AddKickObserver existed. It is independent of +// the fan-out observers. func (m *Manager) SetKickObserver(fn func(agentName, event, detail string)) { if fn == nil { m.kickObserver.Store(nil) return } - if existing := m.kickObserver.Load(); existing != nil { - prev := *existing - chained := func(agentName, event, detail string) { - prev(agentName, event, detail) - fn(agentName, event, detail) - } - m.kickObserver.Store(&chained) - return - } m.kickObserver.Store(&fn) } diff --git a/src/crux/agent/kick_observer_source.go b/src/crux/agent/kick_observer_source.go new file mode 100644 index 0000000000..613204f374 --- /dev/null +++ b/src/crux/agent/kick_observer_source.go @@ -0,0 +1,42 @@ +package agent + +import "strings" + +// v6-only kick-source helpers (hivecommons/hive#7483). They live outside +// kick_observer.go so that file stays byte-identical to v5's and the v5 -> v6 +// top-up carries it without a conflict. + +// kickObserverSourcePrefix separates the archive reason from the kick source +// in a kick-log-archived detail, so the mention responder can tell which +// kicks it summoned (hivecommons/hive#7483). +const kickObserverSourcePrefix = " source=" + +// kickObserverArchiveDetail builds a kick-log-archived detail: the archive +// reason, then the kick source after kickObserverSourcePrefix when there is +// one. +func kickObserverArchiveDetail(reason, source string) string { + reason = strings.TrimSpace(reason) + source = strings.TrimSpace(source) + if source == "" { + return reason + } + if reason == "" { + return kickObserverSourcePrefix + source + } + return reason + kickObserverSourcePrefix + source +} + +// KickObserverDetailSource returns the kick source carried in an archive +// detail, or "" when it has none. +func KickObserverDetailSource(detail string) string { + if _, source, ok := strings.Cut(detail, kickObserverSourcePrefix); ok { + return strings.TrimSpace(source) + } + return "" +} + +// KickObserverDetailReason returns the archive reason without the source. +func KickObserverDetailReason(detail string) string { + reason, _, _ := strings.Cut(detail, kickObserverSourcePrefix) + return strings.TrimSpace(reason) +} diff --git a/src/crux/agent/kick_observer_source_test.go b/src/crux/agent/kick_observer_source_test.go new file mode 100644 index 0000000000..36e16b9cac --- /dev/null +++ b/src/crux/agent/kick_observer_source_test.go @@ -0,0 +1,99 @@ +package agent + +import ( + "testing" + "time" + + "github.com/hivecommons/hive/crux" +) + +func TestKickObserverArchiveDetailCarriesSource(t *testing.T) { + detail := kickObserverArchiveDetail("kick", "mention") + if detail != "kick source=mention" { + t.Fatalf("detail = %q", detail) + } + if got := KickObserverDetailReason(detail); got != "kick" { + t.Fatalf("reason = %q", got) + } + if got := KickObserverDetailSource(detail); got != "mention" { + t.Fatalf("source = %q", got) + } + if got := kickObserverArchiveDetail("kick", ""); got != "kick" { + t.Fatalf("empty source detail = %q", got) + } +} + +// legacyKickObsEvent is one (agent, event, detail) callback. +type legacyKickObsEvent [3]string + +func recvLegacyKickObsEvent(t *testing.T, name string, ch <-chan legacyKickObsEvent) legacyKickObsEvent { + t.Helper() + select { + case ev := <-ch: + return ev + case <-time.After(kickObsTestWait): + t.Fatalf("%s observer never received an event", name) + } + return legacyKickObsEvent{} +} + +// TestLegacyKickObservers_BothFireOnSameKick pins the v6 wiring after the +// convergence with v5's fan-out: the mention responder and the Linear agent +// both register with crux.AddLegacyKickObserver, and on the same kick each +// receives the same events, in emission order, with the archive detail still +// carrying the kick source. Events the single-slot observer never saw +// (queue, output signal) are filtered out, and the single slot no longer +// interferes with either. +func TestLegacyKickObservers_BothFireOnSameKick(t *testing.T) { + m := &Manager{} + mentionCh := make(chan legacyKickObsEvent, 8) + linearCh := make(chan legacyKickObsEvent, 8) + removeMention := crux.AddLegacyKickObserver(m, func(a, e, d string) { mentionCh <- legacyKickObsEvent{a, e, d} }) + defer removeMention() + removeLinear := crux.AddLegacyKickObserver(m, func(a, e, d string) { linearCh <- legacyKickObsEvent{a, e, d} }) + defer removeLinear() + + // The single slot is independent: installing and clearing it must not + // touch either fan-out observer (v6's old chaining had nil clear both). + m.SetKickObserver(func(string, string, string) {}) + m.SetKickObserver(nil) + + const source = "mention:IC_1" + m.emitKickEvent(crux.KickEvent{Agent: "scanner", Event: crux.KickEventQueued, KickID: "k1"}) + m.notifyKickObserver("scanner", KickObserverEventDelivered, source) + m.emitKickEvent(crux.KickEvent{Agent: "scanner", Event: crux.KickEventOutputSignal, Signal: crux.OutputSignalFinding}) + m.notifyKickObserver("scanner", KickObserverEventArchived, kickObserverArchiveDetail("kick", source)) + m.notifyKickObserver("scanner", "kick-dropped", "mention:IC_2") + + want := []legacyKickObsEvent{ + {"scanner", KickObserverEventDelivered, source}, + {"scanner", KickObserverEventArchived, "kick source=" + source}, + {"scanner", "kick-dropped", "mention:IC_2"}, + } + for name, ch := range map[string]chan legacyKickObsEvent{"mention": mentionCh, "linear": linearCh} { + for i, w := range want { + if got := recvLegacyKickObsEvent(t, name, ch); got != w { + t.Fatalf("%s event %d = %v, want %v", name, i, got, w) + } + } + if got := KickObserverDetailSource(want[1][2]); got != source { + t.Fatalf("archive detail source = %q, want %q", got, source) + } + select { + case extra := <-ch: + t.Fatalf("%s observer received a non-legacy event %v", name, extra) + case <-time.After(50 * time.Millisecond): + } + } + + removeLinear() + m.notifyKickObserver("scanner", KickObserverEventDelivered, "governor") + if got := recvLegacyKickObsEvent(t, "mention", mentionCh); got[2] != "governor" { + t.Fatalf("mention event after removing linear = %v", got) + } + select { + case ev := <-linearCh: + t.Fatalf("removed linear observer received %v", ev) + case <-time.After(50 * time.Millisecond): + } +} diff --git a/src/crux/agent/kick_observer_test.go b/src/crux/agent/kick_observer_test.go index a6fedf9a7d..8516f95f53 100644 --- a/src/crux/agent/kick_observer_test.go +++ b/src/crux/agent/kick_observer_test.go @@ -2,7 +2,6 @@ package agent import ( "errors" - "fmt" "testing" "time" @@ -50,234 +49,6 @@ func TestKickObserver_NotifyAndRemove(t *testing.T) { } } -func TestKickObserver_ChainsObservers(t *testing.T) { - m := &Manager{} - got := make(chan string, 2) - m.SetKickObserver(func(agentName, event, detail string) { - got <- "first:" + event - }) - m.SetKickObserver(func(agentName, event, detail string) { - got <- "second:" + event - }) - m.notifyKickObserver("scanner", KickObserverEventArchived, "archive") - want := map[string]bool{ - "first:" + KickObserverEventArchived: false, - "second:" + KickObserverEventArchived: false, - } - for i := 0; i < 2; i++ { - select { - case ev := <-got: - if _, ok := want[ev]; !ok { - t.Fatalf("unexpected event %q", ev) - } - want[ev] = true - case <-time.After(2 * time.Second): - t.Fatal("observer chain did not fire") - } - } - for ev, seen := range want { - if !seen { - t.Fatalf("observer %q did not fire", ev) - } - } -} - -func TestKickObserverArchiveDetailCarriesSource(t *testing.T) { - detail := kickObserverArchiveDetail("kick", "mention") - if detail != "kick source=mention" { - t.Fatalf("detail = %q", detail) - } - if got := KickObserverDetailReason(detail); got != "kick" { - t.Fatalf("reason = %q", got) - } - if got := KickObserverDetailSource(detail); got != "mention" { - t.Fatalf("source = %q", got) - } - if got := kickObserverArchiveDetail("kick", ""); got != "kick" { - t.Fatalf("empty source detail = %q", got) - } -} - -func TestDeferredStartupMentionsQueueFIFOWithoutDrop(t *testing.T) { - m := &Manager{agents: map[string]*AgentProcess{}, logger: discardLogger()} - agent := &AgentProcess{Name: "scanner", startupLaunchQueued: true} - m.agents[agent.Name] = agent - got := make(chan [3]string, 4) - m.SetKickObserver(func(agentName, event, detail string) { - got <- [3]string{agentName, event, detail} - }) - - m.mu.Lock() - for _, k := range []deferredKick{ - {message: "first", source: "mention:first"}, - {message: "second", source: "mention:second"}, - {message: "first again", source: "mention:first"}, - } { - if !m.deferStartupKickLocked(agent, k.message, k.source) { - t.Fatalf("deferred startup kick %q was not accepted", k.source) - } - } - m.mu.Unlock() - - select { - case ev := <-got: - t.Fatalf("queued mentions must not be dropped, got %v", ev) - case <-time.After(50 * time.Millisecond): - } - want := []deferredKick{ - {message: "first again", source: "mention:first"}, - {message: "second", source: "mention:second"}, - } - if len(agent.deferredMentionKicks) != len(want) { - t.Fatalf("deferredMentionKicks = %+v, want %+v", agent.deferredMentionKicks, want) - } - for i := range want { - if agent.deferredMentionKicks[i] != want[i] { - t.Fatalf("deferredMentionKicks[%d] = %+v, want %+v", i, agent.deferredMentionKicks[i], want[i]) - } - } - if agent.pendingStartupKick != "" { - t.Fatalf("mention kick leaked into the coalesced startup slot: %q", agent.pendingStartupKick) - } -} - -func TestDeferredStartupCadenceKickDoesNotEvictMention(t *testing.T) { - m := &Manager{agents: map[string]*AgentProcess{}, logger: discardLogger()} - agent := &AgentProcess{Name: "reviewer", startupLaunchQueued: true} - m.agents[agent.Name] = agent - got := make(chan [3]string, 4) - m.SetKickObserver(func(agentName, event, detail string) { - got <- [3]string{agentName, event, detail} - }) - - m.mu.Lock() - if !m.deferStartupKickLocked(agent, "please review", "mention:IC_1") { - t.Fatal("mention kick was not deferred") - } - if !m.deferStartupKickLocked(agent, "cadence 1", GovernorCadenceKickSource) { - t.Fatal("cadence kick was not deferred") - } - if !m.deferStartupKickLocked(agent, "cadence 2", GovernorCadenceKickSource) { - t.Fatal("second cadence kick was not deferred") - } - m.mu.Unlock() - - select { - case ev := <-got: - t.Fatalf("cadence kick must not drop the deferred mention, got %v", ev) - case <-time.After(50 * time.Millisecond): - } - if len(agent.deferredMentionKicks) != 1 || agent.deferredMentionKicks[0].source != "mention:IC_1" { - t.Fatalf("deferredMentionKicks = %+v, want the mention kept", agent.deferredMentionKicks) - } - if agent.pendingStartupKick != "cadence 2" || agent.pendingStartupSource != GovernorCadenceKickSource { - t.Fatalf("cadence kicks did not coalesce: (%q,%q)", agent.pendingStartupKick, agent.pendingStartupSource) - } - - m.mu.Lock() - msg, trigger, fromQueue := m.nextDeferredMentionKickLocked(agent, "one-shot prompt", "send-kick") - m.mu.Unlock() - if fromQueue || msg != "one-shot prompt" || trigger != "send-kick" { - t.Fatalf("one-shot kick must be delivered as sent, got (%q,%q,%v)", msg, trigger, fromQueue) - } - if len(agent.deferredMentionKicks) != 1 { - t.Fatalf("one-shot kick consumed the queued mention: %+v", agent.deferredMentionKicks) - } - - m.mu.Lock() - msg, trigger, fromQueue = m.nextDeferredMentionKickLocked(agent, "cadence 3", GovernorCadenceKickSource) - m.mu.Unlock() - if !fromQueue || msg != "please review" || trigger != "mention:IC_1" { - t.Fatalf("next kick = (%q,%q,%v), want queued mention", msg, trigger, fromQueue) - } - removeDeferredMentionKickLocked(agent, trigger) - if len(agent.deferredMentionKicks) != 0 { - t.Fatalf("delivered mention not removed: %+v", agent.deferredMentionKicks) - } - - m.mu.Lock() - msg, trigger, fromQueue = m.nextDeferredMentionKickLocked(agent, "cadence 4", GovernorCadenceKickSource) - m.mu.Unlock() - if fromQueue || msg != "cadence 4" || trigger != GovernorCadenceKickSource { - t.Fatalf("empty queue must pass the kick through, got (%q,%q,%v)", msg, trigger, fromQueue) - } -} - -func TestDeferredStartupMentionQueueOverflowNotifiesDropped(t *testing.T) { - m := &Manager{agents: map[string]*AgentProcess{}, logger: discardLogger()} - agent := &AgentProcess{Name: "scanner", startupLaunchQueued: true} - m.agents[agent.Name] = agent - got := make(chan [3]string, 4) - m.SetKickObserver(func(agentName, event, detail string) { - got <- [3]string{agentName, event, detail} - }) - - m.mu.Lock() - for i := 0; i <= maxDeferredMentionKicks; i++ { - src := fmt.Sprintf("mention:%d", i) - if !m.deferStartupKickLocked(agent, "msg "+src, src) { - t.Fatalf("deferred startup kick %q was not accepted", src) - } - } - m.mu.Unlock() - - select { - case ev := <-got: - want := [3]string{"scanner", "kick-dropped", "mention:0"} - if ev != want { - t.Fatalf("event = %v, want %v", ev, want) - } - case <-time.After(2 * time.Second): - t.Fatal("overflow did not notify dropped mention source") - } - if len(agent.deferredMentionKicks) != maxDeferredMentionKicks { - t.Fatalf("queue length = %d, want %d", len(agent.deferredMentionKicks), maxDeferredMentionKicks) - } - if agent.deferredMentionKicks[0].source != "mention:1" { - t.Fatalf("oldest queued = %q, want mention:1", agent.deferredMentionKicks[0].source) - } -} - -func TestStopDropsDeferredMentionKicks(t *testing.T) { - m := &Manager{agents: map[string]*AgentProcess{}, logger: discardLogger()} - agent := &AgentProcess{Name: "scanner", State: StateStopped, startupLaunchQueued: true} - m.agents[agent.Name] = agent - got := make(chan [3]string, 4) - m.SetKickObserver(func(agentName, event, detail string) { - got <- [3]string{agentName, event, detail} - }) - - m.mu.Lock() - for _, src := range []string{"mention:a", "action:b"} { - if !m.deferStartupKickLocked(agent, "msg "+src, src) { - t.Fatalf("deferred startup kick %q was not accepted", src) - } - } - m.mu.Unlock() - - if err := m.Stop("scanner"); err != nil { - t.Fatalf("Stop: %v", err) - } - if len(agent.deferredMentionKicks) != 0 { - t.Fatalf("Stop left queued mentions: %+v", agent.deferredMentionKicks) - } - dropped := map[string]bool{} - for i := 0; i < 2; i++ { - select { - case ev := <-got: - if ev[0] != "scanner" || ev[1] != "kick-dropped" { - t.Fatalf("event = %v, want kick-dropped", ev) - } - dropped[ev[2]] = true - case <-time.After(2 * time.Second): - t.Fatalf("Stop did not report every dropped mention, got %v", dropped) - } - } - if !dropped["mention:a"] || !dropped["action:b"] { - t.Fatalf("dropped sources = %v, want mention:a and action:b", dropped) - } -} - const kickObsTestWait = 2 * time.Second func recvKickEvent(t *testing.T, ch <-chan crux.KickEvent) crux.KickEvent { diff --git a/src/crux/kick_observer_legacy.go b/src/crux/kick_observer_legacy.go new file mode 100644 index 0000000000..b26c3a998c --- /dev/null +++ b/src/crux/kick_observer_legacy.go @@ -0,0 +1,38 @@ +package crux + +// v6-only (hivecommons/hive#7483): v6 has two consumers of the +// (agentName, event, detail) kick callback, the mention responder and the +// Linear agent. Both register through AddLegacyKickObserver on the fan-out +// registry, so SetKickObserver stays the single-slot compatibility path it is +// on v5. + +// kickEventDropped is the event a deferred kick that will never be delivered +// reports through the legacy callback (agent/kick_deferred.go). +const kickEventDropped = "kick-dropped" + +// AddLegacyKickObserver registers fn, a callback in the SetKickObserver shape, +// alongside any other observer and returns its remover. fn receives exactly +// the events the single-slot observer receives (kick-delivered, +// kick-log-archived and kick-dropped) with the same agent, event and detail, +// in emission order, on the observer's own goroutine. +// +// A runtime without the fan-out registry (the sidecar client) has only the +// single slot: fn is installed with SetKickObserver, replacing any observer +// already there, and the remover is a no-op so it never clears another +// consumer's observer. +func AddLegacyKickObserver(rt Runtime, fn func(agentName, event, detail string)) (remove func()) { + if rt == nil || fn == nil { + return func() {} + } + reg, ok := rt.(KickObserverRegistry) + if !ok { + rt.SetKickObserver(fn) + return func() {} + } + return reg.AddKickObserver(func(ev KickEvent) { + switch ev.Event { + case KickEventDelivered, KickEventArchived, kickEventDropped: + fn(ev.Agent, ev.Event, ev.Detail) + } + }) +} diff --git a/src/crux/kick_observer_legacy_test.go b/src/crux/kick_observer_legacy_test.go new file mode 100644 index 0000000000..8586701243 --- /dev/null +++ b/src/crux/kick_observer_legacy_test.go @@ -0,0 +1,42 @@ +package crux + +import "testing" + +// singleSlotRuntime is a runtime with no fan-out registry (the sidecar +// client's shape): only SetKickObserver. +type singleSlotRuntime struct { + Runtime + installed func(agentName, event, detail string) + calls int +} + +func (r *singleSlotRuntime) SetKickObserver(fn func(agentName, event, detail string)) { + r.installed = fn + r.calls++ +} + +func TestAddLegacyKickObserver_FallsBackToSingleSlot(t *testing.T) { + rt := &singleSlotRuntime{} + var got []string + remove := AddLegacyKickObserver(rt, func(_, event, _ string) { got = append(got, event) }) + if rt.calls != 1 || rt.installed == nil { + t.Fatalf("SetKickObserver calls = %d, installed = %v", rt.calls, rt.installed != nil) + } + rt.installed("scanner", KickEventDelivered, "governor") + if len(got) != 1 || got[0] != KickEventDelivered { + t.Fatalf("observer got %v", got) + } + remove() + if rt.calls != 1 { + t.Fatal("the remover must not clear the single slot another consumer may own") + } +} + +func TestAddLegacyKickObserver_NilSafe(t *testing.T) { + AddLegacyKickObserver(nil, func(string, string, string) {})() + rt := &singleSlotRuntime{} + AddLegacyKickObserver(rt, nil)() + if rt.calls != 0 { + t.Fatal("a nil observer must not touch the runtime") + } +} diff --git a/src/pkg/dashboard/api_linear_agent.go b/src/pkg/dashboard/api_linear_agent.go index c3d395a221..c40441cf26 100644 --- a/src/pkg/dashboard/api_linear_agent.go +++ b/src/pkg/dashboard/api_linear_agent.go @@ -59,7 +59,9 @@ func (s *Server) linearAgent() LinearAgentGateway { // unconditionally costs nothing. if svc != nil && s.deps.AgentMgr != nil { if obs := svc.AgentEventObserver(); obs != nil { - s.deps.AgentMgr.SetKickObserver(obs) + // Fan-out, beside the mention responder's observer + // (hivecommons/hive#7483). + crux.AddLegacyKickObserver(s.deps.AgentMgr, obs) } } s.linearAgentSvc = svc From 459952b4d1ca8b0d9be13c227cd0c6c04e68a487 Mon Sep 17 00:00:00 2001 From: clubanderson <407614+clubanderson@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:58:44 +0000 Subject: [PATCH 2/3] changelog: fragment for #18315 Generated by changelog-fragment-guard from the PR title (#5675). Edit the wording freely, or replace this file with the no-changelog label if the change is not user-visible. Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Signed-off-by: clubanderson <407614+clubanderson@users.noreply.github.com> --- changelog.d/changed-18315.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/changed-18315.md diff --git a/changelog.d/changed-18315.md b/changelog.d/changed-18315.md new file mode 100644 index 0000000000..0ae23b5dec --- /dev/null +++ b/changelog.d/changed-18315.md @@ -0,0 +1 @@ +- v6: converge kick_observer.go with v5's fan-out observers (#18315) From 13d5bb9695f0f9cf8bae28c56cc5d89292401177 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Sun, 15 Nov 2026 13:19:37 -0500 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=96=20docs:=20re-cite=20the=20Line?= =?UTF-8?q?ar=20agent=20routes=20after=20the=20observer=20wiring=20change?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- src/docs/api-reference.md | 6 +++--- src/docs/api-reference/dashboard-misc.md | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/docs/api-reference.md b/src/docs/api-reference.md index 22371c54b2..b30e50801d 100644 --- a/src/docs/api-reference.md +++ b/src/docs/api-reference.md @@ -627,9 +627,9 @@ always resolved server-side from the validated token. | `POST` | `/api/plan/{epicID}/approve` | Owner only | Plan Approve | `pkg/dashboard/api.go:494` | | `POST` | `/api/plan/{epicID}/reject` | Owner only | Plan Reject | `pkg/dashboard/api.go:496` | | `POST` | `/api/plan/{epicID}/child/{childID}` | Owner only | Plan Child Action | `pkg/dashboard/api.go:497` | -| `POST` | `/api/linear/agent/install` | Owner only | Linear Agent Install (OAuth start) | `pkg/dashboard/api_linear_agent.go:219` | -| `GET` | `/api/linear/agent/status` | Owner only | Linear Agent Status | `pkg/dashboard/api_linear_agent.go:220` | -| `POST` | `/api/linear/agent/disconnect` | Owner only | Linear Agent Disconnect | `pkg/dashboard/api_linear_agent.go:221` | +| `POST` | `/api/linear/agent/install` | Owner only | Linear Agent Install (OAuth start) | `pkg/dashboard/api_linear_agent.go:221` | +| `GET` | `/api/linear/agent/status` | Owner only | Linear Agent Status | `pkg/dashboard/api_linear_agent.go:222` | +| `POST` | `/api/linear/agent/disconnect` | Owner only | Linear Agent Disconnect | `pkg/dashboard/api_linear_agent.go:223` | | `GET` | `/api/upstream-watch` | Owner only | Upstream-watch divergence: per watched fork the upstream followed, the watermark and last-run time, surfaced/ported/dismissed/skipped counts, and the recent upstream refs with their upstream URL, diff URL, fork issue number and state. Read-only — it reads the config and the watch state file, never GitHub. Also carries `interval`, `project_repos` and each repo's configured entry (`config`) for the editor. | `pkg/dashboard/api.go:325` | | `PUT` | `/api/config/upstream-watch` | Owner only | Upstream Watch Config Update: edit `upstream_watch` from the Features tab. Body `{enabled?, interval?, repos?: {: {upstream?, sources?, pr_labels?, max_issues_per_run?, label?} \| null}}` — absent keys are unchanged, `null` removes a repo, keys are stored as the bare repo name. Validated with the loader's rules before the config changes (400 on failure), then persisted and audited as `config_upstream_watch`. | `pkg/dashboard/api.go:326` | diff --git a/src/docs/api-reference/dashboard-misc.md b/src/docs/api-reference/dashboard-misc.md index c94fe92ac2..1c0359dc8e 100644 --- a/src/docs/api-reference/dashboard-misc.md +++ b/src/docs/api-reference/dashboard-misc.md @@ -45,9 +45,9 @@ The monitor renders CI and Review concurrently on one shared PR row; see | `POST` | `/api/plan/{epicID}/approve` | Owner only | Plan Approve | `pkg/dashboard/api.go:494` | | `POST` | `/api/plan/{epicID}/reject` | Owner only | Plan Reject | `pkg/dashboard/api.go:496` | | `POST` | `/api/plan/{epicID}/child/{childID}` | Owner only | Plan Child Action | `pkg/dashboard/api.go:497` | -| `POST` | `/api/linear/agent/install` | Owner only | Linear Agent Install (OAuth start) | `pkg/dashboard/api_linear_agent.go:219` | -| `GET` | `/api/linear/agent/status` | Owner only | Linear Agent Status | `pkg/dashboard/api_linear_agent.go:220` | -| `POST` | `/api/linear/agent/disconnect` | Owner only | Linear Agent Disconnect | `pkg/dashboard/api_linear_agent.go:221` | +| `POST` | `/api/linear/agent/install` | Owner only | Linear Agent Install (OAuth start) | `pkg/dashboard/api_linear_agent.go:221` | +| `GET` | `/api/linear/agent/status` | Owner only | Linear Agent Status | `pkg/dashboard/api_linear_agent.go:222` | +| `POST` | `/api/linear/agent/disconnect` | Owner only | Linear Agent Disconnect | `pkg/dashboard/api_linear_agent.go:223` | | `GET` | `/api/upstream-watch` | Owner only | Upstream-watch divergence: per watched fork the upstream followed, the watermark and last-run time, surfaced/ported/dismissed/skipped counts, and the recent upstream refs with their upstream URL, diff URL, fork issue number and state. Read-only — it reads the config and the watch state file, never GitHub. Also carries `interval`, `project_repos` and each repo's configured entry (`config`) for the editor. | `pkg/dashboard/api.go:325` | | `PUT` | `/api/config/upstream-watch` | Owner only | Upstream Watch Config Update: edit `upstream_watch` from the Features tab. Body `{enabled?, interval?, repos?: {: {upstream?, sources?, pr_labels?, max_issues_per_run?, label?} \| null}}` — absent keys are unchanged, `null` removes a repo, keys are stored as the bare repo name. Validated with the loader's rules before the config changes (400 on failure), then persisted and audited as `config_upstream_watch`. | `pkg/dashboard/api.go:326` |