sessions: OpenCode child rows no longer crowd the parent out of the trace window

What changed
- traceOpenCodeWindow keeps the latest main sessions. A child is not
  exported, so it no longer takes one of the Limit places. Limit newer
  children were filling the window and the parent was not read. The JSON
  store and the SQLite store share that window.

Run
- TestOpenCodeChildrenDoNotEvictParentTrace failed on a15e576 plus the
  test (sqlite and json: parent dropped behind 200 newer children, no
  spans) and passes with the window. go test -tags nogui -count=1 -p 2
  ./internal/sessions/ passed in a sandbox home (648s). gofmt -l on the
  three files printed nothing. go vet -tags nogui and go build -tags
  nogui with CGO_ENABLED=0 passed for windows, linux and darwin.
- Not run: the rest of the module, -race, a real OpenCode database, or
  a Langfuse server.

Full suite: go test -tags nogui -timeout 45m ./... (pipefail, suite-lock) at the four #2494/#2504/#2505/#2490 commits on origin/main a751f5282: 65 packages ok, among them internal/agent (2124s), internal/gateway (1845s) and internal/sessions (148s). internal/gui was red on TestSetAnswersWhileDevinAndProvidersHang alone (the state after a set took 11.5s at load ~250; #1375); internal/gui run again alone: ok (206s).

Generated-by: Grok 4.7 (Cursor)
This commit is contained in:
ggbdpq
2026-10-17 00:39:35 +08:00
committed by yetone
parent 138188a36d
commit ee2a7c7e86
3 changed files with 138 additions and 10 deletions
+116
View File
@@ -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
}
+21 -4
View File
@@ -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 {
+1 -6
View File
@@ -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{}