Skip to content

fix(query-options): prefer known hosts over unknown defaults#809

Merged
ErikBjare merged 1 commit intoActivityWatch:masterfrom
TimeToBuildBob:fix-queryoptions-unknown-default
Apr 24, 2026
Merged

fix(query-options): prefer known hosts over unknown defaults#809
ErikBjare merged 1 commit intoActivityWatch:masterfrom
TimeToBuildBob:fix-queryoptions-unknown-default

Conversation

@TimeToBuildBob
Copy link
Copy Markdown
Contributor

@TimeToBuildBob TimeToBuildBob commented Apr 23, 2026

Summary

  • prefer known hosts over unknown in the shared QueryOptions hostname chooser
  • keep unknown available as a fallback option when it is the only host
  • add a regression test for the hostname ordering helper

Root cause

QueryOptions defaulted to hostnameChoices[0], and bucketsStore.hosts can legitimately put unknown first when a special bucket like aw-stopwatch was updated more recently than the real window/AFK buckets.

That meant Category Builder could start with queryOptions.hostname = "unknown" already set, which bypassed its existing "if hostname is blank, pick a non-unknown host" fallback and led to synthesized queries like aw-watcher-window_unknown.

Testing

  • npm test -- --runInBand test/unit/hostnames.test.node.ts test/unit/queries.test.node.js

Refs ActivityWatch/activitywatch#1214

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 23, 2026

Greptile Summary

This PR fixes a bug in QueryOptions where 'unknown' could appear first in the hostname list (due to bucket recency ordering in the store), causing Category Builder to default to 'unknown' and generate malformed queries like aw-watcher-window_unknown. The fix introduces a small, pure preferKnownHostnames utility that stably moves 'unknown' to the end of the list while preserving the relative order of real hostnames, and falls back gracefully when 'unknown' is the only available host.

Confidence Score: 5/5

Safe to merge — targeted fix with correct logic, no regressions introduced, and a regression test added.

The change is minimal (6 lines of logic, one computed-property call site), the utility function handles all meaningful edge cases correctly (mixed, all-known, all-unknown, single-unknown), and the three new test cases directly cover the described root cause. No P0 or P1 issues found.

No files require special attention.

Important Files Changed

Filename Overview
src/util/hostnames.ts New utility function preferKnownHostnames that stably sorts known hosts before 'unknown', with correct fallback when 'unknown' is the only entry.
src/components/QueryOptions.vue Minimal one-line change to hostnameChoices computed property; now wraps bucketsStore.hosts with preferKnownHostnames so the first dropdown entry (used as mount-time default) is always a real host when one exists.
test/unit/hostnames.test.node.ts New regression tests covering the three key scenarios: reordering, order preservation, and solo-unknown fallback. The empty-array and all-known-hosts paths are exercised implicitly by the implementation but not explicitly tested.

Sequence Diagram

sequenceDiagram
    participant M as QueryOptions (mounted)
    participant C as hostnameChoices (computed)
    participant P as preferKnownHostnames()
    participant S as bucketsStore.hosts

    M->>S: ensureLoaded()
    S-->>M: hosts loaded
    M->>C: read hostnameChoices
    C->>S: this.bucketsStore.hosts
    S-->>C: ['unknown', 'laptop_ori', ...]
    C->>P: preferKnownHostnames(hosts)
    P-->>C: ['laptop_ori', ..., 'unknown']
    C-->>M: hostnameChoices[0] = 'laptop_ori'
    M->>M: set queryOptionsData.hostname = 'laptop_ori'
    M->>M: emit('input', queryOptionsData)
Loading

Reviews (1): Last reviewed commit: "fix(query-options): prefer known hosts o..." | Re-trigger Greptile

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 31.72%. Comparing base (dd7ae07) to head (db23216).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #809      +/-   ##
==========================================
+ Coverage   31.53%   31.72%   +0.18%     
==========================================
  Files          32       33       +1     
  Lines        1877     1882       +5     
  Branches      345      334      -11     
==========================================
+ Hits          592      597       +5     
- Misses       1217     1264      +47     
+ Partials       68       21      -47     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@TimeToBuildBob
Copy link
Copy Markdown
Contributor Author

TimeToBuildBob commented Apr 24, 2026

All checks are green and the diff is the targeted hostname-ordering fix with regression coverage, so this is ready to merge. I attempted to merge it from project monitoring just now, but the TimeToBuildBob credentials on this runner do not have MergePullRequest permission for ActivityWatch/aw-webui, so a maintainer merge is still needed.

@ErikBjare ErikBjare merged commit 444ed4f into ActivityWatch:master Apr 24, 2026
8 checks passed
@0xbrayo
Copy link
Copy Markdown
Member

0xbrayo commented Apr 24, 2026

One of the most annoying bugs fixed. Good job @TimeToBuildBob

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.

3 participants