Skip to content

feat(server): declare indexed_at for operation policies - #2447

Merged
tkircsi merged 5 commits into
agntcy:mainfrom
paoValle:feat/operation-indexed-at
Nov 4, 2026
Merged

tkircsi merged 5 commits into
agntcy:mainfrom
paoValle:feat/operation-indexed-at

Conversation

@paoValle

@paoValle paoValle commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Closes #2428

OperationInput.indexed_at was declared in the proto (field 6) but neither in the operation CEL environment nor populated, so no operation policy could read it — the age half of #2128's prune rule could not be written.

This declares it as an optional timestamp and populates it at index, the one operation that decides while holding a row: the reconciler reads records.created_at for the record before writing the new row, so a record that was already indexed answers the variable and a first index leaves it absent. IndexedAt(t) is a DecideOption beside FromPeer, so prune can supply the same value when it starts deciding through a policy, without a new API. The lookup is skipped entirely when no policy is configured at index, as readsVerdicts already does for the verdict map.

Absence stays representable, so a policy writes indexed_at.orValue(...) or tests hasValue() rather than comparing against a 1970 timestamp. The docs now say which operations answer it: push and autosync decide from the record alone and never read its row, so they leave it absent even for a record already indexed; index answers it except on a first index. That gap is why this is not the whole of #2428 — prune does not decide through a policy yet, and wiring it is the increment where the motivating rule becomes writable.

go test ./... passes in server and reconciler; the CEL, OPA and indexer tests cover both the present and the absent case.

@paoValle
paoValle requested a review from a team as a code owner November 2, 2026 11:55
@github-actions github-actions Bot added the size/M Denotes a PR that changes 200-999 lines label Nov 2, 2026
OperationInput.indexed_at was in the proto but absent from the operation CEL
environment and never populated, so no operation policy could read it.

Declare it as an optional timestamp and populate it at index, the one operation
that holds a row: the reconciler reads records.created_at before it writes the
row, so a record seen before answers the variable and a first index leaves it
absent. push and autosync decide before any row exists and stay absent, as does
prune, which does not decide through a policy yet. Absence is representable, so
a policy reads it with hasValue()/orValue() instead of comparing against 1970.

IndexedAt(t) is a DecideOption beside FromPeer, so prune can supply the same
value when it starts deciding without a new API.

Signed-off-by: Paolo Valletta <[email protected]>
@paoValle
paoValle force-pushed the feat/operation-indexed-at branch from 2657a00 to d90b1fc Compare November 2, 2026 12:00
@tkircsi

tkircsi commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Thanks @paoValle for this — it's a thoughtful piece of work, and I appreciate that the description is explicit about what it does and doesn't unlock. A few notes below, mostly small.

I pulled the branch and confirmed it builds and tests green across server, reconciler, client, and cli.

Things I particularly liked: reading records.created_at means indexed_at in a policy agrees exactly with what prune.older_than already filters on via WithIndexedBefore, so the two layers can't drift apart. And TestOperationEnv_RequiresUnwrappingIndexedAt is a great call — asserting that indexed_at < now - duration('168h') fails to compile turns "no policy can mistake an absence for 1970" into a load-time guarantee rather than a convention we have to remember.

One thing I'd like changed before merge

FromPeer now writes through input.Context.SourcePeerId directly, where previously it received a *OperationContext that was guaranteed non-nil by its signature. Nothing is broken today, since operationInput is the only caller that applies options and it always sets Context — but pullInput builds an input without going through the option loop, so whoever adds options there next gets a nil dereference. Could we add a if input.GetContext() == nil { return } guard to keep that safe by construction?

Smaller suggestions, take or leave

  • withIndexedAt(cid string, opts *[]policy.DecideOption) error — would returning ([]policy.DecideOption, error) read more naturally than the pointer-to-slice out-param?
  • GetRecordIndexedAt on the wide SearchDatabaseAPI is what forced the three test fakes to grow a stub. The prune task declares a narrow local recordIndex interface for this reason; a similar narrow interface in the indexer would follow that grain and keep the blast radius smaller. Happy to defer if you'd rather not churn it.
  • The GetRecordIndexedAt doc says "first indexed", which is very slightly stronger than reality: a record removed by prune and later re-indexed gets a fresh created_at. A half-sentence noting that would be worth having.
  • Could you drop docs/mkdocs/uv.lock from the PR? That certifi bump also landed in chore(deps): bump certifi from 2026.5.20 to 2026.10.18 in /docs/mkdocs #2446, which is already on main, so it will conflict and it isn't related to the feature.
  • Entirely optional, since you're already editing those lines: the operationAsInput doc block in server/policy/opa.go sits above verdictsAsInput, so the two comments are run together. That's pre-existing on main, not something you introduced, but it'd be a cheap fix while you're in there.

On the scope

I want to think out loud about this rather than ask you to change anything, because I think your framing is right and I'd rather the reasoning be written down.

At index the variable is only populated when a row already exists, but indexRecord runs only for tags new since the last snapshot, and AddRecord is a no-op when the row is there. So in practice indexed_at is answered only on a post-restart re-walk, and an age rule at index can only refuse work that was already going to be a no-op — turning it into a failedCount and an error log. The acceptance criterion in #2428 is met literally, but the motivating rule stays unwritable until prune decides through a policy, exactly as you say.

My conclusion is that this should still land now rather than wait for the prune increment. IndexedAt as a DecideOption and GetRecordIndexedAt are precisely what prune will need, the absence semantics are the part that's easy to get wrong and you've got them right and covered by tests, and the docs are honest about which operations leave the value absent. Folding it into prune later would just mean reviewing the same code twice.

Thanks again — nice work.

pullInput builds an OperationInput without going through the option loop, so
any option added there later would dereference a nil Context. Guard it and pin
it with a test.

Signed-off-by: Paolo Valletta <[email protected]>
…ay what a re-indexed row reports

Signed-off-by: Paolo Valletta <[email protected]>
@paoValle

paoValle commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the careful read, and for pulling the branch.

9401b4e4 adds the guard you asked for — FromPeer returns early when input.GetContext() is nil, with TestFromPeer_NoContextDoesNotPanic pinning it (an input built without the option loop, which is what pullInput produces today), so the next option added there cannot turn into a nil dereference. The other two commits are your take-or-leave list: 9795a6e4 makes indexedAtOption return []policy.DecideOption instead of writing through a pointer-to-slice, 3ad7abc2 says in GetRecordIndexedAt that a pruned-then-re-indexed record gets a fresh row, splits the run-together operationAsInput/verdictsAsInput doc blocks, and drops docs/mkdocs/uv.lock from the PR — you were right that it would conflict with #2446.

One thing I did not take: the narrow interface in the indexer. Task holds a types.SearchDatabaseAPI that the reconciler already receives, so narrowing GetRecordIndexedAt means a new local interface plus a second field for one method; I'd rather not churn it in this PR and would happily do it separately if you want the column kept off the wide interface.

On the scope: I read it the same way you do, and the doc says which operations leave the value absent rather than implying it is always there — the motivation stays with prune, as the acceptance criterion in #2428 is only met literally until then.

go test ./policy/ ./database/gorm/ (server) and ./tasks/indexer/ (reconciler) are green on 3ad7abc2, gofmt -l empty.

@tkircsi tkircsi 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.

Thanks for the quick turnaround — this is nearly there. The guard is exactly what I was after, and TestFromPeer_NoContextDoesNotPanic pins it at the right level: an input built outside the option loop, which is precisely the shape pullInput produces. The indexedAtOption signature reads much better than the out-param, the uv.lock revert is right, and the "current row's time and not necessarily the first one" wording on both GetRecordIndexedAt and the interface method is the honest version.

Thanks again for the care on this one.

Comment thread reconciler/tasks/indexer/task.go Resolved
Comment thread server/policy/opa.go Resolved
@codecov

codecov Bot commented Nov 3, 2026

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Patch % Lines
reconciler/tasks/indexer/task.go 80.0% 2 Missing ⚠️
server/database/gorm/record.go 85.7% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@paoValle

paoValle commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

indexRecord's doc comment is re-attached in c033009e. The blank line between the operationAsInput and verdictsAsInput blocks was already there when you read the diff — it went in with the commit that split the two blocks.

@tkircsi tkircsi 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.

Approving — thanks for seeing this through, and for the patience with my nitpicking on comment placement.

Nice work — this was a pleasure to review.

@tkircsi
tkircsi added this pull request to the merge queue Nov 4, 2026
Merged via the queue into agntcy:main with commit 740f54b Nov 4, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Denotes a PR that changes 200-999 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Give operation policies indexed_at so an age condition can be written

2 participants