Skip to content

feat(effects): state machine - #610

Open
ccf-lisa[bot] wants to merge 2 commits into
effectors/15a-effect-jobsfrom
effectors/15b-effect-machine
Open

ccf-lisa[bot] wants to merge 2 commits into
effectors/15a-effect-jobsfrom
effectors/15b-effect-machine

Conversation

@ccf-lisa

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

Copy link
Copy Markdown
Contributor

What

The effect state machine and the engine that drives it (triggers, grouping and deduplication; API LLD, effect state machine).

Engine.EvaluateSubject(source, subject) (the rules.evaluate-subject job), one transaction: a share lock on each enabled rule of the source (so a disable or delete serializes with it), then an advisory lock on the subject; the subject document is built once; per rule: when (errors count as no match and are recorded), the match saved with matched_since kept from the first match and its document, now-dependent matches flagged and now-dependent non-matches watched (for the sweep, next PR); per action: targets (errors and rejected identities recorded), then the machine.

Machine:

  • a subject joins the effect of each of its targets (created on demand, keyed by rule action, agent, effector action and target) and leaves the others;
  • it enters once matched_since + for + delay has passed; otherwise a tick is scheduled and the effect is pending;
  • transitions: activated (from inactive/pending, or a subject entering a finished effect), updated (subjects entered or left), repeated every repeat, finished (the repeat that reaches max_repeats, with that repeat count), cleared (no subject left). Every entry resets the repeat count.
  • PoC follow-up 4 (follow-ups): a subject leaving a finished effect while others still match delivers updated and the effect stays finished (no repeats).
  • each delivered transition bumps the generation, records history (entered/left), puts the effect in the agent's held set, and enqueues a delivery through the outbox (15a).

Engine.Tick(effect) re-runs the machine at its scheduled time and does nothing for a deleted or repointed action or a disabled rule.

Not here: backfill and the now sweep (16), rule and action changes (16b). A raised max_repeats does not resume a finished effect (open question in the plan).

Why

Plan: stack 2, PR 15.

Size exception: one package: the machine and the engine that drives it, with their tests. The machine is only driven through EvaluateSubject and Tick, and the tests that pin each transition go through them; splitting the machine from the engine would mean testing it twice. The plan marks this PR SE.

Test evidence

engine_integration_test.go (Postgres): a full lifecycle (pending inside for, activated, repeated, a second subject joining and resetting repeats, finished at max_repeats, no repeats after, finished + left → updated and still finished, cleared; history and delivered generations), a finished effect acting again when a subject enters, flapping inside for never delivers, a target change moves the subject, per-action delay, errors recorded (missing key, rejected identity, failing when), disabled rules aren't evaluated, jobs written to the outbox (and left there when enqueuing fails), ticks for deleted/repointed actions and disabled rules, a gone subject leaves, now-dependent subjects recorded.

Gate on this branch: go build ./... pass; go vet -tags integration ./... pass; make lint (golangci-lint v2.15.0, CI config) 0 new issues; make test pass; make swag no diff; go mod tidy no diff; go test -tags integration -p 1 on ./internal/service/effects/ pass.

Size

Counted: 1120 lines (size exception, above).

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Dec 1, 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: 5030d411-8b87-4004-be31-027b3ae45a55

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

ccf-lisa Bot and others added 2 commits December 2, 2026 11:04
internal/service/effects: the effect state machine and the engine that
drives it (triggers.md, Grouping and deduplication; LLD Effect state
machine).

- EvaluateSubject evaluates every enabled rule of a source against one
  subject in one transaction: a share lock on each rule row, then an
  advisory lock on the subject; the match and its document are saved
  with matched_since kept from the first match, now-dependent matches
  flagged and now-dependent non-matches watched; evaluation errors are
  recorded; each action's targets are reconciled with its effects.
- The machine: a subject joins the effect of each target and leaves the
  others; it enters once for + delay has elapsed (a tick is scheduled
  otherwise); activated, updated, repeated, finished (the repeat that
  reaches max_repeats), cleared; every entry resets the repeat count. A
  subject leaving a finished effect while others still match delivers
  updated and the effect stays finished (PoC follow-up 4). Each
  delivered transition bumps the generation, records history, and
  enqueues a delivery through the outbox.
- Tick re-runs the machine for one effect at its scheduled time, and
  does nothing for a deleted or repointed action or a disabled rule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A trigger subject's id is the evidence stream UUID, but its url pointed at
/evidence/{id}, the UI page that loads GET /api/evidence/:id by record id,
so every "View evidence" link (e.g. {{.Subject.URL}} in Slack) showed
"Error loading evidence". Link /evidence/history/{id}, the UI's stream route.

@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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant