transcriptindex

package
v0.16.397 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package transcriptindex reads what an agent's transcript gains once for everything that follows it. A transcript is one append-only file of JSON lines, and several parts of the runtime follow each: the conversation's feed, its background work, what the session says about itself. Each used to keep a cursor of its own, open the file and decode the same lines again. The index keeps one cursor per file: a look stats the file, reads the bytes it gained in bounded blocks, cuts them into whole lines, and hands each block to the file's subscribers while it is held. A line is decoded as JSON at most once, into the few fields the subscribers read (Fields), whoever asks first.

The index holds nothing of a transcript: no line outlives the look that read it. A file is known only while it has subscribers, so a conversation that let go of its transcript (parked, deleted) costs the index nothing, and its file is never opened. The index reads no history: a subscriber reads its own start (a tail, a page of the past) and seats itself where that read ended; from there on the lines come from the index. A subscriber the index cannot serve in order — it could not take a block (a full Queue), the file was replaced or cut, its place is not one the index reads from — is lost: it reads from its own place itself and seats again. A loss is counted and logged.

The index is driven by its subscribers' looks (Sync), not by a clock of its own: a look is a stat, and a look shortly after another subscriber's takes that one's word.

Index

Constants

View Source
const (
	// DefaultFollowBytes is how much of a transcript a Follower holds for its reader.
	DefaultFollowBytes = 64 << 10

	// DefaultFollowIdle is for how long a Follower holds lines for a reader that does not come for them.
	DefaultFollowIdle = 2 * time.Minute
)
View Source
const (
	// DefaultBlock is how many bytes of a transcript one read takes; a longer line is read whole.
	DefaultBlock = 1 << 20
	// DefaultFresh is for how long a look that found a file as it was answers the other subscribers' looks at it.
	DefaultFresh = 500 * time.Millisecond
)
View Source
const DefaultQueueBytes = 1 << 20

DefaultQueueBytes is how much of a transcript a Queue holds for its reader.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch

type Batch struct {
	Path string
	// From is the start of the first line and To the end of the last, its newline included.
	From, To int64
	// contains filtered or unexported fields
}

Batch is the whole lines a transcript gained from a subscriber's place on. Valid only while the Sink is called.

func (*Batch) Bytes

func (b *Batch) Bytes() []byte

Bytes is the batch as it is in the file: whole lines, each with its newline.

func (*Batch) Each

func (b *Batch) Each(fn func(l *Line))

Each calls fn with every line that is not empty, in order.

type Counters

type Counters struct {
	Files int // transcripts with a subscriber
	Subs  int // subscriptions
	// Looks counts the stats of transcripts, Opens the times a transcript was opened to read what it gained, Bytes
	// what was read; all since the runtime started.
	Looks, Opens, Bytes int64
	// Overflows counts the subscribers lost because they could not take a batch; Gaps, those whose place the index
	// could not read from; Resets, the files found replaced, cut or gone.
	Overflows, Gaps, Resets int64
}

Counters are the index's own numbers.

type Fields

type Fields struct {
	Type        string `json:"type"`
	Subtype     string `json:"subtype"`
	Operation   string `json:"operation"`
	Timestamp   any    `json:"timestamp"`
	IsSidechain any    `json:"isSidechain"`
	QuerySource string `json:"querySource"`
	Cwd         string `json:"cwd"`
	Content     any    `json:"content"`
	AITitle     string `json:"aiTitle"`
	CustomTitle string `json:"customTitle"`
	Message     struct {
		Content any `json:"content"`
	} `json:"message"`
	Params struct {
		OutputConfig struct {
			Effort string `json:"effort"`
		} `json:"output_config"`
	} `json:"params"`
	Attachment struct {
		Type      string `json:"type"`
		Met       bool   `json:"met"`
		Condition string `json:"condition"`
		HookName  string `json:"hookName"`
	} `json:"attachment"`
}

Fields is what a transcript line says, as far as the index's subscribers read it: one decode serves them all.

type Follower

type Follower struct {
	// contains filtered or unexported fields
}

Follower serves a reader that reads transcripts when asked, not on a clock: a list of sessions, a count of messages, a page of costs. Such a reader keeps a cursor of its own in many files, most of them at rest. For the files the index follows anyway (a conversation is live on them) the Follower holds the lines the index reads, so the reader does not open those files again; it never makes the index follow a file, and ends with the file's last other subscriber. For every other file, and whenever Take says no, the reader reads the file itself as it always did and tells the Follower where it stands (Seat).

What a Follower holds is bounded twice: by bytes per file (lines that do not fit are not kept and the place is given up) and by time (a reader that has not come for DefaultFollowIdle holds nothing until it comes again). Giving a place up is the ordinary course for such a reader, not a loss: it is neither counted nor logged. A nil Follower serves nothing: every Take says no.

func (*Follower) Forget

func (fo *Follower) Forget(path string)

Forget lets go of the reader's place in the transcript.

func (*Follower) Places

func (fo *Follower) Places() int

Places is how many transcripts the follower has a place in.

func (*Follower) Seat

func (fo *Follower) Seat(path string, offset int64)

Seat says the reader has read the transcript up to offset, the start of a line, itself. A transcript the index does not follow is not followed for the reader's sake.

func (*Follower) Take

func (fo *Follower) Take(path string, offset int64) (data []byte, partial, ok bool)

Take gives the whole lines the transcript gained after offset, the reader's own place: data is the reader's to keep. partial says a line is still being written after them. ok is false when the Follower cannot say what the file gained after offset: the reader reads the file itself and seats again. With ok and no data and no partial the file is as the reader left it.

type Index

type Index struct {
	// contains filtered or unexported fields
}

Index is the shared reader. Safe for concurrent use.

func New

func New(o Options) *Index

New is an index with no file.

func (*Index) Counters

func (ix *Index) Counters() Counters

Counters is the index's numbers now.

func (*Index) Follower

func (ix *Index) Follower(name string, max int) *Follower

Follower is a follower for the reader called name that holds max bytes per file (DefaultFollowBytes when 0). On a nil index it is nil.

func (*Index) Queue

func (ix *Index) Queue(path, name string, max int) *Queue

Queue subscribes a queue of max bytes (DefaultQueueBytes when 0) to the transcript.

func (*Index) Subscribe

func (ix *Index) Subscribe(path, name string, sink Sink) *Sub

Subscribe adds a subscriber of the transcript. It is handed nothing until it seats itself (Seat).

type Line

type Line struct {
	// Offset is where the line starts in the file, End where the next one does.
	Offset, End int64
	Raw         []byte
	// contains filtered or unexported fields
}

Line is one line of a transcript, without its newline. Valid only while the Sink is called.

func (*Line) Fields

func (l *Line) Fields() *Fields

Fields is the line decoded, once, whoever asks; nil when it does not decode into Fields. A line may still decode into a narrower shape (a field Fields types strictly holds something else): a subscriber that cares decodes Raw.

func (*Line) Text

func (l *Line) Text() string

Text is the line as valid UTF-8 text. It is made once, whoever asks.

type Options

type Options struct {
	// Block is how many bytes one read takes (DefaultBlock).
	Block int
	// Fresh is for how long a look answers the other subscribers' unforced looks (DefaultFresh); negative: never.
	Fresh time.Duration
	// Warn logs a subscriber lost; nil logs nothing.
	Warn func(msg string, kv ...any)
	// Now is the clock; nil is time.Now.
	Now func() time.Time
}

Options are the index's settings; the zero value is the defaults.

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue is a subscriber that reads on its own goroutine, in its own time: the lines the transcript gained wait in it, as they are in the file, until it takes them. It holds max bytes at most: lines that do not fit are not kept, the queue is lost, and its reader reads the file from its own place.

func (*Queue) Close

func (q *Queue) Close()

Close ends the queue's subscription and drops what it holds.

func (*Queue) Lines

func (q *Queue) Lines(b *Batch) bool

Lines keeps a copy of the batch for the reader, or nothing at all when it does not fit.

func (*Queue) Seat

func (q *Queue) Seat(offset int64)

Seat says the reader has read the file up to offset itself.

func (*Queue) Take

func (q *Queue) Take() (data []byte, from int64, partial bool, st State)

Take looks at the file and gives the reader the whole lines it gained since the reader's place: data starts at byte from of the file and is the reader's to keep. partial says a line is still being written after them. Anything but Fresh gives no lines: the reader reads from its own place and seats again.

type Sink

type Sink interface {
	Lines(b *Batch) bool
}

Sink takes the whole lines a transcript gained, in order, on the goroutine of whoever looked. The batch and everything reached through it are valid only during the call. It must be quick: the file's other subscribers wait for it. false says the batch could not be taken: the subscriber is lost from here on.

type SinkFunc

type SinkFunc func(b *Batch) bool

SinkFunc is a function as a Sink.

func (SinkFunc) Lines

func (f SinkFunc) Lines(b *Batch) bool

type State

type State int

State is what a look found for the subscriber that looked.

const (
	// Fresh: the subscriber has been handed every whole line up to the file's end.
	Fresh State = iota
	// Lost: the index does not serve the subscriber; it reads from its own place and seats again.
	Lost
	// Gone: the file is not there.
	Gone
)

type Sub

type Sub struct {
	// contains filtered or unexported fields
}

Sub is one subscriber's place in one transcript.

func (*Sub) Close

func (s *Sub) Close()

Close ends the subscription. A file without subscribers is forgotten whole.

func (*Sub) Closed

func (s *Sub) Closed() bool

Closed says the subscription has ended: by Close, or with the file's last subscriber that kept it.

func (*Sub) Seat

func (s *Sub) Seat(offset int64, ino uint64)

Seat says the subscriber has read the file up to offset, the start of a line, itself: the lines from there on are the index's to hand over. ino is the inode of the file the subscriber read, 0 when it does not know.

func (*Sub) Sync

func (s *Sub) Sync(force bool) (st State, partial bool)

Sync looks at the file for the subscriber: what it gained is read and handed to every subscriber in order. force makes the look a stat of its own; without it a look another subscriber made within Options.Fresh answers. partial says the file ends in a line still being written past the subscriber's place.

Directories

Path Synopsis
Package indextest helps a reader's tests hold that it says the same of a transcript whether it reads the file itself or takes the lines from the shared read: two copies of one made-up transcript grow by the same writes, one of them followed by the index as a live conversation's transcript is.
Package indextest helps a reader's tests hold that it says the same of a transcript whether it reads the file itself or takes the lines from the shared read: two copies of one made-up transcript grow by the same writes, one of them followed by the index as a live conversation's transcript is.
Package tally counts what reading transcripts costs the process: the files opened, the bytes read off them, the stats and the lines decoded as JSON.
Package tally counts what reading transcripts costs the process: the files opened, the bytes read off them, the stats and the lines decoded as JSON.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL