Repository navigation
feat(classify): per-field patterns on the existing regex rule - #866
TimeToBuildBob wants to merge 2 commits into
Conversation
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
Review guide (auto-posted)Structured warm-up for reviewers — what changed, what to run, where to look. Key files
Suggested verification
Known risks / watch points
Suggested review focus
Generated by |
[Medium impact] The change reaches query-driven categorizing and tagging. The PR appears safe to merge, with one non-blocking performance cleanup. Findings
SummaryAdds per-field patterns to existing regex rules, so events can match an app and a window title together.
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 |
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
🤖 AI code reviewExtends the existing Safe to merge — no P0/P1 findingsConfidence 5/5 ✅ No findings. The diff looks correct to me on this pass. Files changed (3) — the diff as I read it
Reviewed Maintainer commands
|
Summary
mstscandwinboxcan show the same host in their window titles, so atitle-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
regexrule with anoptional per-field
fieldsmap:{ "type": "regex", "fields": { "app": "mstsc", "title": "office" } }(logical AND). A missing or non-string value fails the rule.
regexrule's unanchored search semantics(
^…$is how an exact match is asked for), so a single-field rule matchesexactly what the equivalent
select_keysrule would.ignore_caseapplies to every field pattern. The shared compiled-regex cacheis reused (
compile_pattern), so field rules cost the same as plain rules.categorizeandtag, including the rank-ordered fast path andthe per-event category cache.
Fail-closed rules
regexof its own. Combining it with a non-emptyregexorselect_keysis rejected (InvalidFunctionParameters) instead ofbeing silently ignored — older servers read only those members, so silently
dropping one would change what the rule means.
regex/select_keyscounts as unset, matching aw-core'sPython
Rule. That lets an editor serialize"regex": ""next tofieldsand keep the rule fail-closed on servers that predate the field map (an empty
regex already never matches here).
RegexRule::from_fieldsadditionally builds such a rule in the never-matchingstate 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_fieldstype, #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
Ruleparsing path — #775 turnsTryFrom<&DataType> for Ruleinto a depth-limitedrule_from_datatype, while thisPR adds an early
fieldsbranch inside theregexarm. Either can merge first;the other needs a small rebase, no semantic conflict.
Verification
cargo test --locked --workspace --lib— pass (aw-transform 108, all cratesgreen).
cargo test --locked -p aw-query— 27 query tests, 4 lib tests, 8 proptesttests pass; includes the new
test_tag_fields_mapandtest_rule_parsing_fields_map.aw-transform: the #939 split, AND semantics,present-string requirement, search/
ignore_casesemantics, one-fieldequivalence with
select_keys, empty map/pattern never matching,tagpath.categorize-vs-best-of-all equivalence test now generatesfields 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
/api/0/info) and the aw-webui editor gate are afollow-up, so the editor only emits
fieldsagainst a server that advertisessupport (avoids mixed-version activation).
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).