Skip to content

feat(reconciler): evaluate records against content policies - #2229

Merged
muscariello merged 4 commits into
feat/policy-evaluation-schemafrom
feat/policy-evaluation-task
Oct 2, 2026
Merged

muscariello merged 4 commits into
feat/policy-evaluation-schemafrom
feat/policy-evaluation-task

Conversation

@muscariello

@muscariello muscariello commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Closes #2207. Part of #2205. Stacked on #2226 — based on feat/policy-evaluation-schema, so this diff shows only the task; it retargets to main once #2226 merges.

What this adds

A policy reconciler task. For each registered Evaluator, it registers the version of the policy it runs, then selects the records without a current verdict — never evaluated, evaluated under a superseded policy version, or whose last evaluation failed — and stores one via UpsertPolicyEvaluation, a batch at a time.

  • Batches. Records are fetched and evaluated batch_size at a time (default 500), each batch starting after the last record of the one before, until none is left. The batch size bounds what a run holds in memory, not how long it lasts: after a policy changes, records stay hidden until evaluated, so evaluation is not throttled. A record whose evaluation failed is selected again by the next run, not by the next batch of the same run.
  • The version is registered first, every run, before any record is evaluated. The server follows the registered version, so an edited policy is picked up without anyone stating its new version.
  • Backfill needs no separate pass. Records that predate a policy are selected by GetRecordsNeedingPolicyEvaluation exactly like new ones. The per-policy count logged each run is the progress signal: it falls to zero once every record has a verdict under the current version.
  • Evaluator is a port, not an engine: PolicyID(), PolicyVersion(), Evaluate(ctx, record). Concrete implementations (OPA, OASF schema validation) come from [Feature]: OPA-based content policy engine #2135 and [Feature]: Add validators #2157. NewTask rejects nil evaluators, empty IDs or versions, and duplicate policy IDs, which would overwrite each other's rows.
  • Fail closed. An evaluator error is stored as a failed evaluation, which reads as non-compliant. The stored reason is a fixed string, not the error's text — evaluator errors can carry internal hosts or file paths, and the row is queryable. The detail goes to the log.
  • A shutdown stores nothing. On cancellation the evaluator reports only the cancellation, which isn't a verdict about the record; storing it would mark every remaining record non-compliant on each restart. A per-record timeout with the run still live is different: that's a slow evaluator, stored as failed and retried.
  • Rows carry the version they were selected under, read once per run. If an evaluator reloads mid-run, those rows are stale next run and re-selected, rather than labelled with a version they weren't selected for.

Configuration

The task's parameters, interval (default 1m), record_timeout (default 30s) and batch_size (default 500), are exposed the way every other reconciler task's are: a policy_evaluation block in the reconciler config (policy is the policy directory), bound to RECONCILER_POLICY_EVALUATION_* with defaults taken from the task's own constants so the values live in one place, and entries in the daemon config, the Helm reconciler config and the docker env file.

policy_evaluation.enabled is off by default. No Evaluator is implemented yet, so enabling it registers the task disabled and logs why, rather than the setting being silently ignored. Evaluators are passed in when the first one lands.

Change to #2226

Designing the retry path surfaced a bug in #2226's query, fixed on that branch rather than here: GetRecordsNeedingPolicyEvaluation treated a failed row as settled, so one transient evaluator error excluded a record from reads until the policy version next changed. Only an evaluated row now suppresses re-selection.

Not in this PR

  • Prometheus metrics. No reconciler task exports metrics today; progress is structured logging, matching the signature and scan tasks.
  • Backoff for a persistently failing evaluator. Failed rows retry every interval. Scan-style exponential backoff is a reasonable follow-up if evaluators turn out to fail expensively.

Testing

Tests in reconciler/tasks/policy, race-clean: evaluator validation, IsEnabled, verdict storage (including a compliant verdict's reason being dropped), fail-closed on evaluator error without persisting its text, record timeout as a failed verdict, shutdown storing nothing (also between batches), a policy's selection error not stopping the others, a store error not stopping the run, the version snapshot, the empty case, batching in record order, a failing record not keeping a run going, the default batch size, and the version being registered first (and a failed registration stopping the policy).

The shutdown guard and the no-leak reason were mutation-checked: removing either makes its test fail. Plus two tests on #2226 for the retry fix, also mutation-checked. go vet and gofmt clean; full reconciler module passes under -race.

@muscariello
muscariello requested a review from a team as a code owner September 29, 2026 21:34
@muscariello muscariello added kind/feature Categorizes issue or PR as related to a new feature. area/dir/server labels Sep 29, 2026
@github-actions github-actions Bot added the size/M Denotes a PR that changes 200-999 lines label Sep 29, 2026
@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.36842% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
reconciler/tasks/policy/task.go 97.7% 2 Missing ⚠️
reconciler/service/service.go 88.9% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@muscariello
muscariello added this pull request to stack #2234 September 30, 2026 07:19
@muscariello
muscariello force-pushed the feat/policy-evaluation-task branch from 2fc9c9f to 23fe646 Compare September 30, 2026 10:49
@muscariello
muscariello force-pushed the feat/policy-evaluation-task branch from 23fe646 to 740d835 Compare September 30, 2026 18:09
@muscariello
muscariello force-pushed the feat/policy-evaluation-task branch from 740d835 to f24b639 Compare October 1, 2026 11:20
muscariello added a commit that referenced this pull request Oct 1, 2026
Brings in the fix that moves the reconciler's policy evaluation settings to policy_evaluation (#2229), which the rebase onto main had left clashing with #2204's policy section.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Oct 1, 2026
Brings in the fix that moves the reconciler's policy evaluation settings to policy_evaluation (#2229), which the rebase onto main had left clashing with #2204's policy section.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Oct 1, 2026
Brings in the fix that moves the reconciler's policy evaluation settings to policy_evaluation (#2229), which the rebase onto main had left clashing with #2204's policy section.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Oct 1, 2026
Brings in the fix that moves the reconciler's policy evaluation settings to policy_evaluation (#2229), which the rebase onto main had left clashing with #2204's policy section.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Oct 1, 2026
…eers

Brings in the fix that moves the reconciler's policy evaluation settings to policy_evaluation (#2229), which the rebase onto main had left clashing with #2204's policy section.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Oct 1, 2026
…cement-modes

Brings in main's #2204, which rewrote the policy config this branch extends and moved its OPA directory into the reconciler's policy section, and the policy_evaluation rename from #2229. The policy config keeps main's struct, without enabled or Validate, plus this branch's enforcement settings; its test file, which main deleted, keeps only the enforcement tests.

# Conflicts:
#	cli/cmd/daemon/daemon.config.yaml
#	install/charts/dir/apiserver/values.yaml
#	install/docker/reconciler.env
#	reconciler/config/config.go
#	reconciler/config/config_test.go
#	reconciler/service/service.go
#	server/config/config_test.go
#	server/database/gorm/gorm.go
#	server/database/gorm/policy_gate.go
#	server/database/gorm/policy_gate_test.go
#	server/policy/config/config.go
#	server/policy/config/config_test.go
#	server/server.go
#	server/types/database.go
muscariello added a commit that referenced this pull request Oct 1, 2026
…ment-backfill

Brings in main's #2204, the policy_evaluation rename from #2229, and #2248's merge of both. The policy config keeps main's struct plus this branch's refresh interval and previous_version.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Oct 1, 2026
…t-bypass

Brings in main's #2204, the policy_evaluation rename from #2229, and the merges below. The runbook now names the evaluation task's Helm key, reconciler.config.policy_evaluation.

Signed-off-by: Luca Muscariello <[email protected]>
@csirmazbendeguz

csirmazbendeguz commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

It's important to have a job like this, but 1m is a very small interval for a job that potentially processes all records

The ideal solution would be to evaluate policies on events. I'm not saying we have to evaluate them synchronously. A record's event could trigger a background job (that would evaluate that single record)

Of course, we would still need this scheduled job for picking up missed records, so "evaluate on events" doesn't replace this

I haven't seen this pattern in this project before, but it would be worth implementing so we can handle use-cases like this


For now, the simplest solution would be to limit the number of records GetRecordsNeedingPolicyEvaluation returns. So it can process them in 1m

@muscariello
muscariello force-pushed the feat/policy-evaluation-task branch from 125a2b5 to 6d40c70 Compare October 1, 2026 15:11
@github-actions github-actions Bot added size/L Denotes a PR that changes 1000-1999 lines and removed size/M Denotes a PR that changes 200-999 lines labels Oct 1, 2026
@muscariello

muscariello commented Oct 1, 2026 •

Copy link
Copy Markdown
Member Author

@csirmazbendeguz Paged now. I kept a run going until no record is left instead of capping it: after an edit records stay hidden until evaluated, so throttling would lengthen that. Evaluating on record events would be a good follow-up: #2270.

csirmazbendeguz
csirmazbendeguz previously approved these changes Oct 2, 2026
@muscariello
muscariello dismissed csirmazbendeguz’s stale review October 2, 2026 12:05

The merge-base changed after approval.

@muscariello
muscariello force-pushed the feat/policy-evaluation-task branch from 07c34fb to a979e8f Compare October 2, 2026 12:05
@muscariello
muscariello force-pushed the feat/policy-evaluation-task branch from a979e8f to 5c27ce6 Compare October 2, 2026 12:33
A policy reconciler task that, for each registered Evaluator, selects
the records without a current verdict (never evaluated, evaluated
under a superseded version, or last evaluation failed) and stores one.
Records that predate a policy are selected the same way as new ones,
so backfill needs no separate pass; the per-run count logged for each
policy is its progress signal.

Evaluator is the port concrete policy engines implement (OPA, OASF
schema validation; #2135, #2157). An evaluator error is stored as a
failed evaluation, which reads as non-compliant, with a fixed reason
rather than the error's text, which can carry internal hosts or paths.
A shutdown stores nothing: the evaluator only reports the
cancellation, which is not a verdict about the record.

Not registered in the reconciler service yet: no Evaluator exists to
give it. Wiring lands with the first one.

Refs #2207, #2205

Signed-off-by: Luca Muscariello <[email protected]>
The policy task's interval and per-record timeout existed only as the
Default constants in its config package: nothing an operator controls
could change them. Exposes them the way every other reconciler task's
parameters are exposed:

- reconciler config: a policy block, with env bindings
  (RECONCILER_POLICY_*) whose defaults are the task's constants, so the
  values live in one place;
- the daemon config, the Helm reconciler config, and the docker env
  file, next to the other tasks.

policy.enabled is off by default. With no policy evaluator implemented
yet, enabling it registers the task disabled and logs why, rather than
the setting being silently ignored.

Refs #2207

Signed-off-by: Luca Muscariello <[email protected]>
files are loaded from, the same one the server has, and the chart fills
it from config.policy. Rebased onto it, this branch declared a second
Policy field and policy import for the evaluation task's settings, and
the reconciler stopped compiling.

The task's settings move to policy_evaluation: RECONCILER_POLICY_EVALUATION_*
in the environment, reconciler.config.policy_evaluation in Helm, and
reconciler.policy_evaluation in the daemon configuration.

Refs #2207

Signed-off-by: Luca Muscariello <[email protected]>
A run fetched every record needing a verdict in one query, which for a
first run is the whole records table, and held them all while it
evaluated them one by one. The task now fetches and evaluates
policy_evaluation.batch_size records at a time (default 500), each batch
starting after the last record of the one before, until none is left.
The batch size bounds a run's memory, not its length: after a policy
changes, records stay hidden until re-evaluated, so evaluation is not
throttled. A record whose evaluation failed is selected again by the
next run, not by the next batch of the same run, so it cannot keep a run
going.

The task also registers the version of each policy it runs, at the start
of every run, before evaluating any record. The server follows the
policy's current version from that registry, so a policy edited on disk
is picked up without anyone stating its new version.

The batch size is a setting in the environment, Helm, the daemon
configuration and the docker env, like the task's other parameters.

Refs #2207

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-evaluation-task branch from 5c27ce6 to bcca0ae Compare October 2, 2026 12:58
@muscariello
muscariello added this pull request to the merge queue Oct 2, 2026
Merged via the queue into main with commit 999eb11 Oct 2, 2026
34 checks passed
@muscariello
muscariello deleted the feat/policy-evaluation-task branch October 2, 2026 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/dir/server kind/feature Categorizes issue or PR as related to a new feature. size/L Denotes a PR that changes 1000-1999 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compute policy verdicts asynchronously and backfill existing records

2 participants