Skip to content

feat(search): filter by project and by the caller's project role - #454

Merged
M0Rf30 merged 2 commits into
openartifact:mainfrom
orcamedia:feat/search-project-filters
Nov 9, 2026
Merged

M0Rf30 merged 2 commits into
openartifact:mainfrom
orcamedia:feat/search-project-filters

Conversation

@orcamedia

Copy link
Copy Markdown
Contributor

Closes #445, with the names and rules agreed there.

GET /api/v1/search gains two repeatable filters, usable as parameters or as qualifiers in q:

Parameter Qualifier Keeps
project project:NAME repositories of these projects (name or slug)
project_role project_role:ROLE repositories of the projects where the caller's effective role is one of these (viewer, developer, maintainer, owner)

project_role=viewer is the "Shared with me" preset; project_role=developer&project_role=maintainer&project_role=owner is "mine".

Behaviour

  • Narrowing only. Both filters shrink the readable repository set (narrowToProjects in internal/search/project_filter.go) before SearchPage and SearchRepositories run, so they can't add access, and the repository hits honour them too. The readable set may be cached and shared, so the narrowed one is a new map.
  • Effective role = what the projects API reports as my_role: auth.Evaluator.ProjectRoles (ownership, membership, group grants; nothing for anonymous, deploy and scoped tokens) plus owner everywhere for a global admin via Evaluator.ProjectRole. For an admin, project_role=viewer returns nothing, deliberately, and is tested.
  • No probing. A repository is kept only where the caller has a role in its project. A project that doesn't exist and one the caller has no role in give byte-identical empty answers (tested), even when the caller can read that project's repositories through a permission target.
  • Repositories readable only through a permission target or anonymous access, and the system project (which grants nothing), never match a project filter.
  • An unknown project_role is 400 invalid_filter (a typo shouldn't look like "no results"). Values per filter are capped at 50 like format and repo.
  • query_too_broad suggests project:<project> when no project filter is used.
  • checksum lookups ignore the project filters, as they already ignore q, format, repo, group and flagged; the parameter description says so.
  • A bare role: stays free text (tested), as you suggested.

OpenAPI (parameters, q description), the generated web client, docs/reference/api/search.md and openapi.json.gz are updated.

Tests

internal/search/project_filter_integration_test.go (integration, Postgres, real auth.Evaluator). Callers: an owner of one project and viewer of another who also reads a third project's repository through a permission target and the system project through another; a user with only a permission target; a developer through a group grant; a global admin; anonymous. 18 cases cover parameters and qualifiers, "shared with me", "mine", both filters together, group grants, the system project, the admin rules, repository hits, and the identical answers for a missing and a foreign project. I checked that removing the "no role, no match" rule or the admin-as-owner rule makes them fail.

Unit tests: the qualifiers and role: in parser_test.go, 400 invalid_filter for unknown roles, project:<project> in narrow_by.

Checked locally (macOS, Postgres 18): make check, cmd/docgen -check, bun run typecheck, the unit suite with -race, the slim suite, and go test -tags integration ./internal/search/.

Not in this PR

  • Web UI: a project select and a "Shared with me" preset on the Packages page, and project: in the palette's "too many matches" hint (shell.palette.tooBroad, in every locale). I'd send that as a follow-up to keep this one about the API. Or would you rather have it here?
  • oa search passes q through, so oa search readme project:acme already works. Dedicated --project / --project-role flags could come with the UI follow-up.

🤖 Generated with Claude Code

@M0Rf30 M0Rf30 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The authorization side holds up: the filter only narrows the readable set, the cached map is copied rather than mutated, missing and foreign projects answer identically, admin-as-owner matches the projects API, and the system project grants nothing. Changes requested:

1. MCP search ignores the new qualifiers. internal/mcp/handlers.go:220 calls search.ParseQuery, which now consumes project: and project_role:, but the MCP tool never applies filter.Projects / filter.ProjectRoles. Before this PR lodash project:acme matched nothing; now it returns lodash from every readable repository, so the result looks scoped but isn't. Its query_too_broad message (search.TooBroadMessage) also suggests project:<project>, which MCP can't honour. Apply the same narrowing on the MCP path (preferred), or reject the qualifiers there and keep them out of its hints.

2. Docs still describe the old filter set.

  • the narrow_by schema description in internal/api/openapi.json (and the generated docs/reference/api/schemas.md and web/src/api/generated/types.gen.ts) still says name:, format:, repo: in q, flagged=true;
  • docs/integrations/api.md:203 lists only q, format, repo: add project and project_role;
  • docs/getting-started/upgrading.md: an entry for the new parameters.

3. Cost (smaller). Every filtered request reads all repositories in all non-system projects, uncached, even for a caller with roles in two projects. Bound it with r.project_id = ANY(...) of the caller's projects for non-admins (and by name/slug when project= is set). ProjectRole(ctx, p, "") also loads ProjectRoles and discards it, then narrowToProjects loads it again; one Evaluate(..., "manage") plus one ProjectRoles is enough.

4. Tests (smaller). Missing cases: scoped token, deploy token, service account and legacy principal (the docs make claims about each); more than 50 project / project_role values -> 400; the cached readable map unchanged after a filtered request.

Nit: callers with no project role (anonymous, deploy tokens) still get the project:<project> hint on query_too_broad, though any project filter returns nothing for them.

orcamedia added a commit to orcamedia/openartifact that referenced this pull request Nov 9, 2026
Review of openartifact#454:

- MCP: search_artifacts parsed project: and project_role: away but never
  applied them, so a filtered query searched every readable repository.
  The narrowing is now search.ProjectScope, shared by the HTTP handler and
  the MCP server (Server.SetProjectScope); without a scope a project-filtered
  MCP search finds nothing.
- Cost: one Evaluate(manage) and one ProjectRoles per filtered request
  instead of ProjectRole plus ProjectRoles, and the repository query is
  bounded to the caller's projects (non-admins) and to the named projects
  (project=).
- query_too_broad suggests project:<project> only to callers that hold a
  project role, on both paths.
- Docs: narrow_by description (OpenAPI, generated schemas and web types),
  docs/integrations/api.md and an upgrade note.
- Tests: scoped token, deploy token (also of an admin), service account and
  legacy principal; more than 50 project / project_role values is 400; the
  cached readable set is unchanged after a filtered request; MCP narrowing,
  the missing-scope case and its hint.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@orcamedia

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review. All points addressed in the new commit:

  1. MCP. The narrowing moved into search.ProjectScope (NarrowToProjects, UsesProjects), which reads the caller from the context. The HTTP handler and search_artifacts both use it; the server installs it with Server.SetProjectScope next to the other MCP hooks in cmd/server/feature_mcp.go. Without a scope, a project-filtered MCP search finds nothing rather than everything. Tests in internal/mcp/search_projects_test.go: the qualifiers reach the scope and only kept repositories are searched, the missing-scope case, and the hint. Both fail if the MCP narrowing is removed.
  2. Docs. narrow_by description (OpenAPI, regenerated schemas.md and types.gen.ts), the /search row of docs/integrations/api.md, and an "Unreleased" entry in upgrading.md (it notes that project: / project_role: words in q are now filters, not text).
  3. Cost. One Evaluate(…, "manage") plus one ProjectRoles per filtered request. The repository query is bounded with r.project_id = ANY($n::uuid[]) of the caller's projects for non-admins, and by p.name/p.slug when project= is set. Anonymous callers and deploy tokens are excluded before any lookup, as ProjectRole does.
  4. Tests. Added scoped token, deploy token (also a deploy token of an admin), service account (its own project only; another membership is ignored) and legacy principal (owner everywhere, viewer finds nothing); more than 50 project / project_role values → 400 invalid_filter; and the cached readable set is unchanged after a filtered request (with WithAuthCache, an unfiltered search after a filtered one still sees everything; fails if the set is modified in place).

Nit: project:<project> is now only suggested to callers that hold a project role (ProjectScope.UsesProjects), on the HTTP and MCP paths.

One note: removing my explicit deploy-token exclusion doesn't change any result, because ProjectRoles already returns nothing for deploy tokens and Evaluate(manage) is false for them. I kept it so the admin flag matches ProjectRole exactly.

Checked locally: make check, unit suite with -race, slim suite, go test -tags integration ./internal/search/.

@M0Rf30 M0Rf30 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, all four points verified. Two non-blocking nits for a follow-up if you like: on the too-broad path with a project filter, UsesProjects resolves the caller's roles a second time (reuse the result of NarrowToProjects); and the MCP path has no 50-value cap on project:/project_role: (the HTTP one does).

@M0Rf30

M0Rf30 commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

#453 is merged, so this now conflicts in docs/getting-started/upgrading.md (both add the "Unreleased" section). Please rebase onto main and merge the two entries into one section; I'll merge once it's green.

orcamedia and others added 2 commits November 9, 2026 22:43
GET /api/v1/search takes project (name or slug) and project_role
(viewer, developer, maintainer, owner), both repeatable, and the
project: and project_role: qualifiers in q.

Both narrow the set of repositories the caller can already read, before
the package and repository queries run, so they never add access and the
repository hits honour them too. The role is the effective one the
projects API reports as my_role (auth.Evaluator.ProjectRoles, owner
everywhere for a global admin): project_role=viewer finds nothing for an
admin, by design. A repository is kept only where the caller has a role in
its project, so a project that does not exist and one without a role give
the same empty answer, and repositories readable only through a permission
target or anonymous access, or in the system project, never match.

An unknown project_role is 400 invalid_filter, and query_too_broad now
suggests project:<project>.

Closes openartifact#445

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review of openartifact#454:

- MCP: search_artifacts parsed project: and project_role: away but never
  applied them, so a filtered query searched every readable repository.
  The narrowing is now search.ProjectScope, shared by the HTTP handler and
  the MCP server (Server.SetProjectScope); without a scope a project-filtered
  MCP search finds nothing.
- Cost: one Evaluate(manage) and one ProjectRoles per filtered request
  instead of ProjectRole plus ProjectRoles, and the repository query is
  bounded to the caller's projects (non-admins) and to the named projects
  (project=).
- query_too_broad suggests project:<project> only to callers that hold a
  project role, on both paths.
- Docs: narrow_by description (OpenAPI, generated schemas and web types),
  docs/integrations/api.md and an upgrade note.
- Tests: scoped token, deploy token (also of an admin), service account and
  legacy principal; more than 50 project / project_role values is 400; the
  cached readable set is unchanged after a filtered request; MCP narrowing,
  the missing-scope case and its hint.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@orcamedia

Copy link
Copy Markdown
Contributor Author

Rebased onto main. The two entries now share one "Unreleased" section in upgrading.md (both must-do lines, then both changes), and openapi.json.gz and the reference docs are regenerated from the merged spec (cmd/docgen -check is clean). Re-verified after the rebase: make check, the unit suite with -race, the slim suite, go test -tags integration ./internal/search/ and the property search integration test from #453.

Thanks for the two nits. I'll send them as a small follow-up: reuse the roles resolved for the narrowing on the too-broad path, and the 50-value cap for project: / project_role: on the MCP path.

@orcamedia
orcamedia force-pushed the feat/search-project-filters branch from 602d458 to 99024bd Compare November 9, 2026 17:50

@M0Rf30 M0Rf30 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the rebase. Code is fine, but the merge of docs/getting-started/upgrading.md went the wrong way: against main, this PR replaces #453's entries instead of adding to them (git diff origin/main pr/454 -- docs/getting-started/upgrading.md removes the oa props search NAME line and the Property search bullet). Please keep both, e.g.:

**What you must do**

- Check scripts that read the text output of `oa props search` line by line: the table now has a `NAME` column after `ARTIFACT_ID`. `--format json` still prints the list of ids.
- In a search query, a word starting with `project:` or `project_role:` is now a filter rather than search text.
- Nothing else is required.

**Changes.**

- **Property search.** …(as on main)
- **Search by project.** …(yours)

I'll merge as soon as that's in and green.

@M0Rf30 M0Rf30 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

My mistake, sorry: I diffed a stale local ref of the pre-rebase commit. The rebased branch keeps both entries in one section, as you said. Merging.

@M0Rf30
M0Rf30 merged commit 52ff51f into openartifact:main Nov 9, 2026
22 checks passed
@orcamedia
orcamedia deleted the feat/search-project-filters branch November 9, 2026 18:26
M0Rf30 pushed a commit that referenced this pull request Nov 9, 2026
)

Follow-up to the review of #454:

- ProjectScope.NarrowToProjects also returns whether the caller holds a
  project role. On a project-filtered search that is too broad, the HTTP
  handler and the MCP search tool reuse it for the project:<project> hint
  instead of resolving the caller's roles a second time.
- search.CheckProjectFilters holds the project_role and 50-value rules.
  GET /api/v1/search answers them with 400 invalid_filter as before, and
  the MCP search tool now applies them too, as a tool error: an unknown
  project_role or more than 50 project: / project_role: values used to be
  accepted there (a typo silently matched nothing).
- docs/integrations/mcp.md: the qualifiers search_artifacts takes.

Co-authored-by: Areef <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
@M0Rf30 M0Rf30 mentioned this pull request Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Search: filter by project, and by my role in it

2 participants