Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
sessions: a dsh frame is read into one buffer of its own size
yetone10939ebdf11f
diff --git a/internal/sessions/frames.go b/internal/sessions/frames.goindex 203f5e093..37034b0b4 100644--- a/internal/sessions/frames.go+++ b/internal/sessions/frames.go@@ -34,9 +34,13 @@ var frameDecoders = sync.Pool{New: func() any {// readFrame reads the frame at r into buf: a zstd frame whole, nil for a// skippable one. io.ErrUnexpectedEOF when the file ends inside it, io.EOF-// when it ends before it.-func readFrame(r *bufio.Reader, buf []byte) ([]byte, int64, error) {+// when it ends before it. size is how long frameSize found the frame to+// be: buf is made that large at once, not grown a block at a time.+func readFrame(r *bufio.Reader, buf []byte, size int) ([]byte, int64, error) {buf = buf[:0]+ if size > cap(buf) {+ buf = make([]byte, 0, size)+ }var n int64take := func(k int) ([]byte, error) {start := len(buf)@@ -77,6 +81,36 @@ func readFrame(r *bufio.Reader, buf []byte) ([]byte, int64, error) {return nil, 0, err}d := fhd[0]+ if _, err := take(frameHeaderLen(d)); err != nil {+ return nil, 0, err+ }+ for {+ h, err := take(3)+ if err != nil {+ return nil, 0, err+ }+ size, last, ok := blockLen(h)+ if !ok {+ return nil, 0, errFrame+ }+ if _, err := take(size); err != nil {+ return nil, 0, err+ }+ if last {+ break+ }+ }+ if d>>2&1 == 1 {+ if _, err := take(4); err != nil {+ return nil, 0, err+ }+ }+ return buf, n, nil+}++// frameHeaderLen is how many bytes of a zstd frame's header follow its+// descriptor d: the window descriptor, dictionary id and content size.+func frameHeaderLen(d byte) int {single := d>>5&1 == 1extra := 0if !single {@@ -95,35 +129,58 @@ func readFrame(r *bufio.Reader, buf []byte) ([]byte, int64, error) {case 3:extra += 8}- if _, err := take(extra); err != nil {- return nil, 0, err+ return extra+}++// blockLen is how long a zstd block is after its 3-byte header h, and+// whether it is its frame's last; not ok for the reserved block type.+func blockLen(h []byte) (size int, last, ok bool) {+ v := uint32(h[0]) | uint32(h[1])<<8 | uint32(h[2])<<16+ size = int(v >> 3)+ switch v >> 1 & 3 {+ case 1:+ size = 1 // RLE: one byte, repeated+ case 3:+ return 0, false, false}+ return size, v&1 == 1, true+}++// frameSize is the length of the zstd frame at off in f, found from its+// headers alone, read ahead without moving f: 0 for a skippable frame, or+// one the file doesn't yet hold whole. A dsh frame holding a pasted image+// is megabytes long, and grown a block at a time its buffer allocated+// about 2.6 times the frame.+func frameSize(f io.ReaderAt, off int64) int {+ var h [4]byte+ if n, _ := f.ReadAt(h[:], off); n < 4 || binary.LittleEndian.Uint32(h[:]) != 0xFD2FB528 {+ return 0+ }+ if n, _ := f.ReadAt(h[:1], off+4); n < 1 {+ return 0+ }+ d := h[0]+ at := off + 5 + int64(frameHeaderLen(d))for {- h, err := take(3)- if err != nil {- return nil, 0, err- }- v := uint32(h[0]) | uint32(h[1])<<8 | uint32(h[2])<<16- size := int(v >> 3)- switch v >> 1 & 3 {- case 1:- size = 1 // RLE: one byte, repeated- case 3:- return nil, 0, errFrame+ if n, _ := f.ReadAt(h[:3], at); n < 3 {+ return 0}- if _, err := take(size); err != nil {- return nil, 0, err+ size, last, ok := blockLen(h[:3])+ if !ok {+ return 0}- if v&1 == 1 {+ at += 3 + int64(size)+ if last {break}}if d>>2&1 == 1 {- if _, err := take(4); err != nil {- return nil, 0, err- }+ at += 4 // checksum}- return buf, n, nil+ if n, _ := f.ReadAt(h[:1], at-1); n < 1 {+ return 0+ }+ return int(at - off)}// scanFrames hands fn each whole line of a file of zstd frames, from the@@ -152,7 +209,7 @@ func scanFrames(path string, off, skip int64, fn func([]byte) bool) (next, nextSfirst := truefor {var n int64- frame, n, err = readFrame(r, frame)+ frame, n, err = readFrame(r, frame, frameSize(f, at))if err != nil {// the end, a frame still being written, or not one at all:// what was read stands, the rest waits for the next readdiff --git a/internal/sessions/frames_size_test.go b/internal/sessions/frames_size_test.gonew file mode 100644index 000000000..53b9961a0--- /dev/null+++ b/internal/sessions/frames_size_test.go@@ -0,0 +1,177 @@+package sessions++import (+ "bufio"+ "bytes"+ "encoding/binary"+ "math/rand"+ "os"+ "path/filepath"+ "reflect"+ "runtime"+ "strings"+ "testing"++ "github.com/klauspost/compress/zstd"+)++// imageLine is a dsh line carrying a pasted image of n base64 bytes, which+// zstd packs to about three quarters of its size.+func imageLine(r *rand.Rand, n int) string {+ const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"+ b := make([]byte, n)+ for i := range b {+ b[i] = b64[r.Intn(64)]+ }+ return `{"type":"user/message","data":{"content":[{"type":"image","data":"` + string(b) + `"}]}}`+}++// frameSize finds each frame's length from its headers, as readFrame+// reads it: frames with and without a checksum, with a long line, a+// skippable one (0, read by readFrame alone), and one cut short (0).+func TestFrameSizeIsWhatReadFrameReads(t *testing.T) {+ r := rand.New(rand.NewSource(5))+ plain, _ := zstd.NewWriter(nil)+ crc, _ := zstd.NewWriter(nil, zstd.WithEncoderCRC(true))+ defer plain.Close()+ defer crc.Close()+ var file []byte+ var starts []int+ add := func(b []byte) { starts = append(starts, len(file)); file = append(file, b...) }+ add(crc.EncodeAll([]byte(imageLine(r, 3<<20)+"\n"), nil))+ add(plain.EncodeAll([]byte("{}\n\n{\"a\":1}\n"), nil))+ skippable := binary.LittleEndian.AppendUint32(binary.LittleEndian.AppendUint32(nil, 0x184D2A5E), 3)+ add(append(skippable, 1, 2, 3))+ add(plain.EncodeAll([]byte(imageLine(r, 2<<20)+"\n"), nil))+ add(crc.EncodeAll([]byte("{\"b\":2}\n"), nil))+ p := filepath.Join(t.TempDir(), "session.v3.jsonl.zstd")+ os.WriteFile(p, file, 0o600)+ f, err := os.Open(p)+ if err != nil {+ t.Fatal(err)+ }+ defer f.Close()+ rd := bufio.NewReader(f)+ for i, at := range starts {+ buf, n, err := readFrame(rd, nil, 0)+ if err != nil {+ t.Fatalf("frame %d: %v", i, err)+ }+ size := frameSize(f, int64(at))+ if buf == nil && size != 0 || buf != nil && int64(size) != n {+ t.Fatalf("frame %d: frameSize %d, readFrame read %d", i, size, n)+ }+ }+ // a frame cut short has no size yet+ last := starts[3]+ if err := os.Truncate(p, int64(last+(starts[4]-last)/2)); err != nil {+ t.Fatal(err)+ }+ if size := frameSize(f, int64(last)); size != 0 {+ t.Fatalf("a frame cut short has size %d", size)+ }+}++// framesLines is every whole line of a file of zstd frames, decoded whole.+func framesLines(t *testing.T, p string) []string {+ t.Helper()+ raw, _ := os.ReadFile(p)+ d, _ := zstd.NewReader(nil)+ defer d.Close()+ dec, err := d.DecodeAll(raw, nil)+ if err != nil {+ t.Fatalf("decode: %v", err)+ }+ var out []string+ for s := string(dec); strings.Contains(s, "\n"); {+ i := strings.IndexByte(s, '\n')+ out, s = append(out, s[:i]), s[i+1:]+ }+ return out+}++// scanFrames hands over the lines it handed over before frames were read+// into buffers of their own size: long lines in frames of their own, a+// line split across frames, empty lines, and a last frame still being+// written, read on once it is whole.+func TestScanFramesLongLinesUnchanged(t *testing.T) {+ r := rand.New(rand.NewSource(6))+ enc, _ := zstd.NewWriter(nil, zstd.WithEncoderCRC(true))+ defer enc.Close()+ long := imageLine(r, 3<<20)+ parts := [][]byte{+ enc.EncodeAll([]byte(`{"type":"session","id":"s"}`+"\n"), nil),+ enc.EncodeAll([]byte(imageLine(r, 5<<20)+"\n\n"+`{"seq":1}`+"\n"), nil),+ enc.EncodeAll([]byte(long[:len(long)/2]), nil), // a line split across frames+ enc.EncodeAll([]byte(long[len(long)/2:]+"\n"+`{"seq":2}`+"\n"), nil),+ enc.EncodeAll([]byte(imageLine(r, 1<<20)+"\n"), nil),+ }+ p := filepath.Join(t.TempDir(), "session.v3.jsonl.zstd")+ var whole []byte+ for _, b := range parts {+ whole = append(whole, b...)+ }+ os.WriteFile(p, whole, 0o600)+ want := framesLines(t, p)+ read := func(off, skip int64) (lines []string, next, nextSkip, end int64) {+ next, nextSkip, end, err := scanFrames(p, off, skip, func(b []byte) bool { lines = append(lines, string(b)); return true })+ if err != nil {+ t.Fatal(err)+ }+ return lines, next, nextSkip, end+ }+ got, _, _, end := read(0, 0)+ if end != int64(len(whole)) || !reflect.DeepEqual(got, want) {+ t.Fatalf("read %d lines to %d, want %d to %d", len(got), end, len(want), len(whole))+ }+ // the file as it was with the last frame half written, then whole+ cut := len(whole) - len(parts[4])/2+ os.WriteFile(p, whole[:cut], 0o600)+ first, next, nextSkip, end := read(0, 0)+ if end != int64(len(whole)-len(parts[4])) {+ t.Fatalf("a half-written last frame read to %d", end)+ }+ os.WriteFile(p, whole, 0o600)+ rest, _, _, _ := read(next, nextSkip)+ if got := append(first, rest...); !reflect.DeepEqual(got, want) {+ t.Fatalf("read on after a half-written frame: %d lines, want %d", len(got), len(want))+ }+}++// A frame holding a pasted image is read into one buffer of its own+// size. Grown a block at a time it allocated about 6 times the frame.+func TestScanFramesReadsALongFrameOnce(t *testing.T) {+ if raceOn {+ t.Skip("-race lets a pool's decoders go")+ }+ r := rand.New(rand.NewSource(7))+ enc, _ := zstd.NewWriter(nil, zstd.WithEncoderCRC(true))+ line := imageLine(r, 6<<20)+ frame := enc.EncodeAll([]byte(line+"\n"), nil)+ enc.Close()+ p := filepath.Join(t.TempDir(), "session.v3.jsonl.zstd")+ os.WriteFile(p, frame, 0o600)+ scan := func() {+ if _, _, _, err := scanFrames(p, 0, 0, func(b []byte) bool {+ if !bytes.Equal(b, []byte(line)) {+ t.Fatal("the line handed over is not the one written")+ }+ return true+ }); err != nil {+ t.Fatal(err)+ }+ }+ scan()+ var a, b runtime.MemStats+ runtime.GC()+ runtime.ReadMemStats(&a)+ scan()+ runtime.ReadMemStats(&b)+ got := b.TotalAlloc - a.TotalAlloc+ t.Logf("a scan of a %d-byte frame of a %d-byte line allocated %d bytes", len(frame), len(line), got)+ // the frame once and the line once, decoded, and 2 MiB for the rest;+ // grown a block at a time the frame alone was ~25 MB of this+ if most := uint64(len(frame)+len(line)) + 2<<20; got > most {+ t.Fatalf("a scan of a %d-byte frame allocated %d bytes, want at most %d", len(frame), got, most)+ }+}