A read of the sessions (List, Stats, Manage, the agents' lists, Codex's threads) took dbReadMu, a plain mutex, for the whole call, parse included. The start's warm-up reads every session file with a quarter of the cores, so the first pages and API calls after a start waited for its whole read before they could even look at the cache. - dbReadMu becomes storeMu, an RWMutex: reads take it shared, and only the changes to the files (Delete, a Codex provider move, a Claude project relocation) take it alone. - A file another refresh is reading now is in `parsing`, with a channel closed when its parse is in the cache. A second read serves the parse it already has of such a file, and waits only for one it has never read; it never reads the same file twice at once. - Each parse is published as it finishes, not in checkpoint batches, so a waiting reader gets it at once. - The shared store handles (OpenCode, Hermes, Alma, Cursor) closed at the end of every read; with reads overlapping, one could close a handle another still used. They now live as long as their last user (useDBs). get() and the titles path closed them without the lock at all before. Tests (internal/sessions/refresh_shared_test.go), each broken by hand: - TestReadNotHeldByAnotherRead: the warm-up's parse of 12 grown files is held; List and Manage answer within 10s from the kept parses, each file is parsed once in all, and a later List parses none. With storeMu.Lock in List: red, "List waited for another read of the same files to end". - TestReadWaitsForAFirstReadUnderWay: a file never read is waited for, not served empty, and read once. Without the waits: red, "List answered without the files being read". Without the skip of a file being read: panic, close of nil channel. - TestStoresLiveAsLongAsTheirLastUser: with the release always closing: red, "closed while a read still used it". All three pass with -race. Measured on a sandbox instance (600k gateway calls, ~4,600 Claude Code and Codex sessions, 300 grown since the last start; 4 API calls asked 3.5s after the routes, when the warm-up starts), base1a3b92688against this branch, back to back. The machine's load average was 350-480 (nine agents running suites), so these timings are mostly CPU wait and vary by tens of seconds run to run: first answer (stats?days=30) base 91s / 81s new 50s / 79s manage?agent=claude base 112s / 95s new 83s / 96s The pages no longer queue behind the warm-up's lock, but at this load the answer is bound by the CPU its own parse gets, so no gain shows above the noise in the second pair. On an idle machine the held reads are what #2776 reports; the tests above show they are no longer held. Run on origin/main2ceff7370with all four #2776 commits' changes (this one and the three beside it), load average 291-568 (nine agents running suites), macOS: - go test -p 4 ./... -timeout 45m, 09:35-10:49: 62 packages ok, among them agent (2310s), gateway (1707s), provider (764s), usage (83s). Red: - internal/sessions: TestWSLOmpProfiles, TestWSLStoppedDistroKept, TestWSLStoppedSinceNotOpened, TestWSLNoneWithoutHomes, each "TempDir: mkdir ...: no space left on device" (the disk filled mid-run). After space was freed, ./internal/sessions alone: ok (139s). - internal/gui: TestDockJoinLeadsCommandTab, TestPanelCloseHidesIt, TestPanelJoinsFullScreenSpaces, TestPluginPageLoadsItsFilesInTheWindow, TestDockReopenKeepsTheWindowsSpace (each killed at 30s) and TestMainWindowCorners (86s): the real-window AppKit tests under load, already filed as #2645 and #1936. Rerun alone at load ~335: all pass, with TestSetAnswersWhileDevinAndProvidersHang (17s). - gofmt -l: none. go vet: darwin, GOOS=linux -tags nogui and GOOS=windows all exit 0. - No internal/gui/assets file changed, so the GUI Playwright suite was not run. - At this commit alone (2ceff7370+ this change): ./internal/sessions ok (82s), and its three new tests with -race ok.
143 lines
3.7 KiB
Go
143 lines
3.7 KiB
Go
package sessions
|
|
|
|
import (
|
|
"encoding/json"
|
|
"sort"
|
|
)
|
|
|
|
// openCodeTranscript reads the conversation in message order from the same
|
|
// store the listing chose, including while a v1 to v2 migration is underway.
|
|
func openCodeTranscript(s Session, add func(bool, Part) bool) error {
|
|
storeMu.RLock()
|
|
defer storeMu.RUnlock()
|
|
// the store handles are shared with the reads that hold mu: they are
|
|
// opened and given back under it, and read from without it
|
|
mu.Lock()
|
|
release := useDBs()
|
|
files := openCodeFiles()
|
|
mu.Unlock()
|
|
defer func() {
|
|
mu.Lock()
|
|
release()
|
|
mu.Unlock()
|
|
}()
|
|
for _, f := range files {
|
|
if f.sid != s.ID {
|
|
continue
|
|
}
|
|
if v, ok := f.oc.(ocV2DB); ok {
|
|
rows, err := v.db.Query("SELECT type,data FROM session_message WHERE session_id=? AND type IN ('user','assistant') ORDER BY seq", s.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var role string
|
|
var raw []byte
|
|
if err := rows.Scan(&role, &raw); err != nil {
|
|
return err
|
|
}
|
|
var m struct {
|
|
Text string `json:"text"`
|
|
Content []json.RawMessage `json:"content"`
|
|
Model struct {
|
|
ID string `json:"id"`
|
|
} `json:"model"`
|
|
}
|
|
if err := json.Unmarshal(raw, &m); err != nil {
|
|
return err
|
|
}
|
|
if role == "user" {
|
|
if !add(true, textPart(role, m.Text)) {
|
|
return nil
|
|
}
|
|
continue
|
|
}
|
|
for _, p := range m.Content {
|
|
if !openCodeTranscriptPart(role, m.Model.ID, p, add) {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
return rows.Err()
|
|
}
|
|
var messages []ocMessage
|
|
for _, b := range f.oc.messages(f.sid) {
|
|
var m ocMessage
|
|
if json.Unmarshal(b, &m) == nil {
|
|
messages = append(messages, m)
|
|
}
|
|
}
|
|
sort.SliceStable(messages, func(i, j int) bool {
|
|
if messages[i].Time.Created != messages[j].Time.Created {
|
|
return messages[i].Time.Created < messages[j].Time.Created
|
|
}
|
|
return messages[i].ID < messages[j].ID
|
|
})
|
|
for _, m := range messages {
|
|
for _, p := range f.oc.parts(m.ID) {
|
|
if !openCodeTranscriptPart(m.Role, m.ModelID, p, add) {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
return ErrNoTranscript
|
|
}
|
|
|
|
// openCodeTranscriptPart tells add one part of a message; model is the
|
|
// message's own (an assistant's modelID, or model.id in OpenCode 2), said
|
|
// with each of its assistant parts (#2253).
|
|
func openCodeTranscriptPart(role, model string, raw []byte, add func(bool, Part) bool) bool {
|
|
say := add
|
|
add = func(input bool, q Part) bool {
|
|
if q.Role == "assistant" {
|
|
q.Model = model
|
|
}
|
|
return say(input, q)
|
|
}
|
|
var p struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
Name string `json:"name"`
|
|
Tool string `json:"tool"`
|
|
Synthetic bool `json:"synthetic"`
|
|
Ignored bool `json:"ignored"`
|
|
State struct {
|
|
Input json.RawMessage `json:"input"`
|
|
Output string `json:"output"`
|
|
Content []struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
} `json:"content"`
|
|
} `json:"state"`
|
|
}
|
|
if json.Unmarshal(raw, &p) != nil || p.Synthetic || p.Ignored {
|
|
return true
|
|
}
|
|
switch p.Type {
|
|
case "text":
|
|
return add(role == "user", textPart(role, p.Text))
|
|
case "reasoning":
|
|
return add(false, Part{Role: role, Kind: "thinking", Text: p.Text})
|
|
case "tool":
|
|
name := p.Name
|
|
if name == "" {
|
|
name = p.Tool
|
|
}
|
|
if !add(false, Part{Role: role, Kind: "tool_use", Name: name, Text: string(p.State.Input)}) {
|
|
return false
|
|
}
|
|
if p.State.Output != "" && !add(false, Part{Role: "tool", Kind: "tool_result", Name: name, Text: p.State.Output}) {
|
|
return false
|
|
}
|
|
for _, r := range p.State.Content {
|
|
if r.Type == "text" && !add(false, Part{Role: "tool", Kind: "tool_result", Name: name, Text: r.Text}) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|