Skip to content

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

Fix Pi session collector: prevent failed emit from skipping remaining events and withhold unterminated trailing lines

Cursor Agentfae00e509bdc
diff --git a/cli/beacon/internal/pisession/collect.go b/cli/beacon/internal/pisession/collect.go
index 5e398006..8b99ef7a 100644
--- a/cli/beacon/internal/pisession/collect.go
+++ b/cli/beacon/internal/pisession/collect.go
@@ -89,9 +89,7 @@ func collectFile(store *Store, ref SessionRef, state *State, opts CollectOptions
}
for i, item := range mapped {
if err := emit(item.Event, opts); err != nil {
- if i > 0 {
- advance(cursor, ref, mapped[i-1].SourceLine)
- }
+ advancePartial(cursor, mapped, i)
return true, err
}
summary.EventsEmitted++
@@ -111,6 +109,29 @@ func advance(cursor *Cursor, ref SessionRef, line int) {
cursor.SizeBytes = ref.SizeBytes
}
+// advancePartial moves the cursor past source lines whose mapped events were
+// all emitted without stamping the file's size/mtime, so the next sweep
+// re-reads the file and retries from the failed source line.
+func advancePartial(cursor *Cursor, mapped []MappedEvent, failedIdx int) {
+ var lastCompleteLine int
+ found := false
+ for i := range mapped {
+ if i == failedIdx {
+ break
+ }
+ if i+1 < len(mapped) && mapped[i+1].SourceLine != mapped[i].SourceLine {
+ lastCompleteLine = mapped[i].SourceLine
+ found = true
+ }
+ }
+ if !found {
+ return
+ }
+ if lastCompleteLine > cursor.LastLine {
+ cursor.LastLine = lastCompleteLine
+ }
+}
+
func emit(event schema.Event, opts CollectOptions) error {
if opts.Print && opts.Out != nil {
data, err := json.Marshal(event)
diff --git a/cli/beacon/internal/pisession/store.go b/cli/beacon/internal/pisession/store.go
index b1832db3..aa7feb9c 100644
--- a/cli/beacon/internal/pisession/store.go
+++ b/cli/beacon/internal/pisession/store.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "io"
"io/fs"
"os"
"path/filepath"
@@ -40,8 +41,9 @@ type Entry struct {
}
type ReadStats struct {
- Malformed int
- MaxLine int
+ Malformed int
+ MaxLine int
+ PartialTail bool
}
func NewStore(sessionsDir string) (*Store, error) {
@@ -181,27 +183,49 @@ func (s *Store) Read(ref SessionRef) ([]Entry, ReadStats, error) {
return nil, ReadStats{}, err
}
defer file.Close()
- scanner := bufio.NewScanner(file)
- scanner.Buffer(make([]byte, 0, 64*1024), maxLineBytes)
+ br := bufio.NewReaderSize(file, 64*1024)
var entries []Entry
stats := ReadStats{}
- for scanner.Scan() {
+ for {
+ raw, err := br.ReadBytes('\n')
+ if len(raw) > 0 && raw[len(raw)-1] != '\n' {
+ stats.PartialTail = true
+ return entries, stats, nil
+ }
+ if len(raw) == 0 && err != nil {
+ if err == io.EOF {
+ return entries, stats, nil
+ }
+ return entries, stats, err
+ }
stats.MaxLine++
- line := strings.TrimSpace(scanner.Text())
+ line := strings.TrimSpace(string(raw))
if line == "" {
+ if err == io.EOF {
+ return entries, stats, nil
+ }
+ continue
+ }
+ if len(raw) > maxLineBytes {
+ stats.Malformed++
+ if err == io.EOF {
+ return entries, stats, nil
+ }
continue
}
var item map[string]interface{}
- if err := json.Unmarshal([]byte(line), &item); err != nil {
+ if jsonErr := json.Unmarshal([]byte(line), &item); jsonErr != nil {
stats.Malformed++
+ if err == io.EOF {
+ return entries, stats, nil
+ }
continue
}
entries = append(entries, Entry{Line: stats.MaxLine, Data: item})
+ if err == io.EOF {
+ return entries, stats, nil
+ }
}
- if err := scanner.Err(); err != nil {
- return entries, stats, err
- }
- return entries, stats, nil
}
func extractSessionIDFromPath(path string) string {