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:
+110
-32
@@ -2,10 +2,12 @@ package ingest
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -17,19 +19,23 @@ type LogIngester struct {
|
||||
}
|
||||
|
||||
func NewLogIngester() *LogIngester {
|
||||
return &LogIngester{Version: "log-parser/v1"}
|
||||
return &LogIngester{Version: "log-parser/v2"}
|
||||
}
|
||||
|
||||
func (l *LogIngester) SourceID() string { return "opencode-log" }
|
||||
|
||||
var (
|
||||
sessionIDRE = regexp.MustCompile(`session[=_ ]?([a-zA-Z0-9_-]+)`)
|
||||
projectIDRE = regexp.MustCompile(`project[=_ ]?([a-zA-Z0-9_.-]+)`)
|
||||
modelRE = regexp.MustCompile(`model[=_ ]?([a-zA-Z0-9_.-]+)`)
|
||||
agentRE = regexp.MustCompile(`agent[=_ ]?([a-zA-Z0-9_.-]+)`)
|
||||
errorRE = regexp.MustCompile(`(?i)(error|fail|exception|panic|timeout)`)
|
||||
levelRE = regexp.MustCompile(`\blevel=([A-Za-z]+)`)
|
||||
)
|
||||
|
||||
// Log lines are structured as `timestamp=... level=... run=... message=...`.
|
||||
// Only real WARN/ERROR/FATAL lines are "errors"; the previous substring match
|
||||
// flagged INFO permission lines that merely contained the word "timeout".
|
||||
var seriousLevels = map[string]bool{"ERROR": true, "WARN": true, "FATAL": true}
|
||||
|
||||
func (l *LogIngester) Ingest(path string) (*IngestResult, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
@@ -41,12 +47,14 @@ func (l *LogIngester) Ingest(path string) (*IngestResult, error) {
|
||||
sessionIDs []string
|
||||
models []string
|
||||
agents []string
|
||||
errors []string
|
||||
signatures []string
|
||||
totalLines int
|
||||
firstError int
|
||||
errCount int
|
||||
warnCount int
|
||||
)
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
totalLines++
|
||||
@@ -60,39 +68,37 @@ func (l *LogIngester) Ingest(path string) (*IngestResult, error) {
|
||||
if m := agentRE.FindStringSubmatch(line); len(m) > 1 {
|
||||
agents = append(agents, m[1])
|
||||
}
|
||||
if errorRE.MatchString(line) {
|
||||
if len(errors) == 0 {
|
||||
firstError = totalLines
|
||||
}
|
||||
errors = append(errors, truncate(line, 120))
|
||||
|
||||
m := levelRE.FindStringSubmatch(line)
|
||||
if len(m) < 2 {
|
||||
continue
|
||||
}
|
||||
level := strings.ToUpper(m[1])
|
||||
if !seriousLevels[level] {
|
||||
continue
|
||||
}
|
||||
if level == "WARN" {
|
||||
warnCount++
|
||||
} else {
|
||||
errCount++
|
||||
}
|
||||
signatures = append(signatures, normalizeLogLine(line))
|
||||
}
|
||||
|
||||
base := filepath.Base(path)
|
||||
fp := Fingerprint([]byte("log:" + base))
|
||||
title := fmt.Sprintf("Log %s (%d lines)", base, totalLines)
|
||||
createdAt := ""
|
||||
if fi, err := os.Stat(path); err == nil {
|
||||
createdAt = fi.ModTime().UTC().Format(time.RFC3339)
|
||||
}
|
||||
|
||||
var summaryParts []string
|
||||
if len(sessionIDs) > 0 {
|
||||
summaryParts = append(summaryParts, fmt.Sprintf("sessions: %s", uniqueJoin(sessionIDs, 5)))
|
||||
}
|
||||
if len(models) > 0 {
|
||||
summaryParts = append(summaryParts, fmt.Sprintf("models: %s", uniqueJoin(models, 3)))
|
||||
}
|
||||
if len(errors) > 0 {
|
||||
summaryParts = append(summaryParts, fmt.Sprintf("%d errors (line %d)", len(errors), firstError))
|
||||
}
|
||||
summary := strings.Join(summaryParts, " | ")
|
||||
if summary == "" {
|
||||
summary = fmt.Sprintf("%d lines", totalLines)
|
||||
}
|
||||
uniq := uniqueSorted(signatures)
|
||||
contentHash := hashSignatures(uniq, errCount, warnCount)
|
||||
|
||||
summary := buildLogSummary(uniq, errCount, warnCount)
|
||||
|
||||
confidence := 0.6
|
||||
if len(errors) > 0 {
|
||||
if errCount > 0 || warnCount > 0 {
|
||||
confidence = 0.8
|
||||
}
|
||||
|
||||
@@ -101,22 +107,94 @@ func (l *LogIngester) Ingest(path string) (*IngestResult, error) {
|
||||
SourceID: l.SourceID(),
|
||||
SourcePath: base,
|
||||
ContentType: ".log",
|
||||
Title: title,
|
||||
Title: "Log " + base,
|
||||
Summary: summary,
|
||||
Content: strings.Join(uniq, "\n"),
|
||||
CreatedAt: createdAt,
|
||||
Confidence: confidence,
|
||||
IngesterVersion: l.Version,
|
||||
LineEnd: totalLines,
|
||||
ContentHash: contentHash,
|
||||
Provenance: map[string]any{
|
||||
"session_ids": uniqueSlice(sessionIDs),
|
||||
"models": uniqueSlice(models),
|
||||
"agents": uniqueSlice(agents),
|
||||
"error_count": len(errors),
|
||||
"file_size": fmt.Sprintf("%d lines", totalLines),
|
||||
"session_ids": uniqueSlice(sessionIDs),
|
||||
"models": uniqueSlice(models),
|
||||
"agents": uniqueSlice(agents),
|
||||
"error_count": errCount,
|
||||
"warn_count": warnCount,
|
||||
"errors_sample": firstN(uniq, 5),
|
||||
"file_size": fmt.Sprintf("%d lines", totalLines),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Normalization patterns strip values that change between identical log events
|
||||
// so the same error yields the same signature.
|
||||
var (
|
||||
tsPrefixRE = regexp.MustCompile(`^timestamp=\S+\s*`)
|
||||
runRE = regexp.MustCompile(`run=[0-9a-fA-F]+`)
|
||||
sesRE = regexp.MustCompile(`ses_[A-Za-z0-9]+`)
|
||||
msgRE = regexp.MustCompile(`msg_[A-Za-z0-9]+`)
|
||||
hexRE = regexp.MustCompile(`\b[0-9a-fA-F]{6,}\b`)
|
||||
numRE = regexp.MustCompile(`[0-9]+`)
|
||||
)
|
||||
|
||||
func normalizeLogLine(line string) string {
|
||||
s := tsPrefixRE.ReplaceAllString(line, "")
|
||||
s = runRE.ReplaceAllString(s, "run=<RUN>")
|
||||
s = sesRE.ReplaceAllString(s, "<SES>")
|
||||
s = msgRE.ReplaceAllString(s, "<MSG>")
|
||||
s = hexRE.ReplaceAllString(s, "<HEX>")
|
||||
s = numRE.ReplaceAllString(s, "<N>")
|
||||
s = strings.Join(strings.Fields(s), " ")
|
||||
return truncate(s, 160)
|
||||
}
|
||||
|
||||
// hashSignatures is the change signal: it depends only on the set of distinct
|
||||
// error signatures and the level counts — not on total line count or ordering.
|
||||
func hashSignatures(sigs []string, errCount, warnCount int) string {
|
||||
h := sha256.New()
|
||||
for _, s := range sigs {
|
||||
h.Write([]byte(s))
|
||||
h.Write([]byte{'\n'})
|
||||
}
|
||||
fmt.Fprintf(h, "E=%d W=%d", errCount, warnCount)
|
||||
return fmt.Sprintf("%x", h.Sum(nil)[:8])
|
||||
}
|
||||
|
||||
func buildLogSummary(sigs []string, errCount, warnCount int) string {
|
||||
if errCount == 0 && warnCount == 0 {
|
||||
return "no errors"
|
||||
}
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "levels: E%d W%d", errCount, warnCount)
|
||||
if len(sigs) > 0 {
|
||||
b.WriteString(" | err: ")
|
||||
b.WriteString(strings.Join(firstN(sigs, 5), " ;; "))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// uniqueSorted returns de-duplicated, lexicographically sorted signatures.
|
||||
func uniqueSorted(items []string) []string {
|
||||
seen := make(map[string]bool)
|
||||
var out []string
|
||||
for _, s := range items {
|
||||
if !seen[s] {
|
||||
seen[s] = true
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
func firstN(items []string, n int) []string {
|
||||
if len(items) > n {
|
||||
return items[:n]
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func uniqueJoin(items []string, max int) string {
|
||||
uniq := uniqueSlice(items)
|
||||
if len(uniq) > max {
|
||||
|
||||
Reference in New Issue
Block a user