Files
Agent-Native/runtime/session/jsonl.go
T
weichen 6ddd3da8df Enable canonical versioned Go module consumption
Why: Allow consumers to download an immutable module revision without nested source checkouts or local path replacements.

Evidence: SELF package tests and the prior canonical-path rejection are recorded in the Dev dependency-workspace task card; remote consumption is replayed against the committed candidate.

Impact: Change the module identity and its Go imports while preserving API behavior. Existing historical submodule revisions remain buildable.

Refs: Versioned dependencies U1/U3; feature/dependency-workspace
2026-10-13 15:36:10 +00:00

55 lines
1.4 KiB
Go

package session
import (
"os"
"path/filepath"
"sync"
"git-ssh.u1680832.nyat.app/weichen/Agent-Native.git/runtime/llm"
)
// Store is an append-only JSONL transcript. New sessions are a tree of
// entries (id / parentId). Load still returns the model-visible message
// path, and files written as one llm.Message per line remain readable.
type Store struct {
path string
mu sync.Mutex
}
func Open(path string) (*Store, error) {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return nil, err
}
f, err := os.OpenFile(path, os.O_CREATE, 0o644)
if err != nil {
return nil, err
}
_ = f.Close()
return &Store{path: path}, nil
}
func (s *Store) Path() string { return s.path }
// Append records one message on the active branch.
func (s *Store) Append(msg llm.Message) error {
s.mu.Lock()
defer s.mu.Unlock()
_, err := s.appendEntryLocked(Entry{Type: TypeMessage, Message: &msg})
return err
}
// Load returns the model-visible messages on the active branch.
// A compaction entry replaces earlier turns with its summary plus the
// retained tail; the file itself is never rewritten.
// Crash residue: a trailing line without a newline that is not valid JSON
// is dropped. A bad line in the middle is still an error.
func (s *Store) Load() ([]llm.Message, error) {
s.mu.Lock()
defer s.mu.Unlock()
entries, err := s.readEntriesLocked()
if err != nil {
return nil, err
}
return visibleMessages(entries, s.leafLocked(entries)), nil
}