Skip to content

feat(dir): persist per-record policy-compliance verdicts - #2226

Merged
muscariello merged 6 commits into
mainfrom
feat/policy-evaluation-schema
Oct 2, 2026
Merged

muscariello merged 6 commits into
mainfrom
feat/policy-evaluation-schema

Conversation

@muscariello

@muscariello muscariello commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Closes #2206. Part of #2205.

What this adds

A policy_evaluations table, one row per (record_cid, policy_id), with an id primary key and a unique index on that pair. Each row carries the policy_version it was reached under, so a later policy change doesn't silently reinterpret an old verdict — a stale-version row is treated as needing re-evaluation, not as authoritative under the new policy.

Storage only, mirroring the existing scan_reports fail-closed convention rather than inventing a new one:

  • Compliant is a placeholder (false) unless Status == "evaluated", the same way ScanReport.IsSafe is a placeholder on a failed scan (see types.ScannedStatuses / the new types.EvaluatedPolicyStatuses). UpsertPolicyEvaluation enforces this in code, not just by convention.
  • GetRecordsNeedingPolicyEvaluation(policyID, policyVersion, afterCID, limit) mirrors GetRecordsNeedingScan's NOT EXISTS shape, for the reconciler backfill/re-evaluation job in Compute policy verdicts asynchronously and backfill existing records #2207. It returns records in record_cid order after afterCID, at most limit of them (zero returns all), so a first run doesn't load the whole records table. Paging by the last record_cid costs the same however far in the caller is, and doesn't return a record whose evaluation failed again within one pass.
  • A policy_versions registry: evaluators register the version of each policy they run, and GetCurrentPolicyVersion returns the one seen most recently. A policy's version comes from its content, so nobody states it; the server follows this registry (feat(dir): enforce content policies per kind of read, with a shadow mode #2248).

Nothing computes a verdict yet (#2207) and no read path filters on it yet (#2208) — this task is deliberately just the schema those two build on.

Testing

New table-driven tests in policy_evaluation_test.go: upsert transitions (including the fail-closed forcing case), re-evaluation replacing a prior verdict, multiple policies coexisting per record, every case of GetRecordsNeedingPolicyEvaluation (never evaluated, current version suppresses, superseded version reselects, unrelated policy doesn't suppress, only the stale record is selected), paging in record order and a failed record not repeating within a pass. policy_version_test.go covers the registry: a new version becoming current, returning to an earlier one, policies being independent, and database failures.

go vet and gofmt clean on every touched file; full database/gorm and types package suites pass.

@muscariello muscariello added kind/feature Categorizes issue or PR as related to a new feature. area/dir/server labels Sep 29, 2026
@muscariello
muscariello requested a review from a team as a code owner September 29, 2026 18:14
@github-actions github-actions Bot added the size/M Denotes a PR that changes 200-999 lines label Sep 29, 2026
muscariello added a commit that referenced this pull request Sep 29, 2026
…s on fakeSignatureDB

fakeSignatureDB spells out types.DatabaseAPI by hand rather than
embedding it, so extending the interface in the prior commit broke its
compile-time satisfaction. The three new methods are no-ops, matching
every other unused method on this fake.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Sep 29, 2026
types.DatabaseAPI aggregates one embedded interface per capability
domain by design; adding PolicyEvaluationDatabaseAPI took it from 10
to 11 members, over golangci-lint's default interfacebloat cap for a
flat method list. Raises interface-max-methods to 15, matching the
headroom already chosen for goconst in this file, so the next domain
added doesn't hit the same wall.

needsEvalCIDs always queried the same policy ID across every test in
the file; only policy_version and the seeded data varied. Drops the
parameter instead of threading a constant through every call site.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Sep 29, 2026
interfacebloat's settings struct field is Max, mapstructure key
"max" (pkg/config/linters_settings.go in golangci-lint v2.13.2,
the version this repo pins) — not interface-max-methods, which I
invented by analogy with the other linters' key names in this file
and never verified against golangci-lint's own source. The unknown
key was silently ignored, so the previous commit's cap never took
effect and CI failed on the same finding.

Fixes the Lint job failure on #2226.

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

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.50649% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
server/database/gorm/policy_evaluation.go 93.1% 4 Missing ⚠️
server/types/database.go 0.0% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Comment on lines +21 to +23
RecordCID string `gorm:"column:record_cid;primaryKey;not null"`
PolicyID string `gorm:"column:policy_id;primaryKey;not null"`
PolicyVersion string `gorm:"column:policy_version;not null;index"`

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

With the key on (record_cid, policy_id), the rollout #2235 suggests for a version bump — evaluating under the new version before it becomes the enforced one — can't work. The upsert (:68-77) overwrites the v1 row with the v2 verdict, and #2235's gate requires pe.policy_version = ? at the enforced version (policy_gate.go:29), so each record drops out of reads the moment it's pre-evaluated and comes back only at the switch. That moves the blackout rather than removing it.

Ran on the #2235 head (09a7d28b, which contains this PR's ac3b60b0) with a scratch test on the policy_gate_test.go helpers: two records compliant under opa:a v1, v1 enforced, GetRecordCIDs returns both. Upsert one under v2 with v1 still enforced: it returns one, and that record's only row is v2. With the change below it returns both, and the record holds a v1 and a v2 row.

#2206, which this closes, asked for one row per record × policy × version. That would be:

  1. primaryKey on policy_version here, policy_version added to the OnConflict columns and dropped from DoUpdates, 3 lines. ReevaluationReplacesThePriorVerdict is then the only failing test in database/gorm; it becomes "same version replaces, a new version adds a row".
  2. Delete superseded rows once the new version is enforced.
  3. Key feat(reconciler): evaluate records against content policies #2229's duplicate check (task.go:55) on (ID, version), so v1 and v2 evaluators can run side by side and records published during the transition get verdicts under both. Its test rejects that pair because they "would overwrite each other's rows" (task_test.go:128-130), which is this key; that case flips. Read, not run.

The other way is a new policy ID per version, keeping this key, but that gives up "unchanged across versions" in evaluator.go:18-19. SQLite only, as the suite runs; not tried on Postgres.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks. We chose the other direction: the new version is enforced at once and records stay hidden until evaluated under it, since content already delivered can't be recalled. So the key stays one row per record and policy.

@csirmazbendeguz

csirmazbendeguz commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

I would avoid using a composite primary key. A unique constraint + index would be sufficient here. I understand it was also used in scan reports, but this is not a good pattern

@muscariello
muscariello force-pushed the feat/policy-evaluation-schema branch from 4d6040a to c14d0ec Compare October 1, 2026 11:20
@csirmazbendeguz

csirmazbendeguz commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

I checked this and the related PRs - what's not clear to me is who is going to bump the version? Do we expect admins to bump the versions manually? Or is this something that will be automatic? I think it should be automatic, because it's not something admins would want to maintain themselves. We could store policy hashes instead of versions to automate that. We would need to store the policies in the database for that as well. I talked about this in the issue: #2135

@Santoshkumarpuppala

Copy link
Copy Markdown

@csirmazbendeguz agreed, a unique index does the job, and that goes for my earlier suggestion of putting policy_version in the primary key too. The open question is the column set. #2249 keeps (record_cid, policy_id) and accepts a verdict under either version during a bump (policy_gate.go:122 there), so a re-evaluation under v2 replacing the v1 row is by design. Adding policy_version to the index and to the OnConflict columns at policy_evaluation.go:69 is what would keep both rows; happy to leave that call to @muscariello.

// (fail closed), so if it also stopped the record being retried, one
// transient evaluator error would exclude the record from every read until
// the policy version changed.
func (d *DB) GetRecordsNeedingPolicyEvaluation(policyID, policyVersion string) ([]coretypes.Record, error) {

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.

This can fetch the entire records table without pagination

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed. GetRecordsNeedingPolicyEvaluation takes the record_cid to start after and a limit, and returns records in order; the task evaluates batch_size (default 500) at a time.

@muscariello

Copy link
Copy Markdown
Member Author

@csirmazbendeguz Done: an id primary key and a unique index on (record_cid, policy_id).

On who bumps the version: nobody. A policy's version is derived from its content; the reconciler registers it and the server follows it. The config lists policy IDs only.

muscariello added a commit that referenced this pull request Oct 1, 2026
…s on fakeSignatureDB

fakeSignatureDB spells out types.DatabaseAPI by hand rather than
embedding it, so extending the interface in the prior commit broke its
compile-time satisfaction. The three new methods are no-ops, matching
every other unused method on this fake.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-evaluation-schema branch from a55426b to dfadb59 Compare October 1, 2026 15:52
muscariello added a commit that referenced this pull request Oct 1, 2026
types.DatabaseAPI aggregates one embedded interface per capability
domain by design; adding PolicyEvaluationDatabaseAPI took it from 10
to 11 members, over golangci-lint's default interfacebloat cap for a
flat method list. Raises interface-max-methods to 15, matching the
headroom already chosen for goconst in this file, so the next domain
added doesn't hit the same wall.

needsEvalCIDs always queried the same policy ID across every test in
the file; only policy_version and the seeded data varied. Drops the
parameter instead of threading a constant through every call site.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Oct 1, 2026
interfacebloat's settings struct field is Max, mapstructure key
"max" (pkg/config/linters_settings.go in golangci-lint v2.13.2,
the version this repo pins) — not interface-max-methods, which I
invented by analogy with the other linters' key names in this file
and never verified against golangci-lint's own source. The unknown
key was silently ignored, so the previous commit's cap never took
effect and CI failed on the same finding.

Fixes the Lint job failure on #2226.

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

fakeSignatureDB spells out types.DatabaseAPI by hand rather than
embedding it, so extending the interface in the prior commit broke its
compile-time satisfaction. The three new methods are no-ops, matching
every other unused method on this fake.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Oct 2, 2026
types.DatabaseAPI aggregates one embedded interface per capability
domain by design; adding PolicyEvaluationDatabaseAPI took it from 10
to 11 members, over golangci-lint's default interfacebloat cap for a
flat method list. Raises interface-max-methods to 15, matching the
headroom already chosen for goconst in this file, so the next domain
added doesn't hit the same wall.

needsEvalCIDs always queried the same policy ID across every test in
the file; only policy_version and the seeded data varied. Drops the
parameter instead of threading a constant through every call site.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Oct 2, 2026
interfacebloat's settings struct field is Max, mapstructure key
"max" (pkg/config/linters_settings.go in golangci-lint v2.13.2,
the version this repo pins) — not interface-max-methods, which I
invented by analogy with the other linters' key names in this file
and never verified against golangci-lint's own source. The unknown
key was silently ignored, so the previous commit's cap never took
effect and CI failed on the same finding.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-evaluation-schema branch from dfadb59 to 4efb8e9 Compare October 2, 2026 12:05
muscariello added a commit that referenced this pull request Oct 2, 2026
…s on fakeSignatureDB

fakeSignatureDB spells out types.DatabaseAPI by hand rather than
embedding it, so extending the interface in the prior commit broke its
compile-time satisfaction. The three new methods are no-ops, matching
every other unused method on this fake.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Oct 2, 2026
types.DatabaseAPI aggregates one embedded interface per capability
domain by design; adding PolicyEvaluationDatabaseAPI took it from 10
to 11 members, over golangci-lint's default interfacebloat cap for a
flat method list. Raises interface-max-methods to 15, matching the
headroom already chosen for goconst in this file, so the next domain
added doesn't hit the same wall.

needsEvalCIDs always queried the same policy ID across every test in
the file; only policy_version and the seeded data varied. Drops the
parameter instead of threading a constant through every call site.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
muscariello added a commit that referenced this pull request Oct 2, 2026
interfacebloat's settings struct field is Max, mapstructure key
"max" (pkg/config/linters_settings.go in golangci-lint v2.13.2,
the version this repo pins) — not interface-max-methods, which I
invented by analogy with the other linters' key names in this file
and never verified against golangci-lint's own source. The unknown
key was silently ignored, so the previous commit's cap never took
effect and CI failed on the same finding.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-evaluation-schema branch from 4efb8e9 to 5e6b955 Compare October 2, 2026 12:33
Adds a policy_evaluations table, one row per (record_cid, policy_id),
versioned so a later policy change doesn't silently reinterpret an old
verdict. Mirrors the existing scan_reports fail-closed convention:
compliant is a placeholder (false) unless status is 'evaluated', the
same way ScanReport.IsSafe is a placeholder on a failed scan.

This is storage only. Nothing computes a verdict yet (#2207) and no
read path filters on it yet (#2208).

Refs #2206, #2205

Signed-off-by: Luca Muscariello <[email protected]>
…s on fakeSignatureDB

fakeSignatureDB spells out types.DatabaseAPI by hand rather than
embedding it, so extending the interface in the prior commit broke its
compile-time satisfaction. The three new methods are no-ops, matching
every other unused method on this fake.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
types.DatabaseAPI aggregates one embedded interface per capability
domain by design; adding PolicyEvaluationDatabaseAPI took it from 10
to 11 members, over golangci-lint's default interfacebloat cap for a
flat method list. Raises interface-max-methods to 15, matching the
headroom already chosen for goconst in this file, so the next domain
added doesn't hit the same wall.

needsEvalCIDs always queried the same policy ID across every test in
the file; only policy_version and the seeded data varied. Drops the
parameter instead of threading a constant through every call site.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
interfacebloat's settings struct field is Max, mapstructure key
"max" (pkg/config/linters_settings.go in golangci-lint v2.13.2,
the version this repo pins) — not interface-max-methods, which I
invented by analogy with the other linters' key names in this file
and never verified against golangci-lint's own source. The unknown
key was silently ignored, so the previous commit's cap never took
effect and CI failed on the same finding.

Fixes the Lint job failure on #2226.

Signed-off-by: Luca Muscariello <[email protected]>
GetRecordsNeedingPolicyEvaluation treated any row at the current
(policy_id, policy_version) as settled, failed rows included. A failed
row reads as non-compliant, so one transient evaluator error excluded
the record from reads until the policy version next changed. Only an
evaluated row now suppresses re-selection; a failed one is retried on
the next reconciler pass. A non-compliant verdict still suppresses,
since re-evaluating it would recompute the same answer.

Surfaced while designing the reconciler task in #2207.

Signed-off-by: Luca Muscariello <[email protected]>
…ersions

Review of the verdict storage raised three points.

The table was keyed by (record_cid, policy_id) as a composite primary
key. It now has an id primary key and a unique index on the same pair,
which is cheaper to change later than a primary key. There is still one
verdict per record and policy: a re-evaluation under another version
replaces it.

GetRecordsNeedingPolicyEvaluation returned every record needing a
verdict in one query, which for a first run is the whole records table.
It now takes the record_cid to start after and a limit, and returns
records in record_cid order. Paging by the last record_cid seen costs the
same however far in the caller is, and does not return a record whose
evaluation failed again within one pass.

Versions come from a policy's content, so nobody should have to state
them. Evaluators register the version they run (policy_versions), and
GetCurrentPolicyVersion returns the one seen most recently, which the
server will follow.

Refs #2206

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-evaluation-schema branch from 5e6b955 to 03772f7 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 79fb893 Oct 2, 2026
34 checks passed
@muscariello
muscariello deleted the feat/policy-evaluation-schema 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/M Denotes a PR that changes 200-999 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Persist a per-record, per-policy-version compliance verdict

3 participants