Compare commits

3 Commits

Author SHA1 Message Date
david 4658bdfc83 fix: keep merged and resolved threads out of the draft queue (#33)
build / image (push) Successful in 36s
build / test (push) Successful in 3m21s
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.
2026-11-04 20:37:53 -08:00
david 45f966d44f fix: read tool calls and prose from the v2 session store (#30) (#32)
build / test (push) Successful in 3m34s
build / image (push) Successful in 29s
Co-authored-by: David Gwilliam <dhgwilliam@gmail.com>
Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
2026-11-05 04:31:48 +00:00
david 380f280ca4 fix: drop the auto_thread marker when a thread is rewritten (#29) (#31)
build / test (push) Has been cancelled
build / image (push) Has been cancelled
Co-authored-by: David Gwilliam <dhgwilliam@gmail.com>
Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
2026-11-05 04:31:39 +00:00
4 changed files with 145 additions and 12 deletions
+1 -1
View File
@@ -340,7 +340,7 @@ func NewMCPServer(kdb *db.KnoxDB) *mcp.Server {
} }
selected := make([]db.Thread, 0, len(threads)) selected := make([]db.Thread, 0, len(threads))
for _, t := range 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) selected = append(selected, t)
} }
if len(selected) >= limit { if len(selected) >= limit {
+1 -8
View File
@@ -390,7 +390,7 @@ a better title and motivation. Apply changes with ` + "`knox thread update <id>`
} }
var shown int var shown int
for _, t := range threads { for _, t := range threads {
if !isAutoThreaded(t) { if !t.IsDraft() {
continue continue
} }
obs, _ := kdb.ThreadObservations(t.ID) obs, _ := kdb.ThreadObservations(t.ID)
@@ -419,13 +419,6 @@ a better title and motivation. Apply changes with ` + "`knox thread update <id>`
} }
} }
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 { func newThreadUpdateCmd(kdb *db.KnoxDB) *cobra.Command {
var title, motivation, priority, tags string var title, motivation, priority, tags string
cmd := &cobra.Command{ cmd := &cobra.Command{
+47 -3
View File
@@ -4,6 +4,7 @@ import (
"crypto/rand" "crypto/rand"
"database/sql" "database/sql"
"encoding/hex" "encoding/hex"
"encoding/json"
"fmt" "fmt"
"math" "math"
"os" "os"
@@ -1601,6 +1602,17 @@ type Thread struct {
LastActivity string // newest linked observation's signal time (created_at) 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 // stallAfterDays is how long a thread may go without activity before it is
// considered stalled (derived, reversible — not a stored state). // considered stalled (derived, reversible — not a stored state).
const stallAfterDays = 14 const stallAfterDays = 14
@@ -1731,6 +1743,10 @@ func (k *KnoxDB) CloseThread(id int64) error {
// UpdateThread rewrites the editorial fields of a thread (used by the LLM // UpdateThread rewrites the editorial fields of a thread (used by the LLM
// draft/polish pass to replace auto-generated titles/motivations). Empty // draft/polish pass to replace auto-generated titles/motivations). Empty
// strings leave the corresponding field unchanged; omitting them all is a no-op. // strings leave the corresponding field unchanged; omitting them all is a no-op.
//
// A rewritten title or motivation counts as editorial: the auto_thread
// provenance marker is dropped so the thread stops reporting as a draft. The
// rest of the provenance (cluster, score, sources) is kept.
func (k *KnoxDB) UpdateThread(id int64, title, motivation, priority, tags string) (bool, error) { func (k *KnoxDB) UpdateThread(id int64, title, motivation, priority, tags string) (bool, error) {
t, err := k.GetThread(id) t, err := k.GetThread(id)
if err != nil || t == nil { if err != nil || t == nil {
@@ -1748,17 +1764,45 @@ func (k *KnoxDB) UpdateThread(id int64, title, motivation, priority, tags string
if tags == "" { if tags == "" {
tags = t.Tags tags = t.Tags
} }
changed := title != t.Title || motivation != t.Motivation || priority != t.Priority || tags != t.Tags provenance := t.Provenance
if title != t.Title || motivation != t.Motivation {
provenance = clearAutoThreadMarker(provenance)
}
changed := title != t.Title || motivation != t.Motivation || priority != t.Priority ||
tags != t.Tags || provenance != t.Provenance
if !changed { if !changed {
return false, nil return false, nil
} }
_, err = k.db.Exec( _, err = k.db.Exec(
`UPDATE threads SET title=?, motivation=?, priority=?, tags=?, updated_at=datetime('now') WHERE id=?`, `UPDATE threads SET title=?, motivation=?, priority=?, tags=?, provenance=?, updated_at=datetime('now') WHERE id=?`,
title, motivation, priority, tags, id, title, motivation, priority, tags, provenance, id,
) )
return true, err return true, err
} }
// clearAutoThreadMarker removes the auto_thread trigger from a thread's
// provenance JSON, recording that a human or agent has since rewritten it.
// Unparseable or unrelated provenance is returned unchanged.
func clearAutoThreadMarker(provenance string) string {
if !strings.Contains(provenance, "auto_thread") {
return provenance
}
var meta map[string]any
if err := json.Unmarshal([]byte(provenance), &meta); err != nil {
return provenance
}
if trigger, _ := meta["trigger"].(string); trigger != "auto_thread" {
return provenance
}
delete(meta, "trigger")
meta["groomed"] = true
out, err := json.Marshal(meta)
if err != nil {
return provenance
}
return string(out)
}
func (k *KnoxDB) LinkObservationToThread(threadID, observationID int64, relevance string) error { func (k *KnoxDB) LinkObservationToThread(threadID, observationID int64, relevance string) error {
tx, err := k.db.Begin() tx, err := k.db.Begin()
if err != nil { if err != nil {
+96
View File
@@ -1,8 +1,10 @@
package db package db
import ( import (
"encoding/json"
"fmt" "fmt"
"path/filepath" "path/filepath"
"strings"
"sync" "sync"
"testing" "testing"
"time" "time"
@@ -734,3 +736,97 @@ func TestRebuildEntriesDeterministicFold(t *testing.T) {
t.Errorf("first/last seen = %q/%q", results[0].FirstSeen, results[0].LastSeen) t.Errorf("first/last seen = %q/%q", results[0].FirstSeen, results[0].LastSeen)
} }
} }
// A polished auto-thread has to leave the draft queue. Drafts are detected by
// the auto_thread provenance marker (or an "Auto-detected" motivation), and
// nothing cleared either of them before: an edited thread was a draft forever.
func TestUpdateThreadClearsAutoThreadMarker(t *testing.T) {
k := newTestDB(t)
prov := `{"cluster":"plant / care / dashboard","score":3.9,"sources":["git","gitea"],"trigger":"auto_thread"}`
id, err := k.CreateThread("plant / care / dashboard", "Auto-detected cluster of 4 observations",
"medium", "[]", prov)
if err != nil {
t.Fatalf("create: %v", err)
}
changed, err := k.UpdateThread(id, "plant-care: watering analytics", "", "", "")
if err != nil {
t.Fatalf("update: %v", err)
}
if !changed {
t.Fatal("update reported no change")
}
got, err := k.GetThread(id)
if err != nil || got == nil {
t.Fatalf("get: %v", err)
}
if strings.Contains(got.Provenance, "auto_thread") {
t.Fatalf("auto_thread marker survived a retitle: %s", got.Provenance)
}
var meta map[string]any
if err := json.Unmarshal([]byte(got.Provenance), &meta); err != nil {
t.Fatalf("provenance is not JSON: %v (%s)", err, got.Provenance)
}
if meta["cluster"] != "plant / care / dashboard" {
t.Fatalf("cluster dropped from provenance: %v", meta)
}
if meta["groomed"] != true {
t.Fatalf("groomed flag not recorded: %v", meta)
}
// A motivation rewrite alone is also an editorial pass.
other, _ := k.CreateThread("zigbee / docker / stack", "Auto-detected cluster", "medium", "[]",
`{"trigger":"auto_thread","cluster":"zigbee"}`)
if _, err := k.UpdateThread(other, "", "Build the klubhaus Zigbee stack", "", ""); err != nil {
t.Fatalf("update motivation: %v", err)
}
if got, _ := k.GetThread(other); strings.Contains(got.Provenance, "auto_thread") {
t.Fatalf("auto_thread marker survived a motivation rewrite: %s", got.Provenance)
}
// Tag-only edits are not editorial: the thread is still a draft.
third, _ := k.CreateThread("kuma / uptime", "Auto-detected cluster", "medium", "[]",
`{"trigger":"auto_thread"}`)
if _, err := k.UpdateThread(third, "", "", "", "uptime monitoring"); err != nil {
t.Fatalf("update tags: %v", err)
}
if got, _ := k.GetThread(third); !strings.Contains(got.Provenance, "auto_thread") {
t.Fatalf("a tag-only edit cleared the draft marker: %s", got.Provenance)
}
// Provenance that was never auto-generated is left alone.
own, _ := k.CreateThread("manual thread", "by hand", "high", "[]", `{"trigger":"display-watch","host":"surf"}`)
if _, err := k.UpdateThread(own, "manual thread, renamed", "", "", ""); err != nil {
t.Fatalf("update manual: %v", err)
}
if got, _ := k.GetThread(own); got.Provenance != `{"trigger":"display-watch","host":"surf"}` {
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)
}
}
}