diff --git a/changelog.d/fixed-15881.md b/changelog.d/fixed-15881.md new file mode 100644 index 0000000000..6f28aee272 --- /dev/null +++ b/changelog.d/fixed-15881.md @@ -0,0 +1 @@ +- CEL trigger rules on `issue.opened` and `pr.opened` now match each issue or PR once per hive process instead of re-kicking the agent every eval cycle. diff --git a/changelog.d/fixed-15889.md b/changelog.d/fixed-15889.md new file mode 100644 index 0000000000..bbac6d7d01 --- /dev/null +++ b/changelog.d/fixed-15889.md @@ -0,0 +1 @@ +- celtrigger: fire issue.opened and pr.opened once per item (#15889) diff --git a/src/cmd/hive/celwire.go b/src/cmd/hive/celwire.go index d153eabe4d..d2d6044827 100644 --- a/src/cmd/hive/celwire.go +++ b/src/cmd/hive/celwire.go @@ -144,7 +144,45 @@ func prToEvent(pr github.PullRequest) celtrigger.NormalizedEvent { } } -// celMatchedAgents returns the set of agent names that operator-defined CEL rules +// celSeenSet remembers which repo#number items have already been surfaced as +// "opened", so issue.opened / pr.opened are edge-triggered per hive process +// instead of re-matching every eval cycle while the item stays actionable. +type celSeenSet struct { + mu sync.Mutex + seen map[string]struct{} +} + +// firstSight reports whether key is new, recording it as seen. +func (s *celSeenSet) firstSight(key string) bool { + s.mu.Lock() + defer s.mu.Unlock() + if s.seen == nil { + s.seen = make(map[string]struct{}) + } + if _, ok := s.seen[key]; ok { + return false + } + s.seen[key] = struct{}{} + return true +} + +// globalCELSeen is the process-wide seen-set used by the eval cycle. +var globalCELSeen celSeenSet + +// celMatchedAgents is the level-triggered form of celMatchedAgentsOnce: every +// enumerated item is evaluated every call. +func celMatchedAgents( + engine *celtrigger.Engine, + actionable *github.ActionableResult, + cfg *config.Config, + gov *governor.Governor, + isPaused func(string) bool, + logger *slog.Logger, +) []string { + return celMatchedAgentsOnce(engine, actionable, cfg, gov, isPaused, logger, nil) +} + +// celMatchedAgentsOnce returns the set of agent names that operator-defined CEL rules // select for the items enumerated this cycle, after applying the governor's own // gating. The result is intended to be UNIONED into the governor's due-agents set // — it is strictly additive and never removes a governor-selected agent. @@ -154,15 +192,19 @@ func prToEvent(pr github.PullRequest) celtrigger.NormalizedEvent { // (cadence-pause / budget / on-demand / kick-channel). A CEL match therefore // never bypasses a paused agent or an exhausted budget. // +// When seen is non-nil, an issue or PR is evaluated only the first time it is +// enumerated; later cycles skip it. A nil seen evaluates every item every call. +// // Cheap and nil-safe: a nil engine, nil actionable, or empty rules returns an // empty result with zero per-item work. -func celMatchedAgents( +func celMatchedAgentsOnce( engine *celtrigger.Engine, actionable *github.ActionableResult, cfg *config.Config, gov *governor.Governor, isPaused func(string) bool, logger *slog.Logger, + seen *celSeenSet, ) []string { if engine == nil || engine.Len() == 0 || actionable == nil || cfg == nil { return nil @@ -178,7 +220,7 @@ func celMatchedAgents( consider := func(ev celtrigger.NormalizedEvent, source string) { for _, agentName := range engine.MatchAgents(ev) { - if _, seen := candidates[agentName]; seen { + if _, dup := candidates[agentName]; dup { continue } candidates[agentName] = candidate{source: source, number: ev.Number} @@ -186,9 +228,15 @@ func celMatchedAgents( } for _, iss := range actionable.Issues.Items { + if seen != nil && !seen.firstSight(fmt.Sprintf("issue:%s#%d", iss.Repo, iss.Number)) { + continue + } consider(issueToEvent(iss), celSourceIssue) } for _, pr := range actionable.PRs.Items { + if seen != nil && !seen.firstSight(fmt.Sprintf("pr:%s#%d", pr.Repo, pr.Number)) { + continue + } consider(prToEvent(pr), celSourcePR) } diff --git a/src/cmd/hive/celwire_test.go b/src/cmd/hive/celwire_test.go index 8027ad5078..350dd4746a 100644 --- a/src/cmd/hive/celwire_test.go +++ b/src/cmd/hive/celwire_test.go @@ -289,3 +289,51 @@ func TestEventMapperKinds(t *testing.T) { t.Errorf("pr event mapping wrong: %+v", ev) } } + +// TestCELOpenedFiresOncePerItem verifies issue.opened / pr.opened are +// edge-triggered: the same snapshot evaluated twice matches once, and a newly +// appearing item still fires. +func TestCELOpenedFiresOncePerItem(t *testing.T) { + cfg := &config.Config{ + Triggers: []config.TriggerRule{ + {Name: "issue", Expr: `event.kind == "issue.opened"`, Agent: "reviewer"}, + {Name: "pr", Expr: `event.kind == "pr.opened"`, Agent: "fixer"}, + }, + AgentRuntimeConfig: config.AgentRuntimeConfig{ + Agents: map[string]config.AgentConfig{"reviewer": enabledAgent(), "fixer": enabledAgent()}, + }, + } + engine := celEngineFor(cfg, celTestLogger()) + gov := newTestGovernor(cfg.Agents) + notPaused := func(string) bool { return false } + snap := func(issues []int, prs []int) *github.ActionableResult { + r := &github.ActionableResult{} + for _, n := range issues { + r.Issues.Items = append(r.Issues.Items, github.Issue{Repo: "o/r", Number: n}) + } + for _, n := range prs { + r.PRs.Items = append(r.PRs.Items, github.PullRequest{Repo: "o/r", Number: n}) + } + return r + } + var seen celSeenSet + run := func(a *github.ActionableResult) int { + return len(celMatchedAgentsOnce(engine, a, cfg, gov, notPaused, celTestLogger(), &seen)) + } + + if got := run(snap([]int{1}, []int{2})); got != 2 { + t.Fatalf("first eval: want 2 agents, got %d", got) + } + if got := run(snap([]int{1}, []int{2})); got != 0 { + t.Fatalf("second eval of same snapshot: want 0 agents, got %d", got) + } + if got := run(snap([]int{1, 3}, []int{2})); got != 1 { + t.Fatalf("new issue: want 1 agent, got %d", got) + } + if got := run(snap([]int{1, 3}, []int{2, 4})); got != 1 { + t.Fatalf("new PR: want 1 agent, got %d", got) + } + if got := len(celMatchedAgents(engine, snap([]int{1}, nil), cfg, gov, notPaused, celTestLogger())); got != 1 { + t.Fatalf("level-triggered wrapper must still match, got %d", got) + } +} diff --git a/src/cmd/hive/main_eval_cycle.go b/src/cmd/hive/main_eval_cycle.go index c5ad25fbec..5c1b115f4a 100644 --- a/src/cmd/hive/main_eval_cycle.go +++ b/src/cmd/hive/main_eval_cycle.go @@ -917,7 +917,7 @@ func runEvalCycle( // paused agent or exhausted budget. Fully guarded/cheap: with no `triggers:` // configured, celEngineFor returns nil and celMatchedAgents does zero work. if celEngine := celEngineFor(cfg, logger); celEngine != nil { - celAgents := celMatchedAgents(celEngine, actionable, cfg, gov, agentMgr.IsPaused, logger) + celAgents := celMatchedAgentsOnce(celEngine, actionable, cfg, gov, agentMgr.IsPaused, logger, &globalCELSeen) if len(celAgents) > 0 { before := len(agentsDue) agentsDue = unionAgents(agentsDue, celAgents) diff --git a/src/docs/cel-triggers.md b/src/docs/cel-triggers.md index 176db3c0f0..07bad03b56 100644 --- a/src/docs/cel-triggers.md +++ b/src/docs/cel-triggers.md @@ -89,6 +89,11 @@ pr.merged comment.created ``` +`issue.opened` and `pr.opened` are edge-triggered: the eval cycle emits them +once per issue/PR (keyed `repo#number`) per hive process, the first time the +item is enumerated, not on every cycle while it stays actionable. The seen-set +is in memory, so a hive restart re-emits currently actionable items once. + ### Available functions Besides the standard CEL string/list operators (`in`, `.contains(...)`,