Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-15881.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions changelog.d/fixed-15889.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- celtrigger: fire issue.opened and pr.opened once per item (#15889)
54 changes: 51 additions & 3 deletions src/cmd/hive/celwire.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -178,17 +220,23 @@ 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}
}
}

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)
}

Expand Down
48 changes: 48 additions & 0 deletions src/cmd/hive/celwire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion src/cmd/hive/main_eval_cycle.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

import (
Expand Down Expand Up @@ -917,7 +917,7 @@
// 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)
Expand Down
5 changes: 5 additions & 0 deletions src/docs/cel-triggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(...)`,
Expand Down
Loading