Skip to content

fix(aggregator): fall back to full-text search for handle-shaped queries - #3841

Merged
ascorbic merged 2 commits into
emdash-cms:mainfrom
KirbyBT:fix/search-handle-shaped-queries
Oct 5, 2026
Merged

ascorbic merged 2 commits into
emdash-cms:mainfrom
KirbyBT:fix/search-handle-shaped-queries

Conversation

@KirbyBT

@KirbyBT KirbyBT commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes the dotted-query case from #3620: any search query that looks like a handle or DID never reaches full-text search.

parsePublisherQuery accepts anything that passes isHandle(), so ordinary terms like standard.site, node.js or json-ld.org take the publisher-lookup path. If the handle doesn't resolve, searchPackages returned { packages: [] } immediately. If it resolved to a DID with no packages, the result was also empty. Either way the FTS query never ran, so standard.site found nothing even though the AT Protocol plugin lists it in its keywords and description.

As suggested in the issue, the endpoint now runs the full-text query whenever the publisher lookup produces no rows. A real publisher match still wins: a resolved handle with packages returns those packages, as before. Pagination stays consistent, because a cursor is only issued when the path that served the page has more rows.

The rest of #3620 (indexing slugs, handles and capabilities, and prefix matching) needs schema and query changes, so I've kept it out of this PR.

Part of #3620

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes (ran tsgo --noEmit in apps/aggregator)
  • pnpm lint passes (ran type-aware oxlint --deny-warnings on the changed files)
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main. — n/a, no UI strings
  • I have added and reviewed the user-facing changeset (if this PR changes a published package) — n/a, @emdash-cms/aggregator is private and ignored by changesets
  • New features link to an approved Discussion: https://github.com/emdash-cms/emdash/discussions/... — n/a, bug fix
  • I have included screenshots below if this PR changes the UI — n/a, no UI change

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Code (Claude Opus 5.5)

Screenshots / test output

Not applicable (no UI change).

Three new tests in apps/aggregator/test/read-api.test.ts:

  • standard.site as a keyword is found when the handle doesn't resolve
  • standard.site as a keyword is found when the handle resolves to a publisher with no packages
  • a resolved publisher's packages are still returned instead of FTS matches

Without the fix: 2 failed | 47 passed (49) (both fallback tests return no packages). With the fix: 49 passed (49). The full aggregator suite passes: 12 passed files, 323 passed tests.

Any search query that parses as an AT Protocol handle or DID was only
treated as a publisher lookup. When the handle didn't resolve, or resolved
to a publisher with no packages, the endpoint returned no results without
trying full-text search. Ordinary dotted terms such as `standard.site` or
`node.js` therefore never matched packages that list them in keywords or
descriptions.

Run the full-text query whenever the publisher lookup produces no rows.

Part of emdash-cms#3620

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

changeset-bot Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 627e6cc

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions Bot added review/needs-review No maintainer or bot review yet size/M cla: signed labels Oct 3, 2026

@emdashbot emdashbot Bot 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.

The approach is right for #3620: keep the publisher-identity path first so a real publisher still wins, and fall back to FTS only when that path produces no rows. That fixes the standard.site-style case with minimal extra queries on the failure path, and the three new tests cover the unresolvable handle, resolved-but-empty publisher, and publisher-wins scenarios.

I read the changed searchPackages route, traced its SQL builders and the parsePublisherQuery/resolvePublisher flow, checked the new tests in read-api.test.ts, and confirmed @emdash-cms/aggregator is private so no changeset is required. The logic itself is sound, but I have two suggestions to tighten the change before merge:

  1. The module-level docstring no longer fully describes the endpoint — it says handle/DID queries resolve the publisher first and "other queries use FTS5", omitting the new fallback path.
  2. The fallback currently also runs for handle/slug queries, which can return literal FTS matches when a publisher resolves but doesn't have that exact slug. If the intent is specifically to rescue bare handle-shaped terms, the fallback should be gated to queries without a slug.

Findings

  • [suggestion] apps/aggregator/src/routes/xrpc/searchPackages.ts:17-19

    The module docstring is now incomplete: it says handle/DID-shaped queries resolve the publisher first and that "other queries use FTS5", but after this change a handle/DID query also falls back to FTS5 when the publisher lookup returns no rows. Update the comment so future readers don't miss the fallback path.

     * A query that is exactly a handle, DID, or either identity plus `/slug`
     * resolves the publisher first and queries by stable DID. If that lookup
     * produces no rows, the same query falls back to FTS5 with defensive quoting.
     * Other queries use FTS5 directly. Empty queries return all visible packages.
    
  • [suggestion] apps/aggregator/src/routes/xrpc/searchPackages.ts:112

    This fallback also runs for handle/slug queries. For example, publisher.example/nonexistent would fall back to FTS and could return packages whose text contains that literal string, even though the user specified a concrete publisher+slug. If the intent is only to rescue bare handle-shaped terms like standard.site, consider gating the fallback to queries without a slug.

    	if (hasQuery && rows.length === 0 && publisherQuery?.slug === undefined) {
    

@emdashbot emdashbot Bot added review/awaiting-author Reviewed; waiting on the author to respond and removed review/needs-review No maintainer or bot review yet labels Oct 3, 2026
A `handle/slug` query names one specific package, so an empty publisher
result should stay empty rather than return loose text matches. Also
describe the fallback in the module comment.

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

KirbyBT commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Both suggestions were applied in 627e6cc:

  • handle/slug queries no longer fall back to FTS, so an exact publisher and slug with no match stays empty. Added a test for publisher.example/nonexistent that fails without the gate.
  • Updated the module comment to describe the fallback.

The aggregator suite passes locally (324 tests).

@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/awaiting-author Reviewed; waiting on the author to respond labels Oct 3, 2026

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

Thanks!

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed review/needs-rereview New commits since the last review labels Oct 5, 2026
@ascorbic
ascorbic merged commit 49ad728 into emdash-cms:main Oct 5, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: signed review/approved Approved; no new commits since size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants