dsh only appends to a session file, a line at a time or, packed
(.zstd), a frame of lines at a time. Every reply still made the
Sessions and Usage pages read, and for a packed file decompress, the
whole file again: a packed file "can't be read on from the middle of a
frame".
It can be read on from the start of one. frames.go walks a file of zstd
frames without decoding them (RFC 8878: the frame header, then blocks
that each give their length, then the checksum), decodes one frame at a
time, and says where to go on from: the frame the first line not yet
whole starts in, and how far into what it holds. A frame not yet
written whole waits for the next read, and so does a line a frame ends
inside of.
parseDsh keeps a tail (tail.go) and the header it read, and counts each
event as it comes (dshCount) instead of collecting them all first. A
fork's inherited end-seed met in a later read starts the counts over
from the session's start, keeping its title and name, as the whole read
did by cutting at the last one. A file that shrank or was replaced is
read whole again. One change from before: a last line not ended by a
newline now waits for it, like every other appended file's.
Measured (CPU, getrusage; 50 MB of events, a reply appended between
reads; packed: 17 MB of frames; machine loaded):
plain before: ~110-120 ms per read after an append
after: 0.4-2.6 ms per read after an append; first read ~120 ms
packed before: ~190-250 ms per read after an append
after: 0.4-0.5 ms per read after an append; first read ~260 ms
Tests: TestDshReadsOnlyWhatWasAppended runs the shared append, half
line, cut-short and rewritten-in-place cases.
TestDshPackedReadsOnlyWhatWasAppended appends a frame (read alone), half a frame (waits), a
frame ending inside a line and the frame that ends it (read on from the
first of the two, the line counted once), then cuts the file short and
replaces it with a longer one (read whole). TestDshEndSeedAcrossReads
writes a fork's seeded lines, its end-seed and its own, a read apart
each. Every read on is checked equal to a whole read.
Broken by hand: resume off -> red in both ReadsOnly tests ("after a
line appended to 1308 bytes, read from 0", "after a frame appended to
654 bytes, read from 0"); a line split over two frames resumed at the
second frame -> red (read on differs from whole); the end-seed reset
removed -> red in TestDshEndSeedAcrossReads and TestDsh.
Run: go test ./internal/sessions -count=1 -timeout 45m ok; gofmt -l
and go vet ./internal/sessions clean. The full-suite record is in the
last commit of this series.
78 lines
2.5 KiB
Go
78 lines
2.5 KiB
Go
package sessions
|
|
|
|
import "os"
|
|
|
|
// tail is where a reader of a file another agent appends to left off: the
|
|
// offset after the last whole line it handled, and enough of the file as it
|
|
// was to know it again. A summary kept with one reads on from Off when the
|
|
// file has only grown, instead of reading it whole again (dsh's, Grok
|
|
// Build's, Droid's and Kimi Code's sessions are kept so). A file that
|
|
// shrank, or whose start or sampled prefix is no longer what was read, is
|
|
// read whole again: it was rewritten or replaced, not appended to.
|
|
//
|
|
// A packed file (frames of zstd, frames.go) is read on from the frame at
|
|
// Off, Skip bytes into what that frame holds, and Size there is the end of
|
|
// that frame: the bytes hashed always take in what is read again.
|
|
type tail struct {
|
|
Off int64 `json:"off"`
|
|
Skip int64 `json:"skip,omitempty"`
|
|
Size int64 `json:"size"`
|
|
Head string `json:"head"`
|
|
HeadSize int `json:"head_size"`
|
|
Hash string `json:"hash"`
|
|
}
|
|
|
|
// markTail is the tail of a file read up to off, its first size bytes the
|
|
// ones read and to be found again.
|
|
func markTail(path string, off, skip, size int64) *tail {
|
|
head := headOf(path)
|
|
if int64(len(head)) > size {
|
|
head = head[:size]
|
|
}
|
|
h := prefixHash(path, size)
|
|
if h == "" && size > 0 {
|
|
return nil
|
|
}
|
|
return &tail{Off: off, Skip: skip, Size: size, Head: hashHead(head), HeadSize: len(head), Hash: h}
|
|
}
|
|
|
|
// holds says whether the file at path is still the one t was read from,
|
|
// grown or as it was, so it can be read on from t.Off.
|
|
func (t *tail) holds(path string) bool {
|
|
if t == nil {
|
|
return false
|
|
}
|
|
fi, err := os.Stat(path)
|
|
if err != nil || fi.Size() < t.Size || t.Off > t.Size {
|
|
return false
|
|
}
|
|
return sameHead(headOf(path), t.Head, t.HeadSize) && prefixHash(path, t.Size) == t.Hash
|
|
}
|
|
|
|
// dropUse takes back what use counted, leaving no model or day it made
|
|
// with nothing in it: a summary read on from where it was is then the one
|
|
// a whole read makes.
|
|
func (s *state) dropUse(date, model string, t Tokens) {
|
|
if t.zero() {
|
|
return
|
|
}
|
|
s.unuse(date, model, t)
|
|
if m, ok := s.Models[model]; ok && m.zero() {
|
|
delete(s.Models, model)
|
|
if len(s.Models) == 0 {
|
|
s.Models = nil
|
|
}
|
|
}
|
|
if d := s.Days[date]; d != nil {
|
|
if m, ok := d.Models[model]; ok && m.zero() {
|
|
delete(d.Models, model)
|
|
if len(d.Models) == 0 {
|
|
d.Models = nil
|
|
}
|
|
}
|
|
if len(d.Models) == 0 && d.Active == 0 && d.Hours == nil && d.Prompts == 0 && d.Replies == 0 && d.Tools == nil && d.Skills == nil {
|
|
delete(s.Days, date)
|
|
}
|
|
}
|
|
}
|