Skip to content

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

Fix partial tail and failed emit bugs in Prime Agent session collector

Cursor Agentb9e7fa45fa25
diff --git a/cli/beacon/internal/primesession/collect.go b/cli/beacon/internal/primesession/collect.go
index bb3baf26..87d154f7 100644
--- a/cli/beacon/internal/primesession/collect.go
+++ b/cli/beacon/internal/primesession/collect.go
@@ -92,9 +92,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, parent)
- }
+ advancePartial(cursor, mapped, i, parent)
return true, err
}
summary.EventsEmitted++
@@ -115,6 +113,27 @@ func advance(cursor *Cursor, ref SessionRef, line int, parent string) {
cursor.SubagentOf = parent
}
+// advancePartial moves the cursor past source lines whose mapped events were
+// all emitted, without updating file identity (ModTimeMS, SizeBytes) so the
+// next sweep re-reads the file and retries the remaining events.
+func advancePartial(cursor *Cursor, mapped []MappedEvent, failedIdx int, parent string) {
+ var lastCompleteLine int
+ found := false
+ for i := 0; i < failedIdx; i++ {
+ 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
+ }
+ cursor.SubagentOf = parent
+}
+
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/primesession/store.go b/cli/beacon/internal/primesession/store.go
index a696b203..694ef7bb 100644
--- a/cli/beacon/internal/primesession/store.go
+++ b/cli/beacon/internal/primesession/store.go
@@ -42,8 +42,9 @@ type Entry struct {
}
type ReadStats struct {
- Malformed int
- MaxLine int
+ Malformed int
+ MaxLine int
+ PartialTail bool
}
func NewStore(sessionsDir, artifactsDir string) (*Store, error) {
@@ -226,5 +227,24 @@ func (s *Store) Read(ref SessionRef) ([]Entry, ReadStats, error) {
if err := scanner.Err(); err != nil {
return entries, stats, err
}
+ if stats.MaxLine > 0 && fileHasPartialTail(file) {
+ stats.PartialTail = true
+ if len(entries) > 0 && entries[len(entries)-1].Line == stats.MaxLine {
+ entries = entries[:len(entries)-1]
+ }
+ stats.MaxLine--
+ }
return entries, stats, nil
}
+
+func fileHasPartialTail(f *os.File) bool {
+ info, err := f.Stat()
+ if err != nil || info.Size() == 0 {
+ return false
+ }
+ buf := make([]byte, 1)
+ if _, err := f.ReadAt(buf, info.Size()-1); err != nil {
+ return false
+ }
+ return buf[0] != '\n'
+}