From 8fa668107d6bbd176ddb7b572421bf8dc3f65391 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Fri, 30 Oct 2026 17:14:35 -0400 Subject: [PATCH 1/3] fix(celtrigger): fire issue.opened/pr.opened once per item, not every eval cycle Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Co-authored-by: hanthor <5840441+hanthor@users.noreply.github.com> --- changelog.d/fixed-15881.md | 1 + src/cmd/hive/celwire.go | 50 ++++++++++++++++++++++++++++++++- src/cmd/hive/celwire_test.go | 48 +++++++++++++++++++++++++++++++ src/cmd/hive/main_eval_cycle.go | 2 +- src/docs/cel-triggers.md | 5 ++++ 5 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 changelog.d/fixed-15881.md diff --git a/changelog.d/fixed-15881.md b/changelog.d/fixed-15881.md new file mode 100644 index 0000000000..a047e50352 --- /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/src/cmd/hive/celwire.go b/src/cmd/hive/celwire.go index d153eabe4d..98640e4a24 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,6 +192,9 @@ 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( @@ -163,6 +204,7 @@ func celMatchedAgents( 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 @@ -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(...)`, From 40ce4cb4bf822269e9c856ede8fb4e0091e76f85 Mon Sep 17 00:00:00 2001 From: "hivecommons-hive[bot]" <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Fri, 30 Oct 2026 21:16:07 +0000 Subject: [PATCH 2/3] changelog: fragment for #15889 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: hivecommons-hive[bot] <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- changelog.d/fixed-15889.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/fixed-15889.md 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) From 4a67519ed24c42f729f3e3a1480e33159987573d Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Fri, 30 Oct 2026 17:23:19 -0400 Subject: [PATCH 3/3] fix(celtrigger): name the once-only evaluator and bullet the changelog fragment Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- changelog.d/fixed-15881.md | 2 +- src/cmd/hive/celwire.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/changelog.d/fixed-15881.md b/changelog.d/fixed-15881.md index a047e50352..6f28aee272 100644 --- a/changelog.d/fixed-15881.md +++ b/changelog.d/fixed-15881.md @@ -1 +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. +- 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/src/cmd/hive/celwire.go b/src/cmd/hive/celwire.go index 98640e4a24..d2d6044827 100644 --- a/src/cmd/hive/celwire.go +++ b/src/cmd/hive/celwire.go @@ -197,7 +197,7 @@ func celMatchedAgents( // // 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, @@ -220,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}