Skip to content

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

Fix three factory session collector bugs: partial line loss, failed-emit skip, missing dedup ID

Cursor Agent70e0bad32cad
diff --git a/cli/beacon/internal/factorysession/collect.go b/cli/beacon/internal/factorysession/collect.go
index 47fa4fdd..49cfa617 100644
--- a/cli/beacon/internal/factorysession/collect.go
+++ b/cli/beacon/internal/factorysession/collect.go
@@ -210,8 +210,6 @@ func advanceCursorPartial(cursor *Cursor, ref SessionRef, mapped []MappedEvent,
cursor.Started = true
}
}
- cursor.ModTimeUnixMS = ref.ModTimeUnixMS
- cursor.SizeBytes = ref.SizeBytes
}
func emit(event schema.Event, opts CollectOptions) error {
diff --git a/cli/beacon/internal/factorysession/mapper.go b/cli/beacon/internal/factorysession/mapper.go
index 9683f469..700bb7da 100644
--- a/cli/beacon/internal/factorysession/mapper.go
+++ b/cli/beacon/internal/factorysession/mapper.go
@@ -1,8 +1,11 @@
package factorysession
import (
+ "crypto/sha1"
+ "encoding/hex"
"encoding/json"
"fmt"
+ "io"
"strings"
"time"
@@ -242,13 +245,39 @@ func (m *mapper) append(record *Record, suffix string, ev schema.Event) {
if record.ID != "" {
ev.Raw["source_record_id"] = record.ID
}
+ dedupID := fmt.Sprintf("%s:%d:%s", m.ref.ID, record.Line, suffix)
+ ev.Event.ID = factoryEventID(dedupID)
m.out = append(m.out, MappedEvent{
SourceLine: record.Line,
- DedupID: fmt.Sprintf("%s:%d:%s", m.ref.ID, record.Line, suffix),
+ DedupID: dedupID,
Event: ev,
})
}
+// factoryIDNamespace is the fixed UUID namespace for Factory dedup-coordinate
+// event IDs, distinct from both the content-based namespace in asymptoteobserve
+// and the fx namespace so the derivations can never collide.
+var factoryIDNamespace = [16]byte{
+ 0x5e, 0x2a, 0x3f, 0x8c, 0x9b, 0x61, 0x4b, 0x1f,
+ 0x6d, 0x7c, 0x0d, 0x5a, 0x14, 0x2c, 0x8e, 0x3a,
+}
+
+// factoryEventID derives a deterministic UUID v5 from a dedup coordinate so the
+// same Factory record always produces the same event ID regardless of how many
+// times it is read.
+func factoryEventID(dedupID string) string {
+ digest := sha1.New()
+ digest.Write(factoryIDNamespace[:])
+ io.WriteString(digest, dedupID)
+ var id [16]byte
+ copy(id[:], digest.Sum(nil))
+ id[6] = (id[6] & 0x0f) | 0x50 // version 5
+ id[8] = (id[8] & 0x3f) | 0x80 // RFC 4122 variant
+ out := make([]byte, 32)
+ hex.Encode(out, id[:])
+ return fmt.Sprintf("%s-%s-%s-%s-%s", out[0:8], out[8:12], out[12:16], out[16:20], out[20:32])
+}
+
func messageTexts(msg *Message) []string {
blocks := messageBlocks(msg)
var out []string
diff --git a/cli/beacon/internal/factorysession/store.go b/cli/beacon/internal/factorysession/store.go
index 6b82fe96..3beb46ac 100644
--- a/cli/beacon/internal/factorysession/store.go
+++ b/cli/beacon/internal/factorysession/store.go
@@ -191,8 +191,10 @@ func (s *Store) Read(ref SessionRef) ([]Record, ReadStats, error) {
scanner.Buffer(make([]byte, 0, 64*1024), MaxLineBytes)
var records []Record
stats := ReadStats{}
+ lastLineMalformed := false
for scanner.Scan() {
stats.Lines++
+ lastLineMalformed = false
text := strings.TrimSpace(scanner.Text())
if text == "" {
continue
@@ -200,6 +202,7 @@ func (s *Store) Read(ref SessionRef) ([]Record, ReadStats, error) {
var record Record
if err := json.Unmarshal([]byte(text), &record); err != nil {
stats.Malformed++
+ lastLineMalformed = true
continue
}
record.Line = stats.Lines
@@ -210,6 +213,23 @@ func (s *Store) Read(ref SessionRef) ([]Record, ReadStats, error) {
stats.Partial = true
return records, stats, nil
}
+ // bufio.Scanner returns the last line even without a trailing newline, and
+ // Err() is nil. An unterminated tail is a record Factory is still writing;
+ // withhold it so the next sweep reads it complete.
+ if stats.Lines > 0 {
+ if pos, seekErr := file.Seek(-1, io.SeekEnd); seekErr == nil && pos >= 0 {
+ var buf [1]byte
+ if _, readErr := file.Read(buf[:]); readErr == nil && buf[0] != '\n' {
+ stats.Partial = true
+ if lastLineMalformed {
+ stats.Malformed--
+ } else if len(records) > 0 && records[len(records)-1].Line == stats.Lines {
+ records = records[:len(records)-1]
+ }
+ stats.Lines--
+ }
+ }
+ }
return records, stats, nil
}