The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
Fix partial tail, failed emit, and oversized line handling in Claude session collector
Cursor Agent1637ba2de41d
diff --git a/cli/beacon/internal/claudesession/collect.go b/cli/beacon/internal/claudesession/collect.goindex 3d1b2aeb..8f75bb4f 100644--- a/cli/beacon/internal/claudesession/collect.go+++ b/cli/beacon/internal/claudesession/collect.go@@ -173,9 +173,7 @@ func collectSession(store *Store, ref SessionRef, state *State, opts CollectOpti}for i, item := range mapped {if err := emit(item.Event, opts); err != nil {- if i > 0 {- cursor.LastLine = mapped[i-1].SourceLine- }+ advanceCursorPartial(cursor, mapped, i)return true, err}summary.EventsEmitted++@@ -193,6 +191,28 @@ func advanceCursor(cursor *Cursor, ref SessionRef, lines int) {cursor.ModTimeUnixMS = ref.ModTimeUnixMS}+// advanceCursorPartial moves the cursor past source lines whose mapped events+// were all emitted, so the next sweep retries only from the source line that+// failed. failedIdx is the index into mapped of the event whose emit returned+// an error.+func advanceCursorPartial(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+ }+ cursor.LastLine = lastCompleteLine+}+func emit(event schema.Event, opts CollectOptions) error {if opts.Print {out := opts.Outdiff --git a/cli/beacon/internal/claudesession/store.go b/cli/beacon/internal/claudesession/store.goindex a2d2e3b6..6f0cd5b8 100644--- a/cli/beacon/internal/claudesession/store.go+++ b/cli/beacon/internal/claudesession/store.go@@ -2,6 +2,7 @@ package claudesessionimport ("bufio"+ "bytes""encoding/json""errors""fmt"@@ -147,15 +148,31 @@ func (s *Store) Read(ref SessionRef) ([]Record, Stats, error) {}func decodeRecords(r io.Reader) ([]Record, Stats, error) {- scanner := bufio.NewScanner(r)- scanner.Buffer(make([]byte, 0, 64*1024), MaxLineBytes)+ br := bufio.NewReaderSize(r, 64*1024)var records []Recordvar stats StatslineNo := 0- for scanner.Scan() {+ for {+ line, status, readErr := readLine(br)+ switch status {+ case lineEOF:+ return records, stats, nil+ case linePartial:+ stats.PartialTail = true+ return records, stats, nil+ case lineError:+ return records, stats, readErr+ case lineOversized:+ lineNo+++ stats.Lines = lineNo+ stats.Malformed+++ if stats.FirstError == nil {+ stats.FirstError = fmt.Errorf("claude session line exceeds %d bytes", MaxLineBytes)+ }+ continue+ }lineNo++stats.Lines = lineNo- line := scanner.Bytes()if len(strings.TrimSpace(string(line))) == 0 {continue}@@ -170,17 +187,53 @@ func decodeRecords(r io.Reader) ([]Record, Stats, error) {stats.Decoded++records = append(records, Record{Line: lineNo, Entry: *entry})}- if err := scanner.Err(); err != nil {- if errors.Is(err, bufio.ErrTooLong) {- stats.Malformed++- if stats.FirstError == nil {- stats.FirstError = fmt.Errorf("claude session line exceeds %d bytes", MaxLineBytes)+}++type lineStatus int++const (+ lineOK lineStatus = iota+ lineOversized // consumed and discarded; too large+ linePartial // unterminated trailing fragment+ lineEOF // clean end of stream+ lineError // I/O error+)++// readLine reads one JSONL line from br, handling oversized lines (consumed and+// discarded so later lines are still reachable) and partial tails (unterminated+// trailing fragments from a file still being written).+func readLine(br *bufio.Reader) ([]byte, lineStatus, error) {+ var buf []byte+ oversized := false+ for {+ chunk, err := br.ReadSlice('\n')+ if oversized || len(buf)+len(chunk) > MaxLineBytes {+ oversized = true+ buf = nil+ } else {+ buf = append(buf, chunk...)+ }+ if errors.Is(err, bufio.ErrBufferFull) {+ continue+ }+ if errors.Is(err, io.EOF) {+ if oversized {+ return nil, lineOversized, nil}- return records, stats, nil+ if len(buf) > 0 {+ return nil, linePartial, nil+ }+ return nil, lineEOF, nil}- return records, stats, err+ if err != nil {+ return nil, lineError, err+ }+ break+ }+ if oversized {+ return nil, lineOversized, nil}- return records, stats, nil+ return bytes.TrimSuffix(bytes.TrimSuffix(buf, []byte("\n")), []byte("\r")), lineOK, nil}func readIndex(projectDir string) map[string]*IndexEntry {