Files
knox/internal/ingest/skills.go
T
david 8d6d73b747 fix: read opencode sessions from session_v2/session_message (#7)
Fixes #6.

opencode 2.0.24 migrated its session store on 2026-11-01 21:01:20 UTC. `session`, `message` and `part` stopped being written; sessions now live in `session_v2` and messages in `session_message`. The ingest kept querying the abandoned tables, so every sweep after the migration logged `0 new` — no error, no new sessions indexed.

## Changes

- **`internal/ingest/opencode_db.go`**
  - Read sessions from `session_v2` (title is nullable there, the existing COALESCE + fallback still apply).
  - Rebuild transcripts from both message shapes: user turns at `$.text`, assistant turns from `$.content[]` filtered to `type='text'` (reasoning and tool parts are not indexed). Ordered by `seq`, then part index.
  - Count messages from `session_message` instead of parts.
  - Drop `<system-reminder>` text — reminders now arrive as `synthetic` messages and would eat the 4000-byte summary budget.
  - Ingester version is now `opencode-db/v2`.
- **`internal/watch/watch.go`** — an entry is no longer skipped on `created_at` alone. Resuming a session appends messages without touching the creation time, so the summary is compared too.
- **`internal/ingest/opencode_db_test.go`** — new test builds the v2 schema in a temp SQLite database and asserts user text and assistant text parts are indexed while reasoning, tool and reminder content is not.

## Validation

- `go build ./...`, `go test ./internal/ingest/ ./internal/db/ ./internal/watch/` pass.
- Rebuilt the binary and restarted `knox.service` against the live store:
  ```
  [knox] opencode.db: 1 new, 387 unchanged
  ```
  The new entry is the session that had been invisible since the migration (`Knox OpenCode session adapter compatibility check`, observation version `opencode-db/v2`), and `knox query "knox opencode session adapter compatibility"` finds it.
- 59 pre-migration sessions re-recorded because their stored transcripts contained reminder text that the new filter drops; summaries still carry the conversation.

## Notes

The first commit on this branch is a checkpoint of pre-existing uncommitted work that the adapter fix sits on top of; the fix itself is the second commit (`e07ae8a`).

Reviewed-on: #7
Co-authored-by: David Gwilliam <dhgwilliam@gmail.com>
Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
2026-11-02 01:16:50 +00:00

137 lines
3.2 KiB
Go

package ingest
import (
"fmt"
"os"
"path/filepath"
"strings"
)
var _ Ingester = (*SkillsIngester)(nil)
type SkillsIngester struct {
Version string
}
func NewSkillsIngester() *SkillsIngester {
return &SkillsIngester{Version: "skills-index/v1"}
}
func (s *SkillsIngester) SourceID() string { return "skills-catalog" }
func (s *SkillsIngester) Ingest(path string) (*IngestResult, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read skill: %w", err)
}
if len(data) < 20 {
return nil, nil
}
content := string(data)
title := extractSkillName(content, path)
summary := extractSkillSummary(content)
tags := extractSkillTags(content)
projects := extractProjects(tags)
createdAt := extractCreatedAt(content)
fp := FingerprintWithMeta(data, map[string]string{
"source": "skills-catalog",
"title": title,
})
return &IngestResult{
Fingerprint: fp,
SourceID: s.SourceID(),
SourcePath: path,
Project: projects,
ContentType: ".md",
Title: title,
Summary: summary,
Content: content,
CreatedAt: createdAt,
LineStart: 0,
LineEnd: len(strings.Split(content, "\n")),
Confidence: 0.9,
IngesterVersion: s.Version,
Provenance: map[string]any{
"skill_name": title,
"tags": tags,
"line_count": len(strings.Split(content, "\n")),
},
}, nil
}
func extractSkillName(content, path string) string {
base := filepath.Base(filepath.Dir(path))
if base != "" && base != "." && base != "/" {
return base
}
return strings.TrimSuffix(filepath.Base(path), ".md")
}
func extractSkillSummary(content string) string {
for _, line := range strings.Split(content, "\n") {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "description:") {
desc := strings.TrimSpace(trimmed[len("description:"):])
if len(desc) > 200 {
desc = desc[:200] + "..."
}
return desc
}
}
for _, line := range strings.Split(content, "\n") {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "# ") {
continue
}
if trimmed != "" && !strings.HasPrefix(trimmed, "---") {
return truncate(trimmed, 200)
}
}
return ""
}
func extractSkillTags(content string) []string {
for _, line := range strings.Split(content, "\n") {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "tags:") {
raw := strings.TrimSpace(trimmed[len("tags:"):])
raw = strings.Trim(raw, "\"[]")
var tags []string
for _, t := range strings.Split(raw, ",") {
t = strings.TrimSpace(t)
t = strings.Trim(t, "\" ")
if t != "" {
tags = append(tags, t)
}
}
return tags
}
}
return nil
}
func extractProjects(tags []string) string {
for _, t := range tags {
if strings.HasPrefix(t, "scope:project:") {
return strings.TrimPrefix(t, "scope:project:")
}
if strings.HasPrefix(t, "scope:machine:") {
return "[machine]"
}
}
return ""
}
func extractCreatedAt(content string) string {
for _, line := range strings.Split(content, "\n") {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "created_at:") || strings.HasPrefix(trimmed, "updated_at:") {
return strings.TrimSpace(trimmed[strings.Index(trimmed, ":")+1:])
}
}
return ""
}