Skip to content

feat(search): support negating a RecordQuery for exclude filters - #1983

Merged
akijakya merged 2 commits into
mainfrom
feat/record-query-negate
Aug 6, 2026
Merged

akijakya merged 2 commits into
mainfrom
feat/record-query-negate

Conversation

@akijakya

@akijakya akijakya commented Aug 6, 2026 •

Copy link
Copy Markdown
Member

Summary

  • add bool negate to RecordQuery so SearchService clients can express "does NOT match X" alongside the existing "matches X"
  • for joined/multi-valued fields (skills, domains, modules, locators, annotations), compile exclusions to a correlated NOT EXISTS subquery rather than negating the include-path JOIN — negating the JOIN would wrongly resurrect a record via an unrelated matching row (e.g. excluding skill "nlp" on a record with skills [nlp, python] must still exclude it, not match it via the "python" row)
  • for nullable scalar columns (description, schema_version, oasf_created_at, authors), use a NULL-safe NOT(...) that retains NULL rows instead of silently dropping them
  • for boolean query types (VERIFIED, TRUSTED, SCAN_SAFE), flip the parsed bool rather than adding new storage
  • regenerate the Go/gRPC API stubs and add database, utils, and controller test coverage

Motivation

RecordQuery could only express inclusion — "records that match value X". There was no way to express exclusion — "records that do NOT match value X" — which blocked tri-state filtering (include / exclude / off) in clients built on SearchService.

Compatibility

Default negate: false preserves current behavior exactly (wire- and behavior-compatible) — this is an additive, backward-compatible change.

Validation

  • task lint
  • task test

Closes #1940

@akijakya akijakya self-assigned this Aug 6, 2026
@akijakya
akijakya requested a review from a team as a code owner August 6, 2026 09:18
@github-actions

github-actions Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

The latest Buf updates on your PR. Results from workflow Buf CI / verify-proto (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed⏩ skipped⏩ skipped✅ passedAug 6, 2026, 12:27 PM

@github-actions github-actions Bot added the size/M Denotes a PR that changes 200-999 lines label Aug 6, 2026
@akijakya
akijakya force-pushed the feat/record-query-negate branch from d42933b to 4423880 Compare August 6, 2026 09:19
@codecov

codecov Bot commented Aug 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 23.11321% with 163 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
server/database/gorm/record_exclude.go 9.0% 100 Missing and 11 partials ⚠️
server/types/search.go 0.0% 51 Missing ⚠️
server/database/utils/utils.go 96.9% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Exclusion SQL for skills/domains/modules incorrectly AND-combines excluded IDs and names, which can fail to exclude records that match either criterion.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.

Pull request overview

Adds first-class “exclude” (negated) filtering to SearchService by introducing a negate flag on RecordQuery, routing negated queries into dedicated RecordFilters.Excluded fields, and compiling those exclusions to SQL using NOT EXISTS for joined/multi-valued fields and NULL-safe negation for nullable scalar columns.

Changes:

  • Extend RecordQuery (proto + generated stubs) with bool negate and update query-to-filter routing to populate exclusion filters.
  • Add SQL helpers for NOT EXISTS and NULL-safe negation; implement GORM exclusion compilation via a new record_exclude.go.
  • Add/extend unit and integration tests covering excluded filters and NULL-survival semantics.
File summaries
File Description
server/types/search.go Adds ExcludedRecordFilters and new Without* filter options to represent negated filters.
server/database/utils/wildcard.go Introduces shared SQL builders for NOT EXISTS and NULL-safe negation.
server/database/utils/wildcard_test.go Adds unit tests for the new SQL builder helpers.
server/database/utils/utils.go Routes negated RecordQuery entries into Without* options and flips boolean query types under negation.
server/database/utils/utils_test.go Adds tests ensuring negated queries populate RecordFilters.Excluded and booleans flip correctly.
server/database/gorm/record.go Hooks exclusion compilation into the main query builder (handleFilterOptions).
server/database/gorm/record_exclude.go Implements exclusion SQL compilation (NOT EXISTS for joins, NULL-safe NOT for nullable scalars).
server/database/gorm/record_exclude_test.go Adds a white-box test to ensure negated description retains NULL rows.
server/database/database_test.go Adds DB-level tests validating negated filters (skills, scalar fields, annotations, severities, NULL authors/description).
server/controller/search_test.go Adds controller test ensuring negated queries are forwarded into excluded filters.
proto/agntcy/dir/search/v1/record_query.proto Adds the negate field to the API contract.
api/search/v1/record_query.pb.go Regenerated Go stubs to include the new Negate field/getter.
Review details

Files not reviewed (1)

  • api/search/v1/record_query.pb.go: Generated file

Suppressed comments (2)

server/database/gorm/record_exclude.go:195

  • The module exclusion subquery currently ANDs the module_id and name predicates together. If both Excluded.ModuleIDs and Excluded.ModuleNames are set, this will fail to exclude records that match either one independently; the match predicates should be OR-combined inside the correlated NOT EXISTS.
	inner := strings.Join(conditions, " AND ")

	return query.Where(utils.BuildNotExistsCondition("modules", "ex", inner), args...)

server/database/gorm/record_exclude.go:171

  • The domain exclusion subquery currently ANDs the ID condition with the name condition, which means a record is only excluded when a single domain row matches both constraints simultaneously. When both excluded IDs and excluded names are present, this should exclude records matching either constraint (ID OR name) while still correlating on record_cid.
	inner := strings.Join(conditions, " AND ")

	return query.Where(utils.BuildNotExistsCondition("domains", "ex", inner), args...)
  • Files reviewed: 11/12 changed files
  • Comments generated: 2
  • Review effort level: Lite

We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.

Comment thread server/database/gorm/record_exclude.go Resolved
Comment thread server/database/database_test.go Resolved
@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 Aug 6, 2026

@muscariello muscariello left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@akijakya
akijakya merged commit 562b40f into main Aug 6, 2026
34 checks passed
@akijakya
akijakya deleted the feat/record-query-negate branch August 6, 2026 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes 1000-1999 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SearchService: support negating a RecordQuery (exclude filters)

3 participants