Add a rules screen that finds and removes dead rules

Nothing showed whether a rule was still earning its place. The new screen, on
5, lists every rule in file order with the number of transactions it claims,
marking those that claim none.

The count comes from Engine.Usage, which counts by first match, so a rule
shadowed by an earlier one reports zero even though its glob matches. That is
the case worth catching: such a rule looks correct in isolation and can never
fire.

d removes the selected rule and p removes every unused one, each behind a y/n
confirmation since this rewrites a hand-maintained file. config.DeleteRules
edits rules.toml textually rather than re-serialising the parsed rules, so
comments and layout survive; a comment directly above a rule goes with it,
while one separated by a blank line is left as a heading. The result is
re-parsed before it replaces the file.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-09 02:02:01 +02:00
co-authored by Claude Opus 5
parent aeca203855
commit c2eba53fd4
8 changed files with 831 additions and 9 deletions
+33 -4
View File
@@ -23,10 +23,26 @@ func New(r *config.Rules) *Engine {
return &Engine{rules: r.Rule}
}
// Rules returns the ordered rules, as loaded from rules.toml.
func (e *Engine) Rules() []config.Rule { return e.rules }
// Match returns the first rule matching a transaction on the given account, or
// nil if none does. Every pattern a rule sets must match: a rule with both
// match and counterparty is an "and", not an "or".
// nil if none does.
func (e *Engine) Match(accountSlug string, t model.Transaction) *config.Rule {
if i := e.MatchIndex(accountSlug, t); i >= 0 {
return &e.rules[i]
}
return nil
}
// MatchIndex returns the position of the first rule matching a transaction, or
// -1 if none does. Every pattern a rule sets must match: a rule with both
// match and counterparty is an "and", not an "or".
//
// The position matters as well as the rule: because the first match wins, a
// rule that is fully shadowed by an earlier one never applies to anything, and
// only the index reveals that.
func (e *Engine) MatchIndex(accountSlug string, t model.Transaction) int {
var (
description = model.NormalizeDescription(t.Description)
counterparty = model.NormalizeDescription(t.Counterparty)
@@ -46,9 +62,22 @@ func (e *Engine) Match(accountSlug string, t model.Transaction) *config.Rule {
if r.Type != "" && !glob.Match(r.Type, kind) {
continue
}
return r
return i
}
return nil
return -1
}
// Usage counts how many transactions each rule actually claims. A rule with a
// count of zero is dead: either nothing matches it, or an earlier rule takes
// everything it would have caught.
func (e *Engine) Usage(txns []model.Transaction) []int {
counts := make([]int, len(e.rules))
for _, t := range txns {
if i := e.MatchIndex(t.AccountSlug, t); i >= 0 {
counts[i]++
}
}
return counts
}
// ApplyTxn returns the tag and transfer flag for a transaction. An unmatched
+46
View File
@@ -139,6 +139,52 @@ func TestFirstMatchWins(t *testing.T) {
}
}
// A rule can be dead two ways: nothing matches it, or an earlier rule already
// claimed everything it would have caught. Usage must report both as zero.
func TestUsageCountsFirstMatchOnly(t *testing.T) {
engine := New(&config.Rules{Rule: []config.Rule{
{Match: "*LIDL*", Tag: "groceries"}, // claims both LIDL rows
{Match: "*LIDL SOFIA*", Tag: "shadowed"}, // fully shadowed by the above
{Match: "*NOTHING MATCHES ME*", Tag: "no"}, // matches nothing at all
{Match: "*PAYROLL*", Tag: "salary"}, // claims one row
}})
txns := []model.Transaction{
{AccountSlug: "checking", Description: "LIDL SOFIA 4412"},
{AccountSlug: "checking", Description: "LIDL VARNA 9911"},
{AccountSlug: "checking", Description: "ACME PAYROLL"},
{AccountSlug: "checking", Description: "UNMATCHED SHOP"},
}
usage := engine.Usage(txns)
want := []int{2, 0, 0, 1}
if len(usage) != len(want) {
t.Fatalf("usage has %d entries, want %d", len(usage), len(want))
}
for i := range want {
if usage[i] != want[i] {
t.Errorf("rule %d used by %d transactions, want %d", i+1, usage[i], want[i])
}
}
}
func TestMatchIndex(t *testing.T) {
engine := New(&config.Rules{Rule: []config.Rule{
{Match: "*LIDL EXPRESS*", Tag: "snacks"},
{Match: "*LIDL*", Tag: "groceries"},
}})
if got := engine.MatchIndex("checking", model.Transaction{Description: "LIDL EXPRESS 1"}); got != 0 {
t.Errorf("index = %d, want 0", got)
}
if got := engine.MatchIndex("checking", model.Transaction{Description: "LIDL 1"}); got != 1 {
t.Errorf("index = %d, want 1", got)
}
if got := engine.MatchIndex("checking", model.Transaction{Description: "OTHER"}); got != -1 {
t.Errorf("index = %d, want -1 for no match", got)
}
}
func TestAccountScopedRule(t *testing.T) {
engine := New(&config.Rules{Rule: []config.Rule{
{Match: "*TRANSFER*", Tag: "transfer", Transfer: true, Account: "savings"},