diff --git a/internal/sessions/traces_clients_test.go b/internal/sessions/traces_clients_test.go index f5b453044..23b87e77b 100644 --- a/internal/sessions/traces_clients_test.go +++ b/internal/sessions/traces_clients_test.go @@ -618,3 +618,119 @@ func TestClaudeSubagentRepeatedEndTurnBlocks(t *testing.T) { } } } + +// OpenCode's trace window counted every session row, then skipped children. +// Limit newer children (which are not exported) dropped the parent, so the +// poll that Claude's children cannot evict (traceRecentFiles) exported +// nothing. The parent must still be exported; a child still is not. +func TestOpenCodeChildrenDoNotEvictParentTrace(t *testing.T) { + t.Run("sqlite", func(t *testing.T) { + path := filepath.Join(t.TempDir(), "opencode.db") + t.Setenv("OPENCODE_DB", path) + db, err := sql.Open("sqlite", path) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { db.Close() }) + for _, q := range []string{ + `CREATE TABLE session_v2 (id TEXT PRIMARY KEY, parent_id TEXT, directory TEXT, title TEXT, time_created INTEGER, time_updated INTEGER)`, + `CREATE TABLE session_message (id TEXT PRIMARY KEY, session_id TEXT, type TEXT, seq INTEGER, time_created INTEGER, time_updated INTEGER, data TEXT)`, + `CREATE TABLE kv (key TEXT PRIMARY KEY, value TEXT)`, + `INSERT INTO kv VALUES ('migration.v1-v2', '{"phase":"completed"}')`, + } { + if _, err = db.Exec(q); err != nil { + t.Fatal(err) + } + } + now := time.Now().Add(-5 * time.Second).UnixMilli() + if _, err = db.Exec(`INSERT INTO session_v2 VALUES ('s', NULL, 'private', 'private', ?, ?)`, now, now+5000); err != nil { + t.Fatal(err) + } + user, _ := json.Marshal(map[string]any{"role": "user", "text": "question", "time": map[string]int64{"created": now}}) + asst, _ := json.Marshal(map[string]any{ + "role": "assistant", "parentID": "u", "modelID": "model", + "model": map[string]string{"id": "model", "providerID": "p"}, + "providerID": "p", + "time": map[string]int64{"created": now + 1000, "completed": now + 4000}, + "tokens": map[string]any{"input": 100, "output": 10, "reasoning": 0, "cache": map[string]int{"read": 0, "write": 0}}, + "content": []any{map[string]string{"type": "text", "text": "answer"}}, + }) + if _, err = db.Exec(`INSERT INTO session_message VALUES ('u', 's', 'user', 0, ?, ?, ?)`, now, now+5000, string(user)); err != nil { + t.Fatal(err) + } + if _, err = db.Exec(`INSERT INTO session_message VALUES ('a', 's', 'assistant', 1, ?, ?, ?)`, now+1000, now+5000, string(asst)); err != nil { + t.Fatal(err) + } + for i := range Limit { + id := fmt.Sprintf("c%d", i) + at := now + 10000 + int64(i) + if _, err = db.Exec(`INSERT INTO session_v2 VALUES (?, 's', 'private', 'child', ?, ?)`, id, at, at); err != nil { + t.Fatal(err) + } + } + r := NewTraceReader(ms(now).Add(-time.Second)) + budget := int64(8 << 20) + spans := r.pollOpenCode(false, &budget) + if !spanSession(spans, "s") { + t.Fatalf("parent dropped behind %d newer children: %+v", Limit, spans) + } + if spanSession(spans, "c0") { + t.Fatal("a child session was exported") + } + }) + t.Run("json", func(t *testing.T) { + dir := t.TempDir() + root := filepath.Join(dir, "opencode", "storage") + t.Setenv("XDG_DATA_HOME", dir) + t.Setenv("OPENCODE_DB", "") + write := func(path string, value any) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { + t.Fatal(err) + } + b, err := json.Marshal(value) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, b, 0o600); err != nil { + t.Fatal(err) + } + } + now := time.Now().Add(-5 * time.Second).UnixMilli() + write(filepath.Join(root, "session", "project", "s.json"), map[string]any{"id": "s", "time": map[string]int64{"created": now, "updated": now}}) + write(filepath.Join(root, "message", "s", "u.json"), map[string]any{"id": "u", "role": "user", "time": map[string]int64{"created": now}}) + write(filepath.Join(root, "message", "s", "a.json"), map[string]any{ + "id": "a", "role": "assistant", "parentID": "u", "modelID": "model", + "time": map[string]int64{"created": now + 100, "completed": now + 500}, + "tokens": map[string]int{"input": 10, "output": 2}, + }) + later := time.Now().Add(time.Hour) + for i := range Limit { + id := fmt.Sprintf("c%d", i) + write(filepath.Join(root, "session", "project", id+".json"), map[string]any{ + "id": id, "parentID": "s", "time": map[string]int64{"created": now, "updated": now}, + }) + if err := os.Chtimes(filepath.Join(root, "session", "project", id+".json"), later, later); err != nil { + t.Fatal(err) + } + } + r := NewTraceReader(ms(now).Add(-time.Second)) + budget := int64(8 << 20) + spans := r.pollOpenCode(false, &budget) + if !spanSession(spans, "s") { + t.Fatalf("parent dropped behind %d newer children: %+v", Limit, spans) + } + if spanSession(spans, "c0") { + t.Fatal("a child session was exported") + } + }) +} + +func spanSession(spans []TraceSpan, id string) bool { + for _, s := range spans { + if s.Session == id { + return true + } + } + return false +} diff --git a/internal/sessions/traces_opencode.go b/internal/sessions/traces_opencode.go index 549b106c3..0de387929 100644 --- a/internal/sessions/traces_opencode.go +++ b/internal/sessions/traces_opencode.go @@ -9,6 +9,26 @@ import ( "time" ) +// traceOpenCodeWindow keeps the latest main sessions, newest first. +// A child is not exported (the poll skips it), so it takes no place: +// Limit newer children would fill the window and the parent would not +// be read. Claude's children are exported, which is why traceRecentFiles +// gives them a quota of their own. +func traceOpenCodeWindow(files []file) []file { + sortTraceOCFiles(files) + out := make([]file, 0, min(len(files), Limit)) + for _, f := range files { + if !f.main { + continue + } + out = append(out, f) + if len(out) == Limit { + break + } + } + return out +} + func traceOCMessages(f file, budget *int64) [][]byte { var out [][]byte for _, b := range f.oc.messages(f.sid) { @@ -77,10 +97,7 @@ func (r *TraceReader) pollOpenCode(bodies bool, budget *int64) []TraceSpan { } } } - sort.Slice(files, func(i, j int) bool { return files[i].mod.After(files[j].mod) }) - if len(files) > Limit { - files = files[:Limit] - } + files = traceOpenCodeWindow(files) seen := map[string]bool{} var out []TraceSpan for _, f := range files { diff --git a/internal/sessions/traces_opencode_db.go b/internal/sessions/traces_opencode_db.go index e1676dcad..cfd386f3f 100644 --- a/internal/sessions/traces_opencode_db.go +++ b/internal/sessions/traces_opencode_db.go @@ -47,12 +47,7 @@ func (r *TraceReader) pollOpenCodeDB(path string, bodies bool, budget *int64) ([ } files = append(files, ocDBRows(db, fmt.Sprintf(q, "session", where), "opencode", path, ocDB{db}, parent)...) } - files = ocRoots(files, parent) - // Retain only the latest stores, as for JSONL discovery. - sortTraceOCFiles(files) - if len(files) > Limit { - files = files[:Limit] - } + files = traceOpenCodeWindow(ocRoots(files, parent)) var out []TraceSpan pending := false seen := map[string]bool{}