chore: checkpoint uncommitted work before session store fix

Pre-existing work-in-progress in the tree: karakeep, snippets,
skills-health and redaction ingesters, the opencode.db session sweep,
gossip and metrics changes. Checkpointed as-is so the adapter fix in the
next commit stays reviewable on its own.
This commit is contained in:
2026-11-01 13:33:36 -08:00
parent 876d2aa45f
commit edf6c03a7c
43 changed files with 6828 additions and 492 deletions
+121
View File
@@ -0,0 +1,121 @@
// Package skillsrepo checks the health of the skills git repository
// (~/.skills). The skills MCP auto-commits and auto-pushes, but it swallows
// git errors, so a corrupt repo (e.g. from disk exhaustion mid-write) can rot
// silently. This package surfaces that state so knox can warn about it.
package skillsrepo
import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
)
// Health is a snapshot of the skills repo's git + disk state.
type Health struct {
Dir string
IsRepo bool
Head string
Branch string
Ahead int
Behind int
Dirty int
FsckOK bool
FreeBytes uint64
Problems []string
}
// Healthy reports whether the repo is in a state the MCP can commit/push from.
func (h Health) Healthy() bool {
return h.IsRepo && h.FsckOK && h.Head != "" && len(h.Problems) == 0
}
// Default checks ~/.skills.
func Default() Health {
home, _ := os.UserHomeDir()
return Check(filepath.Join(home, ".skills"))
}
// Check inspects the git repo at dir. It never mutates anything.
func Check(dir string) Health {
h := Health{Dir: dir, FsckOK: true}
if fi, err := os.Stat(filepath.Join(dir, ".git")); err != nil || !fi.IsDir() {
h.IsRepo = false
h.Problems = append(h.Problems, "no .git directory")
h.FreeBytes = freeBytes(dir)
return h
}
h.IsRepo = true
// HEAD must be readable — the tell-tale of object corruption.
if out, err := git(dir, "rev-parse", "HEAD"); err != nil {
h.Problems = append(h.Problems, "HEAD unreadable ("+firstLine(err.Error())+")")
} else {
h.Head = out
}
if out, err := git(dir, "rev-parse", "--abbrev-ref", "HEAD"); err == nil {
h.Branch = out
}
// Object integrity.
if _, err := git(dir, "fsck", "--connectivity-only", "--no-progress"); err != nil {
h.FsckOK = false
h.Problems = append(h.Problems, "git fsck failed")
}
// Working tree.
if out, err := git(dir, "status", "--porcelain"); err == nil {
h.Dirty = countLines(out)
}
// Ahead/behind vs upstream.
if out, err := git(dir, "rev-list", "--left-right", "--count", "HEAD...@{upstream}"); err == nil {
f := strings.Fields(out)
if len(f) == 2 {
h.Ahead, _ = strconv.Atoi(f[0])
h.Behind, _ = strconv.Atoi(f[1])
}
}
h.FreeBytes = freeBytes(dir)
if h.FreeBytes > 0 && h.FreeBytes < 1<<30 { // < 1 GiB free
h.Problems = append(h.Problems, "low disk space (<1 GiB)")
}
return h
}
func git(dir string, args ...string) (string, error) {
cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
cmd.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1")
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
func freeBytes(path string) uint64 {
var st syscall.Statfs_t
if err := syscall.Statfs(path, &st); err != nil {
return 0
}
return st.Bavail * uint64(st.Bsize)
}
func countLines(s string) int {
s = strings.TrimSpace(s)
if s == "" {
return 0
}
return len(strings.Split(s, "\n"))
}
func firstLine(s string) string {
if i := strings.IndexByte(s, '\n'); i >= 0 {
return s[:i]
}
return s
}
+58
View File
@@ -0,0 +1,58 @@
package skillsrepo
import (
"os"
"os/exec"
"path/filepath"
"testing"
)
func gitInit(t *testing.T, dir string) {
t.Helper()
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
run := func(args ...string) {
full := append([]string{"-c", "user.email=t@t", "-c", "user.name=t", "-C", dir}, args...)
if out, err := exec.Command("git", full...).CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
run("init", "-q")
if err := os.WriteFile(filepath.Join(dir, "a.txt"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
run("add", ".")
run("commit", "-q", "-m", "init")
}
func TestCheckHealthyRepo(t *testing.T) {
dir := t.TempDir()
gitInit(t, dir)
h := Check(dir)
if !h.IsRepo {
t.Fatal("should be a repo")
}
if h.Head == "" {
t.Fatalf("HEAD should be readable: %+v", h)
}
if !h.FsckOK {
t.Fatalf("fsck should pass: %+v", h)
}
if h.FreeBytes == 0 {
t.Fatal("free bytes should be reported")
}
}
func TestCheckNotARepo(t *testing.T) {
h := Check(t.TempDir())
if h.IsRepo {
t.Fatal("should not be a repo")
}
if h.Healthy() {
t.Fatal("non-repo must not be healthy")
}
if len(h.Problems) == 0 {
t.Fatal("expected a problem note")
}
}
+54
View File
@@ -0,0 +1,54 @@
package skillsrepo
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
// RepoDir returns the skills repo root (~/.skills). It is the same location the
// skills MCP owns; knox writes here only when a snippet is explicitly encoded
// as a skill.
func RepoDir() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".skills")
}
var slugRe = regexp.MustCompile(`[^a-z0-9]+`)
// Slugify turns a title into a filesystem-safe skill directory name.
func Slugify(s string) string {
s = strings.ToLower(strings.TrimSpace(s))
s = slugRe.ReplaceAllString(s, "-")
s = strings.Trim(s, "-")
if s == "" {
return "snippet"
}
if len(s) > 60 {
s = strings.Trim(s[:60], "-")
}
return s
}
// WriteSkill writes content to <repo>/<slug>/SKILL.md. It refuses to clobber an
// existing skill unless overwrite is true, and returns the written path. It does
// not commit: the skills MCP owns versioning, so an externally written skill
// simply shows up as a working-tree change for it (or the user) to commit.
func WriteSkill(name, content string, overwrite bool) (string, error) {
slug := Slugify(name)
path := filepath.Join(RepoDir(), slug, "SKILL.md")
if !overwrite {
if _, err := os.Stat(path); err == nil {
return "", fmt.Errorf("skill %q already exists at %s (pass overwrite to replace)", slug, path)
}
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return "", fmt.Errorf("mkdir skill dir: %w", err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
return "", fmt.Errorf("write skill: %w", err)
}
return path, nil
}