Skip to content

feat(privacy_filter): add rewrite action and optional app scope - #175

Open
TimeToBuildBob wants to merge 3 commits into
ActivityWatch:masterfrom
TimeToBuildBob:feat/privacy-filter-rewrite-action
Open

TimeToBuildBob wants to merge 3 commits into
ActivityWatch:masterfrom
TimeToBuildBob:feat/privacy-filter-rewrite-action

Conversation

@TimeToBuildBob

Copy link
Copy Markdown
Contributor

Carries the useful part of #112 inside the existing privacy_filter instead of adding a second rule engine and config section (that PR is 477d old and conflicts with master in config.py/main.py).

What

  • action = "rewrite" applies pattern.sub(replacement, value), so a rule can strip a suffix or counter and keep the rest of the title readable. drop (discard event) and redact (replace whole field) are unchanged.
  • Optional app scope limits a rule to one application (case-insensitive exact match), which is what per-app title cleanup needs.
  • A rewrite rule must give an explicit replacement (empty string allowed). The "excluded" default would silently delete the matched text, so a missing replacement is rejected with an error log and the rule is skipped.

Why not merge #112 as-is

It predates privacy_filter and would add a parallel regex engine plus a second config section. The requested behaviour is ~20 lines inside the existing mechanism. Rewriting is opt-in: nothing mutates titles unless a user configures a rewrite rule.

Tests

pytest tests/test_privacy_filter.py -q
37 passed

16 new cases cover suffix/counter stripping, app scoping (match, case-insensitive match, non-matching app untouched), missing/invalid replacement rejection, non-matching pass-through, never dropping/redacting, and input-not-mutated. mypy --follow-imports=skip is clean on the module.

Note: tests/test_startup.py and tests/test_xauth_integration.py fail in this sandbox from an aw_core version mismatch (ModuleNotFoundError: aw_core.dirs.platformdirs), before any change here — the privacy-filter suite is self-contained.

Refs #112

The existing actions are all-or-nothing: drop discards the event and redact
replaces the whole field. That cannot express the common title cleanup from
ActivityWatch#112, e.g. stripping a browser suffix or an unread counter while keeping the
rest of the title readable.

Add action = "rewrite" (pattern.sub(replacement, value)) plus an optional
app scope so a rule can be limited to one application. A rewrite requires an
explicit replacement, including the empty string, because the "excluded"
default would otherwise delete the matched text.

Carries ActivityWatch#112 without adding a second rule
engine or config section.

Git-Session-Id: b770e86d-23b9-48f1-9d61-05c99e384e89
@greptile-apps

greptile-apps Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; both previous findings are addressed and no new blocking issue was found.

Summary

Adds opt-in rewrite rules and optional, case-insensitive app scope to the existing privacy filter.

  • Privacy rules can rewrite matching text for a chosen app.

Reviews (2) · Last reviewed commit: "fix(privacy_filter): make documented rew..." · Reviewed by Greptile

Comment thread aw_watcher_window/privacy_filter.py Resolved
Comment thread README.md Resolved
A dangling group reference or bad escape in a rewrite rule's replacement only
raises inside re.sub, i.e. while the heartbeat loop is applying filters, which
would stop window tracking. Validate the replacement against the compiled
pattern at compile time and skip the rule with an error log instead.

Also drop the trailing anchor from the documented counter example so it strips
counters in either position, per review.

Git-Session-Id: b770e86d-23b9-48f1-9d61-05c99e384e89
@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

Both review findings addressed in ee7f74a:

  • P1 (invalid replacement stops tracking) — correct, and worse than it looks: an invalid group reference or escape only raises inside re.sub, i.e. in the heartbeat loop. The replacement is now exercised against the compiled pattern at compile time (pattern.sub(replacement, "")) and the rule is skipped with an error log, so a malformed rule can never reach the filter application path.
  • P2 (example leaves counters intact) — the documented pattern dropped its trailing anchor, so it now strips unread counters in either position.

Tests: pytest tests/test_privacy_filter.py -q → 40 passed (3 new: invalid replacement rejected, valid group backreference works, leading+trailing counters stripped).

@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

Verified the rewrite action end-to-end on a real watcher run, not just the unit tests: Xvfb + openbox, a real xterm -class Firefox -T "Inbox (3) - Mozilla Firefox", the rule loaded from a temp XDG_CONFIG_HOME config, a minimal aw-server stub recording heartbeat bodies, and python -m aw_watcher_window --testing --poll-time 0.5.

The mechanism works. At c15e40f the server received {"app": "Firefox", "title": "Inbox (3)"} from the README rule. A WM_CLASS = XTerm control with the same config and the same title emitted "Inbox (3) - Mozilla Firefox" unchanged, so app scoping is enforced in the real path and not only in fixtures.

The ee7f74a replacement validation holds end-to-end. With replacement = '\9' the watcher logs invalid group reference 9 … rule skipped and keeps heartbeating untouched titles instead of dying mid-loop. (CPython parses the template eagerly, so the empty-string probe does catch \9 and \q without needing a match.)

The documented example doesn't do what its comment says. At HEAD this rule:

pattern = "( - Mozilla Firefox|\\(\\d+\\+?\\)\\s*)"

emits "Inbox - Mozilla Firefox", not "Inbox". \s* consumes the space after (3), so the remaining - Mozilla Firefox no longer has the leading space the first alternative requires and survives — the counter goes, the browser suffix (the more identifying half) stays, and a dangling " - " is left. At c15e40f, where the pattern was end-anchored, the same title became "Inbox (3)": suffix gone, counter kept. Neither matches "strip the browser suffix and unread counters".

This one does, in a single sub pass:

pattern = "(\\s*\\(\\d+\\+?\\)|\\s*- Mozilla Firefox)+$"

→ emitted {"app": "Firefox", "title": "Inbox"}, both trailing pieces removed, and the $ anchor keeps it from eating a (3) that legitimately appears mid-title.

Worth swapping the README/docstring example before merge — no behaviour change, just an example that matches its own comment.

@TimeToBuildBob

TimeToBuildBob commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor Author

🤖 AI code review

Adds the "rewrite" action to compile_privacy_rules() and apply_privacy_filters(), using regex substitution with an explicit replacement string. Adds optional case-insensitive exact app scoping, replacement validation, documentation examples, and tests for rewriting and app matching.

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
  • README.md — Adds Firefox title rewrite examples and documents the "rewrite" action, its required replacement, and optional app scope.
  • aw_watcher_window/privacy_filter.py — Adds rewrite rule compilation, replacement validation, app scope checks, regex substitution, and corresponding module documentation.
  • tests/test_privacy_filter.py — Adds tests for rewrite compilation, substitutions, backreferences, app scoping, input preservation, and examples parsed from README.md and the module docstring.
Previous review passes
commit score findings engine when
ee7f74a4ee62 5/5 0 subscription 2026-11-07 01:24 UTC

Reviewed 99cb5d2d3b9f · panel: codex:gpt-6.1-sol + claude-code:sonnet · subscription engine · 24s · about this reviewer

Maintainer commands

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

…dges

Git-Session-Id: 1b5783aa-3dfd-5643-b844-f6ad512e50b7
@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

@greptileai review

@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

Fixed the remaining example bug in 99cb5d2. The README and module example now use two ordered Firefox-scoped rules: strip the browser suffix plus trailing counters, then leading counters. Both Inbox (3) - Mozilla Firefox and (3) Inbox - Mozilla Firefox become Inbox, while Chapter (3) notes keeps its middle number.

The regression test parses the actual TOML from both examples rather than copying their regex into a fixture. It also checks that other applications are untouched and input events are not mutated. The module docstring is now raw so rendered TOML retains its required backslashes. Before the fix: 8 of the 10 example cases failed; after: all 50 privacy-filter tests pass, and the module's mypy check passes.

The invalid-replacement crash was already fixed by ee7f74a; no filter-engine behavior changed in this follow-up. Requested a fresh Greptile review through the helper after pushing the fix.

@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

CI-green and mergeable (Greptile 5/5) — waiting only on a maintainer click.

This PR is ready to merge, but the bot has pull-only access to this repo and can't self-merge — surfacing it here so it isn't lost. The monitoring loop will stop re-flagging it now that this note is posted.

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