Skip to content

Tag rules#90

Draft
MarkIannucci wants to merge 3 commits into
tomfunk:devfrom
MarkIannucci:tag-rules
Draft

Tag rules#90
MarkIannucci wants to merge 3 commits into
tomfunk:devfrom
MarkIannucci:tag-rules

Conversation

@MarkIannucci

Copy link
Copy Markdown
Contributor

Stacked on #84 .

This PR gives us the ability to apply tags using rules. Manual tag removals are remembered and not reapplied.

--- 🤖 Generated with Claude Code

Tag rules with sticky removals

Adds a tag-rule engine that automatically applies tags to transactions, mirroring the existing category/name rule patterns. Rules match by transaction name, regex, or match-all — where match-all + an account scope gives you the "tag everything in this account" default-tag use case.

What it does

  • New rule types: name (case-insensitive substring), regex (against name + merchant), and all (every transaction in scope). Each rule can be scoped to an account and gated by an amount range.
  • Backfill + ongoing apply: rules backfill matching transactions on save, and re-apply to new rows on Plaid sync and CSV import.
  • Sticky removals: removing a tag from a transaction is permanent. removeTagFromTransaction records a suppression so no rule — existing or created later — re-adds that tag. The only undo is manually re-adding the tag, which clears the suppression.
  • Live match count: the rule editor (TUI + GUI) shows how many transactions a rule will match as you build it, so a global match-all rule is never a surprise.
  • Full TUI + GUI parity — new "Tag rules" tab/section in both Rules screens.

Why suppressions

Unlike category stickiness (which can COALESCE over a single manual_category column), tags are many-to-many with no single column to override. A dedicated tag_rule_suppressions table records intentional removals so re-running rules can't undo a user's deliberate choice. All removal paths (TUI, GUI, agent tool) funnel through removeTagFromTransaction, so the guarantee holds at one chokepoint.

Schema

Two new tables (created via CREATE TABLE IF NOT EXISTS):

  • tag_rules — priority, match_type ('name'|'regex'|'all'), pattern, tag_id, account_id, min_amount, max_amount
  • tag_rule_suppressions — (transaction_id, tag_id) PK

Notable files

  • core/tag-rules.ts (new) — loadTagRules, tagRuleMatches, applyTagRules, countTagRuleMatches
  • core/tags.ts — suppression hooks on add/remove; deleteTag clears a tag's rules + suppressions
  • core/rules.ts — saveTagRule / deleteTagRule
  • core/sync.ts, core/accounts.ts — apply rules to newly synced/imported rows
  • tui/Rules.tsx, gui/renderer/src/screens/Rules.tsx — UI parity

Testing

  • 14 new tests in tests/tag-rules.test.ts covering matching (name/regex/all/account/amount), backfill, no-double-count on re-run, stickiness (removed-stays-gone, removal-before-rule, manual-readd-clears), and rule/tag lifecycle.
  • TUI screen tests updated for the new tab; full suite (664 tests) and both typechecks pass.

MarkIannucci and others added 3 commits June 14, 2026 21:57
Add an optional account_id to category_rules and name_rules. When set, a
rule only fires for transactions on that account; rules with account_id
NULL remain global fallbacks. Account-scoped rules take precedence over
global rules at the same priority level, achieved via a per-call account
filter plus ORDER BY priority DESC, (account_id IS NULL) ASC, id ASC.

accountId is threaded through every call site (sync, CSV import,
applyCategoriesToAll, rebuildDisplayNames, seedRules, single-shot
categorize/applyNameRules). CSV import now also passes amount so
amount-filtered rules behave consistently with Plaid sync.

- TUI/GUI Rules screens: account picker in both rule forms + scope badge
- MCP add_rule/add_name_rule accept account_id; list_rules surfaces scope
- deleteAccount removes that account's scoped rules
- Tests cover precedence, scoping, CRUD dedupe, deletion, CSV import

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add a tag-rule engine that applies tags to transactions by name/regex
match or match-all (the per-account default-tag use case = match-all +
an account scope). Rules backfill on save and apply to new rows on Plaid
sync and CSV import.

Removals are permanent: removeTagFromTransaction records a suppression so
no rule (existing or created later) re-adds the tag. Manually re-adding
the tag clears the suppression — the only undo. Editors show a live match
count so a global match-all rule is never a surprise.

Full TUI + GUI parity. New core/tag-rules.ts plus tag_rules and
tag_rule_suppressions tables.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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