From 4658bdfc83c478d44e265321750259df52bfc4af Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Wed, 4 Nov 2026 20:37:53 -0800 Subject: [PATCH] fix: keep merged and resolved threads out of the draft queue (#33) The draft predicate only looked at the auto_thread marker, so a cluster merged into another (resolved, zero observations left) stayed on the list forever and the queue could never drain: 6 of 13 candidates on this index were merged or resolved. IsDraft now owns the predicate for both surfaces: auto-threaded, not resolved, and still holding at least one observation. --- internal/cmd/mcp.go | 2 +- internal/cmd/thread.go | 9 +-------- internal/db/db.go | 11 +++++++++++ internal/db/db_test.go | 25 +++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/internal/cmd/mcp.go b/internal/cmd/mcp.go index fac55d3..d1b6dbc 100644 --- a/internal/cmd/mcp.go +++ b/internal/cmd/mcp.go @@ -340,7 +340,7 @@ func NewMCPServer(kdb *db.KnoxDB) *mcp.Server { } selected := make([]db.Thread, 0, len(threads)) for _, t := range threads { - if strings.Contains(t.Provenance, "auto_thread") || strings.HasPrefix(t.Motivation, "Auto-detected") { + if t.IsDraft() { selected = append(selected, t) } if len(selected) >= limit { diff --git a/internal/cmd/thread.go b/internal/cmd/thread.go index 776c24c..dc7376f 100644 --- a/internal/cmd/thread.go +++ b/internal/cmd/thread.go @@ -390,7 +390,7 @@ a better title and motivation. Apply changes with ` + "`knox thread update ` } var shown int for _, t := range threads { - if !isAutoThreaded(t) { + if !t.IsDraft() { continue } obs, _ := kdb.ThreadObservations(t.ID) @@ -419,13 +419,6 @@ a better title and motivation. Apply changes with ` + "`knox thread update ` } } -func isAutoThreaded(t db.Thread) bool { - if strings.Contains(t.Provenance, "auto_thread") { - return true - } - return strings.HasPrefix(t.Motivation, "Auto-detected") -} - func newThreadUpdateCmd(kdb *db.KnoxDB) *cobra.Command { var title, motivation, priority, tags string cmd := &cobra.Command{ diff --git a/internal/db/db.go b/internal/db/db.go index 521762f..43fa555 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -1602,6 +1602,17 @@ type Thread struct { LastActivity string // newest linked observation's signal time (created_at) } +// IsDraft reports whether a thread still carries the heuristic auto-threader's +// editorial copy and is worth offering for polish. Merged clusters are resolved +// and left with no observations, so they are excluded: there is nothing to +// rewrite and the queue could never drain. +func (t Thread) IsDraft() bool { + if t.Status == "resolved" || t.EntryCount == 0 { + return false + } + return strings.Contains(t.Provenance, "auto_thread") || strings.HasPrefix(t.Motivation, "Auto-detected") +} + // stallAfterDays is how long a thread may go without activity before it is // considered stalled (derived, reversible — not a stored state). const stallAfterDays = 14 diff --git a/internal/db/db_test.go b/internal/db/db_test.go index aa30b57..05773a1 100644 --- a/internal/db/db_test.go +++ b/internal/db/db_test.go @@ -805,3 +805,28 @@ func TestUpdateThreadClearsAutoThreadMarker(t *testing.T) { t.Fatalf("unrelated provenance was rewritten: %s", got.Provenance) } } + +// The draft queue is "auto-threaded clusters still worth polishing". A merged +// cluster is resolved and empty, so it must not come back as a candidate. +func TestThreadIsDraft(t *testing.T) { + cases := []struct { + name string + t Thread + draft bool + }{ + {"marker", Thread{Status: "active", EntryCount: 12, Provenance: `{"trigger":"auto_thread"}`}, true}, + {"prefix", Thread{Status: "stalled", EntryCount: 3, Motivation: "Auto-detected cluster of 4"}, true}, + {"groomed", Thread{Status: "active", EntryCount: 12, + Provenance: `{"groomed":true,"cluster":"plant"}`, Motivation: "Plant watering"}, false}, + {"merged", Thread{Status: "resolved", EntryCount: 0, Provenance: `{"trigger":"auto_thread"}`}, false}, + {"resolved with work", Thread{Status: "resolved", EntryCount: 9, Provenance: `{"trigger":"auto_thread"}`}, false}, + {"empty cluster", Thread{Status: "active", EntryCount: 0, Motivation: "Auto-detected cluster"}, false}, + {"manual", Thread{Status: "active", EntryCount: 4, + Provenance: `{"trigger":"display-watch"}`, Motivation: "By hand"}, false}, + } + for _, c := range cases { + if got := c.t.IsDraft(); got != c.draft { + t.Errorf("%s: IsDraft()=%v, want %v", c.name, got, c.draft) + } + } +} -- 2.52.0