From 61cec63ca97aeed665e5a2eca289c6559b8c03a5 Mon Sep 17 00:00:00 2001 From: mleku Date: Wed, 10 Sep 2025 19:24:24 +0100 Subject: [PATCH] Add detailed tag filter debugging logs in `QueryEvents` and update `rules.md` with context and debugging guidance. --- .aiassistant/rules/rules.md | 4 +++ pkg/database/query-events.go | 61 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/.aiassistant/rules/rules.md b/.aiassistant/rules/rules.md index 6038815..eff5dbe 100644 --- a/.aiassistant/rules/rules.md +++ b/.aiassistant/rules/rules.md @@ -89,3 +89,7 @@ A good typical example: // - Initializes the relay, starting its operation in a separate goroutine. ``` + +use the source of the relay-tester to help guide what expectations the test has, +and use context7 for information about the nostr protocol, and use additional +log statements to help locate the cause of bugs \ No newline at end of file diff --git a/pkg/database/query-events.go b/pkg/database/query-events.go index 94614a2..4cd8848 100644 --- a/pkg/database/query-events.go +++ b/pkg/database/query-events.go @@ -281,6 +281,67 @@ func (d *D) QueryEvents(c context.Context, f *filter.F) ( if ev, err = d.FetchEventBySerial(ser); err != nil { continue } + + // Add logging for tag filter debugging + if f.Tags != nil && f.Tags.Len() > 0 { + var eventTags []string + if ev.Tags != nil && ev.Tags.Len() > 0 { + for _, t := range *ev.Tags { + if t.Len() >= 2 { + eventTags = append(eventTags, string(t.Key())+"="+string(t.Value())) + } + } + } + log.T.F("QueryEvents: processing event ID=%s kind=%d tags=%v", hex.Enc(ev.ID), ev.Kind, eventTags) + + // Check if this event matches ALL required tags in the filter + tagMatches := 0 + for _, filterTag := range *f.Tags { + if filterTag.Len() >= 2 { + filterKey := filterTag.Key() + // Handle filter keys that start with # (remove the prefix for comparison) + var actualKey []byte + if len(filterKey) == 2 && filterKey[0] == '#' { + actualKey = filterKey[1:] + } else { + actualKey = filterKey + } + + // Check if event has this tag key with any of the filter's values + eventHasTag := false + if ev.Tags != nil { + for _, eventTag := range *ev.Tags { + if eventTag.Len() >= 2 && bytes.Equal(eventTag.Key(), actualKey) { + // Check if the event's tag value matches any of the filter's values + for _, filterValue := range filterTag.T[1:] { + if bytes.Equal(eventTag.Value(), filterValue) { + eventHasTag = true + break + } + } + if eventHasTag { + break + } + } + } + } + if eventHasTag { + tagMatches++ + } + log.T.F("QueryEvents: tag filter %s (actual key: %s) matches: %v (total matches: %d/%d)", + string(filterKey), string(actualKey), eventHasTag, tagMatches, f.Tags.Len()) + } + } + + // If not all tags match, skip this event + if tagMatches < f.Tags.Len() { + log.T.F("QueryEvents: event ID=%s SKIPPED - only matches %d/%d required tags", + hex.Enc(ev.ID), tagMatches, f.Tags.Len()) + continue + } + log.T.F("QueryEvents: event ID=%s PASSES all tag filters", hex.Enc(ev.ID)) + } + // Skip events with kind 5 (Deletion) if ev.Kind == kind.Deletion.K { continue