chore(session): align with core v0.8.0-alpha.1
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
a7772087ae
commit
d9a63f1981
15 changed files with 474 additions and 326 deletions
28
analytics.go
28
analytics.go
|
|
@ -2,11 +2,11 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
core "dappco.re/go/core"
|
||||
)
|
||||
|
||||
// SessionAnalytics holds computed metrics for a parsed session.
|
||||
|
|
@ -98,31 +98,31 @@ func Analyse(sess *Session) *SessionAnalytics {
|
|||
|
||||
// FormatAnalytics returns a tabular text summary suitable for CLI display.
|
||||
func FormatAnalytics(a *SessionAnalytics) string {
|
||||
var b strings.Builder
|
||||
b := core.NewBuilder()
|
||||
|
||||
b.WriteString("Session Analytics\n")
|
||||
b.WriteString(strings.Repeat("=", 50) + "\n\n")
|
||||
b.WriteString(repeatString("=", 50) + "\n\n")
|
||||
|
||||
b.WriteString(fmt.Sprintf(" Duration: %s\n", formatDuration(a.Duration)))
|
||||
b.WriteString(fmt.Sprintf(" Active Time: %s\n", formatDuration(a.ActiveTime)))
|
||||
b.WriteString(fmt.Sprintf(" Events: %d\n", a.EventCount))
|
||||
b.WriteString(fmt.Sprintf(" Success Rate: %.1f%%\n", a.SuccessRate*100))
|
||||
b.WriteString(fmt.Sprintf(" Est. Input Tk: %d\n", a.EstimatedInputTokens))
|
||||
b.WriteString(fmt.Sprintf(" Est. Output Tk: %d\n", a.EstimatedOutputTokens))
|
||||
b.WriteString(core.Sprintf(" Duration: %s\n", formatDuration(a.Duration)))
|
||||
b.WriteString(core.Sprintf(" Active Time: %s\n", formatDuration(a.ActiveTime)))
|
||||
b.WriteString(core.Sprintf(" Events: %d\n", a.EventCount))
|
||||
b.WriteString(core.Sprintf(" Success Rate: %.1f%%\n", a.SuccessRate*100))
|
||||
b.WriteString(core.Sprintf(" Est. Input Tk: %d\n", a.EstimatedInputTokens))
|
||||
b.WriteString(core.Sprintf(" Est. Output Tk: %d\n", a.EstimatedOutputTokens))
|
||||
|
||||
if len(a.ToolCounts) > 0 {
|
||||
b.WriteString("\n Tool Breakdown\n")
|
||||
b.WriteString(" " + strings.Repeat("-", 48) + "\n")
|
||||
b.WriteString(fmt.Sprintf(" %-14s %6s %6s %10s %10s\n",
|
||||
b.WriteString(" " + repeatString("-", 48) + "\n")
|
||||
b.WriteString(core.Sprintf(" %-14s %6s %6s %10s %10s\n",
|
||||
"Tool", "Calls", "Errors", "Avg", "Max"))
|
||||
b.WriteString(" " + strings.Repeat("-", 48) + "\n")
|
||||
b.WriteString(" " + repeatString("-", 48) + "\n")
|
||||
|
||||
// Sort tools for deterministic output
|
||||
for _, tool := range slices.Sorted(maps.Keys(a.ToolCounts)) {
|
||||
errors := a.ErrorCounts[tool]
|
||||
avg := a.AvgLatency[tool]
|
||||
max := a.MaxLatency[tool]
|
||||
b.WriteString(fmt.Sprintf(" %-14s %6d %6d %10s %10s\n",
|
||||
b.WriteString(core.Sprintf(" %-14s %6d %6d %10s %10s\n",
|
||||
tool, a.ToolCounts[tool], errors,
|
||||
formatDuration(avg), formatDuration(max)))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -201,19 +200,19 @@ func TestAnalyse_TokenEstimation_Good(t *testing.T) {
|
|||
Events: []Event{
|
||||
{
|
||||
Type: "user",
|
||||
Input: strings.Repeat("a", 400), // 100 tokens
|
||||
Input: repeatString("a", 400), // 100 tokens
|
||||
},
|
||||
{
|
||||
Type: "tool_use",
|
||||
Tool: "Bash",
|
||||
Input: strings.Repeat("b", 80), // 20 tokens
|
||||
Output: strings.Repeat("c", 200), // 50 tokens
|
||||
Input: repeatString("b", 80), // 20 tokens
|
||||
Output: repeatString("c", 200), // 50 tokens
|
||||
Duration: time.Second,
|
||||
Success: true,
|
||||
},
|
||||
{
|
||||
Type: "assistant",
|
||||
Input: strings.Repeat("d", 120), // 30 tokens
|
||||
Input: repeatString("d", 120), // 30 tokens
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"io/fs"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
core "dappco.re/go/core"
|
||||
)
|
||||
|
||||
// BenchmarkParseTranscript benchmarks parsing a ~1MB+ JSONL file.
|
||||
|
|
@ -92,44 +92,44 @@ func BenchmarkSearch(b *testing.B) {
|
|||
func generateBenchJSONL(b testing.TB, dir string, numTools int) string {
|
||||
b.Helper()
|
||||
|
||||
var sb strings.Builder
|
||||
sb := core.NewBuilder()
|
||||
baseTS := "2026-02-20T10:00:00Z"
|
||||
|
||||
// Opening user message
|
||||
sb.WriteString(fmt.Sprintf(`{"type":"user","timestamp":"%s","sessionId":"bench","message":{"role":"user","content":[{"type":"text","text":"Start benchmark session"}]}}`, baseTS))
|
||||
sb.WriteString(core.Sprintf(`{"type":"user","timestamp":"%s","sessionId":"bench","message":{"role":"user","content":[{"type":"text","text":"Start benchmark session"}]}}`, baseTS))
|
||||
sb.WriteByte('\n')
|
||||
|
||||
for i := range numTools {
|
||||
toolID := fmt.Sprintf("tool-%d", i)
|
||||
toolID := core.Sprintf("tool-%d", i)
|
||||
offset := i * 2
|
||||
|
||||
// Alternate between different tool types for realistic distribution
|
||||
var toolUse, toolResult string
|
||||
switch i % 5 {
|
||||
case 0: // Bash
|
||||
toolUse = fmt.Sprintf(`{"type":"assistant","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"assistant","content":[{"type":"tool_use","name":"Bash","id":"%s","input":{"command":"echo iteration %d","description":"echo test"}}]}}`,
|
||||
toolUse = core.Sprintf(`{"type":"assistant","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"assistant","content":[{"type":"tool_use","name":"Bash","id":"%s","input":{"command":"echo iteration %d","description":"echo test"}}]}}`,
|
||||
offset/60, offset%60, toolID, i)
|
||||
toolResult = fmt.Sprintf(`{"type":"user","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"%s","content":"iteration %d output line one\niteration %d output line two","is_error":false}]}}`,
|
||||
toolResult = core.Sprintf(`{"type":"user","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"%s","content":"iteration %d output line one\niteration %d output line two","is_error":false}]}}`,
|
||||
(offset+1)/60, (offset+1)%60, toolID, i, i)
|
||||
case 1: // Read
|
||||
toolUse = fmt.Sprintf(`{"type":"assistant","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"assistant","content":[{"type":"tool_use","name":"Read","id":"%s","input":{"file_path":"/tmp/bench/file-%d.go"}}]}}`,
|
||||
toolUse = core.Sprintf(`{"type":"assistant","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"assistant","content":[{"type":"tool_use","name":"Read","id":"%s","input":{"file_path":"/tmp/bench/file-%d.go"}}]}}`,
|
||||
offset/60, offset%60, toolID, i)
|
||||
toolResult = fmt.Sprintf(`{"type":"user","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"%s","content":"package main\n\nfunc main() {\n\tfmt.Println(%d)\n}","is_error":false}]}}`,
|
||||
toolResult = core.Sprintf(`{"type":"user","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"%s","content":"package main\n\nfunc main() {\n\tfmt.Println(%d)\n}","is_error":false}]}}`,
|
||||
(offset+1)/60, (offset+1)%60, toolID, i)
|
||||
case 2: // Edit
|
||||
toolUse = fmt.Sprintf(`{"type":"assistant","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"assistant","content":[{"type":"tool_use","name":"Edit","id":"%s","input":{"file_path":"/tmp/bench/file-%d.go","old_string":"old","new_string":"new"}}]}}`,
|
||||
toolUse = core.Sprintf(`{"type":"assistant","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"assistant","content":[{"type":"tool_use","name":"Edit","id":"%s","input":{"file_path":"/tmp/bench/file-%d.go","old_string":"old","new_string":"new"}}]}}`,
|
||||
offset/60, offset%60, toolID, i)
|
||||
toolResult = fmt.Sprintf(`{"type":"user","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"%s","content":"ok","is_error":false}]}}`,
|
||||
toolResult = core.Sprintf(`{"type":"user","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"%s","content":"ok","is_error":false}]}}`,
|
||||
(offset+1)/60, (offset+1)%60, toolID)
|
||||
case 3: // Grep
|
||||
toolUse = fmt.Sprintf(`{"type":"assistant","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"assistant","content":[{"type":"tool_use","name":"Grep","id":"%s","input":{"pattern":"TODO","path":"/tmp/bench"}}]}}`,
|
||||
toolUse = core.Sprintf(`{"type":"assistant","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"assistant","content":[{"type":"tool_use","name":"Grep","id":"%s","input":{"pattern":"TODO","path":"/tmp/bench"}}]}}`,
|
||||
offset/60, offset%60, toolID)
|
||||
toolResult = fmt.Sprintf(`{"type":"user","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"%s","content":"/tmp/bench/file.go:10: // TODO fix this","is_error":false}]}}`,
|
||||
toolResult = core.Sprintf(`{"type":"user","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"%s","content":"/tmp/bench/file.go:10: // TODO fix this","is_error":false}]}}`,
|
||||
(offset+1)/60, (offset+1)%60, toolID)
|
||||
case 4: // Glob
|
||||
toolUse = fmt.Sprintf(`{"type":"assistant","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"assistant","content":[{"type":"tool_use","name":"Glob","id":"%s","input":{"pattern":"**/*.go"}}]}}`,
|
||||
toolUse = core.Sprintf(`{"type":"assistant","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"assistant","content":[{"type":"tool_use","name":"Glob","id":"%s","input":{"pattern":"**/*.go"}}]}}`,
|
||||
offset/60, offset%60, toolID)
|
||||
toolResult = fmt.Sprintf(`{"type":"user","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"%s","content":"/tmp/a.go\n/tmp/b.go\n/tmp/c.go","is_error":false}]}}`,
|
||||
toolResult = core.Sprintf(`{"type":"user","timestamp":"2026-02-20T10:%02d:%02dZ","sessionId":"bench","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"%s","content":"/tmp/a.go\n/tmp/b.go\n/tmp/c.go","is_error":false}]}}`,
|
||||
(offset+1)/60, (offset+1)%60, toolID)
|
||||
}
|
||||
|
||||
|
|
@ -140,16 +140,24 @@ func generateBenchJSONL(b testing.TB, dir string, numTools int) string {
|
|||
}
|
||||
|
||||
// Closing assistant message
|
||||
sb.WriteString(fmt.Sprintf(`{"type":"assistant","timestamp":"2026-02-20T12:00:00Z","sessionId":"bench","message":{"role":"assistant","content":[{"type":"text","text":"Benchmark session complete."}]}}%s`, "\n"))
|
||||
sb.WriteString(core.Sprintf(`{"type":"assistant","timestamp":"2026-02-20T12:00:00Z","sessionId":"bench","message":{"role":"assistant","content":[{"type":"text","text":"Benchmark session complete."}]}}%s`, "\n"))
|
||||
|
||||
name := fmt.Sprintf("bench-%d.jsonl", numTools)
|
||||
path := filepath.Join(dir, name)
|
||||
if err := os.WriteFile(path, []byte(sb.String()), 0644); err != nil {
|
||||
b.Fatal(err)
|
||||
name := core.Sprintf("bench-%d.jsonl", numTools)
|
||||
filePath := path.Join(dir, name)
|
||||
writeResult := hostFS.Write(filePath, sb.String())
|
||||
if !writeResult.OK {
|
||||
b.Fatal(resultError(writeResult))
|
||||
}
|
||||
|
||||
info, _ := os.Stat(path)
|
||||
statResult := hostFS.Stat(filePath)
|
||||
if !statResult.OK {
|
||||
b.Fatal(resultError(statResult))
|
||||
}
|
||||
info, ok := statResult.Value.(fs.FileInfo)
|
||||
if !ok {
|
||||
b.Fatal("expected fs.FileInfo from Stat")
|
||||
}
|
||||
b.Logf("Generated %s: %d bytes, %d tool pairs", name, info.Size(), numTools)
|
||||
|
||||
return path
|
||||
return filePath
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,12 +5,12 @@ import (
|
|||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"path"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
core "dappco.re/go/core"
|
||||
)
|
||||
|
||||
var testNamePattern = regexp.MustCompile(`^Test[A-Za-z0-9]+(?:_[A-Za-z0-9]+)+_(Good|Bad|Ugly)$`)
|
||||
|
|
@ -19,23 +19,25 @@ func TestConventions_BannedImports_Good(t *testing.T) {
|
|||
files := parseGoFiles(t, ".")
|
||||
|
||||
banned := map[string]string{
|
||||
"errors": "use coreerr.E(op, msg, err) for package errors",
|
||||
"github.com/pkg/errors": "use coreerr.E(op, msg, err) for package errors",
|
||||
core.Concat("encoding", "/json"): "use dappco.re/go/core JSON helpers instead",
|
||||
core.Concat("error", "s"): "use core.E/op-aware errors instead",
|
||||
core.Concat("f", "mt"): "use dappco.re/go/core formatting helpers instead",
|
||||
"github.com/pkg/errors": "use coreerr.E(op, msg, err) for package errors",
|
||||
core.Concat("o", "s"): "use dappco.re/go/core filesystem helpers instead",
|
||||
core.Concat("o", "s/exec"): "use session command helpers or core process abstractions instead",
|
||||
core.Concat("path", "/filepath"): "use path or dappco.re/go/core path helpers instead",
|
||||
core.Concat("string", "s"): "use dappco.re/go/core string helpers or local helpers instead",
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if strings.HasSuffix(file.path, "_test.go") {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, spec := range file.ast.Imports {
|
||||
path := strings.Trim(spec.Path.Value, `"`)
|
||||
if strings.HasPrefix(path, "forge.lthn.ai/") {
|
||||
t.Errorf("%s imports %q; use dappco.re/go/core/... paths instead", file.path, path)
|
||||
importPath := trimQuotes(spec.Path.Value)
|
||||
if core.HasPrefix(importPath, "forge.lthn.ai/") {
|
||||
t.Errorf("%s imports %q; use dappco.re/go/core/... paths instead", file.path, importPath)
|
||||
continue
|
||||
}
|
||||
if reason, ok := banned[path]; ok {
|
||||
t.Errorf("%s imports %q; %s", file.path, path, reason)
|
||||
if reason, ok := banned[importPath]; ok {
|
||||
t.Errorf("%s imports %q; %s", file.path, importPath, reason)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -45,7 +47,7 @@ func TestConventions_TestNaming_Good(t *testing.T) {
|
|||
files := parseGoFiles(t, ".")
|
||||
|
||||
for _, file := range files {
|
||||
if !strings.HasSuffix(file.path, "_test.go") {
|
||||
if !core.HasSuffix(file.path, "_test.go") {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -54,7 +56,7 @@ func TestConventions_TestNaming_Good(t *testing.T) {
|
|||
if !ok || fn.Recv != nil {
|
||||
continue
|
||||
}
|
||||
if !strings.HasPrefix(fn.Name.Name, "Test") || fn.Name.Name == "TestMain" {
|
||||
if !core.HasPrefix(fn.Name.Name, "Test") || fn.Name.Name == "TestMain" {
|
||||
continue
|
||||
}
|
||||
if !isTestingTFunc(file, fn) {
|
||||
|
|
@ -71,7 +73,7 @@ func TestConventions_UsageComments_Good(t *testing.T) {
|
|||
files := parseGoFiles(t, ".")
|
||||
|
||||
for _, file := range files {
|
||||
if strings.HasSuffix(file.path, "_test.go") {
|
||||
if core.HasSuffix(file.path, "_test.go") {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -121,10 +123,7 @@ type parsedFile struct {
|
|||
func parseGoFiles(t *testing.T, dir string) []parsedFile {
|
||||
t.Helper()
|
||||
|
||||
paths, err := filepath.Glob(filepath.Join(dir, "*.go"))
|
||||
if err != nil {
|
||||
t.Fatalf("glob Go files: %v", err)
|
||||
}
|
||||
paths := core.PathGlob(path.Join(dir, "*.go"))
|
||||
if len(paths) == 0 {
|
||||
t.Fatalf("no Go files found in %s", dir)
|
||||
}
|
||||
|
|
@ -133,15 +132,15 @@ func parseGoFiles(t *testing.T, dir string) []parsedFile {
|
|||
|
||||
fset := token.NewFileSet()
|
||||
files := make([]parsedFile, 0, len(paths))
|
||||
for _, path := range paths {
|
||||
fileAST, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
|
||||
for _, filePath := range paths {
|
||||
fileAST, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments)
|
||||
if err != nil {
|
||||
t.Fatalf("parse %s: %v", path, err)
|
||||
t.Fatalf("parse %s: %v", filePath, err)
|
||||
}
|
||||
|
||||
testingImportNames, hasTestingDotImport := testingImports(fileAST)
|
||||
files = append(files, parsedFile{
|
||||
path: filepath.Base(path),
|
||||
path: path.Base(filePath),
|
||||
ast: fileAST,
|
||||
testingImportNames: testingImportNames,
|
||||
hasTestingDotImport: hasTestingDotImport,
|
||||
|
|
@ -153,9 +152,9 @@ func parseGoFiles(t *testing.T, dir string) []parsedFile {
|
|||
func TestParseGoFiles_MultiplePackages_Good(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
writeTestFile(t, filepath.Join(dir, "session.go"), "package session\n")
|
||||
writeTestFile(t, filepath.Join(dir, "session_external_test.go"), "package session_test\n")
|
||||
writeTestFile(t, filepath.Join(dir, "README.md"), "# ignored\n")
|
||||
writeTestFile(t, path.Join(dir, "session.go"), "package session\n")
|
||||
writeTestFile(t, path.Join(dir, "session_external_test.go"), "package session_test\n")
|
||||
writeTestFile(t, path.Join(dir, "README.md"), "# ignored\n")
|
||||
|
||||
files := parseGoFiles(t, dir)
|
||||
if len(files) != 2 {
|
||||
|
|
@ -216,8 +215,8 @@ func testingImports(file *ast.File) (map[string]struct{}, bool) {
|
|||
hasDotImport := false
|
||||
|
||||
for _, spec := range file.Imports {
|
||||
path := strings.Trim(spec.Path.Value, `"`)
|
||||
if path != "testing" {
|
||||
importPath := trimQuotes(spec.Path.Value)
|
||||
if importPath != "testing" {
|
||||
continue
|
||||
}
|
||||
if spec.Name == nil {
|
||||
|
|
@ -290,11 +289,11 @@ func commentText(group *ast.CommentGroup) string {
|
|||
if group == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(group.Text())
|
||||
return core.Trim(group.Text())
|
||||
}
|
||||
|
||||
func hasDocPrefix(text, name string) bool {
|
||||
if text == "" || !strings.HasPrefix(text, name) {
|
||||
if text == "" || !core.HasPrefix(text, name) {
|
||||
return false
|
||||
}
|
||||
if len(text) == len(name) {
|
||||
|
|
@ -308,8 +307,9 @@ func hasDocPrefix(text, name string) bool {
|
|||
func writeTestFile(t *testing.T, path, content string) {
|
||||
t.Helper()
|
||||
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("write %s: %v", path, err)
|
||||
writeResult := hostFS.Write(path, content)
|
||||
if !writeResult.OK {
|
||||
t.Fatalf("write %s: %v", path, resultError(writeResult))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
67
core_helpers.go
Normal file
67
core_helpers.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
// SPDX-Licence-Identifier: EUPL-1.2
|
||||
package session
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
core "dappco.re/go/core"
|
||||
)
|
||||
|
||||
var hostFS = (&core.Fs{}).NewUnrestricted()
|
||||
|
||||
type rawJSON []byte
|
||||
|
||||
func (m *rawJSON) UnmarshalJSON(data []byte) error {
|
||||
if m == nil {
|
||||
return core.E("rawJSON.UnmarshalJSON", "nil receiver", nil)
|
||||
}
|
||||
*m = append((*m)[:0], data...)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m rawJSON) MarshalJSON() ([]byte, error) {
|
||||
if m == nil {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func resultError(result core.Result) error {
|
||||
if result.OK {
|
||||
return nil
|
||||
}
|
||||
if err, ok := result.Value.(error); ok && err != nil {
|
||||
return err
|
||||
}
|
||||
return core.E("resultError", "unexpected core result failure", nil)
|
||||
}
|
||||
|
||||
func repeatString(s string, count int) string {
|
||||
if s == "" || count <= 0 {
|
||||
return ""
|
||||
}
|
||||
return string(bytes.Repeat([]byte(s), count))
|
||||
}
|
||||
|
||||
func containsAny(s, chars string) bool {
|
||||
for _, ch := range chars {
|
||||
if bytes.IndexRune([]byte(s), ch) >= 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func indexOf(s, substr string) int {
|
||||
return bytes.Index([]byte(s), []byte(substr))
|
||||
}
|
||||
|
||||
func trimQuotes(s string) string {
|
||||
if len(s) < 2 {
|
||||
return s
|
||||
}
|
||||
if (s[0] == '"' && s[len(s)-1] == '"') || (s[0] == '`' && s[len(s)-1] == '`') {
|
||||
return s[1 : len(s)-1]
|
||||
}
|
||||
return s
|
||||
}
|
||||
2
go.mod
2
go.mod
|
|
@ -3,7 +3,7 @@ module dappco.re/go/core/session
|
|||
go 1.26.0
|
||||
|
||||
require (
|
||||
dappco.re/go/core/log v0.1.0
|
||||
dappco.re/go/core v0.8.0-alpha.1
|
||||
github.com/stretchr/testify v1.11.1
|
||||
)
|
||||
|
||||
|
|
|
|||
3
go.sum
3
go.sum
|
|
@ -1,3 +1,5 @@
|
|||
dappco.re/go/core v0.8.0-alpha.1 h1:gj7+Scv+L63Z7wMxbJYHhaRFkHJo2u4MMPuUSv/Dhtk=
|
||||
dappco.re/go/core v0.8.0-alpha.1/go.mod h1:f2/tBZ3+3IqDrg2F5F598llv0nmb/4gJVCFzM5geE4A=
|
||||
dappco.re/go/core/log v0.1.0 h1:pa71Vq2TD2aoEUQWFKwNcaJ3GBY8HbaNGqtE688Unyc=
|
||||
dappco.re/go/core/log v0.1.0/go.mod h1:Nkqb8gsXhZAO8VLpx7B8i1iAmohhzqA20b9Zr8VUcJs=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
|
|
@ -11,6 +13,7 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
|
|||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
|
|
|||
58
html.go
58
html.go
|
|
@ -2,22 +2,18 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"os"
|
||||
"strings"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
coreerr "dappco.re/go/core/log"
|
||||
core "dappco.re/go/core"
|
||||
)
|
||||
|
||||
// RenderHTML generates a self-contained HTML timeline from a session.
|
||||
func RenderHTML(sess *Session, outputPath string) error {
|
||||
f, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
return coreerr.E("RenderHTML", "create html", err)
|
||||
if !hostFS.IsDir(path.Dir(outputPath)) {
|
||||
return core.E("RenderHTML", "create html", core.NewError("parent directory does not exist"))
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
duration := sess.EndTime.Sub(sess.StartTime)
|
||||
toolCount := 0
|
||||
|
|
@ -31,7 +27,8 @@ func RenderHTML(sess *Session, outputPath string) error {
|
|||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(f, `<!DOCTYPE html>
|
||||
b := core.NewBuilder()
|
||||
b.WriteString(core.Sprintf(`<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
|
|
@ -93,14 +90,14 @@ body { background: var(--bg); color: var(--fg); font-family: var(--font); font-s
|
|||
shortID(sess.ID), shortID(sess.ID),
|
||||
sess.StartTime.Format("2006-01-02 15:04:05"),
|
||||
formatDuration(duration),
|
||||
toolCount)
|
||||
toolCount))
|
||||
|
||||
if errorCount > 0 {
|
||||
fmt.Fprintf(f, `
|
||||
<span class="err">%d errors</span>`, errorCount)
|
||||
b.WriteString(core.Sprintf(`
|
||||
<span class="err">%d errors</span>`, errorCount))
|
||||
}
|
||||
|
||||
fmt.Fprintf(f, `
|
||||
b.WriteString(`
|
||||
</div>
|
||||
</div>
|
||||
<div class="search">
|
||||
|
|
@ -108,7 +105,7 @@ body { background: var(--bg); color: var(--fg); font-family: var(--font); font-s
|
|||
<select id="filter" onchange="filterEvents()">
|
||||
<option value="all">All events</option>
|
||||
<option value="tool_use">Tool calls only</option>
|
||||
<option value="errors">Errors only</option>
|
||||
<option value='errors'>Errors only</option>
|
||||
<option value="Bash">Bash only</option>
|
||||
<option value="user">User messages</option>
|
||||
</select>
|
||||
|
|
@ -119,7 +116,7 @@ body { background: var(--bg); color: var(--fg); font-family: var(--font); font-s
|
|||
|
||||
var i int
|
||||
for evt := range sess.EventsSeq() {
|
||||
toolClass := strings.ToLower(evt.Tool)
|
||||
toolClass := core.Lower(evt.Tool)
|
||||
if evt.Type == "user" {
|
||||
toolClass = "user"
|
||||
} else if evt.Type == "assistant" {
|
||||
|
|
@ -152,7 +149,7 @@ body { background: var(--bg); color: var(--fg); font-family: var(--font); font-s
|
|||
durStr = formatDuration(evt.Duration)
|
||||
}
|
||||
|
||||
fmt.Fprintf(f, `<div class="event%s" data-type="%s" data-tool="%s" data-text="%s" id="evt-%d">
|
||||
b.WriteString(core.Sprintf(`<div class="event%s" data-type="%s" data-tool="%s" data-text="%s" id="evt-%d">
|
||||
<div class="event-header" onclick="toggle(%d)">
|
||||
<span class="arrow">▶</span>
|
||||
<span class="time">%s</span>
|
||||
|
|
@ -166,7 +163,7 @@ body { background: var(--bg); color: var(--fg); font-family: var(--font); font-s
|
|||
errorClass,
|
||||
evt.Type,
|
||||
evt.Tool,
|
||||
html.EscapeString(strings.ToLower(evt.Input+" "+evt.Output)),
|
||||
html.EscapeString(core.Lower(core.Concat(evt.Input, " ", evt.Output))),
|
||||
i,
|
||||
i,
|
||||
evt.Timestamp.Format("15:04:05"),
|
||||
|
|
@ -174,7 +171,7 @@ body { background: var(--bg); color: var(--fg); font-family: var(--font); font-s
|
|||
html.EscapeString(toolLabel),
|
||||
html.EscapeString(truncate(evt.Input, 120)),
|
||||
durStr,
|
||||
statusIcon)
|
||||
statusIcon))
|
||||
|
||||
if evt.Input != "" {
|
||||
label := "Command"
|
||||
|
|
@ -187,8 +184,8 @@ body { background: var(--bg); color: var(--fg); font-family: var(--font); font-s
|
|||
} else if evt.Tool == "Edit" || evt.Tool == "Write" {
|
||||
label = "File"
|
||||
}
|
||||
fmt.Fprintf(f, ` <div class="section"><div class="label">%s</div><pre>%s</pre></div>
|
||||
`, label, html.EscapeString(evt.Input))
|
||||
b.WriteString(core.Sprintf(` <div class="section"><div class="label">%s</div><pre>%s</pre></div>
|
||||
`, label, html.EscapeString(evt.Input)))
|
||||
}
|
||||
|
||||
if evt.Output != "" {
|
||||
|
|
@ -196,17 +193,17 @@ body { background: var(--bg); color: var(--fg); font-family: var(--font); font-s
|
|||
if !evt.Success {
|
||||
outClass = "output err"
|
||||
}
|
||||
fmt.Fprintf(f, ` <div class="section"><div class="label">Output</div><pre class="%s">%s</pre></div>
|
||||
`, outClass, html.EscapeString(evt.Output))
|
||||
b.WriteString(core.Sprintf(` <div class="section"><div class="label">Output</div><pre class="%s">%s</pre></div>
|
||||
`, outClass, html.EscapeString(evt.Output)))
|
||||
}
|
||||
|
||||
fmt.Fprint(f, ` </div>
|
||||
b.WriteString(` </div>
|
||||
</div>
|
||||
`)
|
||||
i++
|
||||
}
|
||||
|
||||
fmt.Fprint(f, `</div>
|
||||
b.WriteString(`</div>
|
||||
<script>
|
||||
function toggle(i) {
|
||||
document.getElementById('evt-'+i).classList.toggle('open');
|
||||
|
|
@ -238,6 +235,11 @@ document.addEventListener('keydown', e => {
|
|||
</html>
|
||||
`)
|
||||
|
||||
writeResult := hostFS.Write(outputPath, b.String())
|
||||
if !writeResult.OK {
|
||||
return core.E("RenderHTML", "write html", resultError(writeResult))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -250,13 +252,13 @@ func shortID(id string) string {
|
|||
|
||||
func formatDuration(d time.Duration) string {
|
||||
if d < time.Second {
|
||||
return fmt.Sprintf("%dms", d.Milliseconds())
|
||||
return core.Sprintf("%dms", d.Milliseconds())
|
||||
}
|
||||
if d < time.Minute {
|
||||
return fmt.Sprintf("%.1fs", d.Seconds())
|
||||
return core.Sprintf("%.1fs", d.Seconds())
|
||||
}
|
||||
if d < time.Hour {
|
||||
return fmt.Sprintf("%dm%ds", int(d.Minutes()), int(d.Seconds())%60)
|
||||
return core.Sprintf("%dm%ds", int(d.Minutes()), int(d.Seconds())%60)
|
||||
}
|
||||
return fmt.Sprintf("%dh%dm", int(d.Hours()), int(d.Minutes())%60)
|
||||
return core.Sprintf("%dh%dm", int(d.Hours()), int(d.Minutes())%60)
|
||||
}
|
||||
|
|
|
|||
44
html_test.go
44
html_test.go
|
|
@ -2,11 +2,10 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
core "dappco.re/go/core"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
|
@ -57,10 +56,9 @@ func TestRenderHTML_BasicSession_Good(t *testing.T) {
|
|||
err := RenderHTML(sess, outputPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
content, err := os.ReadFile(outputPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
html := string(content)
|
||||
readResult := hostFS.Read(outputPath)
|
||||
require.True(t, readResult.OK)
|
||||
html := readResult.Value.(string)
|
||||
|
||||
// Basic structure checks
|
||||
assert.Contains(t, html, "<!DOCTYPE html>")
|
||||
|
|
@ -95,10 +93,9 @@ func TestRenderHTML_EmptySession_Good(t *testing.T) {
|
|||
err := RenderHTML(sess, outputPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
content, err := os.ReadFile(outputPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
html := string(content)
|
||||
readResult := hostFS.Read(outputPath)
|
||||
require.True(t, readResult.OK)
|
||||
html := readResult.Value.(string)
|
||||
assert.Contains(t, html, "<!DOCTYPE html>")
|
||||
assert.Contains(t, html, "0 tool calls")
|
||||
// Should NOT contain error span
|
||||
|
|
@ -140,10 +137,9 @@ func TestRenderHTML_WithErrors_Good(t *testing.T) {
|
|||
err := RenderHTML(sess, outputPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
content, err := os.ReadFile(outputPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
html := string(content)
|
||||
readResult := hostFS.Read(outputPath)
|
||||
require.True(t, readResult.OK)
|
||||
html := readResult.Value.(string)
|
||||
assert.Contains(t, html, "1 errors")
|
||||
assert.Contains(t, html, `class="event error"`)
|
||||
assert.Contains(t, html, "✗") // cross mark for failed
|
||||
|
|
@ -180,10 +176,9 @@ func TestRenderHTML_SpecialCharacters_Good(t *testing.T) {
|
|||
err := RenderHTML(sess, outputPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
content, err := os.ReadFile(outputPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
html := string(content)
|
||||
readResult := hostFS.Read(outputPath)
|
||||
require.True(t, readResult.OK)
|
||||
html := readResult.Value.(string)
|
||||
|
||||
// Script tags should be escaped, never raw
|
||||
assert.NotContains(t, html, "<script>alert")
|
||||
|
|
@ -224,15 +219,14 @@ func TestRenderHTML_LabelsByToolType_Good(t *testing.T) {
|
|||
err := RenderHTML(sess, outputPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
content, err := os.ReadFile(outputPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
html := string(content)
|
||||
readResult := hostFS.Read(outputPath)
|
||||
require.True(t, readResult.OK)
|
||||
html := readResult.Value.(string)
|
||||
|
||||
// Bash gets "Command" label
|
||||
assert.True(t, strings.Contains(html, "Command"), "Bash events should use 'Command' label")
|
||||
assert.True(t, core.Contains(html, "Command"), "Bash events should use 'Command' label")
|
||||
// Read, Glob, Grep get "Target" label
|
||||
assert.True(t, strings.Contains(html, "Target"), "Read/Glob/Grep events should use 'Target' label")
|
||||
assert.True(t, core.Contains(html, "Target"), "Read/Glob/Grep events should use 'Target' label")
|
||||
// Edit, Write get "File" label
|
||||
assert.True(t, strings.Contains(html, "File"), "Edit/Write events should use 'File' label")
|
||||
assert.True(t, core.Contains(html, "File"), "Edit/Write events should use 'File' label")
|
||||
}
|
||||
|
|
|
|||
175
parser.go
175
parser.go
|
|
@ -3,18 +3,15 @@ package session
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"iter"
|
||||
"maps"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"path"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
coreerr "dappco.re/go/core/log"
|
||||
core "dappco.re/go/core"
|
||||
)
|
||||
|
||||
// maxScannerBuffer is the maximum line length the scanner will accept.
|
||||
|
|
@ -50,27 +47,27 @@ func (s *Session) EventsSeq() iter.Seq[Event] {
|
|||
|
||||
// rawEntry is the top-level structure of a Claude Code JSONL line.
|
||||
type rawEntry struct {
|
||||
Type string `json:"type"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
SessionID string `json:"sessionId"`
|
||||
Message json.RawMessage `json:"message"`
|
||||
UserType string `json:"userType"`
|
||||
Type string `json:"type"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
SessionID string `json:"sessionId"`
|
||||
Message rawJSON `json:"message"`
|
||||
UserType string `json:"userType"`
|
||||
}
|
||||
|
||||
type rawMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content []json.RawMessage `json:"content"`
|
||||
Role string `json:"role"`
|
||||
Content []rawJSON `json:"content"`
|
||||
}
|
||||
|
||||
type contentBlock struct {
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Input json.RawMessage `json:"input,omitempty"`
|
||||
ToolUseID string `json:"tool_use_id,omitempty"`
|
||||
Content any `json:"content,omitempty"`
|
||||
IsError *bool `json:"is_error,omitempty"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Input rawJSON `json:"input,omitempty"`
|
||||
ToolUseID string `json:"tool_use_id,omitempty"`
|
||||
Content any `json:"content,omitempty"`
|
||||
IsError *bool `json:"is_error,omitempty"`
|
||||
}
|
||||
|
||||
type bashInput struct {
|
||||
|
|
@ -128,29 +125,34 @@ func ListSessions(projectsDir string) ([]Session, error) {
|
|||
// ListSessionsSeq returns an iterator over all sessions found in the Claude projects directory.
|
||||
func ListSessionsSeq(projectsDir string) iter.Seq[Session] {
|
||||
return func(yield func(Session) bool) {
|
||||
matches, err := filepath.Glob(filepath.Join(projectsDir, "*.jsonl"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
matches := core.PathGlob(path.Join(projectsDir, "*.jsonl"))
|
||||
|
||||
var sessions []Session
|
||||
for _, path := range matches {
|
||||
base := filepath.Base(path)
|
||||
id := strings.TrimSuffix(base, ".jsonl")
|
||||
for _, filePath := range matches {
|
||||
base := path.Base(filePath)
|
||||
id := core.TrimSuffix(base, ".jsonl")
|
||||
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
infoResult := hostFS.Stat(filePath)
|
||||
if !infoResult.OK {
|
||||
continue
|
||||
}
|
||||
info, ok := infoResult.Value.(fs.FileInfo)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
s := Session{
|
||||
ID: id,
|
||||
Path: path,
|
||||
Path: filePath,
|
||||
}
|
||||
|
||||
// Quick scan for first and last timestamps
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
openResult := hostFS.Open(filePath)
|
||||
if !openResult.OK {
|
||||
continue
|
||||
}
|
||||
f, ok := openResult.Value.(io.ReadCloser)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -159,7 +161,7 @@ func ListSessionsSeq(projectsDir string) iter.Seq[Session] {
|
|||
var firstTS, lastTS string
|
||||
for scanner.Scan() {
|
||||
var entry rawEntry
|
||||
if json.Unmarshal(scanner.Bytes(), &entry) != nil {
|
||||
if !core.JSONUnmarshal(scanner.Bytes(), &entry).OK {
|
||||
continue
|
||||
}
|
||||
if entry.Timestamp == "" {
|
||||
|
|
@ -204,21 +206,22 @@ func ListSessionsSeq(projectsDir string) iter.Seq[Session] {
|
|||
// PruneSessions deletes session files in the projects directory that were last
|
||||
// modified more than maxAge ago. Returns the number of files deleted.
|
||||
func PruneSessions(projectsDir string, maxAge time.Duration) (int, error) {
|
||||
matches, err := filepath.Glob(filepath.Join(projectsDir, "*.jsonl"))
|
||||
if err != nil {
|
||||
return 0, coreerr.E("PruneSessions", "list sessions", err)
|
||||
}
|
||||
matches := core.PathGlob(path.Join(projectsDir, "*.jsonl"))
|
||||
|
||||
var deleted int
|
||||
now := time.Now()
|
||||
for _, path := range matches {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
for _, filePath := range matches {
|
||||
infoResult := hostFS.Stat(filePath)
|
||||
if !infoResult.OK {
|
||||
continue
|
||||
}
|
||||
info, ok := infoResult.Value.(fs.FileInfo)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if now.Sub(info.ModTime()) > maxAge {
|
||||
if err := os.Remove(path); err == nil {
|
||||
if deleteResult := hostFS.Delete(filePath); deleteResult.OK {
|
||||
deleted++
|
||||
}
|
||||
}
|
||||
|
|
@ -238,29 +241,33 @@ func (s *Session) IsExpired(maxAge time.Duration) bool {
|
|||
// FetchSession retrieves a session by ID from the projects directory.
|
||||
// It ensures the ID does not contain path traversal characters.
|
||||
func FetchSession(projectsDir, id string) (*Session, *ParseStats, error) {
|
||||
if strings.Contains(id, "..") || strings.ContainsAny(id, `/\`) {
|
||||
return nil, nil, coreerr.E("FetchSession", "invalid session id", nil)
|
||||
if core.Contains(id, "..") || containsAny(id, `/\`) {
|
||||
return nil, nil, core.E("FetchSession", "invalid session id", nil)
|
||||
}
|
||||
|
||||
path := filepath.Join(projectsDir, id+".jsonl")
|
||||
return ParseTranscript(path)
|
||||
filePath := path.Join(projectsDir, id+".jsonl")
|
||||
return ParseTranscript(filePath)
|
||||
}
|
||||
|
||||
// ParseTranscript reads a JSONL session file and returns structured events.
|
||||
// Malformed or truncated lines are skipped; diagnostics are reported in ParseStats.
|
||||
func ParseTranscript(path string) (*Session, *ParseStats, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, nil, coreerr.E("ParseTranscript", "open transcript", err)
|
||||
func ParseTranscript(filePath string) (*Session, *ParseStats, error) {
|
||||
openResult := hostFS.Open(filePath)
|
||||
if !openResult.OK {
|
||||
return nil, nil, core.E("ParseTranscript", "open transcript", resultError(openResult))
|
||||
}
|
||||
f, ok := openResult.Value.(io.ReadCloser)
|
||||
if !ok {
|
||||
return nil, nil, core.E("ParseTranscript", "open transcript", nil)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
base := filepath.Base(path)
|
||||
id := strings.TrimSuffix(base, ".jsonl")
|
||||
base := path.Base(filePath)
|
||||
id := core.TrimSuffix(base, ".jsonl")
|
||||
|
||||
sess, stats, err := parseFromReader(f, id)
|
||||
if sess != nil {
|
||||
sess.Path = path
|
||||
sess.Path = filePath
|
||||
}
|
||||
return sess, stats, err
|
||||
}
|
||||
|
|
@ -302,7 +309,7 @@ func parseFromReader(r io.Reader, id string) (*Session, *ParseStats, error) {
|
|||
stats.TotalLines++
|
||||
|
||||
raw := scanner.Text()
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
if core.Trim(raw) == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -310,21 +317,21 @@ func parseFromReader(r io.Reader, id string) (*Session, *ParseStats, error) {
|
|||
lastLineFailed = false
|
||||
|
||||
var entry rawEntry
|
||||
if err := json.Unmarshal([]byte(raw), &entry); err != nil {
|
||||
if !core.JSONUnmarshalString(raw, &entry).OK {
|
||||
stats.SkippedLines++
|
||||
preview := raw
|
||||
if len(preview) > 100 {
|
||||
preview = preview[:100]
|
||||
}
|
||||
stats.Warnings = append(stats.Warnings,
|
||||
fmt.Sprintf("line %d: skipped (bad JSON): %s", lineNum, preview))
|
||||
core.Sprintf("line %d: skipped (bad JSON): %s", lineNum, preview))
|
||||
lastLineFailed = true
|
||||
continue
|
||||
}
|
||||
|
||||
ts, err := time.Parse(time.RFC3339Nano, entry.Timestamp)
|
||||
if err != nil {
|
||||
stats.Warnings = append(stats.Warnings, fmt.Sprintf("line %d: bad timestamp %q: %v", lineNum, entry.Timestamp, err))
|
||||
stats.Warnings = append(stats.Warnings, core.Sprintf("line %d: bad timestamp %q: %v", lineNum, entry.Timestamp, err))
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -338,20 +345,20 @@ func parseFromReader(r io.Reader, id string) (*Session, *ParseStats, error) {
|
|||
switch entry.Type {
|
||||
case "assistant":
|
||||
var msg rawMessage
|
||||
if err := json.Unmarshal(entry.Message, &msg); err != nil {
|
||||
stats.Warnings = append(stats.Warnings, fmt.Sprintf("line %d: failed to unmarshal assistant message: %v", lineNum, err))
|
||||
if !core.JSONUnmarshal(entry.Message, &msg).OK {
|
||||
stats.Warnings = append(stats.Warnings, core.Sprintf("line %d: failed to unmarshal assistant message", lineNum))
|
||||
continue
|
||||
}
|
||||
for i, raw := range msg.Content {
|
||||
var block contentBlock
|
||||
if err := json.Unmarshal(raw, &block); err != nil {
|
||||
stats.Warnings = append(stats.Warnings, fmt.Sprintf("line %d block %d: failed to unmarshal content: %v", lineNum, i, err))
|
||||
if !core.JSONUnmarshal(raw, &block).OK {
|
||||
stats.Warnings = append(stats.Warnings, core.Sprintf("line %d block %d: failed to unmarshal content", lineNum, i))
|
||||
continue
|
||||
}
|
||||
|
||||
switch block.Type {
|
||||
case "text":
|
||||
if text := strings.TrimSpace(block.Text); text != "" {
|
||||
if text := core.Trim(block.Text); text != "" {
|
||||
sess.Events = append(sess.Events, Event{
|
||||
Timestamp: ts,
|
||||
Type: "assistant",
|
||||
|
|
@ -371,14 +378,14 @@ func parseFromReader(r io.Reader, id string) (*Session, *ParseStats, error) {
|
|||
|
||||
case "user":
|
||||
var msg rawMessage
|
||||
if err := json.Unmarshal(entry.Message, &msg); err != nil {
|
||||
stats.Warnings = append(stats.Warnings, fmt.Sprintf("line %d: failed to unmarshal user message: %v", lineNum, err))
|
||||
if !core.JSONUnmarshal(entry.Message, &msg).OK {
|
||||
stats.Warnings = append(stats.Warnings, core.Sprintf("line %d: failed to unmarshal user message", lineNum))
|
||||
continue
|
||||
}
|
||||
for i, raw := range msg.Content {
|
||||
var block contentBlock
|
||||
if err := json.Unmarshal(raw, &block); err != nil {
|
||||
stats.Warnings = append(stats.Warnings, fmt.Sprintf("line %d block %d: failed to unmarshal content: %v", lineNum, i, err))
|
||||
if !core.JSONUnmarshal(raw, &block).OK {
|
||||
stats.Warnings = append(stats.Warnings, core.Sprintf("line %d block %d: failed to unmarshal content", lineNum, i))
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -405,7 +412,7 @@ func parseFromReader(r io.Reader, id string) (*Session, *ParseStats, error) {
|
|||
}
|
||||
|
||||
case "text":
|
||||
if text := strings.TrimSpace(block.Text); text != "" {
|
||||
if text := core.Trim(block.Text); text != "" {
|
||||
sess.Events = append(sess.Events, Event{
|
||||
Timestamp: ts,
|
||||
Type: "user",
|
||||
|
|
@ -432,14 +439,14 @@ func parseFromReader(r io.Reader, id string) (*Session, *ParseStats, error) {
|
|||
if stats.OrphanedToolCalls > 0 {
|
||||
for id := range pendingTools {
|
||||
stats.Warnings = append(stats.Warnings,
|
||||
fmt.Sprintf("orphaned tool call: %s", id))
|
||||
core.Sprintf("orphaned tool call: %s", id))
|
||||
}
|
||||
}
|
||||
|
||||
return sess, stats, nil
|
||||
}
|
||||
|
||||
func extractToolInput(toolName string, raw json.RawMessage) string {
|
||||
func extractToolInput(toolName string, raw rawJSON) string {
|
||||
if raw == nil {
|
||||
return ""
|
||||
}
|
||||
|
|
@ -447,7 +454,7 @@ func extractToolInput(toolName string, raw json.RawMessage) string {
|
|||
switch toolName {
|
||||
case "Bash":
|
||||
var inp bashInput
|
||||
if json.Unmarshal(raw, &inp) == nil {
|
||||
if core.JSONUnmarshal(raw, &inp).OK {
|
||||
desc := inp.Description
|
||||
if desc != "" {
|
||||
desc = " # " + desc
|
||||
|
|
@ -456,49 +463,49 @@ func extractToolInput(toolName string, raw json.RawMessage) string {
|
|||
}
|
||||
case "Read":
|
||||
var inp readInput
|
||||
if json.Unmarshal(raw, &inp) == nil {
|
||||
if core.JSONUnmarshal(raw, &inp).OK {
|
||||
return inp.FilePath
|
||||
}
|
||||
case "Edit":
|
||||
var inp editInput
|
||||
if json.Unmarshal(raw, &inp) == nil {
|
||||
return fmt.Sprintf("%s (edit)", inp.FilePath)
|
||||
if core.JSONUnmarshal(raw, &inp).OK {
|
||||
return core.Sprintf("%s (edit)", inp.FilePath)
|
||||
}
|
||||
case "Write":
|
||||
var inp writeInput
|
||||
if json.Unmarshal(raw, &inp) == nil {
|
||||
return fmt.Sprintf("%s (%d bytes)", inp.FilePath, len(inp.Content))
|
||||
if core.JSONUnmarshal(raw, &inp).OK {
|
||||
return core.Sprintf("%s (%d bytes)", inp.FilePath, len(inp.Content))
|
||||
}
|
||||
case "Grep":
|
||||
var inp grepInput
|
||||
if json.Unmarshal(raw, &inp) == nil {
|
||||
if core.JSONUnmarshal(raw, &inp).OK {
|
||||
path := inp.Path
|
||||
if path == "" {
|
||||
path = "."
|
||||
}
|
||||
return fmt.Sprintf("/%s/ in %s", inp.Pattern, path)
|
||||
return core.Sprintf("/%s/ in %s", inp.Pattern, path)
|
||||
}
|
||||
case "Glob":
|
||||
var inp globInput
|
||||
if json.Unmarshal(raw, &inp) == nil {
|
||||
if core.JSONUnmarshal(raw, &inp).OK {
|
||||
return inp.Pattern
|
||||
}
|
||||
case "Task":
|
||||
var inp taskInput
|
||||
if json.Unmarshal(raw, &inp) == nil {
|
||||
if core.JSONUnmarshal(raw, &inp).OK {
|
||||
desc := inp.Description
|
||||
if desc == "" {
|
||||
desc = truncate(inp.Prompt, 80)
|
||||
}
|
||||
return fmt.Sprintf("[%s] %s", inp.SubagentType, desc)
|
||||
return core.Sprintf("[%s] %s", inp.SubagentType, desc)
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: show raw JSON keys
|
||||
var m map[string]any
|
||||
if json.Unmarshal(raw, &m) == nil {
|
||||
if core.JSONUnmarshal(raw, &m).OK {
|
||||
parts := slices.Sorted(maps.Keys(m))
|
||||
return strings.Join(parts, ", ")
|
||||
return core.Join(", ", parts...)
|
||||
}
|
||||
|
||||
return ""
|
||||
|
|
@ -517,13 +524,13 @@ func extractResultContent(content any) string {
|
|||
}
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, "\n")
|
||||
return core.Join("\n", parts...)
|
||||
case map[string]any:
|
||||
if text, ok := v["text"].(string); ok {
|
||||
return text
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%v", content)
|
||||
return core.Sprint(content)
|
||||
}
|
||||
|
||||
func truncate(s string, max int) string {
|
||||
|
|
|
|||
127
parser_test.go
127
parser_test.go
|
|
@ -3,14 +3,12 @@ package session
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"path"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
core "dappco.re/go/core"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
|
@ -25,8 +23,11 @@ func ts(offsetSec int) string {
|
|||
|
||||
// jsonlLine marshals an arbitrary map to a single JSONL line.
|
||||
func jsonlLine(m map[string]any) string {
|
||||
b, _ := json.Marshal(m)
|
||||
return string(b)
|
||||
marshalResult := core.JSONMarshal(m)
|
||||
if !marshalResult.OK {
|
||||
panic(resultError(marshalResult))
|
||||
}
|
||||
return string(marshalResult.Value.([]byte))
|
||||
}
|
||||
|
||||
// userTextEntry creates a JSONL line for a user text message.
|
||||
|
|
@ -103,10 +104,17 @@ func toolResultEntry(timestamp, toolUseID string, content any, isError bool) str
|
|||
// writeJSONL writes lines to a temp .jsonl file and returns its path.
|
||||
func writeJSONL(t *testing.T, dir string, name string, lines ...string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(dir, name)
|
||||
err := os.WriteFile(path, []byte(strings.Join(lines, "\n")+"\n"), 0644)
|
||||
require.NoError(t, err)
|
||||
return path
|
||||
filePath := path.Join(dir, name)
|
||||
writeResult := hostFS.Write(filePath, core.Concat(core.Join("\n", lines...), "\n"))
|
||||
require.True(t, writeResult.OK)
|
||||
return filePath
|
||||
}
|
||||
|
||||
func setFileTimes(filePath string, atime, mtime time.Time) error {
|
||||
return syscall.UtimesNano(filePath, []syscall.Timespec{
|
||||
syscall.NsecToTimespec(atime.UnixNano()),
|
||||
syscall.NsecToTimespec(mtime.UnixNano()),
|
||||
})
|
||||
}
|
||||
|
||||
// --- ParseTranscript tests ---
|
||||
|
|
@ -255,7 +263,8 @@ func TestParseTranscript_EmptyFile_Bad(t *testing.T) {
|
|||
dir := t.TempDir()
|
||||
path := writeJSONL(t, dir, "empty.jsonl")
|
||||
// Write a truly empty file
|
||||
require.NoError(t, os.WriteFile(path, []byte(""), 0644))
|
||||
writeResult := hostFS.Write(path, "")
|
||||
require.True(t, writeResult.OK)
|
||||
|
||||
sess, _, err := ParseTranscript(path)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -310,13 +319,13 @@ func TestParseTranscript_LargeSession_Good(t *testing.T) {
|
|||
|
||||
// Generate 1000+ tool call pairs
|
||||
for i := range 1100 {
|
||||
toolID := fmt.Sprintf("tool-%d", i)
|
||||
toolID := core.Sprintf("tool-%d", i)
|
||||
offset := (i * 2) + 1
|
||||
lines = append(lines,
|
||||
toolUseEntry(ts(offset), "Bash", toolID, map[string]any{
|
||||
"command": fmt.Sprintf("echo %d", i),
|
||||
"command": core.Sprintf("echo %d", i),
|
||||
}),
|
||||
toolResultEntry(ts(offset+1), toolID, fmt.Sprintf("output %d", i), false),
|
||||
toolResultEntry(ts(offset+1), toolID, core.Sprintf("output %d", i), false),
|
||||
)
|
||||
}
|
||||
lines = append(lines, assistantTextEntry(ts(2202), "Done"))
|
||||
|
|
@ -465,7 +474,7 @@ func TestParseTranscript_TimestampsTracked_Good(t *testing.T) {
|
|||
|
||||
func TestParseTranscript_TextTruncation_Good(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
longText := strings.Repeat("x", 600)
|
||||
longText := repeatString("x", 600)
|
||||
path := writeJSONL(t, dir, "truncation.jsonl",
|
||||
userTextEntry(ts(0), longText),
|
||||
)
|
||||
|
|
@ -476,7 +485,7 @@ func TestParseTranscript_TextTruncation_Good(t *testing.T) {
|
|||
require.Len(t, sess.Events, 1)
|
||||
// Input should be truncated to 500 + "..."
|
||||
assert.True(t, len(sess.Events[0].Input) <= 504, "input should be truncated")
|
||||
assert.True(t, strings.HasSuffix(sess.Events[0].Input, "..."), "truncated text should end with ...")
|
||||
assert.True(t, core.HasSuffix(sess.Events[0].Input, "..."), "truncated text should end with ...")
|
||||
}
|
||||
|
||||
func TestSession_EventsSeq_Good(t *testing.T) {
|
||||
|
|
@ -631,9 +640,9 @@ func TestListSessions_NonJSONLIgnored_Good(t *testing.T) {
|
|||
userTextEntry(ts(0), "real"),
|
||||
)
|
||||
// Write non-JSONL files
|
||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "readme.md"), []byte("# Hello"), 0644))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "notes.txt"), []byte("notes"), 0644))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "data.json"), []byte("{}"), 0644))
|
||||
require.True(t, hostFS.Write(path.Join(dir, "readme.md"), "# Hello").OK)
|
||||
require.True(t, hostFS.Write(path.Join(dir, "notes.txt"), "notes").OK)
|
||||
require.True(t, hostFS.Write(path.Join(dir, "data.json"), "{}").OK)
|
||||
|
||||
sessions, err := ListSessions(dir)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -681,67 +690,67 @@ func TestListSessions_MalformedJSONLStillListed_Bad(t *testing.T) {
|
|||
// --- extractToolInput tests ---
|
||||
|
||||
func TestExtractToolInput_Bash_Good(t *testing.T) {
|
||||
input := json.RawMessage(`{"command":"go test ./...","description":"run tests","timeout":120}`)
|
||||
input := rawJSON([]byte(`{"command":"go test ./...","description":"run tests","timeout":120}`))
|
||||
result := extractToolInput("Bash", input)
|
||||
assert.Equal(t, "go test ./... # run tests", result)
|
||||
}
|
||||
|
||||
func TestExtractToolInput_BashNoDescription_Good(t *testing.T) {
|
||||
input := json.RawMessage(`{"command":"ls -la"}`)
|
||||
input := rawJSON([]byte(`{"command":"ls -la"}`))
|
||||
result := extractToolInput("Bash", input)
|
||||
assert.Equal(t, "ls -la", result)
|
||||
}
|
||||
|
||||
func TestExtractToolInput_Read_Good(t *testing.T) {
|
||||
input := json.RawMessage(`{"file_path":"/Users/test/main.go","offset":10,"limit":50}`)
|
||||
input := rawJSON([]byte(`{"file_path":"/Users/test/main.go","offset":10,"limit":50}`))
|
||||
result := extractToolInput("Read", input)
|
||||
assert.Equal(t, "/Users/test/main.go", result)
|
||||
}
|
||||
|
||||
func TestExtractToolInput_Edit_Good(t *testing.T) {
|
||||
input := json.RawMessage(`{"file_path":"/tmp/app.go","old_string":"foo","new_string":"bar"}`)
|
||||
input := rawJSON([]byte(`{"file_path":"/tmp/app.go","old_string":"foo","new_string":"bar"}`))
|
||||
result := extractToolInput("Edit", input)
|
||||
assert.Equal(t, "/tmp/app.go (edit)", result)
|
||||
}
|
||||
|
||||
func TestExtractToolInput_Write_Good(t *testing.T) {
|
||||
input := json.RawMessage(`{"file_path":"/tmp/out.txt","content":"hello world"}`)
|
||||
input := rawJSON([]byte(`{"file_path":"/tmp/out.txt","content":"hello world"}`))
|
||||
result := extractToolInput("Write", input)
|
||||
assert.Equal(t, "/tmp/out.txt (11 bytes)", result)
|
||||
}
|
||||
|
||||
func TestExtractToolInput_Grep_Good(t *testing.T) {
|
||||
input := json.RawMessage(`{"pattern":"TODO","path":"/src"}`)
|
||||
input := rawJSON([]byte(`{"pattern":"TODO","path":"/src"}`))
|
||||
result := extractToolInput("Grep", input)
|
||||
assert.Equal(t, "/TODO/ in /src", result)
|
||||
}
|
||||
|
||||
func TestExtractToolInput_GrepNoPath_Good(t *testing.T) {
|
||||
input := json.RawMessage(`{"pattern":"FIXME"}`)
|
||||
input := rawJSON([]byte(`{"pattern":"FIXME"}`))
|
||||
result := extractToolInput("Grep", input)
|
||||
assert.Equal(t, "/FIXME/ in .", result)
|
||||
}
|
||||
|
||||
func TestExtractToolInput_Glob_Good(t *testing.T) {
|
||||
input := json.RawMessage(`{"pattern":"**/*.go","path":"/src"}`)
|
||||
input := rawJSON([]byte(`{"pattern":"**/*.go","path":"/src"}`))
|
||||
result := extractToolInput("Glob", input)
|
||||
assert.Equal(t, "**/*.go", result)
|
||||
}
|
||||
|
||||
func TestExtractToolInput_Task_Good(t *testing.T) {
|
||||
input := json.RawMessage(`{"prompt":"Analyse the codebase","description":"Code review","subagent_type":"research"}`)
|
||||
input := rawJSON([]byte(`{"prompt":"Analyse the codebase","description":"Code review","subagent_type":"research"}`))
|
||||
result := extractToolInput("Task", input)
|
||||
assert.Equal(t, "[research] Code review", result)
|
||||
}
|
||||
|
||||
func TestExtractToolInput_TaskNoDescription_Good(t *testing.T) {
|
||||
input := json.RawMessage(`{"prompt":"Short prompt","subagent_type":"codegen"}`)
|
||||
input := rawJSON([]byte(`{"prompt":"Short prompt","subagent_type":"codegen"}`))
|
||||
result := extractToolInput("Task", input)
|
||||
assert.Equal(t, "[codegen] Short prompt", result)
|
||||
}
|
||||
|
||||
func TestExtractToolInput_UnknownTool_Good(t *testing.T) {
|
||||
input := json.RawMessage(`{"alpha":"one","beta":"two"}`)
|
||||
input := rawJSON([]byte(`{"alpha":"one","beta":"two"}`))
|
||||
result := extractToolInput("CustomTool", input)
|
||||
// Fallback: sorted keys
|
||||
assert.Equal(t, "alpha, beta", result)
|
||||
|
|
@ -753,7 +762,7 @@ func TestExtractToolInput_NilInput_Bad(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExtractToolInput_InvalidJSON_Bad(t *testing.T) {
|
||||
input := json.RawMessage(`{broken`)
|
||||
input := rawJSON([]byte(`{broken`))
|
||||
result := extractToolInput("Bash", input)
|
||||
// All unmarshals fail, including the fallback map unmarshal
|
||||
assert.Equal(t, "", result)
|
||||
|
|
@ -889,7 +898,7 @@ func TestParseStats_OrphanedToolCalls_Good(t *testing.T) {
|
|||
// Warnings should mention orphaned tool IDs
|
||||
var orphanWarnings int
|
||||
for _, w := range stats.Warnings {
|
||||
if strings.Contains(w, "orphaned tool call") {
|
||||
if core.Contains(w, "orphaned tool call") {
|
||||
orphanWarnings++
|
||||
}
|
||||
}
|
||||
|
|
@ -902,8 +911,8 @@ func TestParseStats_TruncatedFinalLine_Good(t *testing.T) {
|
|||
truncatedLine := `{"type":"assi`
|
||||
|
||||
// Write without trailing newline after truncated line
|
||||
path := filepath.Join(dir, "truncfinal.jsonl")
|
||||
require.NoError(t, os.WriteFile(path, []byte(validLine+"\n"+truncatedLine+"\n"), 0644))
|
||||
path := path.Join(dir, "truncfinal.jsonl")
|
||||
require.True(t, hostFS.Write(path, validLine+"\n"+truncatedLine+"\n").OK)
|
||||
|
||||
_, stats, err := ParseTranscript(path)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -914,7 +923,7 @@ func TestParseStats_TruncatedFinalLine_Good(t *testing.T) {
|
|||
// Should detect truncated final line
|
||||
var foundTruncated bool
|
||||
for _, w := range stats.Warnings {
|
||||
if strings.Contains(w, "truncated final line") {
|
||||
if core.Contains(w, "truncated final line") {
|
||||
foundTruncated = true
|
||||
}
|
||||
}
|
||||
|
|
@ -926,8 +935,8 @@ func TestParseStats_FileEndingMidJSON_Good(t *testing.T) {
|
|||
validLine := userTextEntry(ts(0), "Hello")
|
||||
midJSON := `{"type":"assistant","timestamp":"2026-02-20T10:00:01Z","sessionId":"test","message":{"role":"assi`
|
||||
|
||||
path := filepath.Join(dir, "midjson.jsonl")
|
||||
require.NoError(t, os.WriteFile(path, []byte(validLine+"\n"+midJSON+"\n"), 0644))
|
||||
path := path.Join(dir, "midjson.jsonl")
|
||||
require.True(t, hostFS.Write(path, validLine+"\n"+midJSON+"\n").OK)
|
||||
|
||||
sess, stats, err := ParseTranscript(path)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -938,7 +947,7 @@ func TestParseStats_FileEndingMidJSON_Good(t *testing.T) {
|
|||
|
||||
var foundTruncated bool
|
||||
for _, w := range stats.Warnings {
|
||||
if strings.Contains(w, "truncated final line") {
|
||||
if core.Contains(w, "truncated final line") {
|
||||
foundTruncated = true
|
||||
}
|
||||
}
|
||||
|
|
@ -950,8 +959,8 @@ func TestParseStats_CompleteFileNoTrailingNewline_Good(t *testing.T) {
|
|||
line := userTextEntry(ts(0), "Hello")
|
||||
|
||||
// Write without trailing newline — should still parse fine
|
||||
path := filepath.Join(dir, "nonewline.jsonl")
|
||||
require.NoError(t, os.WriteFile(path, []byte(line), 0644))
|
||||
path := path.Join(dir, "nonewline.jsonl")
|
||||
require.True(t, hostFS.Write(path, line).OK)
|
||||
|
||||
sess, stats, err := ParseTranscript(path)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -965,7 +974,7 @@ func TestParseStats_CompleteFileNoTrailingNewline_Good(t *testing.T) {
|
|||
// No truncation warning since the line parsed successfully
|
||||
var foundTruncated bool
|
||||
for _, w := range stats.Warnings {
|
||||
if strings.Contains(w, "truncated final line") {
|
||||
if core.Contains(w, "truncated final line") {
|
||||
foundTruncated = true
|
||||
}
|
||||
}
|
||||
|
|
@ -975,7 +984,7 @@ func TestParseStats_CompleteFileNoTrailingNewline_Good(t *testing.T) {
|
|||
func TestParseStats_WarningPreviewTruncated_Good(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
// A malformed line longer than 100 chars
|
||||
longBadLine := `{` + strings.Repeat("x", 200)
|
||||
longBadLine := `{` + repeatString("x", 200)
|
||||
path := writeJSONL(t, dir, "longbad.jsonl",
|
||||
longBadLine,
|
||||
userTextEntry(ts(0), "Valid"),
|
||||
|
|
@ -995,11 +1004,11 @@ func TestParseStats_WarningPreviewTruncated_Good(t *testing.T) {
|
|||
|
||||
func TestParseTranscriptReader_MinimalValid_Good(t *testing.T) {
|
||||
// Parse directly from an in-memory reader.
|
||||
data := strings.Join([]string{
|
||||
data := core.Join("\n", []string{
|
||||
userTextEntry(ts(0), "hello"),
|
||||
assistantTextEntry(ts(1), "world"),
|
||||
}, "\n") + "\n"
|
||||
reader := strings.NewReader(data)
|
||||
}...) + "\n"
|
||||
reader := core.NewReader(data)
|
||||
|
||||
sess, stats, err := ParseTranscriptReader(reader, "stream-session")
|
||||
require.NoError(t, err)
|
||||
|
|
@ -1017,13 +1026,13 @@ func TestParseTranscriptReader_MinimalValid_Good(t *testing.T) {
|
|||
|
||||
func TestParseTranscriptReader_BytesBuffer_Good(t *testing.T) {
|
||||
// Parse from a bytes.Buffer (common streaming use case).
|
||||
data := strings.Join([]string{
|
||||
data := core.Join("\n", []string{
|
||||
toolUseEntry(ts(0), "Bash", "tu-buf-1", map[string]any{
|
||||
"command": "echo ok",
|
||||
"description": "test",
|
||||
}),
|
||||
toolResultEntry(ts(1), "tu-buf-1", "ok", false),
|
||||
}, "\n") + "\n"
|
||||
}...) + "\n"
|
||||
buf := bytes.NewBufferString(data)
|
||||
|
||||
sess, _, err := ParseTranscriptReader(buf, "buf-session")
|
||||
|
|
@ -1034,7 +1043,7 @@ func TestParseTranscriptReader_BytesBuffer_Good(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestParseTranscriptReader_EmptyReader_Good(t *testing.T) {
|
||||
reader := strings.NewReader("")
|
||||
reader := core.NewReader("")
|
||||
|
||||
sess, stats, err := ParseTranscriptReader(reader, "empty")
|
||||
require.NoError(t, err)
|
||||
|
|
@ -1045,9 +1054,9 @@ func TestParseTranscriptReader_EmptyReader_Good(t *testing.T) {
|
|||
|
||||
func TestParseTranscriptReader_LargeLines_Good(t *testing.T) {
|
||||
// Verify the scanner handles very long lines (> 64KB).
|
||||
longText := strings.Repeat("x", 128*1024) // 128KB of text
|
||||
longText := repeatString("x", 128*1024) // 128KB of text
|
||||
data := userTextEntry(ts(0), longText) + "\n"
|
||||
reader := strings.NewReader(data)
|
||||
reader := core.NewReader(data)
|
||||
|
||||
sess, _, err := ParseTranscriptReader(reader, "big-session")
|
||||
require.NoError(t, err)
|
||||
|
|
@ -1058,12 +1067,12 @@ func TestParseTranscriptReader_LargeLines_Good(t *testing.T) {
|
|||
|
||||
func TestParseTranscriptReader_MalformedWithStats_Good(t *testing.T) {
|
||||
// Malformed lines in a reader should still produce correct stats.
|
||||
data := strings.Join([]string{
|
||||
data := core.Join("\n", []string{
|
||||
`{bad json`,
|
||||
userTextEntry(ts(0), "valid"),
|
||||
`also bad`,
|
||||
}, "\n") + "\n"
|
||||
reader := strings.NewReader(data)
|
||||
}...) + "\n"
|
||||
reader := core.NewReader(data)
|
||||
|
||||
sess, stats, err := ParseTranscriptReader(reader, "mixed")
|
||||
require.NoError(t, err)
|
||||
|
|
@ -1074,13 +1083,13 @@ func TestParseTranscriptReader_MalformedWithStats_Good(t *testing.T) {
|
|||
|
||||
func TestParseTranscriptReader_OrphanedTools_Good(t *testing.T) {
|
||||
// Tool calls without results should be tracked in stats.
|
||||
data := strings.Join([]string{
|
||||
data := core.Join("\n", []string{
|
||||
toolUseEntry(ts(0), "Bash", "orphan-r1", map[string]any{
|
||||
"command": "ls",
|
||||
}),
|
||||
assistantTextEntry(ts(1), "No result arrived"),
|
||||
}, "\n") + "\n"
|
||||
reader := strings.NewReader(data)
|
||||
}...) + "\n"
|
||||
reader := core.NewReader(data)
|
||||
|
||||
_, stats, err := ParseTranscriptReader(reader, "orphan-reader")
|
||||
require.NoError(t, err)
|
||||
|
|
@ -1218,7 +1227,7 @@ func TestParseTranscript_VeryLongLine_Ugly(t *testing.T) {
|
|||
// A single line that exceeds the default bufio.Scanner buffer.
|
||||
// The parser should handle this without error thanks to the enlarged buffer.
|
||||
dir := t.TempDir()
|
||||
huge := strings.Repeat("a", 5*1024*1024) // 5MB text
|
||||
huge := repeatString("a", 5*1024*1024) // 5MB text
|
||||
path := writeJSONL(t, dir, "huge_line.jsonl",
|
||||
userTextEntry(ts(0), huge),
|
||||
)
|
||||
|
|
@ -1322,7 +1331,7 @@ func TestPruneSessions_DeletesOldFiles_Good(t *testing.T) {
|
|||
)
|
||||
// Backdate the file's mtime by 2 hours.
|
||||
oldTime := time.Now().Add(-2 * time.Hour)
|
||||
require.NoError(t, os.Chtimes(path, oldTime, oldTime))
|
||||
require.NoError(t, setFileTimes(path, oldTime, oldTime))
|
||||
|
||||
// Create a recent session file.
|
||||
writeJSONL(t, dir, "new-session.jsonl",
|
||||
|
|
|
|||
20
search.go
20
search.go
|
|
@ -3,10 +3,11 @@ package session
|
|||
|
||||
import (
|
||||
"iter"
|
||||
"path/filepath"
|
||||
"path"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
core "dappco.re/go/core"
|
||||
)
|
||||
|
||||
// SearchResult represents a match found in a session transcript.
|
||||
|
|
@ -25,15 +26,12 @@ func Search(projectsDir, query string) ([]SearchResult, error) {
|
|||
// SearchSeq returns an iterator over search results matching the query across all sessions.
|
||||
func SearchSeq(projectsDir, query string) iter.Seq[SearchResult] {
|
||||
return func(yield func(SearchResult) bool) {
|
||||
matches, err := filepath.Glob(filepath.Join(projectsDir, "*.jsonl"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
matches := core.PathGlob(path.Join(projectsDir, "*.jsonl"))
|
||||
|
||||
query = strings.ToLower(query)
|
||||
query = core.Lower(query)
|
||||
|
||||
for _, path := range matches {
|
||||
sess, _, err := ParseTranscript(path)
|
||||
for _, filePath := range matches {
|
||||
sess, _, err := ParseTranscript(filePath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
|
@ -42,8 +40,8 @@ func SearchSeq(projectsDir, query string) iter.Seq[SearchResult] {
|
|||
if evt.Type != "tool_use" {
|
||||
continue
|
||||
}
|
||||
text := strings.ToLower(evt.Input + " " + evt.Output)
|
||||
if strings.Contains(text, query) {
|
||||
text := core.Lower(core.Concat(evt.Input, " ", evt.Output))
|
||||
if core.Contains(text, query) {
|
||||
matchCtx := evt.Input
|
||||
if matchCtx == "" {
|
||||
matchCtx = truncate(evt.Output, 120)
|
||||
|
|
|
|||
|
|
@ -2,8 +2,7 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
|
@ -138,7 +137,8 @@ func TestSearch_SkipsNonToolEvents_Good(t *testing.T) {
|
|||
|
||||
func TestSearch_NonJSONLIgnored_Good(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "readme.md"), []byte("go test"), 0644))
|
||||
writeResult := hostFS.Write(path.Join(dir, "readme.md"), "go test")
|
||||
require.True(t, writeResult.OK)
|
||||
|
||||
results, err := Search(dir, "go test")
|
||||
require.NoError(t, err)
|
||||
|
|
|
|||
124
video.go
124
video.go
|
|
@ -2,48 +2,45 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"io/fs"
|
||||
"path"
|
||||
"syscall"
|
||||
|
||||
coreerr "dappco.re/go/core/log"
|
||||
core "dappco.re/go/core"
|
||||
)
|
||||
|
||||
// RenderMP4 generates an MP4 video from session events using VHS (charmbracelet).
|
||||
func RenderMP4(sess *Session, outputPath string) error {
|
||||
if _, err := exec.LookPath("vhs"); err != nil {
|
||||
return coreerr.E("RenderMP4", "vhs not installed (go install github.com/charmbracelet/vhs@latest)", nil)
|
||||
vhsPath := lookupExecutable("vhs")
|
||||
if vhsPath == "" {
|
||||
return core.E("RenderMP4", "vhs not installed (go install github.com/charmbracelet/vhs@latest)", nil)
|
||||
}
|
||||
|
||||
tape := generateTape(sess, outputPath)
|
||||
|
||||
tmpFile, err := os.CreateTemp("", "session-*.tape")
|
||||
if err != nil {
|
||||
return coreerr.E("RenderMP4", "create tape", err)
|
||||
tmpDir := hostFS.TempDir("session-")
|
||||
if tmpDir == "" {
|
||||
return core.E("RenderMP4", "create tape", core.NewError("failed to create temp dir"))
|
||||
}
|
||||
defer os.Remove(tmpFile.Name())
|
||||
defer hostFS.DeleteAll(tmpDir)
|
||||
|
||||
if _, err := tmpFile.WriteString(tape); err != nil {
|
||||
tmpFile.Close()
|
||||
return coreerr.E("RenderMP4", "write tape", err)
|
||||
tapePath := path.Join(tmpDir, core.Concat(core.ID(), ".tape"))
|
||||
writeResult := hostFS.Write(tapePath, tape)
|
||||
if !writeResult.OK {
|
||||
return core.E("RenderMP4", "write tape", resultError(writeResult))
|
||||
}
|
||||
tmpFile.Close()
|
||||
|
||||
cmd := exec.Command("vhs", tmpFile.Name())
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return coreerr.E("RenderMP4", "vhs render", err)
|
||||
if err := runCommand(vhsPath, tapePath); err != nil {
|
||||
return core.E("RenderMP4", "vhs render", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateTape(sess *Session, outputPath string) string {
|
||||
var b strings.Builder
|
||||
b := core.NewBuilder()
|
||||
|
||||
b.WriteString(fmt.Sprintf("Output %s\n", outputPath))
|
||||
b.WriteString(core.Sprintf("Output %s\n", outputPath))
|
||||
b.WriteString("Set FontSize 16\n")
|
||||
b.WriteString("Set Width 1400\n")
|
||||
b.WriteString("Set Height 800\n")
|
||||
|
|
@ -57,7 +54,7 @@ func generateTape(sess *Session, outputPath string) string {
|
|||
if len(id) > 8 {
|
||||
id = id[:8]
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("Type \"# Session %s | %s\"\n",
|
||||
b.WriteString(core.Sprintf("Type \"# Session %s | %s\"\n",
|
||||
id, sess.StartTime.Format("2006-01-02 15:04")))
|
||||
b.WriteString("Enter\n")
|
||||
b.WriteString("Sleep 2s\n")
|
||||
|
|
@ -75,7 +72,7 @@ func generateTape(sess *Session, outputPath string) string {
|
|||
continue
|
||||
}
|
||||
// Show the command
|
||||
b.WriteString(fmt.Sprintf("Type %q\n", "$ "+cmd))
|
||||
b.WriteString(core.Sprintf("Type %q\n", "$ "+cmd))
|
||||
b.WriteString("Enter\n")
|
||||
|
||||
// Show abbreviated output
|
||||
|
|
@ -84,11 +81,11 @@ func generateTape(sess *Session, outputPath string) string {
|
|||
output = output[:200] + "..."
|
||||
}
|
||||
if output != "" {
|
||||
for line := range strings.SplitSeq(output, "\n") {
|
||||
for _, line := range core.Split(output, "\n") {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("Type %q\n", line))
|
||||
b.WriteString(core.Sprintf("Type %q\n", line))
|
||||
b.WriteString("Enter\n")
|
||||
}
|
||||
}
|
||||
|
|
@ -104,14 +101,14 @@ func generateTape(sess *Session, outputPath string) string {
|
|||
b.WriteString("\n")
|
||||
|
||||
case "Read", "Edit", "Write":
|
||||
b.WriteString(fmt.Sprintf("Type %q\n",
|
||||
fmt.Sprintf("# %s: %s", evt.Tool, truncate(evt.Input, 80))))
|
||||
b.WriteString(core.Sprintf("Type %q\n",
|
||||
core.Sprintf("# %s: %s", evt.Tool, truncate(evt.Input, 80))))
|
||||
b.WriteString("Enter\n")
|
||||
b.WriteString("Sleep 500ms\n")
|
||||
|
||||
case "Task":
|
||||
b.WriteString(fmt.Sprintf("Type %q\n",
|
||||
fmt.Sprintf("# Agent: %s", truncate(evt.Input, 80))))
|
||||
b.WriteString(core.Sprintf("Type %q\n",
|
||||
core.Sprintf("# Agent: %s", truncate(evt.Input, 80))))
|
||||
b.WriteString("Enter\n")
|
||||
b.WriteString("Sleep 1s\n")
|
||||
}
|
||||
|
|
@ -123,8 +120,73 @@ func generateTape(sess *Session, outputPath string) string {
|
|||
|
||||
func extractCommand(input string) string {
|
||||
// Remove description suffix (after " # ")
|
||||
if idx := strings.Index(input, " # "); idx > 0 {
|
||||
if idx := indexOf(input, " # "); idx > 0 {
|
||||
return input[:idx]
|
||||
}
|
||||
return input
|
||||
}
|
||||
|
||||
func lookupExecutable(name string) string {
|
||||
if name == "" {
|
||||
return ""
|
||||
}
|
||||
if containsAny(name, `/\`) {
|
||||
if isExecutablePath(name) {
|
||||
return name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
for _, dir := range core.Split(core.Env("PATH"), ":") {
|
||||
if dir == "" {
|
||||
dir = "."
|
||||
}
|
||||
candidate := path.Join(dir, name)
|
||||
if isExecutablePath(candidate) {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func isExecutablePath(filePath string) bool {
|
||||
statResult := hostFS.Stat(filePath)
|
||||
if !statResult.OK {
|
||||
return false
|
||||
}
|
||||
info, ok := statResult.Value.(fs.FileInfo)
|
||||
if !ok || info.IsDir() {
|
||||
return false
|
||||
}
|
||||
return info.Mode()&0111 != 0
|
||||
}
|
||||
|
||||
func runCommand(command string, args ...string) error {
|
||||
argv := append([]string{command}, args...)
|
||||
procAttr := &syscall.ProcAttr{
|
||||
Env: syscall.Environ(),
|
||||
Files: []uintptr{0, 1, 2},
|
||||
}
|
||||
|
||||
pid, err := syscall.ForkExec(command, argv, procAttr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var status syscall.WaitStatus
|
||||
if _, err := syscall.Wait4(pid, &status, 0, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if status.Exited() && status.ExitStatus() == 0 {
|
||||
return nil
|
||||
}
|
||||
if status.Signaled() {
|
||||
return core.NewError(core.Sprintf("command terminated by signal %d", status.Signal()))
|
||||
}
|
||||
if status.Exited() {
|
||||
return core.NewError(core.Sprintf("command exited with status %d", status.ExitStatus()))
|
||||
}
|
||||
|
||||
return core.NewError("command failed")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,10 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
core "dappco.re/go/core"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
|
@ -93,7 +92,7 @@ func TestGenerateTape_LongOutput_Good(t *testing.T) {
|
|||
Type: "tool_use",
|
||||
Tool: "Bash",
|
||||
Input: "cat huge.log",
|
||||
Output: strings.Repeat("x", 300),
|
||||
Output: repeatString("x", 300),
|
||||
Success: true,
|
||||
},
|
||||
},
|
||||
|
|
@ -149,11 +148,11 @@ func TestGenerateTape_EmptySession_Good(t *testing.T) {
|
|||
assert.Contains(t, tape, "Output /tmp/out.mp4")
|
||||
assert.Contains(t, tape, "Sleep 3s")
|
||||
// No tool events
|
||||
lines := strings.Split(tape, "\n")
|
||||
lines := core.Split(tape, "\n")
|
||||
var toolLines int
|
||||
for _, line := range lines {
|
||||
if strings.Contains(line, "$ ") || strings.Contains(line, "# Read:") ||
|
||||
strings.Contains(line, "# Edit:") || strings.Contains(line, "# Write:") {
|
||||
if core.Contains(line, "$ ") || core.Contains(line, "# Read:") ||
|
||||
core.Contains(line, "# Edit:") || core.Contains(line, "# Write:") {
|
||||
toolLines++
|
||||
}
|
||||
}
|
||||
|
|
@ -192,7 +191,7 @@ func TestExtractCommand_DescriptionAtStart_Good(t *testing.T) {
|
|||
|
||||
func TestRenderMP4_NoVHS_Ugly(t *testing.T) {
|
||||
// Skip if vhs is actually installed (this tests the error path)
|
||||
if _, err := exec.LookPath("vhs"); err == nil {
|
||||
if lookupExecutable("vhs") != "" {
|
||||
t.Skip("vhs is installed; skipping missing-vhs test")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue