RustCats on X: a fresh magpie showed every agent's session history on
the Sessions page before anyone agreed to it, and asked for a per-agent
choice instead.
- sessions.Reads(agent) (internal/sessions/access.go) is the one gate,
from settings.SessionReads {Asked, All, Agents}. Every lister asks it,
the aggregators (allFiles, callFilesWith, callSourcesWith,
traceLineFiles, wslFiles, DesktopDataDirs) filter by it again, and
readers of an already-listed session (TranscriptOf, ContentOf,
SearchSession, SubagentTranscript, CarryOver, ReadCallSource) return
ErrNotRead, so a cache from before a switch was turned off goes no
further. The Sessions list, stats, /api/sessions/overview, the call and
trace indexes, Usage's agent-file calls and the Codex/Pi title readers
all pass it.
- SettleReads decides once and saves: an install with sessions.json or
a calls-v* cache read sessions before and keeps all on; a fresh one
reads none, not asked. A saved value is never replaced; save, sync and
restore keep it (KeepOwn); only SetSessionReads writes it. An
unwritable settings.json reads nothing this run.
- GUI: GET/POST /api/sessions/reads. While not asked, the Sessions page
shows a consent card: each agent found (by a stat of its folder only)
with a switch, Allow selected and Allow all. Settings > Privacy >
Agents' session history has "Agents found later" and a row per agent.
Strings in en, zh, zh-TW, ja, de.
- CLI: magpie sessions read [on|off all|<agent>...]; magpie sessions
says on stderr when nothing is allowed yet; the TUI's empty Sessions
page says how.
Read either way, because none of it lists or shows a session: the
gateway's own usage ledger, its projections and the conversations it
recorded (magpie's own records, so usage and cost keep working); the
agents' config and sign-in files; Claude Code's and Codex's
history.jsonl last-used time and a session file's mtime for a quota's
first use; Codex's newest rollout's last line before magpie restarts
Codex (codexTurnOpen).
Tests: TestFreshInstallReadsNoAgentUntilAllowed,
TestSettleReadsKeepsWhatWasThere, TestSessionReadsRoutes,
TestSessionsRead, TestSessionsNotAskedSaysHow, sessions-reads.test.cjs
(26 cases: consent and Settings in 5 languages, Chromium and WebKit,
390 to 900 wide).
Run on mote:
- full Go suite (gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui,
GOOS=windows go vet, go build, go test -timeout 45m ./...) at this
change on 8c63c5b78: 79 packages ok; internal/gui red on
TestSettingsSaveKeepsWhatItDoesNotSend (mine: SessionReads is written
by SetSessionReads only, the test now sets it so; green after) and
TestMainWindowCorners (the same on origin/main c1da9ccda, #3949).
- whole GUI suite (make test-ui, concurrency 2) at this change on
8c63c5b78: 14985 tests, 276 failed in 44 files. Mine: i18n-keys
(comments named "Allow all"/"Allow selected"; lowercased) and
account-discovery (read the last Privacy heading; now the one above
its list); both green after. The 44 files again on origin/main
c1da9ccda: 36 red there too (#4304, with #3218, #3412, #3414, #3795,
#4120, #4125). stays-strict green alone; gateway-key-models webkit red
1 of 3 alone on main too; plugin-github de 440 red on main (#3218).
- at the pushed head (rebased on cd10dfbe6): go build ./...; go test
internal/sessions, settings, cli, tui ok; internal/gui
TestSettingsSave*, TestSessionReads*, TestSessions* ok; i18n-keys,
account-discovery, sessions-reads: 62 pass, 0 fail.
Broken by hand:
- the aggregator filter off with the leaf gates on: green (the leaf
gates hold); both off: TestFreshInstall red on List, Agents, Calls,
CallSources, traces, stats, TranscriptOf and Search.
- consentApplies=false and Settings' old save({all}) without the rows:
all 13 Chromium cases of sessions-reads red.
- sessNothing's hint removed: TestSessionsNotAskedSaysHow red.
Real binary in a sandbox HOME on mote (removed after): a fresh
`magpie sessions` is empty with the hint on stderr; `sessions read`
says not asked, claude off; `read --help` prints usage and saves
nothing; `read on claude` lists the one session; `read off all`
empties it again.
Not tried: the GUI and real app on Windows and Linux; WSL agents in the
consent list (WSL distros are not looked into; Allow all covers them);
a fresh install of a signed release build.
395 lines
11 KiB
Go
395 lines
11 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
|
|
// Model is the model an assistant part's own line names (#2253):
|
|
// Claude Code's message.model, the model the reply came back from,
|
|
// and Codex's turn_context.model, the one its turn was sent to. ""
|
|
// where the file names none; never one guessed from elsewhere.
|
|
Model string `json:"model,omitempty"`
|
|
}
|
|
|
|
// 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) {
|
|
if !Reads(c.Agent) {
|
|
return Content{Input: []Part{}, Output: []Part{}}, ErrNotRead
|
|
}
|
|
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"`
|
|
Model string `json:"model"`
|
|
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):
|
|
// "<synthetic>" is Claude Code's own line (an error, a refusal
|
|
// it says), which no model wrote
|
|
model := l.Message.Model
|
|
if model == "<synthetic>" {
|
|
model = ""
|
|
}
|
|
for _, p := range claudeParts(l.Message.Content, "assistant") {
|
|
if p.Role == "assistant" {
|
|
p.Model = model
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
// the model the turn was sent to, as its turn_context line names it
|
|
// (#2253): each of the turn's assistant parts is said with it
|
|
model, say := "", add
|
|
add = func(input bool, p Part) bool {
|
|
if p.Role == "assistant" {
|
|
p.Model = model
|
|
}
|
|
return say(input, p)
|
|
}
|
|
_, err := scanAt(path, c.From, nil, func(b []byte, start, end int64) bool {
|
|
if start >= c.To {
|
|
return false
|
|
}
|
|
if !bytes.Contains(b, cxItem) {
|
|
if bytes.Contains(b, cxTurn) {
|
|
var l struct {
|
|
Type string `json:"type"`
|
|
Payload struct {
|
|
Model string `json:"model"`
|
|
} `json:"payload"`
|
|
}
|
|
if json.Unmarshal(b, &l) == nil && l.Type == "turn_context" {
|
|
model = l.Payload.Model
|
|
}
|
|
}
|
|
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)
|
|
}
|