Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286package tui
import ( "albedo/cli/internal/daemon" "encoding/json" "fmt" "slices")
type EntryKind string
const ( EntryUser EntryKind = "user" EntryAssistant EntryKind = "assistant" EntryThinking EntryKind = "thinking" EntryTool EntryKind = "tool" EntryShell EntryKind = "shell_command" EntryNote EntryKind = "note" EntryError EntryKind = "error" EntryCompacted EntryKind = "compacted" // EntryTurnEnd closes a turn with its outcome, length, and tool count. EntryTurnEnd EntryKind = "turn_end")
type HistoryEntry struct { MessageID string ID string Source string MailKind string SenderSessionID string ToolArgs map[string]any // facts is what this entry contributes to a grouped row, computed once // when the entry settles. Targets stay as recorded; naming is at render. facts *entryFacts ToolTrace *daemon.ToolTrace ShellCommand *daemon.ShellCommand ShellOutputLoaded bool ShellExpandedOutput string // Skill is the skill a user's slash activation loaded. Skill *daemon.SkillLoad Speaker string Mood mood Text string ToolName string ToolResult string ToolTitle string // Strategy is the compaction strategy that produced a compacted entry. Strategy string Kind EntryKind Timestamp int64 SizeBytes int64 Evicted int Tools int // Pending is a message of yours the daemon has not echoed back yet. Pending pending ElapsedMs int64 // TokensPerSecond is the output rate of a turn's latest timed call; 0 when // none was timed. TokensPerSecond float64 // CacheMissTokens is what the turn's calls read uncached that the call // before each had left cached, summed; 0 when none lost enough to mention. CacheMissTokens int // CacheDriftTokens is the same loss where albedo caused it, a call that // did not repeat the history the call before it sent. CacheDriftTokens int // Seq is the newest transcript row this entry came from; 0 until a // `committed` event says which rows cover it. Seq int64 // Live is a reply still streaming in. It renders on every token, so it // skips the second rendering that marks wraps for a copy until it settles. Live bool}
// pending records whether a user message is awaiting admission, queued for// delivery, or unresolved after its operation receipt expires.type pending int
const ( settled pending = iota sending queued unresolved)
func (e *HistoryEntry) ComputeSize() int64 { size := int64(len(e.ID) + len(e.MessageID) + len(e.Source) + len(e.MailKind) + len(e.SenderSessionID) + len(e.Speaker) + len(e.Text) + len(e.ToolName) + len(e.ToolResult) + 64) if e.ShellCommand != nil { size += int64(len(e.ShellCommand.ID) + len(e.ShellCommand.Command) + len(e.ShellCommand.Output) + len(daemon.ShellCommandErrorText(*e.ShellCommand)) + 64) } for k, v := range e.ToolArgs { size += int64(len(k) + 16) switch val := v.(type) { case string: size += int64(len(val)) default: data, _ := json.Marshal(val) size += int64(len(data)) } } if e.Skill != nil { size += int64(len(e.Skill.Name) + len(e.Skill.Scope) + len(e.Skill.Source) + 16) } if e.ToolTrace != nil { for _, act := range e.ToolTrace.Activities { size += int64(len(act.Kind) + len(act.Target) + len(act.Detail) + 16) } for _, ch := range e.ToolTrace.Changes { size += int64(len(ch.Path) + len(ch.Kind) + len(ch.Diff) + len(ch.Reason) + 32) } } e.SizeBytes = size return size}
type BoundedHistory struct { entries []HistoryEntry MaxEntries int MaxBytes int64 totalBytes int64 evictedEntries int evictedBytes int64 // evictedThrough is the newest transcript row among evicted entries: the // daemon holds everything up to it, and older pages resume after it. evictedThrough int64 // reading stretches the caps while scrolled up so incoming events do not // evict history above the viewport; Trim restores the normal caps. reading bool}
func NewBoundedHistory(maxEntries int, maxBytes int64) *BoundedHistory { if maxEntries <= 0 { maxEntries = 500 } if maxBytes <= 0 { maxBytes = 2 * 1024 * 1024 } return &BoundedHistory{ MaxEntries: maxEntries, MaxBytes: maxBytes, entries: make([]HistoryEntry, 0, 128), }}
func (h *BoundedHistory) Len() int { return len(h.entries) }func (h *BoundedHistory) EvictedCount() int { return h.evictedEntries }func (h *BoundedHistory) Entries() []HistoryEntry { return h.entries }
func (h *BoundedHistory) TruncationNotice() string { if h.evictedEntries > 0 { return fmt.Sprintf("[%d older messages hidden (%d KB); full history is saved by Albedo]", h.evictedEntries, h.evictedBytes/1024) } return ""}
// EvictedThrough is the newest transcript row among evicted entries, or 0.func (h *BoundedHistory) EvictedThrough() int64 { return h.evictedThrough}
// Stamp marks the trailing entries no row covered yet as covered by seq.func (h *BoundedHistory) Stamp(seq int64) { for i := len(h.entries) - 1; i >= 0 && h.entries[i].Seq == 0; i-- { if h.entries[i].MessageID == "" { h.entries[i].Seq = seq } }}
func (h *BoundedHistory) Find(id string) int { if id == "" { return -1 } return slices.IndexFunc(h.entries, func(entry HistoryEntry) bool { return entry.ID == id })}
func (h *BoundedHistory) Replace(entry HistoryEntry) bool { index := h.Find(entry.ID) if index < 0 { return false } entry.ComputeSize() h.totalBytes += entry.SizeBytes - h.entries[index].SizeBytes h.entries[index] = entry h.enforceBounds() return true}
// Prepend puts older entries before the retained ones. It does not evict:// they were asked for, and Trim or the next Append restores the bounds.func (h *BoundedHistory) Prepend(older []HistoryEntry) { if len(older) == 0 { return } unique := make([]HistoryEntry, 0, len(older)) seen := map[string]bool{} for _, entry := range h.entries { if entry.ID != "" { seen[entry.ID] = true } } for _, entry := range older { if entry.ID != "" && seen[entry.ID] { continue } if entry.ID != "" { seen[entry.ID] = true } h.totalBytes += entry.ComputeSize() unique = append(unique, entry) } h.entries = slices.Concat(unique, h.entries) h.evictedThrough = 0 h.reading = true}
// SetReading stretches or restores the retention caps while scrolling.func (h *BoundedHistory) SetReading(reading bool) { h.reading = reading if !reading { h.enforceBounds() }}
// Trim evicts the oldest entries past the bounds Prepend or reading stretched.func (h *BoundedHistory) Trim() { h.reading = false h.enforceBounds()}
func (h *BoundedHistory) enforceBounds() { maxEntries, maxBytes := h.MaxEntries, h.MaxBytes if h.reading { maxEntries, maxBytes = maxEntries*readingSlack, maxBytes*readingSlack } for len(h.entries) > 0 && (len(h.entries) > maxEntries || h.totalBytes > maxBytes) { h.evictFirst() // A row's entries leave together, so re-reading from the next row // never repeats what is still shown. for seq := h.evictedThrough; seq > 0 && len(h.entries) > 0 && h.entries[0].Seq == seq; { h.evictFirst() } }}
func (h *BoundedHistory) evictFirst() { removed := h.entries[0] h.entries[0] = HistoryEntry{} // Explicitly zero out evicted slot to drop GC references h.entries = h.entries[1:] h.totalBytes = max(0, h.totalBytes-removed.SizeBytes) h.evictedEntries++ h.evictedBytes += removed.SizeBytes h.evictedThrough = max(h.evictedThrough, removed.Seq)}
func (h *BoundedHistory) Append(entry HistoryEntry) { entry.ComputeSize() h.entries = append(h.entries, entry) h.totalBytes += entry.SizeBytes h.enforceBounds()}
func (h *BoundedHistory) Clear() { clear(h.entries) *h = BoundedHistory{MaxEntries: h.MaxEntries, MaxBytes: h.MaxBytes, entries: h.entries[:0]}}
func (h *BoundedHistory) firstLiveIndex(ids []string, all bool) int { return slices.IndexFunc(h.entries, func(entry HistoryEntry) bool { return entry.MessageID != "" && (all || slices.Contains(ids, entry.MessageID)) })}
// Retire provisional chunks without counting them as hidden durable history.func (h *BoundedHistory) retireLive(ids []string, all bool) bool { removed := false h.entries = slices.DeleteFunc(h.entries, func(entry HistoryEntry) bool { retire := entry.MessageID != "" && (all || slices.Contains(ids, entry.MessageID)) if retire { h.totalBytes -= entry.SizeBytes removed = true } return retire }) return removed}