Skip to content

feat(rules): target expressions count the rule's guards - #630

Open
ccf-lisa[bot] wants to merge 3 commits into
effectors/29-plan-refreshfrom
effectors/30-target-guards
Open

ccf-lisa[bot] wants to merge 3 commits into
effectors/29-plan-refreshfrom
effectors/30-target-guards

Conversation

@ccf-lisa

@ccf-lisa ccf-lisa Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

What

A rule's target expression only runs for subjects its condition holds for, so the map-key guards in the condition now count for the target's unguarded-lookup warnings. With

when: '"owner" in subject.labels && subject.status == "not-satisfied"'
target: { expr: '"slack-handle:" + subject.labels["owner"]' }

the target no longer warns about subject.labels["owner"].

  • rules.Compiled keeps its unguarded lookups by key (m["k"] and the dotted m.k), and a condition (any expression kind that returns bool, today only when) also records the guards that hold whenever it's true. Compiled.WarningsGiven(condition) drops the lookups the condition guards. Cached Compiled values stay independent of any condition, and a target compiled alone (Warnings, or WarningsGiven(nil)) behaves as before.
  • Which guards count (conservative): "k" in m and has(m.k) reached through top-level && only: a && (b && "k" in m) counts. A guard under ||, !, a ternary, a macro (exists, all, ...) or any other call doesn't, because it may be false while the condition is true. Guards inside the target expression itself count as before (anywhere in it).
  • Where it applies: POST /api/rules and POST /api/rules/preview use the request's when; POST/PUT /api/rules/{id}/actions[/{actionId}] use the saved rule's when. PUT /api/rules/{id} now also returns the saved actions' target warnings under the new when, so dropping a guard the targets relied on shows up.
  • Event rules: their if plays the same role (ruleCondition says where it plugs in). No source accepts event rules yet, Rule stores only when_expr, and there is no if expression kind, so if isn't tested separately. compile collects guards for every kind that returns bool rather than only ExprWhen, so an if kind gets them when it's added.
  • Docs: triggers.md (validation on save) and user-interface.md (when an expression fails, the validation table) say the condition's guards count.

Tests

  • rules.TestTargetWarningsGivenCondition: guarded in when (also nested in &&, and via has()) → no warning; a dotted target lookup (subject.labels.owner) guarded in when by in or has() → no warning, by another key → warning; guard only under ||, under !, in a ternary, inside a macro, or for another key → warning; a target with two lookups keeps the warning for the key the condition doesn't guard; the cached target's own Warnings are unchanged.
  • TestRulesApi/TestTargetWarningsCountTheConditionsGuards: a saved rule whose when guards owner returns no target warnings on create and preview; an action added to it doesn't warn, one looking up team does; PUT /api/rules/{id} with a when that drops the guard returns the three target warnings, and restoring it leaves only team; a preview with the guard under || warns on actions[0].target.expr.
  • TestCreateAndGet now expects no warnings for validRule (its when guards owner).

Why

User decision for the effectors stack: a target is evaluated only for subjects matching the rule's condition, so a guard there makes the target's lookup safe, and the warning on "slack-handle:" + subject.labels["owner"] under "owner" in subject.labels && … was a false positive.

Gate

On this branch: go build ./... pass; golangci-lint v2.15.0 0 new issues (default config: only the 6 pre-existing issues in untouched workflows/worker files; the shared CI config reports nothing in the changed files); make test pass; go vet -tags integration ./... pass; make swag no diff; go mod tidy no diff; go test -tags integration -p 1 -count=1 on ./internal/service/rules/... and ./internal/api/handler/... pass.

Size

Counted: 350 lines (added + removed, tests and docs included; no generated files changed).

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 2c1ec242-8ec3-4709-bf96-846caa7dfc35

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@gusfcarvalho gusfcarvalho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ccf-review: COMMENT

1 Should-fix.

Stack (gh stack 604): #592 → #593 → #594 → #595 → #596 → #597 → #598 → #599 → #600 → #601 → #602 → #603 → #605 → #606 → #607 → #608 → #609 → #610 → #611 → #612 → #613 → #614 → #615 → #616 → #617 → #618 → #619 → #620 → #621 → #622 → #623 → #624 → #625 → #626 → #627 → #628 → #629 → #630

Comment thread internal/service/rules/cel_test.go Resolved
ccf-lisa Bot and others added 3 commits December 2, 2026 11:12
The "event rule's if" case compiled the same ExprWhen expression as the
when case and tested nothing new. Drop it and say why if isn't covered
separately (there is no if kind; no source accepts event rules). compile now
collects guards for every kind that returns bool rather than only ExprWhen,
so an if kind gets them when it's added. Also cover a dotted lookup
(subject.labels.owner) guarded in when.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@gusfcarvalho gusfcarvalho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ccf-review: APPROVE

no findings.

Stack (gh stack 604): #592 → #593 → #594 → #595 → #596 → #597 → #598 → #599 → #600 → #601 → #602 → #603 → #605 → #606 → #607 → #608 → #609 → #610 → #611 → #612 → #613 → #614 → #615 → #616 → #617 → #618 → #619 → #620 → #621 → #622 → #623 → #624 → #625 → #626 → #627 → #628 → #629 → #630

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant