Files
magpie/internal/sessions/dsh.go
T
yetone 9572a35761 usage: dsh sessions in Context go by their titles (Kumo31 on Discord)
Usage -> Context listed DeepSeek Harness sessions as "magpie-a",
"magpie-f" and so on, while Usage -> Sessions showed their titles. Real
dsh 0.2.0-rc.2 sends no session header on its chat-completions and
messages routes. The gateway therefore keys each request by
conversationID, a hash of the first user message. contextOf joined
titles on agent + session id, and a hash never matches a dsh session
id. The responses route sends session_id and was already joined.

- The dsh reader (sessions) records the ids the gateway gives a
  session's requests: the first typed user/message and the
  session/title-llm-request message. Each is hashed in the forms dsh
  sends it: a string, and a text part with or without a cache mark.
  The text goes through a JSON.stringify-compatible encoder (jsString),
  because encoding/json escapes <, > and &. These ids are
  Session.Convs; cacheVersion goes to 22.
- contextOf (joinConvs) puts a route that names no session under the
  session its conv id came from. An id two sessions share is left as
  it is. The title call merges in and is skipped as a tool-less side
  call. The existing title join then finds the session.

Fixture: internal/sessions/testdata/dsh-gateway holds two real dsh
session files and the four requests that dsh sent for them to a fake
upstream: a turn and a title call on each of the completions and
messages routes, headers and body as captured. Only the cwd is
rewritten to /work/proj.

Siblings: other agents that send no session header have the same gap,
but none was run to see what it sends. Filed as #3263.

Run at bc11b8a5f (on origin/main ccbc72781):
- gofmt -l: clean.
- go vet: darwin, GOOS=linux -tags nogui and GOOS=windows ./...: clean.
- set -o pipefail; go test -timeout 45m ./... | tee: exit 0, every
  package ok.
- No GUI or page files changed, so the GUI suite was not run.

Broken by hand:
- joinConvs commented out: TestContextDshSessionTitles red, the
  sessions keyed magpie-950998..., magpie-3dc586... and so on, with no
  titles.
- json.Marshal in place of jsString: TestDshSessionConvsAreTheGatewayIDs
  red on all four requests.
- title-llm-request not collected: that test red on both title calls.

New tests:
- TestDshRoutesCarryTheSessionConvs sends the captured requests
  through the gateway's handler and finds each route's Conv among its
  session's Convs.
- On the old code the tests don't build, because Convs doesn't exist.

Not tried:
- a masked first prompt (the hash is taken after masking, so the
  session stays untitled);
- a first prompt with an image or several parts;
- dsh's subagent files and forks (seeded sessions);
- a conv after compaction;
- the DSH desktop app;
- Kumo31's own history. Old routes keep their Conv, so they should
  join on the next read, but that was not seen.
2026-10-22 19:03:32 +08:00

464 lines
13 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package sessions
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"unicode/utf16"
"github.com/yetone/magpie/internal/appdir"
)
// DeepSeek Harness (dsh) keeps a session in a folder of its own, under
// $DSH_HOME/sessions/ (~/.dsh), in a folder per working directory: its
// events a line each, in session.jsonl, zstd frames appended a batch at a
// time (session.jsonl.zstd) unless compression is off. A newer format is
// session.v<N>.jsonl[.zstd]; the file of an older one stays behind when
// the session is carried over, so only the latest is read. The first line
// is the header — the session's id, cwd and time made, and for a
// subagent's its parent's id (parentSession, delegationDepth 1 and more) —
// then an event per line: {type, seq, time, data}. A "user/message" is a
// prompt typed when its source's kind is "user" (else instructions, a
// plugin's, a subagent's report…), a "session/title" names the session,
// and an "assistant/message" carries its model and its usage — input
// without the cache, output with the reasoning. A session forked from
// another starts with that one's events: before format 2 the first
// seedLength of them, after it those up to a "session/end-seed" said to be
// inherited. They count where they were first written. dsh has no command
// that picks up a session by its id.
// DshDir is dsh's folder: $DSH_HOME, else ~/.dsh.
func DshDir() string {
if d := appdir.Getenv("DSH_HOME"); d != "" {
return expandHome(d)
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".dsh")
}
// session.jsonl, session.v3.jsonl.zstd, …
var dshName = regexp.MustCompile(`^session(?:\.v(\d+))?\.jsonl(\.zstd)?$`)
// dshHead is a session file's first line.
type dshHead struct {
Type string `json:"type"`
Version int `json:"version"`
ID string `json:"id"`
Cwd string `json:"cwd"`
CreatedAt int64 `json:"createdAt"`
ParentSession string `json:"parentSession"`
DelegationDepth int `json:"delegationDepth"`
SeedLength int64 `json:"seedLength"`
IsSeeded bool `json:"isSeeded"`
}
// dshHeads keeps the headers read, by path: a file's never changes.
var (
dshMu sync.Mutex
dshHeads = map[string]dshHead{}
)
func dshFiles() []file {
dirs, _ := filepath.Glob(filepath.Join(DshDir(), "sessions", "*", "*"))
var out []file
parent := map[string]string{}
seen := map[string]bool{}
dshMu.Lock()
defer dshMu.Unlock()
for _, d := range dirs {
es, err := os.ReadDir(d)
if err != nil {
continue
}
// the latest format's file, the one written last of its two kinds
var f file
gen := -1
for _, e := range es {
m := dshName.FindStringSubmatch(e.Name())
if m == nil {
continue
}
n, _ := strconv.Atoi(m[1])
c := file{agent: "dsh", path: filepath.Join(d, e.Name()), main: true}
if !stat(&c) || n < gen || n == gen && !c.mod.After(f.mod) {
continue
}
f, gen = c, n
}
if gen < 0 {
continue
}
h, ok := dshHeads[f.path]
if !ok {
if h, ok = dshReadHead(f.path); !ok {
continue
}
dshHeads[f.path] = h
}
seen[f.path] = true
f.sid = h.ID
if h.DelegationDepth > 0 {
parent[h.ID] = h.ParentSession
}
out = append(out, f)
}
for p := range dshHeads {
if !seen[p] {
delete(dshHeads, p)
}
}
for i := range out {
root := out[i].sid
for n := 0; parent[root] != "" && n < 64; n++ {
root = parent[root]
}
out[i].key = "dsh:" + root
out[i].main = parent[out[i].sid] == ""
}
return out
}
// dshSessionDir is the folder dsh keeps a session file's session in, all
// of it that session's own: <sessions>/<project>/<the id, escaped>/, with
// the session's generations and its session.lock. "" when the file isn't
// in the folder its id names, so a delete never takes another's.
func dshSessionDir(f file) string {
if f.agent != "dsh" || f.sid == "" {
return ""
}
d := filepath.Dir(f.path)
if filepath.Base(d) != dshSegment(f.sid) || filepath.Dir(filepath.Dir(d)) != filepath.Join(DshDir(), "sessions") {
return ""
}
return d
}
// dshSegment is a session id as dsh names its folder (encodeSegment in
// dsh-session-persistence-jsonl): A–Z, a–z, 0–9, '.', '_' and '-' kept,
// every other UTF-16 unit ~XXXX, and "." and ".." escaped whole.
func dshSegment(id string) string {
switch id {
case ".":
return "~002E"
case "..":
return "~002E~002E"
}
var b strings.Builder
for _, u := range utf16.Encode([]rune(id)) {
if u < 0x80 && (u >= 'A' && u <= 'Z' || u >= 'a' && u <= 'z' || u >= '0' && u <= '9' || u == '.' || u == '_' || u == '-') {
b.WriteByte(byte(u))
} else {
fmt.Fprintf(&b, "~%04X", u)
}
}
return b.String()
}
// dshCacheRecord is the row dsh's session listing caches of a session
// (dsh-session-projection-cache, on dsh-storage-json's per-record layout):
// <dsh>/storages/session_projcache/sessions/<id>.json. "" for an id that
// layout can't hold.
func dshCacheRecord(id string) string {
if !safeID.MatchString(id) {
return ""
}
return filepath.Join(DshDir(), "storages", "session_projcache", "sessions", id+".json")
}
// dshOpen reads a session file's lines, decompressed.
func dshOpen(path string) (io.ReadCloser, error) { return openLines(path) }
func dshReadHead(path string) (dshHead, bool) {
var h dshHead
r, err := dshOpen(path)
if err != nil {
return h, false
}
defer r.Close()
b, _ := bufio.NewReaderSize(r, 64<<10).ReadBytes('\n')
if json.Unmarshal(bytes.TrimSpace(b), &h) != nil || h.Type != "session" || h.ID == "" {
return h, false
}
return h, true
}
// dshResponse is what the vendor's own reply named: the model and provider
// that answered, under a streamed reply's replay state.
type dshResponse struct {
Model string `json:"model"`
Provider string `json:"provider"`
ResponseModel string `json:"responseModel"`
}
// dshReplay is a reply's replay state, where dsh keeps what the vendor's own
// reply named: it is the only place a swapped model shows (the provider
// answered with another).
type dshReplay struct {
Response dshResponse `json:"response"`
}
// dshSource is the source of an assistant/message: the model and provider dsh
// was told to use, and, under ReplayState, the replay state's record of the
// response.
type dshSource struct {
Model string `json:"model"`
Provider string `json:"provider"`
ReplayState dshReplay `json:"replayState"`
}
// dshEvent is an event line: packed rows of a streamed reply's chunks carry
// neither type nor time of their own and are passed over.
type dshEvent struct {
Type string `json:"type"`
Seq *int64 `json:"seq"`
Time int64 `json:"time"`
Data struct {
Content json.RawMessage `json:"content"`
Source struct {
Kind string `json:"kind"`
} `json:"source"`
Title string `json:"title"`
Inherited bool `json:"inherited"`
// Messages are a title call's (session/title-llm-request)
Messages []struct {
Role string `json:"role"`
Content json.RawMessage `json:"content"`
} `json:"messages"`
Message struct {
Source dshSource `json:"source"`
} `json:"message"`
Usage *struct {
Input int `json:"inputTokens"`
Output int `json:"outputTokens"`
CacheRead int `json:"cacheReadTokens"`
CacheWrite int `json:"cacheWriteTokens"`
} `json:"usage"`
} `json:"data"`
}
var dshWanted = [][]byte{[]byte(`"user/message"`), []byte(`"session/title"`), []byte(`"session/title-llm-request"`), []byte(`"assistant/message"`), []byte(`"session/end-seed"`)}
// dshCarry is what a read of a dsh session file carries on with: where it
// read to and the header it read first.
type dshCarry struct {
Tail *tail `json:"tail,omitempty"`
Head dshHead `json:"head"`
}
// parseDsh reads a session file. dsh only appends to one, a line at a time
// or, packed, a frame of lines at a time, so a file read before is read on
// from where that read left off: a packed one from the frame it left off
// in. A file cut short or replaced is read whole again, and a line or frame
// not yet written whole waits for the next read.
func parseDsh(f file, old *state) *state {
var s *state
var c dshCarry
var off, skip int64
if old != nil && old.Dsh != nil && old.Dsh.Tail.holds(f.path) {
s, c = old.clone(), *old.Dsh
off, skip = c.Tail.Off, c.Tail.Skip
} else {
s = &state{}
}
s.Size, s.Mod = f.size, f.mod.UnixNano()
headed := c.Tail != nil
ok := true
each := func(b []byte) bool {
b = bytes.TrimSpace(b)
if len(b) == 0 || len(b) > maxLine {
return true
}
if !headed {
headed = true
if json.Unmarshal(b, &c.Head) != nil || c.Head.Type != "session" {
ok = false
return false
}
s.ID, s.Cwd = c.Head.ID, c.Head.Cwd
s.saw(ms(c.Head.CreatedAt), f.main)
return true
}
dshCount(s, &c.Head, b, f.main)
return true
}
var next, nextSkip, end int64
var err error
if packed(f.path) {
next, nextSkip, end, err = scanFrames(f.path, off, skip, each)
} else {
next, err = scanAt(f.path, off, nil, func(b []byte, _, _ int64) bool { return each(b) })
end = next
}
if !ok {
// not a session's file: only its size and time
return &state{Size: f.size, Mod: f.mod.UnixNano()}
}
c.Tail = nil
if err == nil {
c.Tail = markTail(f.path, next, nextSkip, end)
}
s.Dsh = &c
return s
}
// dshCount counts one event of a session file after its header.
func dshCount(s *state, h *dshHead, b []byte, main bool) {
var e dshEvent
if dshWant(b) {
if json.Unmarshal(b, &e) != nil || e.Type == "" {
return
}
} else if t := dshNum(b, "time"); t > 0 {
// only its time and place looked at
e.Time = t
if n := dshNum(b, "seq"); n >= 0 {
e.Seq = &n
}
} else {
return
}
switch e.Type {
case "user/message":
if main && s.Title == "" && s.First == "" && e.Data.Source.Kind == "user" {
s.Convs = append(s.Convs, dshConvs(e.Data.Content)...)
if t := ccText(e.Data.Content); strings.HasPrefix(t, "<") {
s.First = untagged(t)
} else {
s.Title = title(t)
}
}
case "session/title":
if t := title(e.Data.Title); t != "" {
s.Named = t
}
case "session/title-llm-request":
if main {
for _, m := range e.Data.Messages {
if m.Role == "user" {
s.Convs = append(s.Convs, dshConvs(m.Content)...)
break
}
}
}
case "session/end-seed":
if h.Version >= 2 && h.IsSeeded && e.Data.Inherited {
// the events a fork was seeded with end here: what was
// counted of them is its source's
n := &state{ID: s.ID, Cwd: s.Cwd, Title: s.Title, First: s.First, Named: s.Named, Size: s.Size, Mod: s.Mod}
n.saw(ms(h.CreatedAt), main)
*s = *n
return
}
}
if h.Version < 2 && e.Seq != nil && *e.Seq < h.SeedLength {
return
}
at := ms(e.Time)
s.saw(at, main)
if u := e.Data.Usage; e.Type == "assistant/message" && u != nil && e.Data.Message.Source.Model != "" {
s.use(dateOf(at), e.Data.Message.Source.Model, Tokens{Input: u.Input, Output: u.Output, CacheRead: u.CacheRead, CacheWrite: u.CacheWrite})
}
}
func dshWant(b []byte) bool {
for _, w := range dshWanted {
if bytes.Contains(b, w) {
return true
}
}
return false
}
// dshNum is the number an event's key (its first of the name) holds,
// without decoding the rest of it; -1 for none.
func dshNum(b []byte, key string) int64 {
i := bytes.Index(b, []byte(`"`+key+`":`))
if i < 0 {
return -1
}
rest := b[i+len(key)+3:]
j := 0
for j < len(rest) && j < 20 && rest[j] >= '0' && rest[j] <= '9' {
j++
}
n, err := strconv.ParseInt(string(rest[:j]), 10, 64)
if err != nil {
return -1
}
return n
}
// dshConvs are the conversation ids magpie's gateway gives the requests a
// message starts (gateway's conversationID). dsh sends no session id on
// its chat-completions and messages routes, so the gateway keys each such
// request by the hash of its first user message as sent, and the Context
// page finds the session only through it (Kumo31 on Discord). A message
// of one text part goes out as a plain string, or, as the last user
// message on the messages route, as that part with a cache mark: each
// spelling is hashed. Anything else (an image, several parts) gives none.
func dshConvs(content json.RawMessage) []string {
var parts []struct {
Type string `json:"type"`
Text string `json:"text"`
}
if json.Unmarshal(content, &parts) != nil || len(parts) != 1 || parts[0].Type != "text" || parts[0].Text == "" {
return nil
}
t := jsString(parts[0].Text)
out := make([]string, 0, 3)
for _, m := range []string{
`{"role":"user","content":` + t + `}`,
`{"role":"user","content":[{"type":"text","text":` + t + `}]}`,
`{"role":"user","content":[{"type":"text","text":` + t + `,"cache_control":{"type":"ephemeral"}}]}`,
} {
sum := sha256.Sum256([]byte(m))
out = append(out, "magpie-"+hex.EncodeToString(sum[:12]))
}
return out
}
// jsString is a string as JavaScript's JSON.stringify writes it, the way
// dsh's requests carry it: unlike encoding/json it leaves <, >, &, U+2028
// and U+2029 as they are.
func jsString(s string) string {
var b strings.Builder
b.Grow(len(s) + 2)
b.WriteByte('"')
for _, r := range s {
switch r {
case '"':
b.WriteString(`\"`)
case '\\':
b.WriteString(`\\`)
case '\b':
b.WriteString(`\b`)
case '\f':
b.WriteString(`\f`)
case '\n':
b.WriteString(`\n`)
case '\r':
b.WriteString(`\r`)
case '\t':
b.WriteString(`\t`)
default:
if r < 0x20 {
fmt.Fprintf(&b, `\u%04x`, r)
} else {
b.WriteRune(r)
}
}
}
b.WriteByte('"')
return b.String()
}