Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

6.4 KB
Raw Blame
package writer

import (
	"bytes"
	"crypto/sha256"
	"errors"
	"fmt"
	"os"
)

// ErrRetentionWindowFull is returned by a guarded append whose rotation would delete a file holding
// the guard's output. Nothing is written or rotated when it is returned.
var ErrRetentionWindowFull = errors.New("the runtime log's rotation window is full of this run's own output")

// RetentionGuard keeps one run of appends -- a session backfill sweep, say -- from rotating its
// own output out of the runtime log, and reports how much of that output the log still holds.
//
// The log keeps the live file plus a fixed number of archives, and every rotation deletes the
// oldest archive. A run that writes more than that window used to rotate its own first events away
// before it finished, so a backfill of 595,640 events kept 27,550 and still reported success
// (#619). With a guard, the append whose rotation would delete a file holding the run's output
// fails with ErrRetentionWindowFull instead, and the caller can stop where it is and leave
// the rest for the next run.
//
// The decision is made under the log's lock from where the guard's files actually are, not from how
// many it has written: the hooks and the collector share the log and can rotate it several times
// between two guarded appends. A guarded rotation is refused when the archive it would delete holds
// any of the guard's output. Output another writer has already rotated out cannot be protected;
// Retained reports it.
//
// The zero value is ready to use. A guard is not safe for concurrent use; give each run its own.
type RetentionGuard struct {
	path     string
	archives int
	segments []retentionSegment
}

// retentionSegment is a run of consecutive guarded appends that landed in one file, identified by
// the file and by the first of those lines: where it starts and a digest of its bytes. The file
// identity alone is not enough, because filesystems recycle the inode of a deleted archive for the
// next file created, and a recycled inode would make a rotated-out segment look retained.
//
// file is nil when the file could not be identified; its events are then assumed retained rather
// than reported lost on no evidence.
type retentionSegment struct {
	file   os.FileInfo
	offset int64
	length int
	digest [sha256.Size]byte
	end    int64
	events int
}

// Written is the number of events the guard's appends wrote. Appends the writer suppressed as
// duplicates wrote nothing and are not counted.
func (g *RetentionGuard) Written() int {
	if g == nil {
		return 0
	}
	n := 0
	for _, s := range g.segments {
		n += s.events
	}
	return n
}

// Retained is the number of the guard's written events still in a file the rotation contract
// keeps: the live log or one of its archives. Written minus Retained is what was rotated out.
func (g *RetentionGuard) Retained() int {
	if g == nil || len(g.segments) == 0 {
		return 0
	}
	var kept []os.FileInfo
	var keptPaths []string
	for _, p := range retainedPaths(g.path, g.archives) {
		if info, err := os.Stat(p); err == nil {
			kept = append(kept, info)
			keptPaths = append(keptPaths, p)
		}
	}
	n := 0
	for _, s := range g.segments {
		if s.file == nil {
			n += s.events
			continue
		}
		for i, info := range kept {
			if s.isFile(keptPaths[i], info) {
				n += s.events
				break
			}
		}
	}
	return n
}

// isFile reports whether the file at path, stat'd as info, is the one this segment was written to.
func (s retentionSegment) isFile(path string, info os.FileInfo) bool {
	return s.file != nil && os.SameFile(s.file, info) && s.firstLineAt(path)
}

// firstLineAt reports whether path still holds the segment's first line where it was written.
func (s retentionSegment) firstLineAt(path string) bool {
	f, err := os.Open(path)
	if err != nil {
		return false
	}
	defer f.Close()
	buf := make([]byte, s.length)
	if _, err := f.ReadAt(buf, s.offset); err != nil {
		return false
	}
	digest := sha256.Sum256(buf)
	return bytes.Equal(digest[:], s.digest[:])
}

// rotationWouldDiscardOwnOutput reports whether rotating path now would delete a file holding the
// guard's output. Rotation deletes at most the archive at .archives (and anything numbered past it,
// which no reader looks at), so that is the one file to check; when an earlier rotation left a gap
// below it, asymptoteobserve.RotateRuntimeLog fills the gap and deletes nothing, and the answer is
// merely cautious. Called under the log's lock, so the answer holds for the rotation that follows it.
func (g *RetentionGuard) rotationWouldDiscardOwnOutput(path string, archives int) (bool, error) {
	if len(g.segments) == 0 {
		return false, nil
	}
	if archives < 1 {
		archives = DefaultRotateArchives
	}
	victim := fmt.Sprintf("%s.%d", path, archives)
	info, err := os.Stat(victim)
	if err != nil {
		if os.IsNotExist(err) {
			return false, nil
		}
		return false, err
	}
	for _, s := range g.segments {
		if s.isFile(victim, info) {
			return true, nil
		}
	}
	return false, nil
}

// record notes one written line. info is the file it was appended to, stat'd after the write under
// the log's lock, so its size is where the line ends.
func (g *RetentionGuard) record(path string, archives int, line []byte, info os.FileInfo, statErr error) {
	if archives < 1 {
		archives = DefaultRotateArchives
	}
	g.path = path
	g.archives = archives
	last := len(g.segments) - 1
	if statErr != nil {
		// The write happened, so it counts; attribute it to the file the previous append used,
		// which is where it almost certainly went.
		if last >= 0 {
			g.segments[last].events++
			return
		}
		g.segments = append(g.segments, retentionSegment{events: 1})
		return
	}
	end := info.Size()
	// Same file as the previous append, and grown past where that append ended: a file that was
	// replaced by one with a recycled inode starts over from empty.
	if last >= 0 && g.segments[last].file != nil && os.SameFile(g.segments[last].file, info) && end > g.segments[last].end {
		g.segments[last].events++
		g.segments[last].end = end
		return
	}
	g.segments = append(g.segments, retentionSegment{
		file:   info,
		offset: end - int64(len(line)),
		length: len(line),
		digest: sha256.Sum256(line),
		end:    end,
		events: 1,
	})
}

// retainedPaths is RetainedLogPaths for an explicit archive count.
func retainedPaths(path string, archives int) []string {
	if path == "" {
		return nil
	}
	paths := make([]string, 0, archives+1)
	paths = append(paths, path)
	for i := 1; i <= archives; i++ {
		paths = append(paths, path+fmt.Sprintf(".%d", i))
	}
	return paths
}