Skip to content

feat(classify): per-field patterns on the existing regex rule - #866

Open
TimeToBuildBob wants to merge 2 commits into
ActivityWatch:masterfrom
TimeToBuildBob:feat/regex-rule-fields-map
Open

TimeToBuildBob wants to merge 2 commits into
ActivityWatch:masterfrom
TimeToBuildBob:feat/regex-rule-fields-map

Conversation

@TimeToBuildBob

Copy link
Copy Markdown
Contributor

Summary

mstsc and winbox can show the same host in their window titles, so a
title-only (or any-value) regex cannot tell an RDP session from a Winbox
session — ActivityWatch/aw-webui#939 asks for app AND title matching.

Rather than add a new rule type, this extends the existing regex rule with an
optional per-field fields map:

{ "type": "regex", "fields": { "app": "mstsc", "title": "office" } }
  • Every named field must be present, a string, and match its own pattern
    (logical AND). A missing or non-string value fails the rule.
  • Patterns keep the plain regex rule's unanchored search semantics
    (^…$ is how an exact match is asked for), so a single-field rule matches
    exactly what the equivalent select_keys rule would.
  • ignore_case applies to every field pattern. The shared compiled-regex cache
    is reused (compile_pattern), so field rules cost the same as plain rules.
  • Works in both categorize and tag, including the rank-ordered fast path and
    the per-event category cache.

Fail-closed rules

  • A fields rule carries no regex of its own. Combining it with a non-empty
    regex or select_keys is rejected (InvalidFunctionParameters) instead of
    being silently ignored — older servers read only those members, so silently
    dropping one would change what the rule means.
  • An empty regex / select_keys counts as unset, matching aw-core's
    Python Rule. That lets an editor serialize "regex": "" next to fields
    and keep the rule fail-closed on servers that predate the field map (an empty
    regex already never matches here).
  • Empty field maps, empty patterns and non-string patterns are rejected, and
    RegexRule::from_fields additionally builds such a rule in the never-matching
    state so a direct caller cannot accidentally match every event.

This mirrors the Python contract in ActivityWatch/aw-core#206, so both servers
classify a fields rule identically. It supersedes the earlier Option A shape
(a separate regex_fields type, #670, closed).

Relationship to #775

This is a distinct feature from #775 (and/or logical rule groups, carry of #486),
but both refactor the same Rule parsing path — #775 turns
TryFrom<&DataType> for Rule into a depth-limited rule_from_datatype, while this
PR adds an early fields branch inside the regex arm. Either can merge first;
the other needs a small rebase, no semantic conflict.

Verification

  • cargo test --locked --workspace --lib — pass (aw-transform 108, all crates
    green).
  • cargo test --locked -p aw-query — 27 query tests, 4 lib tests, 8 proptest
    tests pass; includes the new test_tag_fields_map and
    test_rule_parsing_fields_map.
  • New unit tests in aw-transform: the #939 split, AND semantics,
    present-string requirement, search/ignore_case semantics, one-field
    equivalence with select_keys, empty map/pattern never matching, tag path.
  • The randomized categorize-vs-best-of-all equivalence test now generates
    fields rules too (including a numeric field, which must never match), so the
    ranked early-exit path is cross-checked against an independent oracle.
  • cargo clippy --workspace --locked -- -D warnings — clean.
  • cargo fmt --all -- --check — clean.

Not in this PR

  • Capability advertising (/api/0/info) and the aw-webui editor gate are a
    follow-up, so the editor only emits fields against a server that advertises
    support (avoids mixed-version activation).
  • No backend or storage change: rules are still sent by the client in the query,
    as before.

Fixes part of ActivityWatch/aw-webui#939 (product-level verification of the
RDP/Winbox split still needs the editor gate and a release).

mstsc and winbox can show the same host in their window titles, so a
title-only (or any-value) regex cannot separate an RDP session from a
Winbox session. aw-webui#939 asks for app AND title matching.

Instead of a new rule type, extend the existing `regex` rule with an
optional `fields` map: {"type": "regex", "fields": {"app": "…",
"title": "…"}}. Every named field must be present, a string, and match
its own pattern (logical AND). Patterns keep the unanchored search
semantics of a plain regex rule, so a single-field rule behaves exactly
like the equivalent select_keys rule.

A fields rule carries no regex of its own. Mixing it with a non-empty
`regex` or `select_keys` is rejected rather than silently ignored, since
older servers read only those members. An empty `regex`/`select_keys`
counts as unset, which keeps such a rule fail-closed on older servers
instead of misclassifying events. Empty field maps, empty patterns and
non-string patterns are rejected (and defensively never match).

This mirrors the aw-core Python contract (ActivityWatch/aw-core#206) so
both servers classify a fields rule identically.

Git-Session-Id: 99a8d08d-5d27-4922-8357-677f8cd32d3e
@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

Review guide (auto-posted)

Structured warm-up for reviewers — what changed, what to run, where to look.

Key files

  • aw-transform/src/classify.rs (+306/-39, Δ345)
  • aw-query/src/datatype.rs (+112/-12, Δ124)
  • aw-query/tests/query.rs (+120/-0, Δ120)

Suggested verification

  • Run the repo's usual CI-equivalent checks locally

Known risks / watch points

  • No automatic high-risk tags; use file list + diff for judgment.

Suggested review focus

  • Confirm behavior matches the PR description acceptance criteria.
  • Skim the largest diffs first (listed above).
  • If CI is green, spot-check the highest-risk paths called out here.

Generated by scripts/github/pr-warmup-review-guide.py for #866.

@greptile-apps

greptile-apps Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change reaches query-driven categorizing and tagging.

The PR appears safe to merge, with one non-blocking performance cleanup.

Findings

  1. P2 Field rules scan unused values ▶

Summary

Adds per-field patterns to existing regex rules, so events can match an app and a window title together.

  • Regex rules can match several event fields at once.

One non-blocking issue remains: field-only rules still cause an unused scan of event values.

Reviews (1) · Last reviewed commit: "feat(classify): per-field patterns on th..." · Reviewed by Greptile

Comment thread aw-transform/src/classify.rs Resolved
The `Rule` impl of `needs_values` duplicated the `select_keys` check
instead of delegating, so a fields rule (which has no select_keys) still
counted as needing the flattened per-event value set. Matching was
correct, but every event paid for a flatten it never read. Delegate to
the `RegexRule` impl, which already accounts for `fields`, and pin it
with a regression test.

Spotted by Greptile on ActivityWatch#866.

Git-Session-Id: 99a8d08d-5d27-4922-8357-677f8cd32d3e
@codecov

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.85714% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.98%. Comparing base (656f3c9) to head (1ab0e93).
⚠️ Report is 246 commits behind head on master.

Files with missing lines Patch % Lines
aw-transform/src/classify.rs 90.00% 7 Missing ⚠️
aw-query/src/datatype.rs 85.36% 6 Missing ⚠️
aw-query/tests/query.rs 95.31% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master     #866       +/-   ##
===========================================
+ Coverage   70.81%   85.98%   +15.16%     
===========================================
  Files          51       90       +39     
  Lines        2916    13253    +10337     
===========================================
+ Hits         2065    11395     +9330     
- Misses        851     1858     +1007     

☔ View full report in Codecov by Harness.
📢 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.

@TimeToBuildBob

TimeToBuildBob commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 AI code review

Extends the existing regex rule with a fields map whose patterns must all match their named string fields. Adds parse_fields_rule validation and RegexRule::from_fields, extracts shared regex compilation into compile_pattern, and updates value-extraction decisions. Adds query and transform tests for field matching, parsing, tagging, and categorization.

Safe to merge — no P0/P1 findings

Confidence 5/5

✅ No findings. The diff looks correct to me on this pass.

Files changed (3) — the diff as I read it
  • aw-query/src/datatype.rs — Adds a fields branch to regex rule parsing and parse_fields_rule to validate field maps, reject conflicting members, and construct per-field rules.
  • aw-query/tests/query.rs — Adds query tests for per-field tagging, empty compatibility members, conflicting members, malformed field maps, and regex compilation errors.
  • aw-transform/src/classify.rs — Adds per-field regex construction and matching, extracts compile_pattern, updates needs_values, and extends classification tests and the randomized reference comparison.

Reviewed 1ab0e932241f · panel: codex:gpt-6.1-sol · subscription engine · 22s · about this reviewer

Maintainer commands

@TimeToBuildBob review (own line) — fresh review · @TimeToBuildBob fix — a worker acts on the findings. Once per comment; 👀 = received.

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.

1 participant