Files
magpie/internal/sessions/content.go
T
yetone 260ded9af4 sessions: the Sessions page's search finds a session by what was said in it, and a gateway session is titled by what it was first asked (YOU on Discord)
Why: the filter matched only titles, folders, ids and models, and a
session the gateway recorded was listed under its bare ses_… id, so one
couldn't be found by anything a person remembers about it.

What changed:
- sessions.GatewayTitle: the first user text of a recorded session (not
  a <…> context tag, # AGENTS.md or an image), made one line. It reads at
  most 8 complete turns of the oldest file of it, caches a finished
  file's answer by path, and costs one stat when nothing is recorded.
  gatewayListed applies it in every list (/api/sessions, /manage, /one,
  the stats summaries); ExternalSession gained Title.
- GET /api/sessions/search?agent=&q= (searchSessions): an agent's
  sessions, newest first, read by up to 4 workers, each only to its
  first match. Native files go through a byte prefilter (mayHold: the
  query's longest word, ASCII case folded without a copy, 1 MiB chunks,
  skipped for zstd and for queries JSON may escape), and of a Claude or
  Codex file only the lines holding every word are decoded
  (readTranscriptOf; claudeContentOf/codexContentOf take an optional
  line test, nil for every other caller); gateway sessions, and an
  unreadable agent's sessions (unread: true), through their recorded
  transcript. System prompts, tool definitions and images never match.
  It stops at 200 hits or 8s (cut, searched of total) and when the
  request's context ends. Each hit carries the match with 36 runes
  before and 120 after.
- sessions.js: the field says "Search sessions"; title/folder/id/model
  matches show at once, the server is asked 300ms after the last
  keystroke, a new query aborts the request in flight and its answer is
  dropped, under 2 letters asks nothing. A session found shows a line of
  the words round its match with the match in <mark> (text nodes, no
  innerHTML). A note says when it is searching, cut short, failed, or
  limited to recorded text. New strings in zh, zh-TW, ja and de.
- docs/subsystems/gateway-sessions.md describes both.

Run:
- Go: TestSearchSession* (snippet, case and line breaks, said text
  preferred over a tool call, cut ends, cancelled context, a Codex file,
  only matching lines decoded: 1 part of 3), TestContainsFold,
  TestMayHoldSkipsAFileWithoutTheWords (incl. a match across chunks),
  TestGatewayTitleAndSearch (context tag and system prompt skipped),
  TestSessionsSearchFindsGatewaySessionsByWhatWasSaid (endpoint + manage
  titles), TestSessionsSearchStopsWhenItsRequestEnds. Without the
  endpoint/title they fail (404, id as title).
- GUI: new sessions-search.test.cjs in chromium and webkit, en/zh at
  1100, zh-TW/ja at 560, de at 420: title shown, one letter no request,
  one debounced request, snippet mark, cancelled earlier request never
  shown, cut note, empty state, Escape, every language's strings, no
  overflow; 12/12, and 0/12 with the old sessions.js. The sessions-*,
  sess-*, session-* and i18n-keys tests pass in both engines
  (sessions-manage's placeholder updated to "Search sessions").
- gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., the builds: clean. go test -tags nogui
  ./... at load ~25-60 (other sessions' runs): every package passed but
  internal/agent and internal/gateway, which hit the 30m timeout; run
  again alone, agent passed and gateway's only red was
  TestOTelWaterfallIncludesUnbilledFallback, filed as #1794 (passes
  alone, -count=3).
- Real data, read-only (a temporary probe, not committed, pointed at
  the owner's ~/.claude and ~/.codex; one worker, 8s, load ~50):
  Claude, 58 sessions / 3 GB: before the line filter one 1.3 GB session
  took the whole 8s ("redis": 1 of 58); after, all 58 in 4.7s ("redis",
  13 hits), 7.4s ("gateway session"), 2.8s (no match). Codex, 11534
  sessions / 65 GB: 245-656 sessions in 8s either way, which is what
  the cut note ("latest n of total") is for.

Not verified: the endpoint and page against a real recorded gateway
store (only fixtures and temp homes); the timings above were taken on
a machine at load ~50 and vary run to run.
2026-10-12 22:29:46 +08:00

357 lines
10 KiB
Go

package sessions
// What was said in a call — what the agent was given and what came back —
// read from the session file the call is in when it is asked for, from the place
// the call has in it (Call.File, From, To); magpie keeps no copy.
import (
"bytes"
"encoding/json"
"errors"
"os"
"strings"
"time"
"unicode/utf8"
)
// Part is one thing said in a call.
type Part struct {
Role string `json:"role"` // user, assistant or tool
Kind string `json:"kind"` // text, thinking, tool_use, tool_result, context or image
Name string `json:"name,omitempty"` // the tool's, of its call
Text string `json:"text"`
Cut int `json:"cut,omitempty"` // the characters left off its end
}
// Content is what a call was given and what it said, in the order of the file.
type Content struct {
Input []Part `json:"input"`
Output []Part `json:"output"`
Cut bool `json:"cut,omitempty"` // there was more than is kept here
}
const (
partMax = 8000 // characters of one part
contentMax = 160_000 // of all of them
)
var errNoPlace = errors.New("the call has no place in a file")
// add keeps a part, cut to what one may be, unless the content is full; it
// says whether there is room for more.
func (c *Content) add(input bool, p Part) bool {
size := 0
for _, x := range c.Input {
size += len(x.Text)
}
for _, x := range c.Output {
size += len(x.Text)
}
if size >= contentMax {
c.Cut = true
return false
}
p, ok := keep(p)
if !ok {
return true
}
if input {
c.Input = append(c.Input, p)
} else {
c.Output = append(c.Output, p)
}
return true
}
// keep is a part trimmed and cut to what one may be, unless it says nothing.
func keep(p Part) (Part, bool) {
p.Text = strings.TrimSpace(p.Text)
if p.Text == "" {
return p, false
}
if n := utf8.RuneCountInString(p.Text); n > partMax {
r := []rune(p.Text)
p.Text, p.Cut = string(r[:partMax]), p.Cut+n-partMax
}
return p, true
}
// ContentOf reads what was said in a call from its file.
func ContentOf(c Call) (Content, error) {
out := Content{Input: []Part{}, Output: []Part{}}
if c.File == "" || c.Agent == "opencode" { // OpenCode's are rows, not lines of a file
return out, errNoPlace
}
var err error
if c.Agent == "codex" {
err = codexContent(c, out.add)
} else {
err = claudeContent(c, out.add)
}
return out, err
}
// FindCall is the call of a session made between two times that ended
// nearest at (a call's Time is its last line's), if the session files have
// one. A gateway request's window can hold the next calls of a quick tool
// loop too; the newest of them would be another request's.
func FindCall(session string, from, to, at time.Time) (Call, bool) {
if session == "" {
return Call{}, false
}
var best Call
found := false
dist := func(t time.Time) time.Duration { return max(t.Sub(at), at.Sub(t)) }
for _, c := range callsFor(from.Add(-time.Minute), session) {
if c.Session == session && !c.Time.Before(from) && !c.Time.After(to) && (!found || dist(c.Time) < dist(best.Time)) {
best, found = c, true
}
}
return best, found
}
// ---- Claude Code
type ccBlock struct {
Type string `json:"type"`
Text string `json:"text"`
Thinking string `json:"thinking"`
Name string `json:"name"`
Input json.RawMessage `json:"input"`
Content json.RawMessage `json:"content"`
}
// claudeContent reads the lines from where the call was asked to where it ended:
// the user lines — a prompt, a tool's result — are what it was given, and the
// lines of its own message what it said. add is told each part, and says
// whether to read on.
func claudeContent(c Call, add func(input bool, p Part) bool) error {
return claudeContentOf(c, nil, add)
}
// claudeContentOf is claudeContent reading only the lines keep (when given)
// says yes to: a search's cheap test of a line's bytes before it is decoded.
func claudeContentOf(c Call, keep func([]byte) bool, add func(input bool, p Part) bool) error {
_, err := scanAt(c.File, c.From, nil, func(b []byte, start, end int64) bool {
if start >= c.To {
return false
}
if keep != nil && !keep(b) {
return true
}
user := bytes.Contains(b, ccUserLine)
asst := bytes.Contains(b, ccAssistant)
if !user && !asst {
return true
}
var l struct {
Type string `json:"type"`
IsMeta bool `json:"isMeta"`
Message struct {
ID string `json:"id"`
Content json.RawMessage `json:"content"`
} `json:"message"`
}
if json.Unmarshal(b, &l) != nil {
return true
}
more := true
switch {
case l.Type == "user" && !l.IsMeta:
for _, p := range claudeParts(l.Message.Content, "user") {
more = add(true, p) && more
}
case l.Type == "assistant" && (c.Msg == "" || l.Message.ID == c.Msg):
for _, p := range claudeParts(l.Message.Content, "assistant") {
more = add(false, p) && more
}
}
return more
})
return err
}
// claudeParts are the parts of a message's content, which is words, or blocks.
func claudeParts(raw json.RawMessage, role string) []Part {
var text string
if json.Unmarshal(raw, &text) == nil {
return []Part{textPart(role, text)}
}
var blocks []ccBlock
if json.Unmarshal(raw, &blocks) != nil {
return nil
}
var parts []Part
for _, b := range blocks {
switch b.Type {
case "text":
parts = append(parts, textPart(role, b.Text))
case "thinking":
parts = append(parts, Part{Role: role, Kind: "thinking", Text: b.Thinking})
case "tool_use":
parts = append(parts, Part{Role: role, Kind: "tool_use", Name: b.Name, Text: pretty(b.Input)})
case "tool_result":
parts = append(parts, Part{Role: "tool", Kind: "tool_result", Text: resultText(b.Content)})
case "image":
parts = append(parts, Part{Role: role, Kind: "image", Text: "[image]"})
}
}
return parts
}
// textPart is words said, or, of what the agent put in itself (a reminder, a
// command's note), its context.
func textPart(role, text string) Part {
t := strings.TrimSpace(text)
if strings.HasPrefix(t, "<") || strings.HasPrefix(t, "# AGENTS.md") {
return Part{Role: role, Kind: "context", Text: text}
}
return Part{Role: role, Kind: "text", Text: text}
}
// resultText is what a tool answered: words, or blocks of them.
func resultText(raw json.RawMessage) string {
var s string
if json.Unmarshal(raw, &s) == nil {
return s
}
var blocks []struct {
Type string `json:"type"`
Text string `json:"text"`
}
if json.Unmarshal(raw, &blocks) != nil {
return string(raw)
}
var out []string
for _, b := range blocks {
switch b.Type {
case "text":
out = append(out, b.Text)
case "image":
out = append(out, "[image]")
}
}
return strings.Join(out, "\n")
}
// pretty is JSON laid out for reading, when it is short.
func pretty(raw json.RawMessage) string {
if len(raw) == 0 || len(raw) > 4000 {
return string(raw)
}
var b bytes.Buffer
if json.Indent(&b, raw, "", " ") != nil {
return string(raw)
}
return b.String()
}
// ---- Codex
var cxItem = []byte(`"type":"response_item"`)
// codexContent reads the items between the call before and this one: what the
// model was given (the prompt, a tool's output) and what it said (words,
// reasoning, a call of a tool). A rollout the Codex app has compressed (or
// unpacked) since the call was read is read in its new form: its lines and
// their places are the same. add is as claudeContent's.
func codexContent(c Call, add func(input bool, p Part) bool) error {
return codexContentOf(c, nil, add)
}
// codexContentOf is codexContent reading only the lines keep (when given)
// says yes to, as claudeContentOf.
func codexContentOf(c Call, keep func([]byte) bool, add func(input bool, p Part) bool) error {
path := c.File
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
if _, err := os.Stat(rolloutTwin(path)); err == nil {
path = rolloutTwin(path)
}
}
_, err := scanAt(path, c.From, nil, func(b []byte, start, end int64) bool {
if start >= c.To {
return false
}
if !bytes.Contains(b, cxItem) {
return true
}
if keep != nil && !keep(b) {
return true
}
var l struct {
Payload struct {
Type string `json:"type"`
Role string `json:"role"`
Name string `json:"name"`
Arguments string `json:"arguments"`
Input string `json:"input"`
Output json.RawMessage `json:"output"`
Content []struct {
Text string `json:"text"`
} `json:"content"`
Summary []struct {
Text string `json:"text"`
} `json:"summary"`
Action struct {
Command []string `json:"command"`
} `json:"action"`
} `json:"payload"`
}
if json.Unmarshal(b, &l) != nil {
return true
}
p := &l.Payload
words := func() string {
var s []string
for _, x := range p.Content {
s = append(s, x.Text)
}
return strings.Join(s, "\n")
}
switch p.Type {
case "message":
switch p.Role {
case "user":
return add(true, textPart("user", words()))
case "assistant":
return add(false, textPart("assistant", words()))
}
case "reasoning":
var s []string
for _, x := range p.Summary {
s = append(s, x.Text)
}
return add(false, Part{Role: "assistant", Kind: "thinking", Text: strings.Join(s, "\n\n")})
case "function_call":
return add(false, Part{Role: "assistant", Kind: "tool_use", Name: p.Name, Text: pretty(json.RawMessage(p.Arguments))})
case "custom_tool_call":
return add(false, Part{Role: "assistant", Kind: "tool_use", Name: p.Name, Text: p.Input})
case "local_shell_call":
return add(false, Part{Role: "assistant", Kind: "tool_use", Name: "shell", Text: strings.Join(p.Action.Command, " ")})
case "function_call_output", "custom_tool_call_output":
return add(true, Part{Role: "tool", Kind: "tool_result", Text: codexOutput(p.Output)})
}
return true
})
return err
}
// codexOutput is what a tool answered: words, or an object with them in.
func codexOutput(raw json.RawMessage) string {
var s string
if json.Unmarshal(raw, &s) == nil {
return s
}
var o struct {
Content string `json:"content"`
Output string `json:"output"`
}
if json.Unmarshal(raw, &o) == nil && (o.Content != "" || o.Output != "") {
if o.Content != "" {
return o.Content
}
return o.Output
}
return string(raw)
}