Skip to content

fix(collections): require a rule before a smart collection previews or saves - #3710

Merged
Quick104 merged 2 commits into
Silo-Server:mainfrom
blurbery:fix/smart-collection-no-rules
Nov 9, 2026
Merged

Quick104 merged 2 commits into
Silo-Server:mainfrom
blurbery:fix/smart-collection-no-rules

Conversation

@blurbery

@blurbery blurbery commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #3695
Related issue: #192
Validation tasks: unblocks #194 A2 (alongside #3694 and #3697). The passed #195 cases (W1, W2, W3, W6) are unaffected, and so is #195 W4: a rule set that matches nothing still saves.

A smart collection could be previewed, created and saved with no rules. Empty rule groups match every title, so the live preview filled with the whole catalog and Create collection went through, leaving a "smart" collection that holds everything. Removing the last rule from a saved smart collection could be saved too, because the save bar's saveBlockedReason never looked at the rules. The API accepted the same thing from any client. This affects server smart collections (Admin > Libraries > Collections) and personal ones (Collections), which share the editor.

With this change a smart collection needs at least one complete rule: a field, an operator and a value. The editor won't preview or save without one, and /api/v2 refuses it.

Approach

Web editor. hasCompleteRule in web/src/lib/collections/draft.ts decides whether a rule can filter. A blank value, a missing value or a range with an empty end doesn't count, so a fresh "Genre is …" row doesn't satisfy it. Numbers (including 0) and booleans (including false) do. While no rule is complete:

  • the preview query is disabled and the pane shows "Add a rule to see what it matches" instead of the last result;
  • Create collection stays off and the save bar says "Add a rule, then create it." (after the name and library hints, in the order the page asks for them);
  • on a saved collection, Save stays off with "Add at least one rule.", the same way it already works for a missing name.

The rule builder's empty-state line for collections no longer says the collection holds every title.

API. QueryDefinition.HasCompleteRule in internal/catalog mirrors the web check. createAdminCollection, updateAdminCollection, createCollection and updateCollection refuse a smart collection without a complete rule with 422 validation_failed at body.query_definition.groups. An update is checked when it sends a smart collection's query_definition, or when it switches a collection to smart (the stored rules become live).

What I deliberately left alone:

  • Collections already saved with no rules keep listing and playing. API updates that don't send rules still work on them, and so do the list's pin and visibility toggles. The editor is different: it always sends the rules when it saves a smart collection, so opening one of these and changing only the title still needs a rule before Save turns on. That is the issue's "the next save of one must add a rule", and it stops anyone saving the whole-catalog rules again by accident.
  • /api/v1 is frozen, so its create and update routes keep accepting empty rules. They share the service code, and the check is skipped when V1Rules is set.
  • Backup restore doesn't run the check, so a backup holding an old empty smart collection still restores.

The user manual on siloserver.org already tells people to add rules and never says an empty rule set is allowed, so I haven't opened a manual issue. The API and editor docs are updated in docs/catalog-api.md (rule section), docs/collections-api.md and docs/architecture/collection-editor.md.

Overlap: #3694 (creating a server smart collection without picking libraries) and #3696 (where the editor goes after Save/Create) are being fixed in parallel PRs and touch the same lines in CollectionEditor.tsx (canCreate, createHint and saveBlockedReason). Whichever lands second will need a small merge there.

Client impact: silo-apple only creates manual collections. silo-android still has a CreateCollectionSheet with a Smart option, and its shared CollectionRepository.createCollection would send collection_type: "smart" with no query_definition, which now gets the 422. Nothing in the app calls either of them today, so nothing breaks for users. If that sheet comes back, it has to send a rule and show the query_definition.groups error against the rules. jellycompat only reads collections, so it needs no change.

Validation

  • Go: go test ./internal/catalog/ -run 'TestQueryDefinitionHasCompleteRule|TestQueryDefinitionValidation' passed. go test ./internal/api/handlers/ (whole package) passed. go test ./internal/apiv2/ -run Collection passed. None of the new tests need Postgres.
  • Web: vitest run over the collections pages, src/lib/collections, src/components/collections, src/components/homeRows and AddToCollectionDialog: 56 files, 534 tests passed. eslint on the changed files and tsc --noEmit were clean.
  • The two smart-create goldens (adminSmartCreate, personalSmartCreate) now carry a Genre rule, since a create with no rules is no longer possible from the editor. The editor tests add that rule through the UI.
  • Full lint, the full Go suite and the DB contract jobs run on CI. I didn't run them locally.
  • Benchmarks: not applicable, this is a validation change with no performance impact.

Evidence

Evidence: https://evidence.siloserver.org/r/silo-server/pr-3710/

Surface and build: web, personal smart collection editor (Collections > New collection > Smart), desktop (1440 px) and phone (390 px) width. Before is upstream main at 4018816da, after is this branch. Both ran against a mocked /api/v2 with made-up titles. Server smart collections use the same editor and gating.

Risks

  • Clients that create smart collections with no rules through /api/v2 now get a 422. That is the point of the change. The web editor no longer sends such a request.
  • Existing empty smart collections still hold the whole catalog until someone edits them and adds a rule. The docs say how the check treats them. Release notes may want to mention them so admins can find and fix them.
  • The check makes sure a rule is filled in, not that it narrows anything. A complete rule that still matches nearly everything, such as Duration is at least 0 (number fields start at 0), passes, and the live preview shows that. Changing the rule defaults would also affect Home rows, so I've kept it out of this PR.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • Each test this pull request adds meets "Decide what to test" in CONTRIBUTING.md.
  • The Evidence section shows every change a user can see, or says there is none.

AI Disclosure

  • Harness: Claude Code
  • Tool(s): Claude Code, GitHub CLI, Git
  • Model(s): claude-opus-5-5
  • Involvement: AI-assisted. AI-assisted with Claude Opus. I directed the task and designed the work.
  • Adversarial review: a separate Claude Code subagent (claude-opus-5-5) reviewed the full diff for agreement between the web and server checks, v2 paths still left open or wrongly refused, stale previews, accessibility and docs. It found no blocking defects. From its findings I added the web hasCompleteRule cases, a stricter preview assertion, a test row for the list toggle on an old empty collection, and a doc fix. The editor behaviour for old empty collections and the rule-default limitation are described above.

Note

Require a complete rule before Smart collection preview or save

  • Adds a QueryDefinition.HasCompleteRule check in query_definition.go and an equivalent hasCompleteRule draft helper in draft.ts. A rule is complete when it has a nonblank field, operator, and filled value (zero, false, and numbers count as filled).
  • Server-side validation rejects admin and personal Smart collection creates and updates that lack a complete rule, via requireSmartCollectionRule and adminSmartRuleCheck in collections.go.
  • The editor blocks Create and Save and skips preview requests while a Smart draft has no complete rule, showing a rule-required prompt and hints.
  • Behavioral Change: /api/v1 requests, updates that leave existing rules unchanged, and backup restore bypass the new validation. Clients that previously saved Smart collections without rules now get a validation error at query_definition.groups.

Macroscope summarized 3a73765.

…r saves

A smart collection with no complete rule matched every title: the editor
previewed the whole catalog and let it be created or saved, and the
/api/v2 create and update operations accepted it.

The editor now sends no preview, asks for a rule, and keeps Create and
Save off until one rule has a field, an operator and a value. The v2
admin and personal create and update operations answer 422 at
body.query_definition.groups for a smart collection left without one.
Updates that leave the rules alone, /api/v1 and backup restore are
unchanged, so collections saved before the check keep working.

Closes Silo-Server#3695
Add web cases for hasCompleteRule matching the server's, check that the
editor previews only the complete rule, and cover a list toggle on a
smart collection saved with no rules. Say in the API doc that only the
admin update can switch a collection to smart.
@silo-triage silo-triage Bot added the size/L 200–499 added + deleted lines (all files); change volume, not priority. label Nov 9, 2026
@silo-kody

silo-kody Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Code Review Could Not Complete ⚠️

The review failed before suggestions could be generated.

Reason: Transient error reaching the provider (openai_compatible). Try again.

openai_compatible · gpt-6.1-terra · HTTP 503 · generalist

Provider said: auth_unavailable: no auth available (providers=codex, model=gpt-6.1-terra; last upstream error: unauthorized: token: [REDACTED] "type": "invalid_request_error", "param": null, "code": "refresh_token_expired" } })

After fixing the issue, comment @kody review on this PR to re-run the review.

Silo Kody — review incomplete

This review did not finish. Resolve the error reported above, then retry with @kody start-review in a PR comment. Treat any comments already posted as a partial review.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Validate Business Logic: Ask Kody to validate your code against business rules by adding a comment with the @kody -v business-logic command.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

Access your configuration settings here.

​

@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Smart collections now require at least one complete rule. The API rejects qualifying create and update requests without one, and the web editor withholds preview, creation, and saving until a rule is complete.

Changes: Smart collection rule validation

Layer / File(s) Summary
Complete-rule checks
internal/catalog/query_definition.go, internal/catalog/query_definition_test.go, web/src/lib/collections/draft.ts, web/src/lib/collections/draft.test.ts
Server and web helpers recognize a rule with a nonblank field and operator and a filled value. Tests cover blank values, ranges, zero, and false.
API enforcement and documentation
internal/api/handlers/collections.go, internal/api/handlers/collections_service.go, internal/api/handlers/collections_lifecycle_service.go, internal/api/handlers/admin_collections_service.go, internal/api/handlers/admin_collections_service_test.go, docs/architecture/collection-editor.md, docs/catalog-api.md, docs/collections-api.md
Admin and personal collection API paths validate rules for applicable Smart collection creates and updates. Tests cover rejected and accepted cases. Documentation describes the validation error and exceptions.
Editor preview and save behavior
web/src/components/collections/editor/CollectionEditor.tsx, web/src/components/collections/editor/CollectionPreviewPane.tsx, web/src/components/homeRows/addRow/RuleBuilder.tsx, web/src/lib/collections/copy.ts, web/src/lib/collections/scope.test.ts, web/src/pages/CollectionEditorPage.smart.test.tsx, web/src/pages/CollectionForms.goldens.test.tsx, web/src/pages/collectionEditorPage.test-support.tsx, web/src/test/fixtures/collectionBodies.ts
The editor blocks preview, Create, and Save until a complete rule exists, and displays rule-specific guidance. Related tests and request fixtures now include complete rules.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Severity of issue fixed: Medium

Possibly related issues

  • Issue 195: The PR adds rule-completeness checks to personal Smart collection authoring in the web editor.
  • Issue 194: The PR implements admin Smart collection rule validation for creation and updates.
  • Issue 192: The PR implements Smart collection behavior tracked by this parent acceptance issue.

Possibly related PRs

Suggested reviewers: quick104

Merge Risk: Minimal · up to 3a737

Smart collection rule validation is ready to merge after normal checks; the reported editor/API mismatch does not affect the supported editing workflow.

Security architecture risk: Low · up to 3a737

The change tightens Smart collection creation and editing without demonstrated expansion of access. The main design risk is compatibility with clients that previously submitted empty rules. Remaining coverage is incomplete.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The inspected change affects which collection definitions existing callers may persist, not which identities may administer collections. Personal updates retain profile visibility and creator ownership checks; admin operations retain acting-admin classification. No broader library or cross-account access path was demonstrated.

Trust Boundaries and Controls

  • observed — Untrusted v2 admin bodies are converted into explicit service commands before reaching validation and persistence. The converters neither expose nor assign V1Rules, so a v2 client cannot select the legacy exemption through these request fields.

Resilience and Maintainability Implications

  • observed — The new rejection path is pure validation before mutation, so repeated invalid requests do not create partial collection state. Existing v2 revision guards and transactional updates continue to contain concurrent writes. Artwork processing can fail after primary persistence, but that partial-failure behavior predates this PR and is not reached when the new gate rejects a request.
Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title "fix(collections): require a rule before a smart collection previews or saves" matches the main change. The PR adds a complete-rule requirement before Smart collections preview, create, or s…
Linked Issues check Passed The changes address #3695. hasCompleteRule and QueryDefinition.HasCompleteRule require a field, operator, and filled value. The editor disables preview, Create, and Save until a complete rule exis…
Out of Scope Changes check Passed The changed production code, tests, and documentation support #3695. The editor copy and preview changes implement the required user feedback. API documentation describes the new validation and its ex…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Description check Passed The PR description is specific and directly related to the changeset. It explains that smart collections need at least one complete rule, which matches the web editor, draft helpers, API validation, a…
Full details: Title check

Explanation

The title "fix(collections): require a rule before a smart collection previews or saves" matches the main change. The PR adds a complete-rule requirement before Smart collections preview, create, or save, in both the web editor and the /api/v2 validation. The title is specific and concise. It does not mention the API 422 validation, but it does not need to cover every detail.

Full details: Linked Issues check

Explanation

The changes address #3695. hasCompleteRule and QueryDefinition.HasCompleteRule require a field, operator, and filled value. The editor disables preview, Create, and Save until a complete rule exists. The collection handlers enforce the same rule for /api/v2 personal and admin create and update paths. The changes preserve existing empty collections and exclude /api/v1 and backup restore as documented. Tests cover empty, incomplete, numeric, boolean, range, personal, admin, and editor cases.

Full details: Out of Scope Changes check

Explanation

The changed production code, tests, and documentation support #3695. The editor copy and preview changes implement the required user feedback. API documentation describes the new validation and its exceptions. Test fixture updates keep existing collection tests valid. No unrelated production behavior is demonstrated.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 12 functions across 18 files. (3 skipped: 3 unsupported.)

Full details: Description check

Explanation

The PR description is specific and directly related to the changeset. It explains that smart collections need at least one complete rule, which matches the web editor, draft helpers, API validation, and docs in the raw summary. It also names the affected files, endpoints, and tests, and it lists the intended exceptions (/api/v1, backup restore, existing empty collections).

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 71134bd3-4e4a-433a-95da-a6965073eaba
Commits

Reviewing files that changed from the base of the PR and between 4018816 and 3a73765.

Files selected for processing: 21

  • docs/architecture/collection-editor.md
  • docs/catalog-api.md
  • docs/collections-api.md
  • internal/api/handlers/admin_collections_service.go
  • internal/api/handlers/admin_collections_service_test.go
  • internal/api/handlers/collections.go
  • internal/api/handlers/collections_lifecycle_service.go
  • internal/api/handlers/collections_service.go
  • internal/catalog/query_definition.go
  • internal/catalog/query_definition_test.go
  • web/src/components/collections/editor/CollectionEditor.tsx
  • web/src/components/collections/editor/CollectionPreviewPane.tsx
  • web/src/components/homeRows/addRow/RuleBuilder.tsx
  • web/src/lib/collections/copy.ts
  • web/src/lib/collections/draft.test.ts
  • web/src/lib/collections/draft.ts
  • web/src/lib/collections/scope.test.ts
  • web/src/pages/CollectionEditorPage.smart.test.tsx
  • web/src/pages/CollectionForms.goldens.test.tsx
  • web/src/pages/collectionEditorPage.test-support.tsx
  • web/src/test/fixtures/collectionBodies.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@Quick104

Quick104 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Review of 3a73765

I found no defects in this PR, so I didn't post a line-by-line review or push any commits. One product question is left for maintainers, below.

What I checked

  • Two tests from this PR fail on main at the expected point: "personal: previews nothing and waits for a rule before Create" and "server: won't save once its last rule is removed" (Create and Save aren't disabled there). The remaining web test changes fail on main only because they use helpers this PR adds. All 91 tests in the touched web files pass on this head.
  • The new Go handler test calls adminSmartRuleCheck, so it doesn't build on main. I ran a throwaway test through CreatePersonalCollection and UpdatePersonalCollection instead. On main, a smart create with {"groups":[]} gets past validation to the store write. On this head it's refused at query_definition.groups, and an update that sends a complete rule still saves.
  • /api/v1 is unchanged. Every v1 collection create and update route sets V1Rules, and no /api/v2 adapter does. Backup restore doesn't run the check, and nothing else in the server creates smart collections.
  • /api/v2 answers 422 validation_failed at body.query_definition.groups through collectionProblem, which matches docs/catalog-api.md and docs/collections-api.md.
  • The web hasCompleteRule and Go HasCompleteRule agree on every value the rule editors produce: blank text, half-filled ranges, 0, false, empty selects and "in the last" values.
  • A second review by a different model (Codex, gpt-6.1-sol) found nothing beyond the two points below.

Open for maintainers

  1. Maintainer decision: smart collections narrowed only by Max titles or Movies/Shows. The smart editor also offers Max titles and the Movies/Shows picker. A rule-less "newest 20 movies" collection (newest first, Max titles 20) could be saved before this PR and is refused now. The options are to keep the PR as it is, so that collection needs a rule such as "Added in the last …", or to count a set Max titles as enough in both hasCompleteRule and HasCompleteRule. [bug] A smart collection can be previewed and saved with no rules, and then matches every title #3695 asks for at least one rule, so the PR follows the issue as written.
  2. Number rules start complete. Picking a number field sets its value to 0, so "IMDb rating is at least 0" passes the check and matches nearly everything. The PR's Risks section already covers this, and changing the default would also change Home rows. I'd leave it out of this PR.

Validation

I corrected the Validation tasks: line in the body: "#195 smart authoring" isn't a case ID. This PR unblocks #194 A2 together with #3694 and #3697. The passed #195 cases (W1, W2, W3, W6) are unaffected. W2 is manual-only, and the manual create and save paths don't change.

User manual, clients, jellycompat

  • The collections pages on siloserver.org already say to add rules for a smart collection and never say rules are optional, so no manual issue is needed.
  • Apple only creates manual collections. Android's smart-collection create sheet isn't reachable in the app. I agree with the PR body that neither app needs a paired change now. jellycompat only reads collections.

Overlap: #3711 changes the createHint lines in CollectionEditor.tsx that this PR also changes, and #3709 edits the same file. Whichever merges second will need a small conflict fix.

Checks: CI is green. The Kody check failed because Kody couldn't reach its model provider. It didn't review the code, and the failure isn't caused by this branch.

Fit: 8/10 · Readiness: 9/10, conditional

  • Fit: it fixes a blocking bug a maintainer reported, at the editor and at the /api/v2 write paths, and leaves /api/v1 and restore alone. It scores below 9 because it also refuses limit-only smart collections that people made on purpose (decision 1).
  • Readiness: tests that fail on main cover both the web and server behavior, and I found no defects. Once decision 1 is settled, the PR is ready.

🤖 Claude Code (claude-opus-5-5)

@Quick104
Quick104 merged commit 26062f8 into Silo-Server:main Nov 9, 2026
19 of 20 checks passed
This was referenced Nov 10, 2026
Quick104 added a commit that referenced this pull request Nov 14, 2026
Resolve the conflicts with main's changes since d941225:

- Query normalization moved to catalog.NormalizeQueryDefinitionJSON;
  structural admission moves with it, so backup restores also pass it.
- /api/v1 is retired (#3200): drop the v1-only item loaders and doc text
  and the V1Rules test field.
- The web collections pages are translated (#3902): "Count unavailable"
  and the manual editor's add-blocked messages become catalog keys
  (synced locally until the silo-translations change merges).
- Server smart rules name their own libraries (#3711): a server save
  sends query_definition only when the rules change.
- Save returns to the list (#3709) and smart collections need a rule
  (#3710): adjust the PR's tests to observe what they still can.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L 200–499 added + deleted lines (all files); change volume, not priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] A smart collection can be previewed and saved with no rules, and then matches every title

2 participants