Skip to content

feat(search): add opt-in Local-First Search - #19

Merged
CubeSeven merged 13 commits into
CubeSeven:mainfrom
xpepper:feat/local-first-search
Oct 21, 2026
Merged

CubeSeven merged 13 commits into
CubeSeven:mainfrom
xpepper:feat/local-first-search

Conversation

@xpepper

@xpepper xpepper commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds an opt-in Local-First Search toggle (popup, Tools, off by default). When it's on, the new tab page and the in-page palette list tabs, bookmarks and history before web suggestions, and a strong URL match becomes a pre-selected Top Hit.

pulls with the toggle on:

+TOP HIT
+  Inbox · Pull requests      github.com/pulls/inbox      TAB
   Search Google for "pulls"
-  5 Google suggestions
 OPEN TABS
-  Inbox · Pull requests      github.com/pulls/inbox
 BOOKMARKS
   PRs                        github.com/pulls/authored
 HISTORY
   ...
+SUGGESTIONS
+  5 Google suggestions

How it works: when the setting is on, handleSearchItems passes the deduped results to applyLocalFirst (new services/searchRanking.js, no imports, so it runs under node --test). It tags rows with _section: 'tophit' | 'suggestion' instead of adding result types, so Enter and click handling are untouched. The renderers in ntp.js and content.js group by _section first, then by type.

Changes

  • New popup toggle Local-First Search (Tools), off by default. With it off, rows render exactly as before (suggestion rows only carry an unused _suggestion flag), and nothing is migrated, so it's easy to revert.
  • With it on, on the new tab page and in the in-page palette:
    • Open Tabs, Bookmarks and History come before engine suggestions, which move to the bottom under a Suggestions header.
    • Top Hit: a tab, bookmark or history entry with an http(s) URL whose domain label or path segment starts with the query (3+ characters; scheme, www./www2., TLD, query string and fragment are ignored). Tabs beat bookmarks, bookmarks beat history, the shortest URL wins within a type, and the tab you're searching from is skipped.
    • No Top Hit with bangs, URLs, calculator/converter/action results or > mode: today's first row stays first.
  • Docs: a "Local-First Search" paragraph in docs/docs.html, and CONTRIBUTING.md explains how to run the tests. tests/ is excluded from the store zip.
  • CI: pr-checks.yml runs node --test on Node 24 (actions/setup-node@v7).

Test Plan

  • node --test from the repo root: 20/20 on Node 24 and 26 (match rule incl. http(s) only and www2., precedence and shortest URL, current tab skipped, no-promotion cases, input not mutated)
  • pr-checks.yml steps run locally: manifest, node --check on all JS, no console.log/debugger, git diff --check; actionlint on the workflow
  • Manual, unpacked in Chrome, toggle on:
    • pulls with github.com/pulls/inbox open (NTP and palette): Top Hit = that tab; Enter switches to it
    • pulls on that inbox tab itself (palette): Top Hit = the /pulls/authored bookmark; the tab stays under Open Tabs
    • corr (NTP): Top Hit = the corriere.it/ homepage, not a recent article; Enter opens it
    • weather and pu (NTP): no Top Hit
    • !gh pulls, github.com, 2+2, 16px (NTP): the bang, "Go to" or converter row stays first
  • Toggle off, pulls (NTP and palette): same as before
  • After the review fixes: pulls (NTP) still gives the inbox tab as Top Hit

Before / after (new tab page, pulls):

before-ntp-pulls after-ntp-pulls-tophit-tab-cropped

Related

Closes #18

Not in this PR (happy to follow up):

  • Known limits: pressing Enter before results arrive still runs a web search, because the reply waits for suggestions (up to 2 s). 3- or 6-letter hex words like bbc produce Color Converter rows, which block the Top Hit (kept on purpose, per review). Tabs (5) and history (10) are capped before ranking.
  • The 500 ms same-query cache in handleSearchItems is keyed on the query only, so it ignores the searching tab and the setting. It only shows when the same query comes from two tabs within half a second.
  • Pre-existing: SECTION_ORDER / SECTION_LABELS are duplicated in ntp.js and content.js, and search rows say "Google Search" whatever the engine.

Summary by CodeRabbit

  • New Features
    • Added an optional Local-First Search setting. When enabled, matching tabs, bookmarks, and history entries appear before web suggestions.
    • Strong local matches can appear as a Top Hit and open when you press Enter. Actions and navigation results retain priority.
    • Results are organized into labeled sections, including Top Hit, History, and Suggestions.
  • Documentation
    • Updated the Command Palette guide with details on Local-First Search and Top Hit behavior.

Local-first search will pre-select a tab, bookmark or history entry
when the query starts a host label or path segment of its URL (e.g.
"pulls" for github.com/pulls/). The rule lives in an import-free
module so it can be unit-tested with Node's built-in test runner.

- the TLD is ignored so queries like "com" or "dev" don't match
  every site
- query string and fragment are ignored so past Google searches
  for the same words don't count as strong matches
applyLocalFirst marks the strongest local match as the Top Hit and
engine suggestions as Suggestions, so the palette can show local
results before remote ones without changing how rows are activated.

- precedence is open tab, bookmark, history, so Enter reuses an open
  tab instead of opening a duplicate
- nothing is promoted over bangs, URLs to open or actions, which
  already outrank the plain web search today
- placement uses a _section flag rather than new result types, so
  results without it render exactly as before
When enabled, palette and new tab search results are passed through
applyLocalFirst so a strong local match can become the Top Hit and
engine suggestions can move below tabs, bookmarks and history.

- off by default, so existing users see no change
- engine suggestions are tagged at creation, since they share the
  'search' type with the explicit "Search ... for" row
- applied after the URL dedupe so the Top Hit is always a row that
  is actually sent to the palette
The tests/ folder added for searchRanking is dev-only and is not loaded by the extension.
The new tab page and the in-page palette now honour the _section tag
set by Local-First Search: the Top Hit renders first (so Enter opens
it) and engine suggestions render last under their own header.

Results without _section are grouped by type exactly as before, so
nothing changes while the setting is off.
Manual testing showed "corr" promoting a recent corriere.it article
instead of the homepage, because history comes back most recent first
and the first strong match won. Within the winning type the shortest
matching URL now wins; type precedence (tab, bookmark, history) is
unchanged, so an open tab still beats a shorter history entry.
In the in-page palette the tab you are on can strongly match the query
(e.g. "pulls" on github.com/pulls/) and become the Top Hit, so Enter
switches to the same tab and nothing happens. The new tab page's own
tab had the same problem. The tab the search comes from is no longer
a Top Hit candidate; it is still listed under Open Tabs.
@coderabbitai

coderabbitai Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 67e30a51-c592-4e09-95d2-7d57283b93e7
📥 Commits

Reviewing files that changed from the base of the PR and between 302961e and 4377be9.

📒 Files selected for processing: 3
  • .github/workflows/pr-checks.yml
  • services/searchRanking.js
  • tests/searchRanking.test.js

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

📝 Walkthrough

Adds an opt-in Local-First Search setting. When enabled, matching local results can receive a Top Hit section, and engine suggestions receive a Suggestions section. The command palette and new tab page recognize these sections.

Changes: Local-First Search

Layer / File(s) Summary
Local matching and Top Hit selection
services/searchRanking.js, tests/searchRanking.test.js
Adds URL matching and Top Hit selection helpers. Tests cover matching rules, result priority, exclusions, suggestion tagging, and input immutability.
Setting and search integration
constants.js, popup.js, services/paletteService.js
Adds the default-off preferLocalResults setting and its popup control. The search service tags suggestions, deduplicates URLs, and applies local-first ranking when the setting is enabled.
Result sections and supporting updates
content.js, ntp.js, docs/docs.html, CONTRIBUTING.md, build-zip.sh, .github/workflows/pr-checks.yml
Adds Top Hit and Suggestions ordering and labels. Both result views prefer a recognized _section over the result type and use History when neither is recognized. Documentation describes the setting and Top Hit rule. The contribution guide describes running tests, the ZIP packaging command excludes tests, and the workflow runs the unit tests.

Priority: ➖ Normal

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

Change: Feature · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant PaletteService
  participant SearchRanking
  participant ContentPalette
  participant NewTabPage
  PaletteService->>SearchRanking: Apply local-first ranking when enabled
  SearchRanking-->>PaletteService: Return tagged results
  PaletteService->>ContentPalette: Provide search results
  PaletteService->>NewTabPage: Provide search results
Loading
Merge Risk: ⚪ Minimal · up to 4377b

The local-first option remains off for existing users unless enabled, and both search views display the new sections in the intended order. No concrete merge-blocking impact is evident.

Security architecture risk: 🔵 Low · up to 30296

The feature is off by default and retains user-confirmed navigation and existing data-access settings. No new privilege escalation was established. Browser handling of unusual links remains unverified, and briefly cached results can retain an earlier ranking.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The examined exposure is within the user's browser extension context: locally available tab, bookmark and history URLs can influence the default destination in either search view. An attacker would need a matching URL present in those sources and subsequent user activation; ranking itself does not navigate.

Security Findings and Attack Paths

  • inferred — No introduced privilege escalation was established in the traced path. A matching non-web URL could receive Top Hit metadata because matching does not restrict schemes, but whether browser enforcement permits a security-relevant outcome is unresolved. The raw-string navigation handler predates this PR.

Trust Boundaries and Controls

  • observed — Section metadata does not grant action authority or substitute a destination. Promoted tabs retain the switch-to-tab route; other URL results retain open-url. The views still display the result URL and use the original result identity for activation.

Resilience and Maintainability Implications

  • observed — The ranking step adds no browser-side mutation or cleanup transition. Existing generation checks still discard superseded assembly, failures still return empty results, and both views reject responses for a changed query. These mechanisms do not resolve the same-query memo's new sender dependence.

Hardening Proposals

  • proposed — Define an explicit non-web URL policy at the shared navigation boundary and align Top Hit eligibility with it, preserving intentionally supported schemes. This would make the security guarantee independent of unverified browser behavior; it is hardening, not a verified finding.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 8 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Linked Issues check Passed Issue #18 requirements are implemented. preferLocalResults defaults to false and the popup exposes the toggle in Tools. When enabled, applyLocalFirst promotes HTTP(S) local matches in tab, bookm…
Out of Scope Changes check Passed The changes stay within Issue #18. Ranking logic, popup settings, renderer grouping, tests, CI execution, documentation, test instructions, and test exclusion from the store ZIP all support Local-Firs…
Title check Passed The title clearly identifies the opt-in Local-First Search feature, which is the main change.
Description check Passed The description includes all required sections and explains the feature, user-visible changes, test plan, and related issue. The details are sufficient and relevant.
Full details: Linked Issues check

Explanation

Issue #18 requirements are implemented. preferLocalResults defaults to false and the popup exposes the toggle in Tools. When enabled, applyLocalFirst promotes HTTP(S) local matches in tab, bookmark, history order, selects the shortest URL within the winning type, skips the current tab, and tags engine suggestions for the Suggestions section. The renderers use these sections. Tests cover matching, precedence, exclusions, and immutability. The CI workflow runs node --test.

Full details: Out of Scope Changes check

Explanation

The changes stay within Issue #18. Ranking logic, popup settings, renderer grouping, tests, CI execution, documentation, test instructions, and test exclusion from the store ZIP all support Local-First Search and its delivery.

Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 8 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@xpepper
xpepper marked this pull request as ready for review October 4, 2026 20:04
@xpepper

xpepper commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor Author

PS I noticed CodeRabbit's security notes point at two small hardening gaps in this PR. Neither is a vulnerability on its own, and both are cheap to close. Here's what I'd do.

1. The Top Hit doesn't restrict URL schemes

A bookmarklet such as javascript:document.title strong-matches doc. Bookmarks beat history, so it becomes the pre-selected Top Hit ahead of, say, docs.google.com from history. The usual javascript:(function(){...})() form doesn't match. This PR doesn't change what activating the row does (same open-url path as clicking it today). What's new is that Enter can pick it without the user moving the selection.

Fix: only http: and https: URLs can be the Top Hit. Other schemes still show in their usual section, they're just never pre-selected. That's one check in findTopHit plus a test. chrome:// and file:// entries would lose Top Hit eligibility too; easy to allow if you'd rather keep them.

2. The 500 ms search cache ignores which tab is searching

handleSearchItems reuses the last response for the same query within LAST_QUERY_TTL (500 ms), keyed on the query only. The Top Hit now skips the tab you're searching from, so the same query typed in two tabs within half a second could reuse the first tab's ranking. Toggling the setting has the same gap.

Fix: include the sender tab and the setting value in the cache key.

Both are small, test-first changes. Would you like them in this PR, or in a follow-up after this one is merged?

@xpepper

xpepper commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Hello @CubeSeven! Thanks for building Tabs++ 🙏 , I use it daily and this PR adds the thing I missed most (issue #18).

TL;DR: opt-in "Local-First Search" toggle (off by default), tests included, CodeRabbit is green, and it follows AGENTS.md.

I know you may be busy, so no rush. To make the review easy:

  • The diff is small (10 files, ~200 lines). I can split it if you prefer smaller PRs.
  • I flagged two hardening follow-ups above. I'm happy to include them here or open a separate PR, whichever you prefer.

Could you let me know how you'd like contributions to be reviewed (for example, any timing or process I should follow)? And if the feature doesn't fit the direction of the project, I'd also appreciate knowing, so I don't keep it hanging.

Thanks!
Pietro (@xpepper)

@CubeSeven

Copy link
Copy Markdown
Owner

Hi Pietro — sorry for the silence, work buried me and this sat in the queue far longer than it should have. Thanks for the PR and for the write-up, the test plan and the tests; that is a better contribution than most and I appreciate you using the extension daily and fixing the thing that annoyed you most.

I went through the diff properly, and I ran your tests myself: node --test from the repo root, 18/18 pass.

Verdict: I want this in. A few answers before you push the last changes.

  1. Yes — add the node --test step to pr-checks.yml in this PR. One step, actions/setup-node + node --test is fine. tests/ is already excluded from the store zip by build-zip.sh, so nothing leaks into the release.
  2. Leave Color Converter alone. The rule "nothing is promoted when an action, bang or navigate row is present" is the right one and converter results are explicit actions — a 3-or-6-letter hex word blocking a Top Hit is acceptable. Not this PR's job.
  3. Yes, include the scheme hardening here, not as a follow-up. Please restrict isStrongMatch to http:/https::
const parsed = new URL(url);
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return false;

I checked it: isStrongMatch('doc', 'javascript:document.title') is true today, because new URL() parses the bookmarklet and pathname becomes document.title. Bookmarks outrank history, so a bookmarklet can become the pre-selected Top Hit and Enter runs it against whatever page is open. Clicking that bookmark row does the same thing today, so it isn't a new capability — but making it the default Enter target is new, and it is two lines plus one test.

Three small notes, none blocking:

  • labels.pop() strips only one label, so a host like docs.google.co.uk keeps co as a matchable label. Harmless today only because the 3-character minimum rules out every two-letter suffix — worth a // shortcut: comment so it doesn't surprise us later.
  • replace(/^www\./, '') misses www2. (minor, matches www2.example.com on www2).
  • SECTION_ORDER / SECTION_LABELS are now duplicated in ntp.js and content.js with two more entries each. Pre-existing, you flagged it yourself — leaving it, just noting it is the kind of duplication AGENTS.md calls out.

On your "Enter before results arrive" limit and the caps (tabs 5 / history 10): documented, fine, no change wanted.

On process, since you asked: small single-feature PRs like this are exactly what I want, tests are welcome (CI will run them from here), and AGENTS.md is what I review against. I'll answer within a few days from now on, and if an idea doesn't fit the project I'll say so instead of leaving it hanging. Sorry again for the wait — and thanks.

A bookmarklet such as javascript:document.title strong-matched "doc",
so it could become the pre-selected Top Hit and Enter would run it on
the current page. chrome:// and file:// URLs matched as well. Asked
for in the review of CubeSeven#19.
Only a leading "www." was stripped, so hosts such as www2.example.com
kept a "www2" label and the query "www" strong-matched them. Noted in
the review of CubeSeven#19.
Dropping only the last host label leaves "co" in docs.google.co.uk and
"com" in bbc.com.au. Say why that is acceptable, as asked in the review
of CubeSeven#19, and note that 3-letter labels still match their exact query.
The maintainer asked for tests/ to run in CI as part of CubeSeven#19. Pin Node
24 (the current LTS). setup-node@v7 runs on Node 24, so it does not add
another Node 20 deprecation warning like the one checkout@v4 gets.
@xpepper

xpepper commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @CubeSeven, no worries at all about the wait, and thanks for the careful review!

Everything you asked for is in (4 new commits), plus the www2. fix. The new CI runs are waiting for your approval.

Verified: 20/20 tests on Node 24 and 26, the CI steps run locally (plus actionlint), and pulls re-checked in Chrome. The PR description is updated.

Left out on purpose

  • The cache-key point from my earlier comment: it only shows when the same query comes from two tabs within 500 ms, so I kept it out of this PR. Happy to send it as a small follow-up if you want it.
  • SECTION_ORDER / SECTION_LABELS: left as you said. I can open a separate PR to share them between ntp.js and content.js if you'd like.

Could you approve the workflow runs and take another look when you have a moment?

Thanks!
Pietro

@CubeSeven
CubeSeven merged commit 4cd74d7 into CubeSeven:main Oct 21, 2026
1 check passed
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.

feat: option to list local results before web suggestions

2 participants